Package ognl

Class AccessibleObjectHandlerJDK9Plus

  • All Implemented Interfaces:
    AccessibleObjectHandler

    class AccessibleObjectHandlerJDK9Plus
    extends java.lang.Object
    implements AccessibleObjectHandler
    Utilizes a JDK 9 and later mechanism for changing the accessibility level of a given AccessibleObject.

    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 Detail

      • CLAZZ_UNSAFE

        private static final java.lang.Class<?> CLAZZ_UNSAFE
      • UNSAFE_INSTANCE

        private static final java.lang.Object UNSAFE_INSTANCE
      • UNSAFE_OBJECT_FIELD_OFFSET_METHOD

        private static final java.lang.reflect.Method UNSAFE_OBJECT_FIELD_OFFSET_METHOD
      • UNSAFE_PUT_BOOLEAN_METHOD

        private static final java.lang.reflect.Method UNSAFE_PUT_BOOLEAN_METHOD
      • ACCESSIBLE_OBJECT_OVERRIDE_FIELD

        private static final java.lang.reflect.Field ACCESSIBLE_OBJECT_OVERRIDE_FIELD
      • ACCESSIBLE_OBJECT_OVERRIDE_FIELD_OFFSET

        private static final long ACCESSIBLE_OBJECT_OVERRIDE_FIELD_OFFSET
    • Constructor Detail

      • AccessibleObjectHandlerJDK9Plus

        private AccessibleObjectHandlerJDK9Plus()
        Private constructor
    • Method Detail

      • unsafeOrDescendant

        static boolean unsafeOrDescendant​(java.lang.Class<?> clazz)
        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

        private static java.lang.Class<?> instantiateClazzUnsafe()
        Instantiate an instance of the Unsafe class.
        Returns:
        class if available, null otherwise
      • instantiateUnsafeInstance

        private static java.lang.Object instantiateUnsafeInstance()
        Instantiate an instance of Unsafe object.
        Returns:
        instance if available, null otherwise
      • instantiateUnsafeObjectFieldOffsetMethod

        private static java.lang.reflect.Method instantiateUnsafeObjectFieldOffsetMethod()
        Instantiate an Unsafe.objectFieldOffset() method instance.
        Returns:
        method if available, null otherwise
      • instantiateUnsafePutBooleanMethod

        private static java.lang.reflect.Method instantiateUnsafePutBooleanMethod()
        Instantiate an Unsafe.putBoolean() method instance.
        Returns:
        method if available, null otherwise
      • instantiateAccessibleObjectOverrideField

        private static java.lang.reflect.Field 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

        static AccessibleObjectHandler 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

        public void setAccessible​(java.lang.reflect.AccessibleObject accessibleObject,
                                  boolean flag)
        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:
        setAccessible in interface AccessibleObjectHandler
        Parameters:
        accessibleObject - the AccessibleObject upon which to apply the flag.
        flag - the new accessible flag value.