Package org.fluentlenium.utils
Class ReflectionUtils
- java.lang.Object
-
- org.fluentlenium.utils.ReflectionUtils
-
public final class ReflectionUtils extends java.lang.ObjectUtility class for reflection.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.Objectget(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>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 <T> TgetDefault(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.Objectinvoke(java.lang.reflect.Method method, java.lang.Object obj, java.lang.Object... args)Invoke the method event if not accessible.static <T> TnewInstance(java.lang.Class<T> cls, java.lang.Object... args)Creates a new instance matching possible constructors with provided args.static <T> TnewInstanceOptionalArgs(int mandatoryCount, java.lang.Class<T> cls, java.lang.Object... args)Creates a new instance by trying every possible constructors with provided args.static <T> TnewInstanceOptionalArgs(java.lang.Class<T> cls, java.lang.Object... args)Creates a new instance by trying every possible constructors with provided args.static voidset(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 ofClassandFunctionsupplying value for each class into an array ofObjectstatic java.lang.Class<?>[]toClass(java.lang.Object... array)Converts an array ofObjectinto an array ofClassobjects.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.
-
-
-
Method Detail
-
wrapPrimitive
public 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.- Type Parameters:
T- type of class- Parameters:
clazz- primitive class or not- Returns:
- class or primitive class
-
toClass
public static java.lang.Class<?>[] toClass(java.lang.Object... array)
Converts an array ofObjectinto an array ofClassobjects.If any of these objects is null, a null element will be inserted into the array.
This method returns
nullfor anullinput array.- Parameters:
array- anObjectarray- Returns:
- a
Classarray,nullif null array input
-
toArgs
public 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 ofClassandFunctionsupplying value for each class into an array ofObject- Parameters:
valueSupplier- supplier of values for each classarray- array of class- Returns:
- array of values
-
getDefault
public static <T> T getDefault(java.lang.Class<T> type)
Get default value for given type.- Type Parameters:
T- type of value- Parameters:
type- type of value to get the default- Returns:
- default value
-
getConstructor
public static <T> java.lang.reflect.Constructor<T> getConstructor(java.lang.Class<T> cls, java.lang.Class<?>... argsTypes) throws java.lang.NoSuchMethodExceptionRetrieve the constructor of a class for given argument types.- Type Parameters:
T- type to retrieve the constructor from- Parameters:
cls- class to retrieve the constructor fromargsTypes- argument types- Returns:
- matching constructor for given argument values
- Throws:
java.lang.NoSuchMethodException- if a matching method is not found.
-
getConstructorOptional
public static <T> java.lang.reflect.Constructor<T> getConstructorOptional(int mandatoryCount, java.lang.Class<T> cls, java.lang.Class<?>... argsTypes) throws java.lang.NoSuchMethodExceptionRetrieve the constructor of a class for given optional argument types, considering mandatory values at the beginning of the given types.- Type Parameters:
T- type to retrieve the constructor from- Parameters:
mandatoryCount- number of mandatory arguments at the beginning of the given argumentscls- class to retrieve the constructor fromargsTypes- argument types- Returns:
- matching constructor for given optional argument values
- Throws:
java.lang.NoSuchMethodException- if a matching method is not found.
-
newInstance
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.InstantiationExceptionCreates a new instance matching possible constructors with provided args.- Type Parameters:
T- type of the instance- Parameters:
cls- class to instantiateargs- arguments of the constructor- Returns:
- new instance
- Throws:
java.lang.NoSuchMethodException- if a matching method is not found.java.lang.IllegalAccessException- if thisConstructorobject 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.
-
newInstanceOptionalArgs
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.InstantiationExceptionCreates a new instance by trying every possible constructors with provided args.- Type Parameters:
T- type of the instance- Parameters:
cls- class to instantiateargs- arguments of the constructor- Returns:
- new instance
- Throws:
java.lang.NoSuchMethodException- if a matching method is not found.java.lang.IllegalAccessException- if thisConstructorobject 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.
-
newInstanceOptionalArgs
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.InstantiationExceptionCreates a new instance by trying every possible constructors with provided args.- Type Parameters:
T- type of the instance- Parameters:
mandatoryCount- count of mandatory argumentscls- class to instantiateargs- arguments of the constructor- Returns:
- new instance
- Throws:
java.lang.NoSuchMethodException- if a matching method is not found.java.lang.IllegalAccessException- if thisConstructorobject 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.
-
invoke
public 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.IllegalAccessExceptionInvoke the method event if not accessible.- Parameters:
method- method to invokeobj- object to invokeargs- arguments of the method- Returns:
- return value from the method invocation
- Throws:
java.lang.IllegalAccessException- if thisMethodobject is enforcing Java language access control and the underlying method is inaccessible.java.lang.reflect.InvocationTargetException- if the underlying method throws an exception.- See Also:
Method.invoke(Object, Object...)
-
get
public static java.lang.Object get(java.lang.reflect.Field field, java.lang.Object obj) throws java.lang.IllegalAccessExceptionGet the field value even if not accessible.- Parameters:
field- field to getobj- instance to get- Returns:
- field value
- Throws:
java.lang.IllegalAccessException- if thisFieldobject is enforcing Java language access control and the underlying field is inaccessible.- See Also:
Field.get(Object)
-
set
public static void set(java.lang.reflect.Field field, java.lang.Object obj, java.lang.Object value) throws java.lang.IllegalAccessExceptionSet the field even if not accessible.- Parameters:
field- field to setobj- instance to setvalue- value of the field to set- Throws:
java.lang.IllegalAccessException- if thisFieldobject is enforcing Java language access control and the underlying field is either inaccessible or final.- See Also:
Field.set(Object, Object)
-
getFirstGenericType
public static java.lang.Class<?> getFirstGenericType(java.lang.reflect.Field field)
Retrieve the first generic type of the field type.- Parameters:
field- field to analyze- Returns:
- first generic type, or null if no generic type is found
-
-