Class ReflectionUtils


  • public final class ReflectionUtils
    extends java.lang.Object
    Utility class for reflection.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.Object get​(java.lang.reflect.Field field, java.lang.Object obj)
      Get the field value even if not accessible.
      static <T> java.lang.reflect.Constructor<T> getConstructor​(java.lang.Class<T> cls, java.lang.Class<?>... argsTypes)
      Retrieve the constructor of a class for given argument types.
      static <T> java.lang.reflect.Constructor<T> getConstructorOptional​(int mandatoryCount, java.lang.Class<T> cls, java.lang.Class<?>... argsTypes)
      Retrieve the constructor of a class for given optional argument types, considering mandatory values at the beginning of the given types.
      static <T> T getDefault​(java.lang.Class<T> type)
      Get default value for given type.
      static java.lang.Class<?> getFirstGenericType​(java.lang.reflect.Field field)
      Retrieve the first generic type of the field type.
      static java.lang.reflect.Method getMethod​(java.lang.Class<?> declaringClass, java.lang.String name, java.lang.Class... types)
      Get public method by name from the declaring class.
      static java.lang.Object invoke​(java.lang.reflect.Method method, java.lang.Object obj, java.lang.Object... args)
      Invoke the method event if not accessible.
      static <T> T newInstance​(java.lang.Class<T> cls, java.lang.Object... args)
      Creates a new instance matching possible constructors with provided args.
      static <T> T newInstanceOptionalArgs​(int mandatoryCount, java.lang.Class<T> cls, java.lang.Object... args)
      Creates a new instance by trying every possible constructors with provided args.
      static <T> T newInstanceOptionalArgs​(java.lang.Class<T> cls, java.lang.Object... args)
      Creates a new instance by trying every possible constructors with provided args.
      static void set​(java.lang.reflect.Field field, java.lang.Object obj, java.lang.Object value)
      Set the field even if not accessible.
      static java.lang.Object[] toArgs​(java.util.function.Function<java.lang.Class<?>,​java.lang.Object> valueSupplier, java.lang.Class<?>... array)
      Converts an array of values provided by an array of Class and Function supplying value for each class into an array of Object
      static java.lang.Class<?>[] toClass​(java.lang.Object... array)
      Converts an array of Object into an array of Class objects.
      static <T> java.lang.Class<T> wrapPrimitive​(java.lang.Class<T> clazz)
      Wrap given class to it's primitive class if it's matching a primitive class.
      • Methods inherited from class java.lang.Object

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

      • wrapPrimitive

        public static <T> java.lang.Class<T> wrapPrimitive​(java.lang.Class<T> clazz)
        Wrap given class to it's primitive class if it's matching a primitive class.
        Type Parameters:
        T - type of class
        Parameters:
        clazz - primitive class or not
        Returns:
        class or primitive class
      • toClass

        public static java.lang.Class<?>[] toClass​(java.lang.Object... array)
        Converts an array of Object into an array of Class objects.

        If any of these objects is null, a null element will be inserted into the array.

        This method returns null for a null input array.

        Parameters:
        array - an Object array
        Returns:
        a Class array, null if null array input
      • toArgs

        public static java.lang.Object[] toArgs​(java.util.function.Function<java.lang.Class<?>,​java.lang.Object> valueSupplier,
                                                java.lang.Class<?>... array)
        Converts an array of values provided by an array of Class and Function supplying value for each class into an array of Object
        Parameters:
        valueSupplier - supplier of values for each class
        array - array of class
        Returns:
        array of values
      • getDefault

        public static <T> T getDefault​(java.lang.Class<T> type)
        Get default value for given type.
        Type Parameters:
        T - type of value
        Parameters:
        type - type of value to get the default
        Returns:
        default value
      • getConstructor

        public static <T> java.lang.reflect.Constructor<T> getConstructor​(java.lang.Class<T> cls,
                                                                          java.lang.Class<?>... argsTypes)
                                                                   throws java.lang.NoSuchMethodException
        Retrieve the constructor of a class for given argument types.
        Type Parameters:
        T - type to retrieve the constructor from
        Parameters:
        cls - class to retrieve the constructor from
        argsTypes - argument types
        Returns:
        matching constructor for given argument values
        Throws:
        java.lang.NoSuchMethodException - if a matching method is not found.
      • getConstructorOptional

        public static <T> java.lang.reflect.Constructor<T> getConstructorOptional​(int mandatoryCount,
                                                                                  java.lang.Class<T> cls,
                                                                                  java.lang.Class<?>... argsTypes)
                                                                           throws java.lang.NoSuchMethodException
        Retrieve the constructor of a class for given optional argument types, considering mandatory values at the beginning of the given types.
        Type Parameters:
        T - type to retrieve the constructor from
        Parameters:
        mandatoryCount - number of mandatory arguments at the beginning of the given arguments
        cls - class to retrieve the constructor from
        argsTypes - argument types
        Returns:
        matching constructor for given optional argument values
        Throws:
        java.lang.NoSuchMethodException - if a matching method is not found.
      • newInstance

        public static <T> T newInstance​(java.lang.Class<T> cls,
                                        java.lang.Object... args)
                                 throws java.lang.NoSuchMethodException,
                                        java.lang.IllegalAccessException,
                                        java.lang.reflect.InvocationTargetException,
                                        java.lang.InstantiationException
        Creates a new instance matching possible constructors with provided args.
        Type Parameters:
        T - type of the instance
        Parameters:
        cls - class to instantiate
        args - arguments of the constructor
        Returns:
        new instance
        Throws:
        java.lang.NoSuchMethodException - if a matching method is not found.
        java.lang.IllegalAccessException - if this Constructor object is enforcing Java language access control and the underlying constructor is inaccessible.
        java.lang.InstantiationException - if the class that declares the underlying constructor represents an abstract class.
        java.lang.reflect.InvocationTargetException - if the underlying constructor throws an exception.
      • newInstanceOptionalArgs

        public static <T> T newInstanceOptionalArgs​(java.lang.Class<T> cls,
                                                    java.lang.Object... args)
                                             throws java.lang.NoSuchMethodException,
                                                    java.lang.IllegalAccessException,
                                                    java.lang.reflect.InvocationTargetException,
                                                    java.lang.InstantiationException
        Creates a new instance by trying every possible constructors with provided args.
        Type Parameters:
        T - type of the instance
        Parameters:
        cls - class to instantiate
        args - arguments of the constructor
        Returns:
        new instance
        Throws:
        java.lang.NoSuchMethodException - if a matching method is not found.
        java.lang.IllegalAccessException - if this Constructor object is enforcing Java language access control and the underlying constructor is inaccessible.
        java.lang.InstantiationException - if the class that declares the underlying constructor represents an abstract class.
        java.lang.reflect.InvocationTargetException - if the underlying constructor throws an exception.
      • newInstanceOptionalArgs

        public static <T> T newInstanceOptionalArgs​(int mandatoryCount,
                                                    java.lang.Class<T> cls,
                                                    java.lang.Object... args)
                                             throws java.lang.NoSuchMethodException,
                                                    java.lang.IllegalAccessException,
                                                    java.lang.reflect.InvocationTargetException,
                                                    java.lang.InstantiationException
        Creates a new instance by trying every possible constructors with provided args.
        Type Parameters:
        T - type of the instance
        Parameters:
        mandatoryCount - count of mandatory arguments
        cls - class to instantiate
        args - arguments of the constructor
        Returns:
        new instance
        Throws:
        java.lang.NoSuchMethodException - if a matching method is not found.
        java.lang.IllegalAccessException - if this Constructor object is enforcing Java language access control and the underlying constructor is inaccessible.
        java.lang.InstantiationException - if the class that declares the underlying constructor represents an abstract class.
        java.lang.reflect.InvocationTargetException - if the underlying constructor throws an exception.
      • invoke

        public static java.lang.Object invoke​(java.lang.reflect.Method method,
                                              java.lang.Object obj,
                                              java.lang.Object... args)
                                       throws java.lang.reflect.InvocationTargetException,
                                              java.lang.IllegalAccessException
        Invoke the method event if not accessible.
        Parameters:
        method - method to invoke
        obj - object to invoke
        args - arguments of the method
        Returns:
        return value from the method invocation
        Throws:
        java.lang.IllegalAccessException - if this Method object is enforcing Java language access control and the underlying method is inaccessible.
        java.lang.reflect.InvocationTargetException - if the underlying method throws an exception.
        See Also:
        Method.invoke(Object, Object...)
      • get

        public static java.lang.Object get​(java.lang.reflect.Field field,
                                           java.lang.Object obj)
                                    throws java.lang.IllegalAccessException
        Get the field value even if not accessible.
        Parameters:
        field - field to get
        obj - instance to get
        Returns:
        field value
        Throws:
        java.lang.IllegalAccessException - if this Field object is enforcing Java language access control and the underlying field is inaccessible.
        See Also:
        Field.get(Object)
      • set

        public static void set​(java.lang.reflect.Field field,
                               java.lang.Object obj,
                               java.lang.Object value)
                        throws java.lang.IllegalAccessException
        Set the field even if not accessible.
        Parameters:
        field - field to set
        obj - instance to set
        value - value of the field to set
        Throws:
        java.lang.IllegalAccessException - if this Field object is enforcing Java language access control and the underlying field is either inaccessible or final.
        See Also:
        Field.set(Object, Object)
      • getFirstGenericType

        public static java.lang.Class<?> getFirstGenericType​(java.lang.reflect.Field field)
        Retrieve the first generic type of the field type.
        Parameters:
        field - field to analyze
        Returns:
        first generic type, or null if no generic type is found
      • getMethod

        public static java.lang.reflect.Method getMethod​(java.lang.Class<?> declaringClass,
                                                         java.lang.String name,
                                                         java.lang.Class... types)
        Get public method by name from the declaring class.
        Parameters:
        declaringClass - declaring class
        name - method name
        types - argument types
        Returns:
        the public method by the specified name
        Throws:
        java.lang.IllegalArgumentException - when there is no method found with the specified name