org.springframework.util

## 类 ReflectionUtils

- java.lang.Object
- - org.springframework.util.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

- - ### 嵌套类概要

    嵌套类
    限定符和类型 | 类和说明
    --- | ---
    `static interface` | `ReflectionUtils.FieldCallback` Callback interface invoked on each field in the hierarchy.
    `static interface` | `ReflectionUtils.FieldFilter` Callback optionally used to filter fields to be operated on by a field callback.
    `static interface` | `ReflectionUtils.MethodCallback` Action to take on each method.
    `static interface` | `ReflectionUtils.MethodFilter` Callback optionally used to filter methods to be operated on by a method callback.

  - ### 字段概要

    字段
    限定符和类型 | 字段和说明
    --- | ---
    `static ReflectionUtils.FieldFilter` | `COPYABLE_FIELDS` Pre-built FieldFilter that matches all non-static, non-final fields.
    `static ReflectionUtils.MethodFilter` | `NON_BRIDGED_METHODS` 已过时。 as of 5.0.11, in favor of a custom `ReflectionUtils.MethodFilter`
    `static ReflectionUtils.MethodFilter` | `USER_DECLARED_METHODS` Pre-built MethodFilter that matches all non-bridge non-synthetic methods which are not declared on `java.lang.Object`.

  - ### 构造器概要

    构造器
    构造器和说明
    `ReflectionUtils()`

  - ### 方法概要

    所有方法 静态方法 具体方法 已过时的方法
    限定符和类型 | 方法和说明
    --- | ---
    `static <T> Constructor<T>` | `accessibleConstructor(Class<T> clazz, Class<?>... parameterTypes)` Obtain an accessible constructor for the given class and parameters.
    `static void` | `clearCache()` Clear the internal method/field cache.
    `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.
    `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.
    `static void` | `doWithFields(Class<?> clazz, ReflectionUtils.FieldCallback fc, ReflectionUtils.FieldFilter ff)` Invoke the given callback on all fields in the target class, going up the class hierarchy to get all declared fields.
    `static void` | `doWithLocalFields(Class<?> clazz, ReflectionUtils.FieldCallback fc)` Invoke the given callback on all locally declared fields in the given class.
    `static void` | `doWithLocalMethods(Class<?> clazz, ReflectionUtils.MethodCallback mc)` Perform the given callback operation on all matching methods of the given class, as locally declared or equivalent thereof (such as default methods on Java 8 based interfaces that the given class implements).
    `static void` | `doWithMethods(Class<?> clazz, ReflectionUtils.MethodCallback mc)` Perform the given callback operation on all matching methods of the given class and superclasses.
    `static void` | `doWithMethods(Class<?> clazz, ReflectionUtils.MethodCallback mc, ReflectionUtils.MethodFilter mf)` Perform the given callback operation on all matching methods of the given class and superclasses (or given interface and super-interfaces).
    `static Field` | `findField(Class<?> clazz, String name)` Attempt to find a `field` on the supplied `Class` with the supplied `name`.
    `static Field` | `findField(Class<?> clazz, String name, Class<?> type)` Attempt to find a `field` on the supplied `Class` with the supplied `name` and/or `type`.
    `static Method` | `findMethod(Class<?> clazz, String name)` Attempt to find a `Method` on the supplied class with the supplied name and no parameters.
    `static Method` | `findMethod(Class<?> clazz, String name, Class<?>... paramTypes)` Attempt to find a `Method` on the supplied class with the supplied name and parameter types.
    `static Method[]` | `getAllDeclaredMethods(Class<?> leafClass)` Get all declared methods on the leaf class and all superclasses.
    `static Object` | `getField(Field field, Object target)` Get the field represented by the supplied `field object` on the specified `target object`.
    `static Method[]` | `getUniqueDeclaredMethods(Class<?> leafClass)` Get the unique set of declared methods on the leaf class and all superclasses.
    `static void` | `handleInvocationTargetException(InvocationTargetException ex)` Handle the given invocation target exception.
    `static void` | `handleReflectionException(Exception ex)` Handle the given reflection exception.
    `static Object` | `invokeJdbcMethod(Method method, Object target)` 已过时。 as of 5.0.11, in favor of custom SQLException handling
    `static Object` | `invokeJdbcMethod(Method method, Object target, Object... args)` 已过时。 as of 5.0.11, in favor of custom SQLException handling
    `static Object` | `invokeMethod(Method method, Object target)` Invoke the specified `Method` against the supplied target object with no arguments.
    `static Object` | `invokeMethod(Method method, Object target, Object... args)` Invoke the specified `Method` against the supplied target object with the supplied arguments.
    `static boolean` | `isCglibRenamedMethod(Method renamedMethod)` Determine whether the given method is a CGLIB 'renamed' method, following the pattern "CGLIB$methodName$0".
    `static boolean` | `isEqualsMethod(Method method)` Determine whether the given method is an "equals" method.
    `static boolean` | `isHashCodeMethod(Method method)` Determine whether the given method is a "hashCode" method.
    `static boolean` | `isObjectMethod(Method method)` Determine whether the given method is originally declared by `Object`.
    `static boolean` | `isPublicStaticFinal(Field field)` Determine whether the given field is a "public static final" constant.
    `static boolean` | `isToStringMethod(Method method)` Determine whether the given method is a "toString" method.
    `static void` | `makeAccessible(Constructor<?> ctor)` Make the given constructor accessible, explicitly setting it accessible if necessary.
    `static void` | `makeAccessible(Field field)` Make the given field accessible, explicitly setting it accessible if necessary.
    `static void` | `makeAccessible(Method method)` Make the given method accessible, explicitly setting it accessible if necessary.
    `static void` | `rethrowException(Throwable ex)` Rethrow the given `exception`, which is presumably the target exception of an `InvocationTargetException`.
    `static void` | `rethrowRuntimeException(Throwable ex)` Rethrow the given `exception`, which is presumably the target exception of an `InvocationTargetException`.
    `static void` | `setField(Field field, Object target, Object value)` Set the field represented by the supplied `field object` on the specified `target object` to the specified `value`.
    `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.

    - ### 从类继承的方法 java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### 字段详细资料

    - #### NON_BRIDGED_METHODS

          @Deprecated
          public static final ReflectionUtils.MethodFilter NON_BRIDGED_METHODS

      已过时。 as of 5.0.11, in favor of a custom `ReflectionUtils.MethodFilter`
      Pre-built MethodFilter that matches all non-bridge methods.
      从以下版本开始:
        3.0

    - #### USER_DECLARED_METHODS

          public static final ReflectionUtils.MethodFilter USER_DECLARED_METHODS

      Pre-built MethodFilter that matches all non-bridge non-synthetic methods which are not declared on `java.lang.Object`.
      从以下版本开始:
        3.0.5

    - #### COPYABLE_FIELDS

          public static final ReflectionUtils.FieldFilter COPYABLE_FIELDS

      Pre-built FieldFilter that matches all non-static, non-final fields.

  - ### 构造器详细资料

    - #### ReflectionUtils

          public ReflectionUtils()

  - ### 方法详细资料

    - #### 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 the target method.

      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

    - #### rethrowRuntimeException

          public static void rethrowRuntimeException(Throwable ex)

      Rethrow the given `exception`, which is presumably the target exception of an `InvocationTargetException`. Should only be called if no checked exception is expected to be thrown by the target method.

      Rethrows the underlying exception cast to a `RuntimeException` or `Error` if appropriate; otherwise, throws an `UndeclaredThrowableException`.

      参数:
        `ex` - the exception to rethrow
      抛出:
        `RuntimeException` - the rethrown exception

    - #### rethrowException

          public static void rethrowException(Throwable ex)
                                       throws Exception

      Rethrow the given `exception`, which is presumably the target exception of an `InvocationTargetException`. Should only be called if no checked exception is expected to be thrown by the target method.

      Rethrows the underlying exception cast to an `Exception` or `Error` if appropriate; otherwise, throws an `UndeclaredThrowableException`.

      参数:
        `ex` - the exception to rethrow
      抛出:
        `Exception` - the rethrown exception (in case of a checked exception)

    - #### 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
      另请参阅:
        `AccessibleObject.setAccessible(java.lang.reflect.AccessibleObject[], 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

    - #### invokeMethod

          @Nullable
          public static Object invokeMethod(Method method,
                                                       @Nullable
                                                       Object target)

      Invoke the specified `Method` against the supplied target object with no arguments. The target object can be `null` when invoking a static `Method`.

      Thrown exceptions are handled via a call to `handleReflectionException(java.lang.Exception)`.

      参数:
        `method` - the method to invoke
        `target` - the target object to invoke the method on
      返回:
        the invocation result, if any
      另请参阅:
        `invokeMethod(java.lang.reflect.Method, Object, Object[])`

    - #### invokeMethod

          @Nullable
          public static Object invokeMethod(Method method,
                                                       @Nullable
                                                       Object target,
                                                       @Nullable
                                                       Object... args)

      Invoke the specified `Method` against the supplied target object with the supplied arguments. The target object can be `null` when invoking a static `Method`.

      Thrown exceptions are handled via a call to `handleReflectionException(java.lang.Exception)`.

      参数:
        `method` - the method to invoke
        `target` - the target object to invoke the method on
        `args` - the invocation arguments (may be `null`)
      返回:
        the invocation result, if any

    - #### invokeJdbcMethod

          @Deprecated
           @Nullable
          public static Object invokeJdbcMethod(Method method,
                                                                         @Nullable
                                                                         Object target)
                                                                  throws SQLException

      已过时。 as of 5.0.11, in favor of custom SQLException handling
      Invoke the specified JDBC API `Method` against the supplied target object with no arguments.
      参数:
        `method` - the method to invoke
        `target` - the target object to invoke the method on
      返回:
        the invocation result, if any
      抛出:
        `SQLException` - the JDBC API SQLException to rethrow (if any)
      另请参阅:
        `invokeJdbcMethod(java.lang.reflect.Method, Object, Object[])`

    - #### invokeJdbcMethod

          @Deprecated
           @Nullable
          public static Object invokeJdbcMethod(Method method,
                                                                         @Nullable
                                                                         Object target,
                                                                         @Nullable
                                                                         Object... args)
                                                                  throws SQLException

      已过时。 as of 5.0.11, in favor of custom SQLException handling
      Invoke the specified JDBC API `Method` against the supplied target object with the supplied arguments.
      参数:
        `method` - the method to invoke
        `target` - the target object to invoke the method on
        `args` - the invocation arguments (may be `null`)
      返回:
        the invocation result, if any
      抛出:
        `SQLException` - the JDBC API SQLException to rethrow (if any)
      另请参阅:
        `invokeMethod(java.lang.reflect.Method, Object, Object[])`

    - #### 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

    - #### doWithLocalMethods

          public static void doWithLocalMethods(Class<?> clazz,
                                                ReflectionUtils.MethodCallback mc)

      Perform the given callback operation on all matching methods of the given class, as locally declared or equivalent thereof (such as default methods on Java 8 based interfaces that the given class implements).
      参数:
        `clazz` - the class to introspect
        `mc` - the callback to invoke for each method
      抛出:
        `IllegalStateException` - if introspection fails
      从以下版本开始:
        4.2
      另请参阅:
        `doWithMethods(java.lang.Class<?>, org.springframework.util.ReflectionUtils.MethodCallback)`

    - #### doWithMethods

          public static void doWithMethods(Class<?> clazz,
                                           ReflectionUtils.MethodCallback mc)

      Perform the given callback operation on all matching methods of the given class and superclasses.

      The same named method occurring on subclass and superclass will appear twice, unless excluded by a `ReflectionUtils.MethodFilter`.

      参数:
        `clazz` - the class to introspect
        `mc` - the callback to invoke for each method
      抛出:
        `IllegalStateException` - if introspection fails
      另请参阅:
        `doWithMethods(Class, MethodCallback, MethodFilter)`

    - #### doWithMethods

          public static void doWithMethods(Class<?> clazz,
                                           ReflectionUtils.MethodCallback mc,
                                           @Nullable
                                           ReflectionUtils.MethodFilter mf)

      Perform the given callback operation on all matching methods of the given class and superclasses (or given interface and super-interfaces).

      The same named method occurring on subclass and superclass will appear twice, unless excluded by the specified `ReflectionUtils.MethodFilter`.

      参数:
        `clazz` - the class to introspect
        `mc` - the callback to invoke for each method
        `mf` - the filter that determines the methods to apply the callback to
      抛出:
        `IllegalStateException` - if introspection fails

    - #### 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

    - #### isEqualsMethod

          public static boolean isEqualsMethod(@Nullable
                                               Method method)

      Determine whether the given method is an "equals" method.
      另请参阅:
        `Object.equals(Object)`

    - #### isHashCodeMethod

          public static boolean isHashCodeMethod(@Nullable
                                                 Method method)

      Determine whether the given method is a "hashCode" method.
      另请参阅:
        `Object.hashCode()`

    - #### isToStringMethod

          public static boolean isToStringMethod(@Nullable
                                                 Method method)

      Determine whether the given method is a "toString" method.
      另请参阅:
        `Object.toString()`

    - #### isObjectMethod

          public static boolean isObjectMethod(@Nullable
                                               Method method)

      Determine whether the given method is originally declared by `Object`.

    - #### 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
      另请参阅:
        `AccessibleObject.setAccessible(java.lang.reflect.AccessibleObject[], 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

    - #### setField

          public static void setField(Field field,
                                      @Nullable
                                      Object target,
                                      @Nullable
                                      Object value)

      Set the field represented by the supplied `field object` on the specified `target object` to the specified `value`. In accordance with `Field.set(Object, Object)` semantics, the new value is automatically unwrapped if the underlying field has a primitive type.

      Thrown exceptions are handled via a call to `handleReflectionException(Exception)`.

      参数:
        `field` - the field to set
        `target` - the target object on which to set the field
        `value` - the value to set (may be `null`)

    - #### getField

          @Nullable
          public static Object getField(Field field,
                                                   @Nullable
                                                   Object target)

      Get the field represented by the supplied `field object` on the specified `target object`. In accordance with `Field.get(Object)` semantics, the returned value is automatically wrapped if the underlying field has a primitive type.

      Thrown exceptions are handled via a call to `handleReflectionException(Exception)`.

      参数:
        `field` - the field to get
        `target` - the target object from which to get the field
      返回:
        the field's current value

    - #### doWithLocalFields

          public static void doWithLocalFields(Class<?> clazz,
                                               ReflectionUtils.FieldCallback fc)

      Invoke the given callback on all locally declared fields in the given class.
      参数:
        `clazz` - the target class to analyze
        `fc` - the callback to invoke for each field
      抛出:
        `IllegalStateException` - if introspection fails
      从以下版本开始:
        4.2
      另请参阅:
        `doWithFields(java.lang.Class<?>, org.springframework.util.ReflectionUtils.FieldCallback)`

    - #### 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

    - #### doWithFields

          public static void doWithFields(Class<?> clazz,
                                          ReflectionUtils.FieldCallback fc,
                                          @Nullable
                                          ReflectionUtils.FieldFilter ff)

      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
        `ff` - the filter that determines the fields to apply the callback to
      抛出:
        `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
      另请参阅:
        `AccessibleObject.setAccessible(java.lang.reflect.AccessibleObject[], boolean)`

    - #### clearCache

          public static void clearCache()

      Clear the internal method/field cache.
      从以下版本开始:
        4.2.4