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