类 ReflectionUtils


  • public abstract class ReflectionUtils
    extends Object
    Simple utility class for working with the reflection API and handling reflection exceptions.

    Only intended for internal use.

    从以下版本开始:
    1.2.2
    作者:
    Juergen Hoeller, Rob Harrop, Rod Johnson, Costin Leau, Sam Brannen, Chris Beams
    • 方法详细资料

      • handleReflectionException

        public static void handleReflectionException​(Exception ex)
        Handle the given reflection exception.

        Should only be called if no checked exception is expected to be thrown by a target method, or if an error occurs while accessing a method or field.

        Throws the underlying RuntimeException or Error in case of an InvocationTargetException with such a root cause. Throws an IllegalStateException with an appropriate message or UndeclaredThrowableException otherwise.

        参数:
        ex - the reflection exception to handle
      • handleInvocationTargetException

        public static void handleInvocationTargetException​(InvocationTargetException ex)
        Handle the given invocation target exception. Should only be called if no checked exception is expected to be thrown by the target method.

        Throws the underlying RuntimeException or Error in case of such a root cause. Throws an UndeclaredThrowableException otherwise.

        参数:
        ex - the invocation target exception to handle
      • accessibleConstructor

        public static <T> Constructor<T> accessibleConstructor​(Class<T> clazz,
                                                               Class<?>... parameterTypes)
                                                        throws NoSuchMethodException
        Obtain an accessible constructor for the given class and parameters.
        参数:
        clazz - the clazz to check
        parameterTypes - the parameter types of the desired constructor
        返回:
        the constructor reference
        抛出:
        NoSuchMethodException - if no such constructor exists
        从以下版本开始:
        5.0
      • makeAccessible

        public static void makeAccessible​(Constructor<?> ctor)
        Make the given constructor accessible, explicitly setting it accessible if necessary. The setAccessible(true) method is only called when actually necessary, to avoid unnecessary conflicts with a JVM SecurityManager (if active).
        参数:
        ctor - the constructor to make accessible
        另请参阅:
        Constructor.setAccessible(boolean)
      • findMethod

        @Nullable
        public static Method findMethod​(Class<?> clazz,
                                        String name)
        Attempt to find a Method on the supplied class with the supplied name and no parameters. Searches all superclasses up to Object.

        Returns null if no Method can be found.

        参数:
        clazz - the class to introspect
        name - the name of the method
        返回:
        the Method object, or null if none found
      • findMethod

        @Nullable
        public static Method findMethod​(Class<?> clazz,
                                        String name,
                                        @Nullable
                                        Class<?>... paramTypes)
        Attempt to find a Method on the supplied class with the supplied name and parameter types. Searches all superclasses up to Object.

        Returns null if no Method can be found.

        参数:
        clazz - the class to introspect
        name - the name of the method
        paramTypes - the parameter types of the method (may be null to indicate any signature)
        返回:
        the Method object, or null if none found
      • declaresException

        public static boolean declaresException​(Method method,
                                                Class<?> exceptionType)
        Determine whether the given method explicitly declares the given exception or one of its superclasses, which means that an exception of that type can be propagated as-is within a reflective invocation.
        参数:
        method - the declaring method
        exceptionType - the exception to throw
        返回:
        true if the exception can be thrown as-is; false if it needs to be wrapped
      • getAllDeclaredMethods

        public static Method[] getAllDeclaredMethods​(Class<?> leafClass)
        Get all declared methods on the leaf class and all superclasses. Leaf class methods are included first.
        参数:
        leafClass - the class to introspect
        抛出:
        IllegalStateException - if introspection fails
      • getUniqueDeclaredMethods

        public static Method[] getUniqueDeclaredMethods​(Class<?> leafClass)
        Get the unique set of declared methods on the leaf class and all superclasses. Leaf class methods are included first and while traversing the superclass hierarchy any methods found with signatures matching a method already included are filtered out.
        参数:
        leafClass - the class to introspect
        抛出:
        IllegalStateException - if introspection fails
      • getUniqueDeclaredMethods

        public static Method[] getUniqueDeclaredMethods​(Class<?> leafClass,
                                                        @Nullable
                                                        ReflectionUtils.MethodFilter mf)
        Get the unique set of declared methods on the leaf class and all superclasses. Leaf class methods are included first and while traversing the superclass hierarchy any methods found with signatures matching a method already included are filtered out.
        参数:
        leafClass - the class to introspect
        mf - the filter that determines the methods to take into account
        抛出:
        IllegalStateException - if introspection fails
        从以下版本开始:
        5.2
      • getDeclaredMethods

        public static Method[] getDeclaredMethods​(Class<?> clazz)
        Variant of Class.getDeclaredMethods() that uses a local cache in order to avoid the JVM's SecurityManager check and new Method instances. In addition, it also includes Java 8 default methods from locally implemented interfaces, since those are effectively to be treated just like declared methods.
        参数:
        clazz - the class to introspect
        返回:
        the cached array of methods
        抛出:
        IllegalStateException - if introspection fails
        从以下版本开始:
        5.2
        另请参阅:
        Class.getDeclaredMethods()
      • isCglibRenamedMethod

        public static boolean isCglibRenamedMethod​(Method renamedMethod)
        Determine whether the given method is a CGLIB 'renamed' method, following the pattern "CGLIB$methodName$0".
        参数:
        renamedMethod - the method to check
      • makeAccessible

        public static void makeAccessible​(Method method)
        Make the given method accessible, explicitly setting it accessible if necessary. The setAccessible(true) method is only called when actually necessary, to avoid unnecessary conflicts with a JVM SecurityManager (if active).
        参数:
        method - the method to make accessible
        另请参阅:
        Method.setAccessible(boolean)
      • findField

        @Nullable
        public static Field findField​(Class<?> clazz,
                                      String name)
        Attempt to find a field on the supplied Class with the supplied name. Searches all superclasses up to Object.
        参数:
        clazz - the class to introspect
        name - the name of the field
        返回:
        the corresponding Field object, or null if not found
      • findField

        @Nullable
        public static Field findField​(Class<?> clazz,
                                      @Nullable
                                      String name,
                                      @Nullable
                                      Class<?> type)
        Attempt to find a field on the supplied Class with the supplied name and/or type. Searches all superclasses up to Object.
        参数:
        clazz - the class to introspect
        name - the name of the field (may be null if type is specified)
        type - the type of the field (may be null if name is specified)
        返回:
        the corresponding Field object, or null if not found
      • doWithFields

        public static void doWithFields​(Class<?> clazz,
                                        ReflectionUtils.FieldCallback fc)
        Invoke the given callback on all fields in the target class, going up the class hierarchy to get all declared fields.
        参数:
        clazz - the target class to analyze
        fc - the callback to invoke for each field
        抛出:
        IllegalStateException - if introspection fails
      • shallowCopyFieldState

        public static void shallowCopyFieldState​(Object src,
                                                 Object dest)
        Given the source object and the destination, which must be the same class or a subclass, copy all fields, including inherited fields. Designed to work on objects with public no-arg constructors.
        抛出:
        IllegalStateException - if introspection fails
      • isPublicStaticFinal

        public static boolean isPublicStaticFinal​(Field field)
        Determine whether the given field is a "public static final" constant.
        参数:
        field - the field to check
      • makeAccessible

        public static void makeAccessible​(Field field)
        Make the given field accessible, explicitly setting it accessible if necessary. The setAccessible(true) method is only called when actually necessary, to avoid unnecessary conflicts with a JVM SecurityManager (if active).
        参数:
        field - the field to make accessible
        另请参阅:
        Field.setAccessible(boolean)
      • clearCache

        public static void clearCache()
        Clear the internal method/field cache.
        从以下版本开始:
        4.2.4