src/java.base/share/classes/jdk/internal/reflect/ReflectionFactory.java
changeset 57503 042dfb697624
parent 53926 7272e7c5164b
equal deleted inserted replaced
57502:650335128b9d 57503:042dfb697624
    41 import java.lang.reflect.Modifier;
    41 import java.lang.reflect.Modifier;
    42 import java.security.PrivilegedAction;
    42 import java.security.PrivilegedAction;
    43 import java.util.Objects;
    43 import java.util.Objects;
    44 import java.util.Properties;
    44 import java.util.Properties;
    45 
    45 
       
    46 import jdk.internal.access.JavaLangReflectAccess;
       
    47 import jdk.internal.access.SharedSecrets;
    46 import jdk.internal.misc.VM;
    48 import jdk.internal.misc.VM;
    47 import sun.reflect.misc.ReflectUtil;
    49 import sun.reflect.misc.ReflectUtil;
    48 import sun.security.action.GetPropertyAction;
    50 import sun.security.action.GetPropertyAction;
    49 import sun.security.util.SecurityConstants;
    51 import sun.security.util.SecurityConstants;
    50 
    52 
    62 
    64 
    63 public class ReflectionFactory {
    65 public class ReflectionFactory {
    64 
    66 
    65     private static boolean initted = false;
    67     private static boolean initted = false;
    66     private static final ReflectionFactory soleInstance = new ReflectionFactory();
    68     private static final ReflectionFactory soleInstance = new ReflectionFactory();
    67     // Provides access to package-private mechanisms in java.lang.reflect
    69 
    68     private static volatile LangReflectAccess langReflectAccess;
       
    69 
    70 
    70     /* Method for static class initializer <clinit>, or null */
    71     /* Method for static class initializer <clinit>, or null */
    71     private static volatile Method hasStaticInitializerMethod;
    72     private static volatile Method hasStaticInitializerMethod;
    72 
    73 
    73     //
    74     //
    88     private static int     inflationThreshold = 15;
    89     private static int     inflationThreshold = 15;
    89 
    90 
    90     // true if deserialization constructor checking is disabled
    91     // true if deserialization constructor checking is disabled
    91     private static boolean disableSerialConstructorChecks = false;
    92     private static boolean disableSerialConstructorChecks = false;
    92 
    93 
       
    94     private final JavaLangReflectAccess langReflectAccess;
    93     private ReflectionFactory() {
    95     private ReflectionFactory() {
       
    96         this.langReflectAccess = SharedSecrets.getJavaLangReflectAccess();
    94     }
    97     }
    95 
    98 
    96     /**
    99     /**
    97      * A convenience class for acquiring the capability to instantiate
   100      * A convenience class for acquiring the capability to instantiate
    98      * reflective objects.  Use this instead of a raw call to {@link
   101      * reflective objects.  Use this instead of a raw call to {@link
   158     //
   161     //
   159     // Routines used by java.lang.reflect
   162     // Routines used by java.lang.reflect
   160     //
   163     //
   161     //
   164     //
   162 
   165 
   163     /** Called only by java.lang.reflect.Modifier's static initializer */
   166     /*
   164     public void setLangReflectAccess(LangReflectAccess access) {
       
   165         langReflectAccess = access;
       
   166     }
       
   167 
       
   168     /**
       
   169      * Note: this routine can cause the declaring class for the field
   167      * Note: this routine can cause the declaring class for the field
   170      * be initialized and therefore must not be called until the
   168      * be initialized and therefore must not be called until the
   171      * first get/set of this field.
   169      * first get/set of this field.
   172      * @param field the field
   170      * @param field the field
   173      * @param override true if caller has overridden accessibility
   171      * @param override true if caller has overridden accessibility
   174      */
   172      */
   175     public FieldAccessor newFieldAccessor(Field field, boolean override) {
   173     public FieldAccessor newFieldAccessor(Field field, boolean override) {
   176         checkInitted();
   174         checkInitted();
   177 
   175 
   178         Field root = langReflectAccess().getRoot(field);
   176         Field root = langReflectAccess.getRoot(field);
   179         if (root != null) {
   177         if (root != null) {
   180             // FieldAccessor will use the root unless the modifiers have
   178             // FieldAccessor will use the root unless the modifiers have
   181             // been overrridden
   179             // been overrridden
   182             if (root.getModifiers() == field.getModifiers() || !override) {
   180             if (root.getModifiers() == field.getModifiers() || !override) {
   183                 field = root;
   181                 field = root;
   195                 method = altMethod;
   193                 method = altMethod;
   196             }
   194             }
   197         }
   195         }
   198 
   196 
   199         // use the root Method that will not cache caller class
   197         // use the root Method that will not cache caller class
   200         Method root = langReflectAccess().getRoot(method);
   198         Method root = langReflectAccess.getRoot(method);
   201         if (root != null) {
   199         if (root != null) {
   202             method = root;
   200             method = root;
   203         }
   201         }
   204 
   202 
   205         if (noInflation && !ReflectUtil.isVMAnonymousClass(method.getDeclaringClass())) {
   203         if (noInflation && !ReflectUtil.isVMAnonymousClass(method.getDeclaringClass())) {
   231             return new InstantiationExceptionConstructorAccessorImpl
   229             return new InstantiationExceptionConstructorAccessorImpl
   232                 ("Can not instantiate java.lang.Class");
   230                 ("Can not instantiate java.lang.Class");
   233         }
   231         }
   234 
   232 
   235         // use the root Constructor that will not cache caller class
   233         // use the root Constructor that will not cache caller class
   236         Constructor<?> root = langReflectAccess().getRoot(c);
   234         Constructor<?> root = langReflectAccess.getRoot(c);
   237         if (root != null) {
   235         if (root != null) {
   238             c = root;
   236             c = root;
   239         }
   237         }
   240 
   238 
   241         // Bootstrapping issue: since we use Class.newInstance() in
   239         // Bootstrapping issue: since we use Class.newInstance() in
   266     //
   264     //
   267     // Routines used by java.lang
   265     // Routines used by java.lang
   268     //
   266     //
   269     //
   267     //
   270 
   268 
   271     /** Creates a new java.lang.reflect.Field. Access checks as per
       
   272         java.lang.reflect.AccessibleObject are not overridden. */
       
   273     public Field newField(Class<?> declaringClass,
       
   274                           String name,
       
   275                           Class<?> type,
       
   276                           int modifiers,
       
   277                           int slot,
       
   278                           String signature,
       
   279                           byte[] annotations)
       
   280     {
       
   281         return langReflectAccess().newField(declaringClass,
       
   282                                             name,
       
   283                                             type,
       
   284                                             modifiers,
       
   285                                             slot,
       
   286                                             signature,
       
   287                                             annotations);
       
   288     }
       
   289 
       
   290     /** Creates a new java.lang.reflect.Method. Access checks as per
       
   291         java.lang.reflect.AccessibleObject are not overridden. */
       
   292     public Method newMethod(Class<?> declaringClass,
       
   293                             String name,
       
   294                             Class<?>[] parameterTypes,
       
   295                             Class<?> returnType,
       
   296                             Class<?>[] checkedExceptions,
       
   297                             int modifiers,
       
   298                             int slot,
       
   299                             String signature,
       
   300                             byte[] annotations,
       
   301                             byte[] parameterAnnotations,
       
   302                             byte[] annotationDefault)
       
   303     {
       
   304         return langReflectAccess().newMethod(declaringClass,
       
   305                                              name,
       
   306                                              parameterTypes,
       
   307                                              returnType,
       
   308                                              checkedExceptions,
       
   309                                              modifiers,
       
   310                                              slot,
       
   311                                              signature,
       
   312                                              annotations,
       
   313                                              parameterAnnotations,
       
   314                                              annotationDefault);
       
   315     }
       
   316 
       
   317     /** Creates a new java.lang.reflect.Constructor. Access checks as
   269     /** Creates a new java.lang.reflect.Constructor. Access checks as
   318         per java.lang.reflect.AccessibleObject are not overridden. */
   270         per java.lang.reflect.AccessibleObject are not overridden. */
   319     public Constructor<?> newConstructor(Class<?> declaringClass,
   271     public Constructor<?> newConstructor(Class<?> declaringClass,
   320                                          Class<?>[] parameterTypes,
   272                                          Class<?>[] parameterTypes,
   321                                          Class<?>[] checkedExceptions,
   273                                          Class<?>[] checkedExceptions,
   323                                          int slot,
   275                                          int slot,
   324                                          String signature,
   276                                          String signature,
   325                                          byte[] annotations,
   277                                          byte[] annotations,
   326                                          byte[] parameterAnnotations)
   278                                          byte[] parameterAnnotations)
   327     {
   279     {
   328         return langReflectAccess().newConstructor(declaringClass,
   280         return langReflectAccess.newConstructor(declaringClass,
   329                                                   parameterTypes,
   281                                                 parameterTypes,
   330                                                   checkedExceptions,
   282                                                 checkedExceptions,
   331                                                   modifiers,
   283                                                 modifiers,
   332                                                   slot,
   284                                                 slot,
   333                                                   signature,
   285                                                 signature,
   334                                                   annotations,
   286                                                 annotations,
   335                                                   parameterAnnotations);
   287                                                 parameterAnnotations);
   336     }
       
   337 
       
   338     /** Gets the MethodAccessor object for a java.lang.reflect.Method */
       
   339     public MethodAccessor getMethodAccessor(Method m) {
       
   340         return langReflectAccess().getMethodAccessor(m);
       
   341     }
       
   342 
       
   343     /** Sets the MethodAccessor object for a java.lang.reflect.Method */
       
   344     public void setMethodAccessor(Method m, MethodAccessor accessor) {
       
   345         langReflectAccess().setMethodAccessor(m, accessor);
       
   346     }
   288     }
   347 
   289 
   348     /** Gets the ConstructorAccessor object for a
   290     /** Gets the ConstructorAccessor object for a
   349         java.lang.reflect.Constructor */
   291         java.lang.reflect.Constructor */
   350     public ConstructorAccessor getConstructorAccessor(Constructor<?> c) {
   292     public ConstructorAccessor getConstructorAccessor(Constructor<?> c) {
   351         return langReflectAccess().getConstructorAccessor(c);
   293         return langReflectAccess.getConstructorAccessor(c);
   352     }
   294     }
   353 
   295 
   354     /** Sets the ConstructorAccessor object for a
   296     /** Sets the ConstructorAccessor object for a
   355         java.lang.reflect.Constructor */
   297         java.lang.reflect.Constructor */
   356     public void setConstructorAccessor(Constructor<?> c,
   298     public void setConstructorAccessor(Constructor<?> c,
   357                                        ConstructorAccessor accessor)
   299                                        ConstructorAccessor accessor)
   358     {
   300     {
   359         langReflectAccess().setConstructorAccessor(c, accessor);
   301         langReflectAccess.setConstructorAccessor(c, accessor);
   360     }
   302     }
   361 
   303 
   362     /** Makes a copy of the passed method. The returned method is a
   304     /** Makes a copy of the passed method. The returned method is a
   363         "child" of the passed one; see the comments in Method.java for
   305         "child" of the passed one; see the comments in Method.java for
   364         details. */
   306         details. */
   365     public Method copyMethod(Method arg) {
   307     public Method copyMethod(Method arg) {
   366         return langReflectAccess().copyMethod(arg);
   308         return langReflectAccess.copyMethod(arg);
   367     }
   309     }
   368 
   310 
   369     /** Makes a copy of the passed method. The returned method is NOT
   311     /** Makes a copy of the passed method. The returned method is NOT
   370      * a "child" but a "sibling" of the Method in arg. Should only be
   312      * a "child" but a "sibling" of the Method in arg. Should only be
   371      * used on non-root methods. */
   313      * used on non-root methods. */
   372     public Method leafCopyMethod(Method arg) {
   314     public Method leafCopyMethod(Method arg) {
   373         return langReflectAccess().leafCopyMethod(arg);
   315         return langReflectAccess.leafCopyMethod(arg);
   374     }
   316     }
   375 
   317 
   376 
   318 
   377     /** Makes a copy of the passed field. The returned field is a
   319     /** Makes a copy of the passed field. The returned field is a
   378         "child" of the passed one; see the comments in Field.java for
   320         "child" of the passed one; see the comments in Field.java for
   379         details. */
   321         details. */
   380     public Field copyField(Field arg) {
   322     public Field copyField(Field arg) {
   381         return langReflectAccess().copyField(arg);
   323         return langReflectAccess.copyField(arg);
   382     }
   324     }
   383 
   325 
   384     /** Makes a copy of the passed constructor. The returned
   326     /** Makes a copy of the passed constructor. The returned
   385         constructor is a "child" of the passed one; see the comments
   327         constructor is a "child" of the passed one; see the comments
   386         in Constructor.java for details. */
   328         in Constructor.java for details. */
   387     public <T> Constructor<T> copyConstructor(Constructor<T> arg) {
   329     public <T> Constructor<T> copyConstructor(Constructor<T> arg) {
   388         return langReflectAccess().copyConstructor(arg);
   330         return langReflectAccess.copyConstructor(arg);
   389     }
   331     }
   390 
   332 
   391     /** Gets the byte[] that encodes TypeAnnotations on an executable.
   333     /** Gets the byte[] that encodes TypeAnnotations on an executable.
   392      */
   334      */
   393     public byte[] getExecutableTypeAnnotationBytes(Executable ex) {
   335     public byte[] getExecutableTypeAnnotationBytes(Executable ex) {
   394         return langReflectAccess().getExecutableTypeAnnotationBytes(ex);
   336         return langReflectAccess.getExecutableTypeAnnotationBytes(ex);
   395     }
   337     }
   396 
   338 
   397     public Class<?>[] getExecutableSharedParameterTypes(Executable ex) {
   339     public Class<?>[] getExecutableSharedParameterTypes(Executable ex) {
   398         return langReflectAccess().getExecutableSharedParameterTypes(ex);
   340         return langReflectAccess.getExecutableSharedParameterTypes(ex);
   399     }
   341     }
   400 
   342 
   401     public <T> T newInstance(Constructor<T> ctor, Object[] args, Class<?> caller)
   343     public <T> T newInstance(Constructor<T> ctor, Object[] args, Class<?> caller)
   402         throws IllegalAccessException, InstantiationException, InvocationTargetException
   344         throws IllegalAccessException, InstantiationException, InvocationTargetException
   403     {
   345     {
   404         return langReflectAccess().newInstance(ctor, args, caller);
   346         return langReflectAccess.newInstance(ctor, args, caller);
   405     }
   347     }
   406 
   348 
   407     //--------------------------------------------------------------------------
   349     //--------------------------------------------------------------------------
   408     //
   350     //
   409     // Routines used by serialization
   351     // Routines used by serialization
   524                                              constructorToCall.getDeclaringClass());
   466                                              constructorToCall.getDeclaringClass());
   525         Constructor<?> c = newConstructor(constructorToCall.getDeclaringClass(),
   467         Constructor<?> c = newConstructor(constructorToCall.getDeclaringClass(),
   526                                           constructorToCall.getParameterTypes(),
   468                                           constructorToCall.getParameterTypes(),
   527                                           constructorToCall.getExceptionTypes(),
   469                                           constructorToCall.getExceptionTypes(),
   528                                           constructorToCall.getModifiers(),
   470                                           constructorToCall.getModifiers(),
   529                                           langReflectAccess().
   471                                           langReflectAccess.
   530                                           getConstructorSlot(constructorToCall),
   472                                           getConstructorSlot(constructorToCall),
   531                                           langReflectAccess().
   473                                           langReflectAccess.
   532                                           getConstructorSignature(constructorToCall),
   474                                           getConstructorSignature(constructorToCall),
   533                                           langReflectAccess().
   475                                           langReflectAccess.
   534                                           getConstructorAnnotations(constructorToCall),
   476                                           getConstructorAnnotations(constructorToCall),
   535                                           langReflectAccess().
   477                                           langReflectAccess.
   536                                           getConstructorParameterAnnotations(constructorToCall));
   478                                           getConstructorParameterAnnotations(constructorToCall));
   537         setConstructorAccessor(c, acc);
   479         setConstructorAccessor(c, acc);
   538         c.setAccessible(true);
   480         c.setAccessible(true);
   539         return c;
   481         return c;
   540     }
   482     }
   723             "true".equals(props.getProperty("jdk.disableSerialConstructorChecks"));
   665             "true".equals(props.getProperty("jdk.disableSerialConstructorChecks"));
   724 
   666 
   725         initted = true;
   667         initted = true;
   726     }
   668     }
   727 
   669 
   728     private static LangReflectAccess langReflectAccess() {
       
   729         if (langReflectAccess == null) {
       
   730             // Call a static method to get class java.lang.reflect.Modifier
       
   731             // initialized. Its static initializer will cause
       
   732             // setLangReflectAccess() to be called from the context of the
       
   733             // java.lang.reflect package.
       
   734             Modifier.isPublic(Modifier.PUBLIC);
       
   735         }
       
   736         return langReflectAccess;
       
   737     }
       
   738 
       
   739     /**
   670     /**
   740      * Returns true if classes are defined in the classloader and same package, false
   671      * Returns true if classes are defined in the classloader and same package, false
   741      * otherwise.
   672      * otherwise.
   742      * @param cl1 a class
   673      * @param cl1 a class
   743      * @param cl2 another class
   674      * @param cl2 another class