Class AccessibleObjectHandlerJDK9Plus
- All Implemented Interfaces:
AccessibleObjectHandler
If the JDK 9+ mechanism fails, this class will fall back to a standard pre-JDK 9 reflection mechanism. Note: That may cause "WARNING: Illegal reflective access" output to be generated to stdout/stderr.
For reference, this class draws on information from the following locations: - Post about Illegal Reflective Access what is an illegal reflective access - Blog on Unsafe Java Magic. Part 4: sun.misc.Unsafe - Blog on Unsafe Guide to sun.misc.Unsafe - JEP about access to Unsafe being retained in JDK 9 JEP 260: Encapsulate Most Internal APIs
In addition to the above, inspiration was drawn from Gson: PR 1218, PR 1306.
Appreciation and credit to the authors, contributors and commenters for the information contained in the preceding links.
- Since:
- 3.1.24
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) static AccessibleObjectHandlerPackage-level generator of an AccessibleObjectHandlerJDK9Plus instance.private static longAttempt to determine the AccessibleObject override field offset.private static FieldInstantiate an AccessibleObject override field instance.private static Class<?> Instantiate an instance of the Unsafe class.private static ObjectInstantiate an instance of Unsafe object.private static MethodInstantiate an Unsafe.objectFieldOffset() method instance.private static MethodInstantiate an Unsafe.putBoolean() method instance.voidsetAccessible(AccessibleObject accessibleObject, boolean flag) Utilize accessibility modification mechanism for JDK 9 (Java Major Version 9) and later.(package private) static booleanunsafeOrDescendant(Class<?> clazz) Package-accessible method to determine if a given class is Unsafe or a descendant of Unsafe.
-
Field Details
-
CLAZZ_UNSAFE
-
UNSAFE_INSTANCE
-
UNSAFE_OBJECT_FIELD_OFFSET_METHOD
-
UNSAFE_PUT_BOOLEAN_METHOD
-
ACCESSIBLE_OBJECT_OVERRIDE_FIELD
-
ACCESSIBLE_OBJECT_OVERRIDE_FIELD_OFFSET
private static final long ACCESSIBLE_OBJECT_OVERRIDE_FIELD_OFFSET
-
-
Constructor Details
-
AccessibleObjectHandlerJDK9Plus
private AccessibleObjectHandlerJDK9Plus()Private constructor
-
-
Method Details
-
unsafeOrDescendant
Package-accessible method to determine if a given class is Unsafe or a descendant of Unsafe.- Parameters:
clazz- the Class upon which to perform the unsafe check.- Returns:
- true if parameter is Unsafe or a descendant, false otherwise
-
instantiateClazzUnsafe
Instantiate an instance of the Unsafe class.- Returns:
- class if available, null otherwise
-
instantiateUnsafeInstance
Instantiate an instance of Unsafe object.- Returns:
- instance if available, null otherwise
-
instantiateUnsafeObjectFieldOffsetMethod
Instantiate an Unsafe.objectFieldOffset() method instance.- Returns:
- method if available, null otherwise
-
instantiateUnsafePutBooleanMethod
Instantiate an Unsafe.putBoolean() method instance.- Returns:
- method if available, null otherwise
-
instantiateAccessibleObjectOverrideField
Instantiate an AccessibleObject override field instance.- Returns:
- field if available, null otherwise
-
determineAccessibleObjectOverrideFieldOffset
private static long determineAccessibleObjectOverrideFieldOffset()Attempt to determine the AccessibleObject override field offset.- Returns:
- field offset if available, -1 otherwise
-
createHandler
Package-level generator of an AccessibleObjectHandlerJDK9Plus instance.Not intended for use outside of the package.
Note: An AccessibleObjectHandlerJDK9Plus will only be created if running on a JDK9+ and the environment flag is set. Otherwise this method will return an AccessibleHandlerPreJDK9 instance instead,
- Returns:
- an AccessibleObjectHandler instance
- Since:
- 3.1.24
-
setAccessible
Utilize accessibility modification mechanism for JDK 9 (Java Major Version 9) and later. Should that mechanism fail, attempt a standard pre-JDK9 accessibility modification.- Specified by:
setAccessiblein interfaceAccessibleObjectHandler- Parameters:
accessibleObject- the AccessibleObject upon which to apply the flag.flag- the new accessible flag value.
-