Package io.fluentlenium.utils
Class ReflectionUtils
java.lang.Object
io.fluentlenium.utils.ReflectionUtils
Utility class for reflection.
-
Method Summary
Modifier and TypeMethodDescriptionstatic Object
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>
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 <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 Method
Get public method by name from the declaring class.static Object
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
(int mandatoryCount, Class<T> cls, Object... args) Creates a new instance by trying every 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 void
Set the field even if not accessible.static Object[]
static Class<?>[]
Converts an array ofObject
into an array ofClass
objects.static <T> Class<T>
wrapPrimitive
(Class<T> clazz) Wrap given class to it's primitive class if it's matching a primitive class.
-
Method Details
-
wrapPrimitive
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
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
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
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> Constructor<T> getConstructor(Class<T> cls, Class<?>... argsTypes) throws 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:
NoSuchMethodException
- if a matching method is not found.
-
getConstructorOptional
public static <T> Constructor<T> getConstructorOptional(int mandatoryCount, Class<T> cls, Class<?>... argsTypes) throws 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:
NoSuchMethodException
- if a matching method is not found.
-
newInstance
public static <T> T newInstance(Class<T> cls, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, 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:
NoSuchMethodException
- if a matching method is not found.IllegalAccessException
- if thisConstructor
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.
-
newInstanceOptionalArgs
public static <T> T newInstanceOptionalArgs(Class<T> cls, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, 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:
NoSuchMethodException
- if a matching method is not found.IllegalAccessException
- if thisConstructor
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.
-
newInstanceOptionalArgs
public static <T> T newInstanceOptionalArgs(int mandatoryCount, Class<T> cls, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, 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:
NoSuchMethodException
- if a matching method is not found.IllegalAccessException
- if thisConstructor
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.
-
invoke
public static Object invoke(Method method, Object obj, Object... args) throws InvocationTargetException, 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:
IllegalAccessException
- if thisMethod
object is enforcing Java language access control and the underlying method is inaccessible.InvocationTargetException
- if the underlying method throws an exception.- See Also:
-
get
Get the field value even if not accessible.- Parameters:
field
- field to getobj
- instance to get- Returns:
- field value
- Throws:
IllegalAccessException
- if thisField
object is enforcing Java language access control and the underlying field is inaccessible.- See Also:
-
set
Set the field even if not accessible.- Parameters:
field
- field to setobj
- instance to setvalue
- value of the field to set- Throws:
IllegalAccessException
- if thisField
object is enforcing Java language access control and the underlying field is either inaccessible or final.- See Also:
-
getFirstGenericType
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
-
getMethod
Get public method by name from the declaring class.- Parameters:
declaringClass
- declaring classname
- method nametypes
- argument types- Returns:
- the public method by the specified name
- Throws:
IllegalArgumentException
- when there is no method found with the specified name
-