java.lang.reflect.Method.copyOf

Remi Forax forax at univ-mlv.fr
Wed Oct 14 10:38:54 UTC 2015


Given that j.l.r.Method is mutable, the best way to have performance is too encapsulate it in a non mutable class, if possible.

As far as i know j.l.r.Method was introduced in Java 1.1 as non mutable and become mutable with Java 1.2, (yes, someone seriously fucked up !)
so methods like copyOf were introduced to do defensive copies (the sharing mechanism was later introduced, in 1.4, i believe).

cheers,
Rémi

----- Mail original -----
> De: "Kasper Nielsen" <kasperni at gmail.com>
> À: "core-libs-dev" <core-libs-dev at openjdk.java.net>
> Envoyé: Mercredi 14 Octobre 2015 12:02:01
> Objet: java.lang.reflect.Method.copyOf
> 
> Hi,
> 
> I was wondering if there is any reason for Method.copyOf (and friends)
> being package private.
> 
> I'm trying to make a copy of a large number of Method objects in some
> performance sensitive code to avoid sharing the isAccessible flag.
> 
> And while this is fairly fast.
> 
> Class<?> declaringClass = method.getDeclaringClass();
> try {
>     return declaringClass.getDeclaredMethod(method.getName(),
> method.getParameterTypes());
> } catch (NoSuchMethodException e) {
>     throw new RuntimeException("This should never happen", e);
> }
> 
> I would to like to avoid the performance penalty if possible.
> 
> - Kasper
> 



More information about the core-libs-dev mailing list