Class ReflectionUtils


  • public class ReflectionUtils
    extends Object
    This class contains utility methods related to Java reflection.
    • Constructor Detail

      • ReflectionUtils

        public ReflectionUtils()
    • Method Detail

      • getGenericListElementType

        public static Class<?> getGenericListElementType​(Field field)
        Returns the class of elements in a List field. If the field has the raw List class, this method returns Object. Otherwise it gets the raw type from the bounded parameter type using getGenericTypeClass().
        Parameters:
        field - the field
        Returns:
        the element type
      • getGenericMapValueType

        public static Class<?> getGenericMapValueType​(Field field)
        Returns the class of values in a Map field. If the field has the raw Map class, this method returns Object. Otherwise it gets the raw type from the bounded parameter type using getGenericTypeClass().
        Parameters:
        field - the field
        Returns:
        the element type
      • getGenericTypeClass

        public static Class<?> getGenericTypeClass​(Type type)
        Returns the raw class of the specified generic type. It supports the following possibilities:

        • Type: returns Type
        • Type<...>: returns Type
        • ?: returns Object
        • ? extends Type: returns Type
        • ? extends Type<...>: returns Type

        Parameters:
        type - the generic type
        Returns:
        the raw class
      • newInstance

        public static <T> T newInstance​(Class<T> clazz,
                                        Object... params)
                                 throws InstantiationException,
                                        InvocationTargetException
        Tries to invoke a constructor of the specified class and return the result.
        Type Parameters:
        T - the class type
        Parameters:
        clazz - the class that should be constructed
        params - the parameters
        Returns:
        the new instance
        Throws:
        InstantiationException - if the constructor can't be invoked (for example the class or constructor is not public or the class is abstract or no matching constructor for the specified parameters is found)
        InvocationTargetException - if the constructor throws an exception
      • invokeMethod

        public static <T> T invokeMethod​(Object object,
                                         Class<T> resultClass,
                                         String methodName,
                                         Object... params)
                                  throws NoSuchMethodException,
                                         InvocationTargetException
        Tries to invoke a method on the specified object and return the result. It checks whether the method is public, non-static and non-abstract. If no method is found that matches the specified result class, method name and parameter list, this method throws a NoSuchMethodException. If you set the result class to null, this method returns null, even if the target method returns a value. If the target method is a void method, you must set the result class to null.
        Type Parameters:
        T - the result type
        Parameters:
        object - the object on which the method should be called
        resultClass - the desired result or null
        methodName - the method name
        params - the parameters
        Returns:
        the result or null
        Throws:
        NoSuchMethodException - if no matching method is found
        InvocationTargetException - if the target method throws an exception
      • invokeStaticMethod

        public static <T> T invokeStaticMethod​(Class<?> clazz,
                                               Class<T> resultClass,
                                               String methodName,
                                               Object... params)
                                        throws NoSuchMethodException,
                                               InvocationTargetException
        Tries to invoke a static method on the specified class and return the result. It checks whether the method is public, static and non-abstract. If no method is found that matches the specified result class, method name and parameter list, this method throws a NoSuchMethodException. If you set the result class to null, this method returns null, even if the target method returns a value. If the target method is a void method, you must set the result class to null.
        Type Parameters:
        T - the result type
        Parameters:
        clazz - the class on which the method should be called
        resultClass - the desired result or null
        methodName - the method name
        params - the parameters
        Returns:
        the result or null
        Throws:
        NoSuchMethodException - if no matching method is found
        InvocationTargetException - if the target method throws an exception
      • getField

        public static <T> T getField​(Object object,
                                     Class<T> resultClass,
                                     String fieldName)
                              throws NoSuchFieldException
        Tries to get the value of a field from the specified object. It checks whether the field is public and non-static. If no field is found that matches the specified result class and field name, this method throws a NoSuchFieldException.
        Type Parameters:
        T - the result type
        Parameters:
        object - the object from which the field value should be retrieved
        resultClass - the desired result type
        fieldName - the field name
        Returns:
        the field value
        Throws:
        NoSuchFieldException - if no matching field is found
      • getStaticField

        public static <T> T getStaticField​(Class<?> clazz,
                                           Class<T> resultClass,
                                           String fieldName)
                                    throws NoSuchFieldException
        Tries to get the value of a static field from the specified class. It checks whether the field is public and static. If no field is found that matches the specified result class and field name, this method throws a NoSuchFieldException.
        Type Parameters:
        T - the result type
        Parameters:
        clazz - the class from which the field value should be retrieved
        resultClass - the desired result type
        fieldName - the field name
        Returns:
        the field value
        Throws:
        NoSuchFieldException - if no matching field is found
      • primitiveTypeToObjectClass

        public static Class<?> primitiveTypeToObjectClass​(Class<?> clazz)
        If the specified class is a primitive type, this method returns its class equivalent. For example if the specified class is "int", it returns "Integer". If the specified class is not a primitive type, this method just returns the class.
        Parameters:
        clazz - the class or primitive type
        Returns:
        the class
      • getConstantIntMap

        public static Map<String,​Integer> getConstantIntMap​(Class<?> clazz,
                                                                  String prefix)
        Returns a map with all integer constants whose names start with the specified prefix.
        Parameters:
        clazz - the class where the constants are searched
        prefix - the prefix of the constant names
        Returns:
        a map from constant name to constant value
      • getConstantStringMap

        public static Map<String,​String> getConstantStringMap​(Class<?> clazz,
                                                                    String prefix)
        Returns a map with all string constants whose names start with the specified prefix.
        Parameters:
        clazz - the class where the constants are searched
        prefix - the prefix of the constant names
        Returns:
        a map from constant name to constant value
      • getConstantMap

        public static <T> Map<String,​T> getConstantMap​(Class<?> constantClass,
                                                             String prefix,
                                                             Class<T> valueClass)
        Returns a map with constant names and values that are defined in the "constantClass", whose names start with the specified prefix and whose values can be assigned to the specified class.
        Type Parameters:
        T - the type of values
        Parameters:
        constantClass - the class that defines the constants
        prefix - the prefix of the constant names
        valueClass - the value class
        Returns:
        a map from constant name to constant value
      • findConstantInt

        public static String findConstantInt​(Class<?> clazz,
                                             String prefix,
                                             int value)
        Tries to find an int constant with the specified value whose name starts with the specified prefix. It returns the name of the constant. If no such constant is found, it returns the integer value as a string.
        Parameters:
        clazz - the class where the constant is searched
        prefix - the prefix of the constant name
        value - the constant value
        Returns:
        the constant name or the integer value as a string