RFR: 8332586: Avoid cloning empty arrays in java.lang.reflect.{Method,Constructor}

Chen Liang liach at openjdk.org
Thu May 30 19:30:14 UTC 2024


On Wed, 22 May 2024 09:39:50 GMT, Aleksey Shipilev <shade at openjdk.org> wrote:

>> Improve `java/lang/reflect/Method.java`  by eliminating needless cloning of Class[0] instances.  This cloning is intended to prevent callers from changing array contents, but smany Methods have zero exceptions or zero parameters, and returning the original `Class[0]` is sufficient.
>
> src/java.base/share/classes/jdk/internal/reflect/ReflectionFactory.java line 249:
> 
>> 247:      * the original can safely be reused.
>> 248:      */
>> 249:     public Class<?>[] copyClasses(Class<?>[] classes) {
> 
> There is no need to make this utility method non-static. This would also obviate the need for having an instance of `ReflectionFactory` to access it. 
> 
> Additionally, at the risk of more bikeshedding, I think this utility method is better suited in `AccessibleObject` base class, with package-private visibility. Putting the util methods in `ReflectionFactory` erodes this comment a bit:
> 
> 
>     <P> The methods in this class are extremely unsafe and can cause
>     subversion of both the language and the verifier. For this reason,
>     they are all instance methods, and access to the constructor of
>     this factory is guarded by a security check, in similar style to
>     {@link jdk.internal.misc.Unsafe}. </P>

Can't this be used by class cloning interfaces too?

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/19327#discussion_r1609710019


More information about the core-libs-dev mailing list