42 import sun.reflect.ReflectionFactory; 43 44 /** This class provides the methods for fundamental JVM operations 45 * needed in the ORB that are not part of the public Java API. This includes: 46 * <ul> 47 * <li>throwException, which can throw undeclared checked exceptions. 48 * This is needed to handle throwing arbitrary exceptions across a standardized 49 * OMG interface that (incorrectly) does not specify appropriate exceptions.</li> 50 * <li>putXXX/getXXX methods that allow unchecked access to fields of objects. 51 * This is used for setting uninitialzed non-static final fields (which is 52 * impossible with reflection) and for speed.</li> 53 * <li>objectFieldOffset to obtain the field offsets for use in the putXXX/getXXX methods</li> 54 * <li>newConstructorForSerialization to get the special constructor required for a 55 * Serializable class</li> 56 * <li>latestUserDefinedLoader to get the latest user defined class loader from 57 * the call stack as required by the RMI-IIOP specification (really from the 58 * JDK 1.1 days)</li> 59 * </ul> 60 * The code that calls Bridge.get() must have the following Permissions: 61 * <ul> 62 * <li>RuntimePermission "reflectionFactoryAccess"</li> 63 * <li>BridgePermission "getBridge"</li> 64 * <li>ReflectPermission "suppressAccessChecks"</li> 65 * <li>StackFramePermission "retainClassReference"</li> 66 * </ul> 67 * <p> 68 * All of these permissions are required to obtain and correctly initialize 69 * the instance of Bridge. No security checks are performed on calls 70 * made to Bridge instance methods, so access to the Bridge instance 71 * must be protected. 72 * <p> 73 * This class is a singleton (per ClassLoader of course). Access to the 74 * instance is obtained through the Bridge.get() method. 75 */ 76 public final class Bridge 77 { 78 private static final Permission getBridgePermission = 79 new BridgePermission("getBridge"); 80 private static Bridge bridge = null ; 81 82 /** Access to Unsafe to read/write fields. */ 83 private static final Unsafe unsafe = AccessController.doPrivileged( 84 (PrivilegedAction<Unsafe>)() -> { 85 try { 88 return (Unsafe)field.get(null); 89 90 } catch (NoSuchFieldException |IllegalAccessException ex) { 91 throw new InternalError("Unsafe.theUnsafe field not available", ex); 92 } 93 } 94 ) ; 95 96 private final ReflectionFactory reflectionFactory ; 97 private final StackWalker stackWalker; 98 99 private Bridge() { 100 reflectionFactory = ReflectionFactory.getReflectionFactory(); 101 stackWalker = StackWalker.getInstance( 102 StackWalker.Option.RETAIN_CLASS_REFERENCE); 103 } 104 105 /** Fetch the Bridge singleton. This requires the following 106 * permissions: 107 * <ul> 108 * <li>RuntimePermission "reflectionFactoryAccess"</li> 109 * <li>BridgePermission "getBridge"</li> 110 * <li>ReflectPermission "suppressAccessChecks"</li> 111 * <li>StackFramePermission "retainClassReference"</li> 112 * </ul> 113 * @return The singleton instance of the Bridge class 114 * @throws SecurityException if the caller does not have the 115 * required permissions and the caller has a non-null security manager. 116 */ 117 public static final synchronized Bridge get() 118 { 119 SecurityManager sman = System.getSecurityManager() ; 120 if (sman != null) 121 sman.checkPermission( getBridgePermission ) ; 122 123 if (bridge == null) { 124 bridge = new Bridge() ; 125 } 126 127 return bridge ; 128 } 129 130 /** Returns true if the loader that loaded the frame's declaring class 131 * is a user loader (if it is not the platform class loader or one of | 42 import sun.reflect.ReflectionFactory; 43 44 /** This class provides the methods for fundamental JVM operations 45 * needed in the ORB that are not part of the public Java API. This includes: 46 * <ul> 47 * <li>throwException, which can throw undeclared checked exceptions. 48 * This is needed to handle throwing arbitrary exceptions across a standardized 49 * OMG interface that (incorrectly) does not specify appropriate exceptions.</li> 50 * <li>putXXX/getXXX methods that allow unchecked access to fields of objects. 51 * This is used for setting uninitialzed non-static final fields (which is 52 * impossible with reflection) and for speed.</li> 53 * <li>objectFieldOffset to obtain the field offsets for use in the putXXX/getXXX methods</li> 54 * <li>newConstructorForSerialization to get the special constructor required for a 55 * Serializable class</li> 56 * <li>latestUserDefinedLoader to get the latest user defined class loader from 57 * the call stack as required by the RMI-IIOP specification (really from the 58 * JDK 1.1 days)</li> 59 * </ul> 60 * The code that calls Bridge.get() must have the following Permissions: 61 * <ul> 62 * <li>BridgePermission "getBridge"</li> 63 * <li>ReflectPermission "suppressAccessChecks"</li> 64 * <li>RuntimePermission "getStackWalkerWithClassReference"</li> 65 * <li>RuntimePermission "reflectionFactoryAccess"</li> 66 * </ul> 67 * <p> 68 * All of these permissions are required to obtain and correctly initialize 69 * the instance of Bridge. No security checks are performed on calls 70 * made to Bridge instance methods, so access to the Bridge instance 71 * must be protected. 72 * <p> 73 * This class is a singleton (per ClassLoader of course). Access to the 74 * instance is obtained through the Bridge.get() method. 75 */ 76 public final class Bridge 77 { 78 private static final Permission getBridgePermission = 79 new BridgePermission("getBridge"); 80 private static Bridge bridge = null ; 81 82 /** Access to Unsafe to read/write fields. */ 83 private static final Unsafe unsafe = AccessController.doPrivileged( 84 (PrivilegedAction<Unsafe>)() -> { 85 try { 88 return (Unsafe)field.get(null); 89 90 } catch (NoSuchFieldException |IllegalAccessException ex) { 91 throw new InternalError("Unsafe.theUnsafe field not available", ex); 92 } 93 } 94 ) ; 95 96 private final ReflectionFactory reflectionFactory ; 97 private final StackWalker stackWalker; 98 99 private Bridge() { 100 reflectionFactory = ReflectionFactory.getReflectionFactory(); 101 stackWalker = StackWalker.getInstance( 102 StackWalker.Option.RETAIN_CLASS_REFERENCE); 103 } 104 105 /** Fetch the Bridge singleton. This requires the following 106 * permissions: 107 * <ul> 108 * <li>BridgePermission "getBridge"</li> 109 * <li>ReflectPermission "suppressAccessChecks"</li> 110 * <li>RuntimePermission "getStackWalkerWithClassReference"</li> 111 * <li>RuntimePermission "reflectionFactoryAccess"</li> 112 * </ul> 113 * @return The singleton instance of the Bridge class 114 * @throws SecurityException if the caller does not have the 115 * required permissions and the caller has a non-null security manager. 116 */ 117 public static final synchronized Bridge get() 118 { 119 SecurityManager sman = System.getSecurityManager() ; 120 if (sman != null) 121 sman.checkPermission( getBridgePermission ) ; 122 123 if (bridge == null) { 124 bridge = new Bridge() ; 125 } 126 127 return bridge ; 128 } 129 130 /** Returns true if the loader that loaded the frame's declaring class 131 * is a user loader (if it is not the platform class loader or one of |