[PROPOSAL][JDK10] Introduce Executable.getParameterType(index)

Claes Redestad claes.redestad at oracle.com
Fri Oct 20 12:27:33 UTC 2017


Hi Christoph,


On 2017-10-20 13:45, Christoph Dreis wrote:
> Hey,
>
> while discussing JDK-8029019 & JDK-8189266 Claes came up with the idea to
> introduce non-copying access to the underlying parameter types of an
> Executable [1].

I only recalled Peter had a version of his patch for 8029019 that
included making a shared secret out of
Method.getSharedParameterTypes(), which is kept package-private
for good reason (giving anyone outside the module direct access
to shared arrays is never safe - clone defensively!).

A non-intrusive public API to get at the parameter types safely might
be better overall, though, so if you want to give me some credit for
this then I don't mind... :-)

> While there is a (for a good reason?) package-private
> Method.getSharedParameterTypes() already I thought of an alternative that
> doesn't open up the parameterTypes array to the outside world and actually
> solves the use-case where we wanted an allocation-free alternative for
> Method.getParameterTypes()[index].
>
> Here is a draft of what I'm thinking of:
>
> Executable:
>      /**
>       * Returns the {@code Class} object that represents the formal
>       * parameter type, at the given index, of the executable
>       * represented by this object. The given index starts at 0 and
>       * represents the declaration order of the parameters.
>       *
>       *
>       * @param index index to look for the parameter type
>       * @return the parameter type at the given index for the executable this
> object
>       * represents
>       * @throws IndexOutOfBoundsException if the parameter type
>       *      at the given index of the underlying executable could not be
> found
>       */
>      public abstract Class<?> getParameterType(int index);
>
> Method & Constructor:
>     /**
>       * {@inheritDoc}
>       * @throws IndexOutOfBoundsException {@inheritDoc}
>       * @since 18.3
>       */
>      public Class<?> getParameterType(int index) {
>          if (index < 0 || index > getParameterCount()) {
>              throw new IndexOutOfBoundsException("No parameter found on index
> " + index);
>          }

I don't think we need the explicit range check here.

>          return parameterTypes[index];
>      }
>
> As I don't know what the official @since should look like in the future, do
> not pay too much attention on that. I'm not happy with the documentation
> wording though.

I'm sure there are some who can comment on the wording around
here... :-)

>
> What do you think about that? Is someone willing to sponsor this given it's
> considered worthwhile.

Overall this design captures the desire to allow performant access to
parameter types, and experience and experiments show that the JIT fails
to eliminate the cloning more often than not.

This means there may be both internal[1] and possibly external use cases
where using this API is an optimization, and a simple getter like this can
also make some code cleaner. It does this without introducing another
shared secret in the meantime, which would only have limited applicability.

All in all I think it can pull its weight by allowing us to reduce JDK 
internal
use of getParameterTypes() alone, thus I'm in favor and can volunteer
to sponsor (this will need a CSR etc..)

Thanks!

/Claes

[1] 30+ uses of {Method|Constructor}.getParameterTypes() in java.base
alone, many of which could be improved with this.  Admittedly a few of
them are getParameterTypes().length != 0 that should be replaced with
getParameterCount() != 0 regardless..

>
> Cheers,
> Christoph
>
> [1]
> http://mail.openjdk.java.net/pipermail/core-libs-dev/2017-October/049520.html
>



More information about the core-libs-dev mailing list