public final class ReflectionUtils extends Object
| Modifier and Type | Method and Description |
|---|---|
static Class<?> |
getCollectionType(Class<?> mainType,
Type genericType)
Returns the type the collection (array, Iterable) consists of.
|
static Object |
getFieldValue(Field field,
Object instance)
Gets the value of the given field in the given instance.
|
static Class<?> |
getGenericType(Type genericType)
Retrieves the generic subtype of the given type.
|
static Object |
invokeMethod(Method method,
Object instance,
Object... parameters)
Invokes the given method on the given instance with the provided parameters.
|
static <T> T |
newInstance(Constructor<T> constructor,
Object... parameters)
Invokes the given constructor with the provided parameters.
|
static Field[] |
safeGetDeclaredFields(Class<?> clazz)
Returns all fields of a class if not annotated with
NoFieldScan. |
static Method[] |
safeGetDeclaredMethods(Class<?> clazz)
Returns all methods of a class if not annotated with
NoMethodScan. |
static void |
setField(Field field,
Object instance,
Object value)
Sets the given field on the given instance.
|
static <T> Object |
toSuitableCollectionType(Class<?> rawType,
Set<T> result)
Attempts to convert the given
result Set to the given collection type. |
public static Object getFieldValue(Field field, Object instance)
field - the field to retrieveinstance - the instance to retrieve it from (null for static fields)public static void setField(Field field, Object instance, Object value)
field - the field to setinstance - the instance to set the field in (null for static fields)value - the value to setpublic static Object invokeMethod(Method method, Object instance, Object... parameters)
method - the method to invokeinstance - the instance to invoke it from (null for static methods)parameters - the parameters to usepublic static <T> T newInstance(Constructor<T> constructor, Object... parameters)
T - the type the constructor belongs toconstructor - the constructorparameters - the parameters to invoke the constructor with@Nullable public static Class<?> getGenericType(@Nullable Type genericType)
genericType - the type to get the generic type from@Nullable public static Class<?> getCollectionType(Class<?> mainType, @Nullable Type genericType)
mainType - the main typegenericType - the generic type if availablepublic static <T> Object toSuitableCollectionType(Class<?> rawType, Set<T> result)
result Set to the given collection type. If the given class
to match is an array, an array will be returned without any type checks. Otherwise, tries to satisfy
the field by returning a Set or a List instance. Throws an exception if no type could be used.T - the set's typerawType - the collection type to matchresult - the result Set to potentially convertpublic static Method[] safeGetDeclaredMethods(Class<?> clazz)
NoMethodScan. Otherwise returns an empty array.clazz - the class to processpublic static Field[] safeGetDeclaredFields(Class<?> clazz)
NoFieldScan. Otherwise returns an empty array.clazz - the class to processCopyright © 2017. All rights reserved.