< prev index next >

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

Print this page

 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 package jdk.internal.reflect;
 27 
 28 import java.lang.reflect.*;
 29 import java.util.HashMap;
 30 import java.util.Map;
 31 import java.util.Objects;
 32 import java.util.Set;
 33 

 34 import jdk.internal.access.SharedSecrets;
 35 import jdk.internal.misc.VM;
 36 import jdk.internal.module.ModuleBootstrap;
 37 import jdk.internal.vm.annotation.ForceInline;
 38 import jdk.internal.vm.annotation.IntrinsicCandidate;
 39 
 40 /** Common utility routines used by both java.lang and
 41     java.lang.reflect */
 42 
 43 public class Reflection {
 44 
 45     /** Used to filter out fields and methods from certain classes from public
 46         view, where they are sensitive or they may contain VM-internal objects.
 47         These Maps are updated very rarely. Rather than synchronize on
 48         each access, we use copy-on-write */
 49     private static volatile Map<Class<?>, Set<String>> fieldFilterMap;
 50     private static volatile Map<Class<?>, Set<String>> methodFilterMap;
 51     private static final String WILDCARD = "*";
 52     public static final Set<String> ALL_MEMBERS = Set.of(WILDCARD);
 53 

 98      * @param modifiers the member's access modifiers
 99      * @throws IllegalAccessException if access to member is denied
100      */
101     public static void ensureMemberAccess(Class<?> currentClass,
102                                           Class<?> memberClass,
103                                           Class<?> targetClass,
104                                           int modifiers)
105         throws IllegalAccessException
106     {
107         if (!verifyMemberAccess(currentClass, memberClass, targetClass, modifiers)) {
108             throw newIllegalAccessException(currentClass, memberClass, targetClass, modifiers);
109         }
110     }
111 
112     @ForceInline
113     public static void ensureNativeAccess(Class<?> currentClass, Class<?> owner, String methodName) {
114         // if there is no caller class, act as if the call came from unnamed module of system class loader
115         Module module = currentClass != null ?
116                 currentClass.getModule() :
117                 ClassLoader.getSystemClassLoader().getUnnamedModule();
118         SharedSecrets.getJavaLangAccess().ensureNativeAccess(module, owner, methodName);



119     }
120 
121     /**
122      * Verify access to a member and return {@code true} if it is granted.
123      *
124      * @param currentClass the class performing the access
125      * @param memberClass the declaring class of the member being accessed
126      * @param targetClass the class of target object if accessing instance
127      *                    field or method;
128      *                    or the declaring class if accessing constructor;
129      *                    or null if accessing static field or method
130      * @param modifiers the member's access modifiers
131      * @return {@code true} if access to member is granted
132      */
133     public static boolean verifyMemberAccess(Class<?> currentClass,
134                                              Class<?> memberClass,
135                                              Class<?> targetClass,
136                                              int modifiers)
137     {
138         Objects.requireNonNull(currentClass);

 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 package jdk.internal.reflect;
 27 
 28 import java.lang.reflect.*;
 29 import java.util.HashMap;
 30 import java.util.Map;
 31 import java.util.Objects;
 32 import java.util.Set;
 33 
 34 import jdk.internal.access.JavaLangAccess;
 35 import jdk.internal.access.SharedSecrets;
 36 import jdk.internal.misc.VM;
 37 import jdk.internal.module.ModuleBootstrap;
 38 import jdk.internal.vm.annotation.ForceInline;
 39 import jdk.internal.vm.annotation.IntrinsicCandidate;
 40 
 41 /** Common utility routines used by both java.lang and
 42     java.lang.reflect */
 43 
 44 public class Reflection {
 45 
 46     /** Used to filter out fields and methods from certain classes from public
 47         view, where they are sensitive or they may contain VM-internal objects.
 48         These Maps are updated very rarely. Rather than synchronize on
 49         each access, we use copy-on-write */
 50     private static volatile Map<Class<?>, Set<String>> fieldFilterMap;
 51     private static volatile Map<Class<?>, Set<String>> methodFilterMap;
 52     private static final String WILDCARD = "*";
 53     public static final Set<String> ALL_MEMBERS = Set.of(WILDCARD);
 54 

 99      * @param modifiers the member's access modifiers
100      * @throws IllegalAccessException if access to member is denied
101      */
102     public static void ensureMemberAccess(Class<?> currentClass,
103                                           Class<?> memberClass,
104                                           Class<?> targetClass,
105                                           int modifiers)
106         throws IllegalAccessException
107     {
108         if (!verifyMemberAccess(currentClass, memberClass, targetClass, modifiers)) {
109             throw newIllegalAccessException(currentClass, memberClass, targetClass, modifiers);
110         }
111     }
112 
113     @ForceInline
114     public static void ensureNativeAccess(Class<?> currentClass, Class<?> owner, String methodName) {
115         // if there is no caller class, act as if the call came from unnamed module of system class loader
116         Module module = currentClass != null ?
117                 currentClass.getModule() :
118                 ClassLoader.getSystemClassLoader().getUnnamedModule();
119         class Holder {
120             static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();
121         }
122         Holder.JLA.ensureNativeAccess(module, owner, methodName, currentClass);
123     }
124 
125     /**
126      * Verify access to a member and return {@code true} if it is granted.
127      *
128      * @param currentClass the class performing the access
129      * @param memberClass the declaring class of the member being accessed
130      * @param targetClass the class of target object if accessing instance
131      *                    field or method;
132      *                    or the declaring class if accessing constructor;
133      *                    or null if accessing static field or method
134      * @param modifiers the member's access modifiers
135      * @return {@code true} if access to member is granted
136      */
137     public static boolean verifyMemberAccess(Class<?> currentClass,
138                                              Class<?> memberClass,
139                                              Class<?> targetClass,
140                                              int modifiers)
141     {
142         Objects.requireNonNull(currentClass);
< prev index next >