< prev index next >

src/java.base/share/classes/jdk/internal/reflect/ReflectionFactory.java

Print this page

        

@@ -171,10 +171,16 @@
      * @param field the field
      * @param override true if caller has overridden accessibility
      */
     public FieldAccessor newFieldAccessor(Field field, boolean override) {
         checkInitted();
+
+        // use the root Field that will not cache caller class
+        Field f = langReflectAccess.getRoot(field);
+        if (f != null) {
+            field = f;
+        }
         return UnsafeFieldAccessorFactory.newFieldAccessor(field, override);
     }
 
     public MethodAccessor newMethodAccessor(Method method) {
         checkInitted();

@@ -184,10 +190,16 @@
             if (altMethod != null) {
                 method = altMethod;
             }
         }
 
+        // use the root Method that will not cache caller class
+        Method m = langReflectAccess.getRoot(method);
+        if (m != null) {
+            method = m;
+        }
+
         if (noInflation && !ReflectUtil.isVMAnonymousClass(method.getDeclaringClass())) {
             return new MethodAccessorGenerator().
                 generateMethod(method.getDeclaringClass(),
                                method.getName(),
                                method.getParameterTypes(),

@@ -213,10 +225,17 @@
         }
         if (declaringClass == Class.class) {
             return new InstantiationExceptionConstructorAccessorImpl
                 ("Can not instantiate java.lang.Class");
         }
+
+        // use the root Constructor that will not cache caller class
+        Constructor<?> ctr = langReflectAccess.getRoot(c);
+        if (ctr != null) {
+            c = ctr;
+        }
+
         // Bootstrapping issue: since we use Class.newInstance() in
         // the ConstructorAccessor generation process, we have to
         // break the cycle here.
         if (Reflection.isSubclassOf(declaringClass,
                                     ConstructorAccessorImpl.class)) {
< prev index next >