org.springframework.util

Class ReflectionUtils

  • java.lang.Object
    • org.springframework.util.ReflectionUtils


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

    Only intended for internal use.

    Since:
    1.2.2
    Author:
    Juergen Hoeller, Rob Harrop, Rod Johnson, Costin Leau, Sam Brannen, Chris Beams
    • Constructor Summary

      Constructors 
      Constructor and Description
      ReflectionUtils() 
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method and Description
      static <T> java.lang.reflect.Constructor<T> accessibleConstructor(java.lang.Class<T> clazz, java.lang.Class<?>... parameterTypes)
      Obtain an accessible constructor for the given class and parameters.
      static void clearCache()
      Clear the internal method/field cache.
      static boolean declaresException(java.lang.reflect.Method method, java.lang.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(java.lang.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(java.lang.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(java.lang.Class<?> clazz, ReflectionUtils.FieldCallback fc)
      Invoke the given callback on all locally declared fields in the given class.
      static void doWithLocalMethods(java.lang.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(java.lang.Class<?> clazz, ReflectionUtils.MethodCallback mc)
      Perform the given callback operation on all matching methods of the given class and superclasses.
      static void doWithMethods(java.lang.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 java.lang.reflect.Field findField(java.lang.Class<?> clazz, java.lang.String name)
      Attempt to find a field on the supplied Class with the supplied name.
      static java.lang.reflect.Field findField(java.lang.Class<?> clazz, java.lang.String name, java.lang.Class<?> type)
      Attempt to find a field on the supplied Class with the supplied name and/or type.
      static java.lang.reflect.Method findMethod(java.lang.Class<?> clazz, java.lang.String name)
      Attempt to find a Method on the supplied class with the supplied name and no parameters.
      static java.lang.reflect.Method findMethod(java.lang.Class<?> clazz, java.lang.String name, java.lang.Class<?>... paramTypes)
      Attempt to find a Method on the supplied class with the supplied name and parameter types.
      static java.lang.reflect.Method[] getAllDeclaredMethods(java.lang.Class<?> leafClass)
      Get all declared methods on the leaf class and all superclasses.
      static java.lang.Object getField(java.lang.reflect.Field field, java.lang.Object target)
      Get the field represented by the supplied field object on the specified target object.
      static java.lang.reflect.Method[] getUniqueDeclaredMethods(java.lang.Class<?> leafClass)
      Get the unique set of declared methods on the leaf class and all superclasses.
      static void handleInvocationTargetException(java.lang.reflect.InvocationTargetException ex)
      Handle the given invocation target exception.
      static void handleReflectionException(java.lang.Exception ex)
      Handle the given reflection exception.
      static java.lang.Object invokeJdbcMethod(java.lang.reflect.Method method, java.lang.Object target)
      Invoke the specified JDBC API Method against the supplied target object with no arguments.
      static java.lang.Object invokeJdbcMethod(java.lang.reflect.Method method, java.lang.Object target, java.lang.Object... args)
      Invoke the specified JDBC API Method against the supplied target object with the supplied arguments.
      static java.lang.Object invokeMethod(java.lang.reflect.Method method, java.lang.Object target)
      Invoke the specified Method against the supplied target object with no arguments.
      static java.lang.Object invokeMethod(java.lang.reflect.Method method, java.lang.Object target, java.lang.Object... args)
      Invoke the specified Method against the supplied target object with the supplied arguments.
      static boolean isCglibRenamedMethod(java.lang.reflect.Method renamedMethod)
      Determine whether the given method is a CGLIB 'renamed' method, following the pattern "CGLIB$methodName$0".
      static boolean isEqualsMethod(java.lang.reflect.Method method)
      Determine whether the given method is an "equals" method.
      static boolean isHashCodeMethod(java.lang.reflect.Method method)
      Determine whether the given method is a "hashCode" method.
      static boolean isObjectMethod(java.lang.reflect.Method method)
      Determine whether the given method is originally declared by Object.
      static boolean isPublicStaticFinal(java.lang.reflect.Field field)
      Determine whether the given field is a "public static final" constant.
      static boolean isToStringMethod(java.lang.reflect.Method method)
      Determine whether the given method is a "toString" method.
      static void makeAccessible(java.lang.reflect.Constructor<?> ctor)
      Make the given constructor accessible, explicitly setting it accessible if necessary.
      static void makeAccessible(java.lang.reflect.Field field)
      Make the given field accessible, explicitly setting it accessible if necessary.
      static void makeAccessible(java.lang.reflect.Method method)
      Make the given method accessible, explicitly setting it accessible if necessary.
      static void rethrowException(java.lang.Throwable ex)
      Rethrow the given exception, which is presumably the target exception of an InvocationTargetException.
      static void rethrowRuntimeException(java.lang.Throwable ex)
      Rethrow the given exception, which is presumably the target exception of an InvocationTargetException.
      static void setField(java.lang.reflect.Field field, java.lang.Object target, java.lang.Object value)
      Set the field represented by the supplied field object on the specified target object to the specified value.
      static void shallowCopyFieldState(java.lang.Object src, java.lang.Object dest)
      Given the source object and the destination, which must be the same class or a subclass, copy all fields, including inherited fields.
      • Methods inherited from class java.lang.Object

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

      • COPYABLE_FIELDS

        public static final ReflectionUtils.FieldFilter COPYABLE_FIELDS
        Pre-built FieldFilter that matches all non-static, non-final fields.
      • NON_BRIDGED_METHODS

        public static final ReflectionUtils.MethodFilter NON_BRIDGED_METHODS
        Pre-built MethodFilter that matches all non-bridge methods.
      • 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.
    • Constructor Detail

      • ReflectionUtils

        public ReflectionUtils()
    • Method Detail

      • findField

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

        @Nullable
        public static java.lang.reflect.Field findField(java.lang.Class<?> clazz,
                                                                  @Nullable
                                                                  java.lang.String name,
                                                                  @Nullable
                                                                  java.lang.Class<?> type)
        Attempt to find a field on the supplied Class with the supplied name and/or type. Searches all superclasses up to Object.
        Parameters:
        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)
        Returns:
        the corresponding Field object, or null if not found
      • setField

        public static void setField(java.lang.reflect.Field field,
                                    @Nullable
                                    java.lang.Object target,
                                    @Nullable
                                    java.lang.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).

        Parameters:
        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 java.lang.Object getField(java.lang.reflect.Field field,
                                                          @Nullable
                                                          java.lang.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).

        Parameters:
        field - the field to get
        target - the target object from which to get the field
        Returns:
        the field's current value
      • findMethod

        @Nullable
        public static java.lang.reflect.Method findMethod(java.lang.Class<?> clazz,
                                                                    java.lang.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.

        Parameters:
        clazz - the class to introspect
        name - the name of the method
        Returns:
        the Method object, or null if none found
      • findMethod

        @Nullable
        public static java.lang.reflect.Method findMethod(java.lang.Class<?> clazz,
                                                                    java.lang.String name,
                                                                    @Nullable
                                                                    java.lang.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.

        Parameters:
        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)
        Returns:
        the Method object, or null if none found
      • invokeMethod

        @Nullable
        public static java.lang.Object invokeMethod(java.lang.reflect.Method method,
                                                              @Nullable
                                                              java.lang.Object target,
                                                              @Nullable
                                                              java.lang.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).

        Parameters:
        method - the method to invoke
        target - the target object to invoke the method on
        args - the invocation arguments (may be null)
        Returns:
        the invocation result, if any
      • invokeJdbcMethod

        @Nullable
        public static java.lang.Object invokeJdbcMethod(java.lang.reflect.Method method,
                                                                  @Nullable
                                                                  java.lang.Object target)
                                                           throws java.sql.SQLException
        Invoke the specified JDBC API Method against the supplied target object with no arguments.
        Parameters:
        method - the method to invoke
        target - the target object to invoke the method on
        Returns:
        the invocation result, if any
        Throws:
        java.sql.SQLException - the JDBC API SQLException to rethrow (if any)
        See Also:
        invokeJdbcMethod(java.lang.reflect.Method, Object, Object[])
      • invokeJdbcMethod

        @Nullable
        public static java.lang.Object invokeJdbcMethod(java.lang.reflect.Method method,
                                                                  @Nullable
                                                                  java.lang.Object target,
                                                                  @Nullable
                                                                  java.lang.Object... args)
                                                           throws java.sql.SQLException
        Invoke the specified JDBC API Method against the supplied target object with the supplied arguments.
        Parameters:
        method - the method to invoke
        target - the target object to invoke the method on
        args - the invocation arguments (may be null)
        Returns:
        the invocation result, if any
        Throws:
        java.sql.SQLException - the JDBC API SQLException to rethrow (if any)
        See Also:
        invokeMethod(java.lang.reflect.Method, Object, Object[])
      • handleReflectionException

        public static void handleReflectionException(java.lang.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.

        Parameters:
        ex - the reflection exception to handle
      • handleInvocationTargetException

        public static void handleInvocationTargetException(java.lang.reflect.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.

        Parameters:
        ex - the invocation target exception to handle
      • rethrowRuntimeException

        public static void rethrowRuntimeException(java.lang.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.

        Parameters:
        ex - the exception to rethrow
        Throws:
        java.lang.RuntimeException - the rethrown exception
      • rethrowException

        public static void rethrowException(java.lang.Throwable ex)
                                     throws java.lang.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.

        Parameters:
        ex - the exception to rethrow
        Throws:
        java.lang.Exception - the rethrown exception (in case of a checked exception)
      • declaresException

        public static boolean declaresException(java.lang.reflect.Method method,
                                                java.lang.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.
        Parameters:
        method - the declaring method
        exceptionType - the exception to throw
        Returns:
        true if the exception can be thrown as-is; false if it needs to be wrapped
      • isPublicStaticFinal

        public static boolean isPublicStaticFinal(java.lang.reflect.Field field)
        Determine whether the given field is a "public static final" constant.
        Parameters:
        field - the field to check
      • isEqualsMethod

        public static boolean isEqualsMethod(@Nullable
                                             java.lang.reflect.Method method)
        Determine whether the given method is an "equals" method.
        See Also:
        Object.equals(Object)
      • isHashCodeMethod

        public static boolean isHashCodeMethod(@Nullable
                                               java.lang.reflect.Method method)
        Determine whether the given method is a "hashCode" method.
        See Also:
        Object.hashCode()
      • isToStringMethod

        public static boolean isToStringMethod(@Nullable
                                               java.lang.reflect.Method method)
        Determine whether the given method is a "toString" method.
        See Also:
        Object.toString()
      • isObjectMethod

        public static boolean isObjectMethod(@Nullable
                                             java.lang.reflect.Method method)
        Determine whether the given method is originally declared by Object.
      • isCglibRenamedMethod

        public static boolean isCglibRenamedMethod(java.lang.reflect.Method renamedMethod)
        Determine whether the given method is a CGLIB 'renamed' method, following the pattern "CGLIB$methodName$0".
        Parameters:
        renamedMethod - the method to check
        See Also:
        Enhancer.rename(org.springframework.cglib.core.Signature, int)
      • makeAccessible

        public static void makeAccessible(java.lang.reflect.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).
        Parameters:
        field - the field to make accessible
        See Also:
        AccessibleObject.setAccessible(java.lang.reflect.AccessibleObject[], boolean)
      • makeAccessible

        public static void makeAccessible(java.lang.reflect.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).
        Parameters:
        method - the method to make accessible
        See Also:
        AccessibleObject.setAccessible(java.lang.reflect.AccessibleObject[], boolean)
      • makeAccessible

        public static void makeAccessible(java.lang.reflect.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).
        Parameters:
        ctor - the constructor to make accessible
        See Also:
        AccessibleObject.setAccessible(java.lang.reflect.AccessibleObject[], boolean)
      • accessibleConstructor

        public static <T> java.lang.reflect.Constructor<T> accessibleConstructor(java.lang.Class<T> clazz,
                                                                                 java.lang.Class<?>... parameterTypes)
                                                                          throws java.lang.NoSuchMethodException
        Obtain an accessible constructor for the given class and parameters.
        Parameters:
        clazz - the clazz to check
        parameterTypes - the parameter types of the desired constructor
        Returns:
        the constructor reference
        Throws:
        java.lang.NoSuchMethodException - if no such constructor exists
        Since:
        5.0
      • doWithMethods

        public static void doWithMethods(java.lang.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.

        Parameters:
        clazz - the class to introspect
        mc - the callback to invoke for each method
        Throws:
        java.lang.IllegalStateException - if introspection fails
        See Also:
        doWithMethods(Class, MethodCallback, MethodFilter)
      • doWithMethods

        public static void doWithMethods(java.lang.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.

        Parameters:
        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
        Throws:
        java.lang.IllegalStateException - if introspection fails
      • getAllDeclaredMethods

        public static java.lang.reflect.Method[] getAllDeclaredMethods(java.lang.Class<?> leafClass)
        Get all declared methods on the leaf class and all superclasses. Leaf class methods are included first.
        Parameters:
        leafClass - the class to introspect
        Throws:
        java.lang.IllegalStateException - if introspection fails
      • getUniqueDeclaredMethods

        public static java.lang.reflect.Method[] getUniqueDeclaredMethods(java.lang.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.
        Parameters:
        leafClass - the class to introspect
        Throws:
        java.lang.IllegalStateException - if introspection fails
      • doWithFields

        public static void doWithFields(java.lang.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.
        Parameters:
        clazz - the target class to analyze
        fc - the callback to invoke for each field
        Throws:
        java.lang.IllegalStateException - if introspection fails
      • doWithFields

        public static void doWithFields(java.lang.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.
        Parameters:
        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
        Throws:
        java.lang.IllegalStateException - if introspection fails
      • shallowCopyFieldState

        public static void shallowCopyFieldState(java.lang.Object src,
                                                 java.lang.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.
        Throws:
        java.lang.IllegalStateException - if introspection fails
      • clearCache

        public static void clearCache()
        Clear the internal method/field cache.
        Since:
        4.2.4