Class ReflectionUtils


  • public final class ReflectionUtils
    extends Object
    Utility class for reflection.
    • Method Detail

      • wrapPrimitive

        public static <T> Class<T> wrapPrimitive​(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 Class<?>[] toClass​(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 Object[] toArgs​(Function<Class<?>,​Object> valueSupplier,
                                      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​(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> Constructor<T> getConstructor​(Class<T> cls,
                                                        Class<?>... argsTypes)
                                                 throws 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:
        NoSuchMethodException - if a matching method is not found.
      • getConstructorOptional

        public static <T> Constructor<T> getConstructorOptional​(int mandatoryCount,
                                                                Class<T> cls,
                                                                Class<?>... argsTypes)
                                                         throws 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:
        NoSuchMethodException - if a matching method is not found.
      • getFirstGenericType

        public static Class<?> getFirstGenericType​(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 Method getMethod​(Class<?> declaringClass,
                                       String name,
                                       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:
        IllegalArgumentException - when there is no method found with the specified name