11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 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 sun.reflect; 27 28 import java.lang.reflect.*; 29 import java.util.HashMap; 30 import java.util.Map; 31 32 /** Common utility routines used by both java.lang and 33 java.lang.reflect */ 34 35 public class Reflection { 36 37 /** Used to filter out fields and methods from certain classes from public 38 view, where they are sensitive or they may contain VM-internal objects. 39 These Maps are updated very rarely. Rather than synchronize on 40 each access, we use copy-on-write */ 41 private static volatile Map<Class<?>,String[]> fieldFilterMap; 42 private static volatile Map<Class<?>,String[]> methodFilterMap; 43 44 static { 45 Map<Class<?>,String[]> map = new HashMap<Class<?>,String[]>(); 46 map.put(Reflection.class, 47 new String[] {"fieldFilterMap", "methodFilterMap"}); 48 map.put(System.class, new String[] {"security"}); 49 map.put(Class.class, new String[] {"classLoader"}); 50 fieldFilterMap = map; 51 52 methodFilterMap = new HashMap<>(); 53 } 54 55 /** Returns the class of the caller of the method calling this method, 56 ignoring frames associated with java.lang.reflect.Method.invoke() 57 and its implementation. */ 58 @CallerSensitive 59 public static native Class<?> getCallerClass(); 60 61 /** 62 * @deprecated This method will be removed in JDK 9. 63 * This method is a private JDK API and retained temporarily for 64 * existing code to run until a replacement API is defined. 65 */ 66 @Deprecated 67 public static native Class<?> getCallerClass(int depth); 68 69 /** Retrieves the access flags written to the class file. For 70 inner classes these flags may differ from those returned by 71 Class.getModifiers(), which searches the InnerClasses 72 attribute to find the source-level access flags. This is used 73 instead of Class.getModifiers() for run-time access checks due 74 to compatibility reasons; see 4471811. Only the values of the 75 low 13 bits (i.e., a mask of 0x1FFF) are guaranteed to be 76 valid. */ 77 public static native int getClassAccessFlags(Class<?> c); 78 79 /** A quick "fast-path" check to try to avoid getCallerClass() 80 calls. */ 81 public static boolean quickCheckMemberAccess(Class<?> memberClass, 82 int modifiers) 83 { 84 return Modifier.isPublic(getClassAccessFlags(memberClass) & modifiers); 85 } 86 87 public static void ensureMemberAccess(Class<?> currentClass, 88 Class<?> memberClass, 89 Object target, 90 int modifiers) 91 throws IllegalAccessException 92 { 93 if (currentClass == null || memberClass == null) { 94 throw new InternalError(); 95 } 96 | 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 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 sun.reflect; 27 28 import java.lang.reflect.*; 29 import java.util.HashMap; 30 import java.util.Map; 31 import jdk.internal.HotSpotIntrinsicCandidate; 32 33 /** Common utility routines used by both java.lang and 34 java.lang.reflect */ 35 36 public class Reflection { 37 38 /** Used to filter out fields and methods from certain classes from public 39 view, where they are sensitive or they may contain VM-internal objects. 40 These Maps are updated very rarely. Rather than synchronize on 41 each access, we use copy-on-write */ 42 private static volatile Map<Class<?>,String[]> fieldFilterMap; 43 private static volatile Map<Class<?>,String[]> methodFilterMap; 44 45 static { 46 Map<Class<?>,String[]> map = new HashMap<Class<?>,String[]>(); 47 map.put(Reflection.class, 48 new String[] {"fieldFilterMap", "methodFilterMap"}); 49 map.put(System.class, new String[] {"security"}); 50 map.put(Class.class, new String[] {"classLoader"}); 51 fieldFilterMap = map; 52 53 methodFilterMap = new HashMap<>(); 54 } 55 56 /** Returns the class of the caller of the method calling this method, 57 ignoring frames associated with java.lang.reflect.Method.invoke() 58 and its implementation. */ 59 @CallerSensitive 60 @HotSpotIntrinsicCandidate 61 public static native Class<?> getCallerClass(); 62 63 /** 64 * @deprecated This method will be removed in JDK 9. 65 * This method is a private JDK API and retained temporarily for 66 * existing code to run until a replacement API is defined. 67 */ 68 @Deprecated 69 public static native Class<?> getCallerClass(int depth); 70 71 /** Retrieves the access flags written to the class file. For 72 inner classes these flags may differ from those returned by 73 Class.getModifiers(), which searches the InnerClasses 74 attribute to find the source-level access flags. This is used 75 instead of Class.getModifiers() for run-time access checks due 76 to compatibility reasons; see 4471811. Only the values of the 77 low 13 bits (i.e., a mask of 0x1FFF) are guaranteed to be 78 valid. */ 79 @HotSpotIntrinsicCandidate 80 public static native int getClassAccessFlags(Class<?> c); 81 82 /** A quick "fast-path" check to try to avoid getCallerClass() 83 calls. */ 84 public static boolean quickCheckMemberAccess(Class<?> memberClass, 85 int modifiers) 86 { 87 return Modifier.isPublic(getClassAccessFlags(memberClass) & modifiers); 88 } 89 90 public static void ensureMemberAccess(Class<?> currentClass, 91 Class<?> memberClass, 92 Object target, 93 int modifiers) 94 throws IllegalAccessException 95 { 96 if (currentClass == null || memberClass == null) { 97 throw new InternalError(); 98 } 99 |