Class StandardReflectionDriver


  • class StandardReflectionDriver
    extends ReflectionDriver
    Standard reflection driver (uses AccessibleObject.setAccessible(boolean) to access non-public fields if necessary).
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private <T> T doPrivileged​(java.util.concurrent.Callable<T> callable)
      Call a method in the AccessController.doPrivileged(PrivilegedAction) context, using reflection, if possible (AccessController is deprecated in JDK 17).
      (package private) java.lang.Class<?> findClass​(java.lang.String className)
      Find a class by name.
      (package private) <T> java.lang.reflect.Constructor<T>[] getDeclaredConstructors​(java.lang.Class<T> cls)
      Get declared constructors for class.
      (package private) java.lang.reflect.Field[] getDeclaredFields​(java.lang.Class<?> cls)
      Get declared fields for class.
      (package private) java.lang.reflect.Method[] getDeclaredMethods​(java.lang.Class<?> cls)
      Get declared methods for class.
      (package private) java.lang.Object getField​(java.lang.Object object, java.lang.reflect.Field field)
      Get the value of a non-static field, boxing the value if necessary.
      (package private) java.lang.Object getStaticField​(java.lang.reflect.Field field)
      Get the value of a static field, boxing the value if necessary.
      (package private) java.lang.Object invokeMethod​(java.lang.Object object, java.lang.reflect.Method method, java.lang.Object... args)
      Invoke a non-static method, boxing the result if necessary.
      (package private) java.lang.Object invokeStaticMethod​(java.lang.reflect.Method method, java.lang.Object... args)
      Invoke a static method, boxing the result if necessary.
      boolean makeAccessible​(java.lang.Object instance, java.lang.reflect.AccessibleObject obj)
      Make a field or method accessible.
      (package private) void setField​(java.lang.Object object, java.lang.reflect.Field field, java.lang.Object value)
      Set the value of a non-static field, unboxing the value if necessary.
      (package private) void setStaticField​(java.lang.reflect.Field field, java.lang.Object value)
      Set the value of a static field, unboxing the value if necessary.
      private static boolean tryMakeAccessible​(java.lang.reflect.AccessibleObject obj)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • setAccessibleMethod

        private static java.lang.reflect.Method setAccessibleMethod
      • trySetAccessibleMethod

        private static java.lang.reflect.Method trySetAccessibleMethod
      • accessControllerClass

        private static java.lang.Class<?> accessControllerClass
      • privilegedActionClass

        private static java.lang.Class<?> privilegedActionClass
      • accessControllerDoPrivileged

        private static java.lang.reflect.Method accessControllerDoPrivileged
    • Constructor Detail

      • StandardReflectionDriver

        StandardReflectionDriver()
    • Method Detail

      • doPrivileged

        private <T> T doPrivileged​(java.util.concurrent.Callable<T> callable)
                            throws java.lang.Throwable
        Call a method in the AccessController.doPrivileged(PrivilegedAction) context, using reflection, if possible (AccessController is deprecated in JDK 17).
        Throws:
        java.lang.Throwable
      • tryMakeAccessible

        private static boolean tryMakeAccessible​(java.lang.reflect.AccessibleObject obj)
      • makeAccessible

        public boolean makeAccessible​(java.lang.Object instance,
                                      java.lang.reflect.AccessibleObject obj)
        Description copied from class: ReflectionDriver
        Make a field or method accessible.
        Specified by:
        makeAccessible in class ReflectionDriver
        Parameters:
        instance - the object instance, or null if static.
        obj - the field or method.
        Returns:
        true if successful.
      • findClass

        java.lang.Class<?> findClass​(java.lang.String className)
                              throws java.lang.Exception
        Description copied from class: ReflectionDriver
        Find a class by name.
        Specified by:
        findClass in class ReflectionDriver
        Parameters:
        className - the class name
        Returns:
        the class reference
        Throws:
        java.lang.Exception
      • getDeclaredMethods

        java.lang.reflect.Method[] getDeclaredMethods​(java.lang.Class<?> cls)
                                               throws java.lang.Exception
        Description copied from class: ReflectionDriver
        Get declared methods for class.
        Specified by:
        getDeclaredMethods in class ReflectionDriver
        Parameters:
        cls - the class
        Returns:
        the declared methods
        Throws:
        java.lang.Exception
      • getDeclaredConstructors

        <T> java.lang.reflect.Constructor<T>[] getDeclaredConstructors​(java.lang.Class<T> cls)
                                                                throws java.lang.Exception
        Description copied from class: ReflectionDriver
        Get declared constructors for class.
        Specified by:
        getDeclaredConstructors in class ReflectionDriver
        Type Parameters:
        T - the generic type
        Parameters:
        cls - the class
        Returns:
        the declared constructors
        Throws:
        java.lang.Exception
      • getDeclaredFields

        java.lang.reflect.Field[] getDeclaredFields​(java.lang.Class<?> cls)
                                             throws java.lang.Exception
        Description copied from class: ReflectionDriver
        Get declared fields for class.
        Specified by:
        getDeclaredFields in class ReflectionDriver
        Parameters:
        cls - the class
        Returns:
        the declared fields
        Throws:
        java.lang.Exception
      • getField

        java.lang.Object getField​(java.lang.Object object,
                                  java.lang.reflect.Field field)
                           throws java.lang.Exception
        Description copied from class: ReflectionDriver
        Get the value of a non-static field, boxing the value if necessary.
        Specified by:
        getField in class ReflectionDriver
        Parameters:
        object - the object instance to get the field value from
        field - the non-static field
        Returns:
        the value of the field
        Throws:
        java.lang.Exception
      • setField

        void setField​(java.lang.Object object,
                      java.lang.reflect.Field field,
                      java.lang.Object value)
               throws java.lang.Exception
        Description copied from class: ReflectionDriver
        Set the value of a non-static field, unboxing the value if necessary.
        Specified by:
        setField in class ReflectionDriver
        Parameters:
        object - the object instance to get the field value from
        field - the non-static field
        value - the value to set
        Throws:
        java.lang.Exception
      • getStaticField

        java.lang.Object getStaticField​(java.lang.reflect.Field field)
                                 throws java.lang.Exception
        Description copied from class: ReflectionDriver
        Get the value of a static field, boxing the value if necessary.
        Specified by:
        getStaticField in class ReflectionDriver
        Parameters:
        field - the static field
        Returns:
        the static field
        Throws:
        java.lang.Exception
      • setStaticField

        void setStaticField​(java.lang.reflect.Field field,
                            java.lang.Object value)
                     throws java.lang.Exception
        Description copied from class: ReflectionDriver
        Set the value of a static field, unboxing the value if necessary.
        Specified by:
        setStaticField in class ReflectionDriver
        Parameters:
        field - the static field
        value - the value to set
        Throws:
        java.lang.Exception
      • invokeMethod

        java.lang.Object invokeMethod​(java.lang.Object object,
                                      java.lang.reflect.Method method,
                                      java.lang.Object... args)
                               throws java.lang.Exception
        Description copied from class: ReflectionDriver
        Invoke a non-static method, boxing the result if necessary.
        Specified by:
        invokeMethod in class ReflectionDriver
        Parameters:
        object - the object instance to invoke the method on
        method - the non-static method
        args - the method arguments (or new Object[0] if there are no args)
        Returns:
        the return value (possibly a boxed value)
        Throws:
        java.lang.Exception
      • invokeStaticMethod

        java.lang.Object invokeStaticMethod​(java.lang.reflect.Method method,
                                            java.lang.Object... args)
                                     throws java.lang.Exception
        Description copied from class: ReflectionDriver
        Invoke a static method, boxing the result if necessary.
        Specified by:
        invokeStaticMethod in class ReflectionDriver
        Parameters:
        method - the static method
        args - the method arguments (or new Object[0] if there are no args)
        Returns:
        the return value (possibly a boxed value)
        Throws:
        java.lang.Exception