public final class ReflectionUtils extends Object
| Modifier and Type | Method and Description |
|---|---|
static Object |
get(Field field,
Object obj)
Get the field value even if not accessible.
|
static <T> Constructor<T> |
getConstructor(Class<T> cls,
Class<?>... argsTypes)
Retrieve the constructor of a class for given argument types.
|
static <T> Constructor<T> |
getConstructor(Class<T> cls,
Object... args)
Retrieve the constructor of a class for given argument values.
|
static <T> Constructor<T> |
getConstructorOptional(Class<T> cls,
Class<?>... argsTypes)
Retrieve the constructor of a class for given optional argument types.
|
static <T> Constructor<T> |
getConstructorOptional(int mandatoryCount,
Class<T> cls,
Class<?>... argsTypes)
Retrieve the constructor of a class for given optional argument types, considering mandatory values at the
beginning of the given types.
|
static List<Method> |
getDeclaredMethodsWithAnnotation(Class<?> objectClass,
Class<? extends Annotation> annotation)
Retrieve declared methods marked with given annotation.
|
static List<Method> |
getDeclaredMethodsWithAnnotation(Object object,
Class<? extends Annotation> annotation)
Get declared methods that have the given annotation defined.
|
static <T> T |
getDefault(Class<T> type)
Get default value for given type.
|
static Class<?> |
getFirstGenericType(Field field)
Retrieve the first generic type of the field type.
|
static Object |
invoke(Method method,
Object obj,
Object... args)
Invoke the method event if not accessible.
|
static <T> T |
newInstance(Class<T> cls,
Object... args)
Creates a new instance matching possible constructors with provided args.
|
static <T> T |
newInstanceOptionalArgs(Class<T> cls,
Object... args)
Creates a new instance by trying every possible constructors with provided args.
|
static <T> T |
newInstanceOptionalArgs(int mandatoryCount,
Class<T> cls,
Object... args)
Creates a new instance by trying every possible constructors with provided args.
|
static void |
set(Field field,
Object obj,
Object value)
Set the field even if not accessible.
|
static Object[] |
toArgs(com.google.common.base.Function<Class<?>,Object> valueSupplier,
Class<?>... array)
|
static Class<?>[] |
toClass(Object... array)
Converts an array of
Object into an array of Class objects. |
static <T> Class<T> |
wrapPrimitive(Class<T> clazz)
Wrap given class to it's primitive class if it's matching a primitive class.
|
public static <T> Class<T> wrapPrimitive(Class<T> clazz)
T - type of classclazz - primitive class or notpublic static Class<?>[] toClass(Object... array)
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.
array - an Object arrayClass array, null if null array inputpublic static Object[] toArgs(com.google.common.base.Function<Class<?>,Object> valueSupplier, Class<?>... array)
Class and Function supplying value for each
class into an array of ObjectvalueSupplier - supplier of values for each classarray - array of classpublic static <T> T getDefault(Class<T> type)
T - type of valuetype - type of value to get the defaultpublic static <T> Constructor<T> getConstructor(Class<T> cls, Object... args) throws NoSuchMethodException
T - type to retrieve the constructor fromcls - class to retrieve the constructor fromargs - argument valuesNoSuchMethodException - if a matching method is not found.public static <T> Constructor<T> getConstructor(Class<T> cls, Class<?>... argsTypes) throws NoSuchMethodException
T - type to retrieve the constructor fromcls - class to retrieve the constructor fromargsTypes - argument typesNoSuchMethodException - if a matching method is not found.public static <T> Constructor<T> getConstructorOptional(Class<T> cls, Class<?>... argsTypes) throws NoSuchMethodException
T - type to retrieve the constructor fromcls - class to retrieve the constructor fromargsTypes - argument typesNoSuchMethodException - if a matching method is not found.public static <T> Constructor<T> getConstructorOptional(int mandatoryCount, Class<T> cls, Class<?>... argsTypes) throws NoSuchMethodException
T - type to retrieve the constructor frommandatoryCount - number of mandatory arguments at the beginning of the given argumentscls - class to retrieve the constructor fromargsTypes - argument typesNoSuchMethodException - if a matching method is not found.public static <T> T newInstance(Class<T> cls, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException
T - type of the instancecls - class to instantiateargs - arguments of the constructorNoSuchMethodException - if a matching method is not found.IllegalAccessException - if this Constructor object
is enforcing Java language access control and the underlying
constructor is inaccessible.InstantiationException - if the class that declares the
underlying constructor represents an abstract class.InvocationTargetException - if the underlying constructor
throws an exception.public static <T> T newInstanceOptionalArgs(Class<T> cls, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException
T - type of the instancecls - class to instantiateargs - arguments of the constructorNoSuchMethodException - if a matching method is not found.IllegalAccessException - if this Constructor object
is enforcing Java language access control and the underlying
constructor is inaccessible.InstantiationException - if the class that declares the
underlying constructor represents an abstract class.InvocationTargetException - if the underlying constructor
throws an exception.public static <T> T newInstanceOptionalArgs(int mandatoryCount,
Class<T> cls,
Object... args)
throws NoSuchMethodException,
IllegalAccessException,
InvocationTargetException,
InstantiationException
T - type of the instancemandatoryCount - count of mandatory argumentscls - class to instantiateargs - arguments of the constructorNoSuchMethodException - if a matching method is not found.IllegalAccessException - if this Constructor object
is enforcing Java language access control and the underlying
constructor is inaccessible.InstantiationException - if the class that declares the
underlying constructor represents an abstract class.InvocationTargetException - if the underlying constructor
throws an exception.public static List<Method> getDeclaredMethodsWithAnnotation(Object object, Class<? extends Annotation> annotation)
object - object instanceannotation - annotation to look forpublic static List<Method> getDeclaredMethodsWithAnnotation(Class<?> objectClass, Class<? extends Annotation> annotation)
objectClass - class to analyzeannotation - marker annotationpublic static Object invoke(Method method, Object obj, Object... args) throws InvocationTargetException, IllegalAccessException
method - method to invokeobj - object to invokeargs - arguments of the methodIllegalAccessException - if this Method object
is enforcing Java language access control and the underlying
method is inaccessible.InvocationTargetException - if the underlying method
throws an exception.Method.invoke(Object, Object...)public static Object get(Field field, Object obj) throws IllegalAccessException
field - field to getobj - instance to getIllegalAccessException - if this Field object
is enforcing Java language access control and the underlying
field is inaccessible.Field.get(Object)public static void set(Field field, Object obj, Object value) throws IllegalAccessException
field - field to setobj - instance to setvalue - value of the field to setIllegalAccessException - if this Field object
is enforcing Java language access control and the underlying
field is either inaccessible or final.Field.set(Object, Object)Copyright © 2016 FluentLenium. All Rights Reserved.