Package eu.woolplatform.utils
Class ReflectionUtils
- java.lang.Object
-
- eu.woolplatform.utils.ReflectionUtils
-
public class ReflectionUtils extends Object
This class contains utility methods related to Java reflection.
-
-
Constructor Summary
Constructors Constructor Description ReflectionUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static StringfindConstantInt(Class<?> clazz, String prefix, int value)Tries to find an int constant with the specified value whose name starts with the specified prefix.static Map<String,Integer>getConstantIntMap(Class<?> clazz, String prefix)Returns a map with all integer constants whose names start with the specified prefix.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.static Map<String,String>getConstantStringMap(Class<?> clazz, String prefix)Returns a map with all string constants whose names start with the specified prefix.static <T> TgetField(Object object, Class<T> resultClass, String fieldName)Tries to get the value of a field from the specified object.static Class<?>getGenericListElementType(Field field)Returns the class of elements in a List field.static Class<?>getGenericMapValueType(Field field)Returns the class of values in a Map field.static Class<?>getGenericTypeClass(Type type)Returns the raw class of the specified generic type.static <T> TgetStaticField(Class<?> clazz, Class<T> resultClass, String fieldName)Tries to get the value of a static field from the specified class.static <T> TinvokeMethod(Object object, Class<T> resultClass, String methodName, Object... params)Tries to invoke a method on the specified object and return the result.static <T> TinvokeStaticMethod(Class<?> clazz, Class<T> resultClass, String methodName, Object... params)Tries to invoke a static method on the specified class and return the result.static <T> TnewInstance(Class<T> clazz, Object... params)Tries to invoke a constructor of the specified class and return the result.static Class<?>primitiveTypeToObjectClass(Class<?> clazz)If the specified class is a primitive type, this method returns its class equivalent.
-
-
-
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 usinggetGenericTypeClass().- 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 usinggetGenericTypeClass().- 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 constructedparams- 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 aNoSuchMethodException. 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 calledresultClass- the desired result or nullmethodName- the method nameparams- the parameters- Returns:
- the result or null
- Throws:
NoSuchMethodException- if no matching method is foundInvocationTargetException- 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 aNoSuchMethodException. 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 calledresultClass- the desired result or nullmethodName- the method nameparams- the parameters- Returns:
- the result or null
- Throws:
NoSuchMethodException- if no matching method is foundInvocationTargetException- 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 aNoSuchFieldException.- Type Parameters:
T- the result type- Parameters:
object- the object from which the field value should be retrievedresultClass- the desired result typefieldName- 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 aNoSuchFieldException.- Type Parameters:
T- the result type- Parameters:
clazz- the class from which the field value should be retrievedresultClass- the desired result typefieldName- 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 searchedprefix- 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 searchedprefix- 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 constantsprefix- the prefix of the constant namesvalueClass- 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 searchedprefix- the prefix of the constant namevalue- the constant value- Returns:
- the constant name or the integer value as a string
-
-