Package org.fluentlenium.utils
Class ReflectionUtils
- java.lang.Object
-
- org.fluentlenium.utils.ReflectionUtils
-
public final class ReflectionUtils extends java.lang.Object
Utility class for reflection.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method 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>
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> 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(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> T
newInstanceOptionalArgs(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 ofClass
andFunction
supplying value for each class into an array ofObject
static java.lang.Class<?>[]
toClass(java.lang.Object... array)
Converts an array ofObject
into an array ofClass
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.
-
-
-
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 ofObject
into an array ofClass
objects.If any of these objects is null, a null element will be inserted into the array.
This method returns
null
for anull
input array.- Parameters:
array
- anObject
array- Returns:
- a
Class
array,null
if 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 ofClass
andFunction
supplying 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.NoSuchMethodException
Retrieve 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.NoSuchMethodException
Retrieve 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.InstantiationException
Creates 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 thisConstructor
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.
-
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.InstantiationException
Creates 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 thisConstructor
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.
-
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.InstantiationException
Creates 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 thisConstructor
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.
-
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.IllegalAccessException
Invoke 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 thisMethod
object 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.IllegalAccessException
Get the field value even if not accessible.- Parameters:
field
- field to getobj
- instance to get- Returns:
- field value
- Throws:
java.lang.IllegalAccessException
- if thisField
object 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.IllegalAccessException
Set 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 thisField
object 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
-
-