< prev index next >

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

Print this page

 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 import jdk.internal.access.SharedSecrets;
 34 import jdk.internal.misc.VM;

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

 89      * @param currentClass the class performing the access
 90      * @param memberClass the declaring class of the member being accessed
 91      * @param targetClass the class of target object if accessing instance
 92      *                    field or method;
 93      *                    or the declaring class if accessing constructor;
 94      *                    or null if accessing static field or method
 95      * @param modifiers the member's access modifiers
 96      * @throws IllegalAccessException if access to member is denied
 97      */
 98     public static void ensureMemberAccess(Class<?> currentClass,
 99                                           Class<?> memberClass,
100                                           Class<?> targetClass,
101                                           int modifiers)
102         throws IllegalAccessException
103     {
104         if (!verifyMemberAccess(currentClass, memberClass, targetClass, modifiers)) {
105             throw newIllegalAccessException(currentClass, memberClass, targetClass, modifiers);
106         }
107     }
108 

109     public static void ensureNativeAccess(Class<?> currentClass) {
110         Module module = currentClass.getModule();
111         if (!SharedSecrets.getJavaLangAccess().isEnableNativeAccess(module)) {
112             throw new IllegalCallerException("Illegal native access from: " + module);
113         }
114     }
115 
116     /**
117      * Verify access to a member and return {@code true} if it is granted.
118      *
119      * @param currentClass the class performing the access
120      * @param memberClass the declaring class of the member being accessed
121      * @param targetClass the class of target object if accessing instance
122      *                    field or method;
123      *                    or the declaring class if accessing constructor;
124      *                    or null if accessing static field or method
125      * @param modifiers the member's access modifiers
126      * @return {@code true} if access to member is granted
127      */
128     public static boolean verifyMemberAccess(Class<?> currentClass,

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

 90      * @param currentClass the class performing the access
 91      * @param memberClass the declaring class of the member being accessed
 92      * @param targetClass the class of target object if accessing instance
 93      *                    field or method;
 94      *                    or the declaring class if accessing constructor;
 95      *                    or null if accessing static field or method
 96      * @param modifiers the member's access modifiers
 97      * @throws IllegalAccessException if access to member is denied
 98      */
 99     public static void ensureMemberAccess(Class<?> currentClass,
100                                           Class<?> memberClass,
101                                           Class<?> targetClass,
102                                           int modifiers)
103         throws IllegalAccessException
104     {
105         if (!verifyMemberAccess(currentClass, memberClass, targetClass, modifiers)) {
106             throw newIllegalAccessException(currentClass, memberClass, targetClass, modifiers);
107         }
108     }
109 
110     @ForceInline
111     public static void ensureNativeAccess(Class<?> currentClass) {
112         Module module = currentClass.getModule();
113         if (!SharedSecrets.getJavaLangAccess().isEnableNativeAccess(module)) {
114             throw new IllegalCallerException("Illegal native access from: " + module);
115         }
116     }
117 
118     /**
119      * Verify access to a member and return {@code true} if it is granted.
120      *
121      * @param currentClass the class performing the access
122      * @param memberClass the declaring class of the member being accessed
123      * @param targetClass the class of target object if accessing instance
124      *                    field or method;
125      *                    or the declaring class if accessing constructor;
126      *                    or null if accessing static field or method
127      * @param modifiers the member's access modifiers
128      * @return {@code true} if access to member is granted
129      */
130     public static boolean verifyMemberAccess(Class<?> currentClass,
< prev index next >