WARNING: An illegal reflective access operation has occurred WARNING: Illegal reflective access by org.example.Main (file:/Users/alphag0/Desktop/Demo/target/classes/) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int) WARNING: Please consider reporting this to the maintainers of org.example.Main WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations WARNING: All illegal access operations will be denied in a future release
Exception in thread "main" java.lang.reflect.InaccessibleObjectException: Unable to make protectedfinal java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int) throws java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module @4bde3f8a at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354) at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297) at java.base/java.lang.reflect.Method.checkCanSetAccessible(Method.java:199) at java.base/java.lang.reflect.Method.setAccessible(Method.java:193) at org.example.Main.main(Main.java:12)
/** * Provides the caller with the capability of performing unsafe * operations. * * <p>The returned {@code Unsafe} object should be carefully guarded * by the caller, since it can be used to read and write data at arbitrary * memory addresses. It must never be passed to untrusted code. * * <p>Most methods in this class are very low-level, and correspond to a * small number of hardware instructions (on typical machines). Compilers * are encouraged to optimize these methods accordingly. * * <p>Here is a suggested idiom for using unsafe operations: * * <pre> {@code * class MyTrustedClass { * private static final Unsafe unsafe = Unsafe.getUnsafe(); * ... * private long myCountAddress = ...; * public int getCount() { return unsafe.getByte(myCountAddress); } * }}</pre> * * (It may assist compilers to make the local variable {@code final}.) * * @throws SecurityException if the class loader of the caller * class is not in the system domain in which all permissions * are granted. */ @CallerSensitive publicstatic Unsafe getUnsafe() { Class<?> caller = Reflection.getCallerClass(); if (!VM.isSystemDomainLoader(caller.getClassLoader())) thrownewSecurityException("Unsafe"); return theUnsafe; }
publicvoidsetAccessible(boolean flag) { AccessibleObject.checkPermission(); if (flag) checkCanSetAccessible(Reflection.getCallerClass()); setAccessible0(flag); }
1 2 3 4 5 6 7 8 9 10
staticvoidcheckPermission() { @SuppressWarnings("removal") SecurityManagersm= System.getSecurityManager(); if (sm != null) { // SecurityConstants.ACCESS_PERMISSION is used to check // whether a client has sufficient privilege to defeat Java // language access control checks. sm.checkPermission(SecurityConstants.ACCESS_PERMISSION); } }