< prev index next >

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

Print this page




 118      * <code>"reflectionFactoryAccess"</code>.  This may result in a
 119      * security exception.
 120      *
 121      * <p> The returned <code>ReflectionFactory</code> object should be
 122      * carefully guarded by the caller, since it can be used to read and
 123      * write private data and invoke private methods, as well as to load
 124      * unverified bytecodes.  It must never be passed to untrusted code.
 125      *
 126      * @exception SecurityException if a security manager exists and its
 127      *             <code>checkPermission</code> method doesn't allow
 128      *             access to the RuntimePermission "reflectionFactoryAccess".  */
 129     public static ReflectionFactory getReflectionFactory() {
 130         SecurityManager security = System.getSecurityManager();
 131         if (security != null) {
 132             // TO DO: security.checkReflectionFactoryAccess();
 133             security.checkPermission(reflectionFactoryAccessPerm);
 134         }
 135         return soleInstance;
 136     }
 137 


















 138     //--------------------------------------------------------------------------
 139     //
 140     // Routines used by java.lang.reflect
 141     //
 142     //
 143 
 144     /** Called only by java.lang.reflect.Modifier's static initializer */
 145     public void setLangReflectAccess(LangReflectAccess access) {
 146         langReflectAccess = access;
 147     }
 148 
 149     /**
 150      * Note: this routine can cause the declaring class for the field
 151      * be initialized and therefore must not be called until the
 152      * first get/set of this field.
 153      * @param field the field
 154      * @param override true if caller has overridden accessibility
 155      */
 156     public FieldAccessor newFieldAccessor(Field field, boolean override) {
 157         checkInitted();
 158         return UnsafeFieldAccessorFactory.newFieldAccessor(field, override);
 159     }
 160 
 161     public MethodAccessor newMethodAccessor(Method method) {
 162         checkInitted();
 163 








 164         if (noInflation && !ReflectUtil.isVMAnonymousClass(method.getDeclaringClass())) {
 165             return new MethodAccessorGenerator().
 166                 generateMethod(method.getDeclaringClass(),
 167                                method.getName(),
 168                                method.getParameterTypes(),
 169                                method.getReturnType(),
 170                                method.getExceptionTypes(),
 171                                method.getModifiers());
 172         } else {
 173             NativeMethodAccessorImpl acc =
 174                 new NativeMethodAccessorImpl(method);
 175             DelegatingMethodAccessorImpl res =
 176                 new DelegatingMethodAccessorImpl(acc);
 177             acc.setParent(res);
 178             return res;
 179         }
 180     }
 181 
 182     public ConstructorAccessor newConstructorAccessor(Constructor<?> c) {
 183         checkInitted();




 118      * <code>"reflectionFactoryAccess"</code>.  This may result in a
 119      * security exception.
 120      *
 121      * <p> The returned <code>ReflectionFactory</code> object should be
 122      * carefully guarded by the caller, since it can be used to read and
 123      * write private data and invoke private methods, as well as to load
 124      * unverified bytecodes.  It must never be passed to untrusted code.
 125      *
 126      * @exception SecurityException if a security manager exists and its
 127      *             <code>checkPermission</code> method doesn't allow
 128      *             access to the RuntimePermission "reflectionFactoryAccess".  */
 129     public static ReflectionFactory getReflectionFactory() {
 130         SecurityManager security = System.getSecurityManager();
 131         if (security != null) {
 132             // TO DO: security.checkReflectionFactoryAccess();
 133             security.checkPermission(reflectionFactoryAccessPerm);
 134         }
 135         return soleInstance;
 136     }
 137 
 138     /**
 139      * Returns an alternate reflective Method instance for the given method
 140      * intended for reflection to invoke, if present.
 141      *
 142      * A trusted method can define an alternate implementation for a method `foo`
 143      * by defining a method named "reflected$foo" that will be invoked
 144      * reflectively.
 145      */
 146     private static Method findMethodForReflection(Method method) {
 147         String altName = "reflected$" + method.getName();
 148         try {
 149            return method.getDeclaringClass()
 150                         .getDeclaredMethod(altName, method.getParameterTypes());
 151         } catch (NoSuchMethodException ex) {
 152             return null;
 153         }
 154     }
 155 
 156     //--------------------------------------------------------------------------
 157     //
 158     // Routines used by java.lang.reflect
 159     //
 160     //
 161 
 162     /** Called only by java.lang.reflect.Modifier's static initializer */
 163     public void setLangReflectAccess(LangReflectAccess access) {
 164         langReflectAccess = access;
 165     }
 166 
 167     /**
 168      * Note: this routine can cause the declaring class for the field
 169      * be initialized and therefore must not be called until the
 170      * first get/set of this field.
 171      * @param field the field
 172      * @param override true if caller has overridden accessibility
 173      */
 174     public FieldAccessor newFieldAccessor(Field field, boolean override) {
 175         checkInitted();
 176         return UnsafeFieldAccessorFactory.newFieldAccessor(field, override);
 177     }
 178 
 179     public MethodAccessor newMethodAccessor(Method method) {
 180         checkInitted();
 181 
 182         boolean noInflation = ReflectionFactory.noInflation;
 183         if (Reflection.isCallerSensitive(method)) {
 184             Method altMethod = findMethodForReflection(method);
 185             if (altMethod != null) {
 186                 method = altMethod;
 187             }
 188         }
 189 
 190         if (noInflation && !ReflectUtil.isVMAnonymousClass(method.getDeclaringClass())) {
 191             return new MethodAccessorGenerator().
 192                 generateMethod(method.getDeclaringClass(),
 193                                method.getName(),
 194                                method.getParameterTypes(),
 195                                method.getReturnType(),
 196                                method.getExceptionTypes(),
 197                                method.getModifiers());
 198         } else {
 199             NativeMethodAccessorImpl acc =
 200                 new NativeMethodAccessorImpl(method);
 201             DelegatingMethodAccessorImpl res =
 202                 new DelegatingMethodAccessorImpl(acc);
 203             acc.setParent(res);
 204             return res;
 205         }
 206     }
 207 
 208     public ConstructorAccessor newConstructorAccessor(Constructor<?> c) {
 209         checkInitted();


< prev index next >