Class ReflectionUtils


  • public class ReflectionUtils
    extends java.lang.Object
    Inspired heavily by Spring's ReflectionUtils.
    Since:
    1.0-beta-1
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private ReflectionUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      private static java.util.List<java.lang.reflect.Method> findConcreteMethodsOnInterfaces​(java.lang.Class<?> clazz)  
      static java.lang.reflect.Constructor<?> findConstructor​(java.lang.Class<?> clazz, java.lang.Class<?>... paramTypes)
      Attempt to find a Constructor on the supplied class with the supplied parameter types.
      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, java.lang.Class<?>... paramTypes)
      Attempt to find a Method on the supplied class with the supplied name and parameter types.
      private static java.lang.reflect.Method[] getDeclaredMethods​(java.lang.Class<?> clazz)
      This variant retrieves Class.getDeclaredMethods() from a local cache in order to avoid the JVM's SecurityManager check and defensive array copying.
      static java.lang.Object getEnumValue​(java.lang.Class<?> clazz, java.lang.String valueName)
      Find and return the specified value from the specified enum class.
      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.Object getStaticField​(java.lang.reflect.Field field)
      Get the field represented by the supplied field object on the specified target object.
      static java.lang.Object invokeConstructor​(java.lang.reflect.Constructor<?> constructor, java.lang.Object... args)
      Invoke the specified Constructor with the supplied 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 java.lang.Object invokeStaticMethod​(java.lang.reflect.Method method, java.lang.Object... args)
      Invoke the specified static Method with the supplied arguments.
      • Methods inherited from class java.lang.Object

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

      • ReflectionUtils

        private ReflectionUtils()
    • Method Detail

      • findConstructor

        public static java.lang.reflect.Constructor<?> findConstructor​(java.lang.Class<?> clazz,
                                                                       java.lang.Class<?>... paramTypes)
        Attempt to find a Constructor on the supplied class with the supplied parameter types. Searches all superclasses up to Object.
        Parameters:
        clazz - The class to introspect
        paramTypes - The parameter types of the method (may be null to indicate any signature)
        Returns:
        The Constructor object
      • findField

        public 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. 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
      • findMethod

        public 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. Searches all superclasses up to Object.
        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
      • getEnumValue

        public static java.lang.Object getEnumValue​(java.lang.Class<?> clazz,
                                                    java.lang.String valueName)
        Find and return the specified value from the specified enum class.
        Parameters:
        clazz - The enum class to introspect
        valueName - The name of the enum value to get
        Returns:
        The enum value
      • getField

        public static java.lang.Object getField​(java.lang.reflect.Field field,
                                                java.lang.Object target)
                                         throws java.lang.IllegalAccessException
        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.
        Parameters:
        field - The field to get
        target - The target object from which to get the field
        Returns:
        The field's current value
        Throws:
        java.lang.IllegalAccessException - when unable to access the specified field because access modifiers prevent it
      • getStaticField

        public static java.lang.Object getStaticField​(java.lang.reflect.Field field)
                                               throws java.lang.IllegalAccessException
        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.
        Parameters:
        field - The field to get
        Returns:
        The field's current value
        Throws:
        java.lang.IllegalAccessException - when unable to access the specified field because access modifiers prevent it
      • invokeConstructor

        public static java.lang.Object invokeConstructor​(java.lang.reflect.Constructor<?> constructor,
                                                         java.lang.Object... args)
                                                  throws java.lang.reflect.InvocationTargetException,
                                                         java.lang.IllegalAccessException,
                                                         java.lang.InstantiationException
        Invoke the specified Constructor with the supplied arguments.
        Parameters:
        constructor - The method to invoke
        args - The invocation arguments (may be null)
        Returns:
        The invocation result, if any
        Throws:
        java.lang.IllegalAccessException - when unable to access the specified constructor because access modifiers prevent it
        java.lang.reflect.InvocationTargetException - when a reflection invocation fails
        java.lang.InstantiationException - when an instantiation fails
      • invokeMethod

        public static java.lang.Object invokeMethod​(java.lang.reflect.Method method,
                                                    java.lang.Object target,
                                                    java.lang.Object... args)
                                             throws java.lang.reflect.InvocationTargetException,
                                                    java.lang.IllegalAccessException
        Invoke the specified Method against the supplied target object with the supplied arguments. The target object can be null when invoking a static Method.
        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.lang.IllegalAccessException - when unable to access the specified method because access modifiers prevent it
        java.lang.reflect.InvocationTargetException - when a reflection invocation fails
      • invokeStaticMethod

        public static java.lang.Object invokeStaticMethod​(java.lang.reflect.Method method,
                                                          java.lang.Object... args)
                                                   throws java.lang.reflect.InvocationTargetException,
                                                          java.lang.IllegalAccessException
        Invoke the specified static Method with the supplied arguments.
        Parameters:
        method - The method to invoke
        args - The invocation arguments (may be null)
        Returns:
        The invocation result, if any
        Throws:
        java.lang.IllegalAccessException - when unable to access the specified method because access modifiers prevent it
        java.lang.reflect.InvocationTargetException - when a reflection invocation fails
      • getDeclaredMethods

        private static java.lang.reflect.Method[] getDeclaredMethods​(java.lang.Class<?> clazz)
        This variant retrieves Class.getDeclaredMethods() from a local cache in order to avoid the JVM's SecurityManager check and defensive array copying. In addition, it also includes Java 8 default methods from locally implemented interfaces, since those are effectively to be treated just like declared methods.
        Parameters:
        clazz - the class to introspect
        Returns:
        the cached array of methods
        See Also:
        Class.getDeclaredMethods()
      • findConcreteMethodsOnInterfaces

        private static java.util.List<java.lang.reflect.Method> findConcreteMethodsOnInterfaces​(java.lang.Class<?> clazz)