Class ReflectionUtils
- java.lang.Object
-
- de.rwth.swc.coffee4j.junit.engine.annotation.util.ReflectionUtils
-
public class ReflectionUtils extends Object
Utility class that provides helper methods for reflection related tasks
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> T
createNewInstance(Class<T> clazz, Object... constructorArgs)
Creates a new instance of a provided class.static String
extractMethodParameterName(Parameter parameter)
Extracts the name of theinput parameter
from amethod parameter
static List<String>
extractMethodParameterNames(Method method)
Extracts the parameter names from allParameter
of aMethod
according toextractMethodParameterName(Parameter)
static Map<String,Integer>
extractMethodParameterPositionMap(Method method)
Extracts the assignment ofinput parameters
to themethod parameters
static Method
findQualifiedMethod(Class<?> clazz, String methodName)
Attempts to find the method defined by the given name.static Method
findRequiredMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes)
Finds a method with provided parameter type in a classstatic Object[]
getInputForMethod(Combination combination, Method method)
Extracts the input for a method from aCombination
static Object
getObjectReturnedByMethod(Method method)
Gets the object returned by a method by potentially instantiating its declaring class in case the method is notstatic
static Object
invokeMethod(Method method, Object target, Object... args)
Invokes the method on the target with the provided arguments
-
-
-
Method Detail
-
getInputForMethod
public static Object[] getInputForMethod(Combination combination, Method method)
Extracts the input for a method from aCombination
- Parameters:
combination
- the combination from which to extract the inputmethod
- the method for which to extract the inputs. ItsParameter
parameters should be annotated withInputParameter
- Returns:
- the extracted input for the method
- See Also:
extractMethodParameterName(Parameter)
-
extractMethodParameterPositionMap
public static Map<String,Integer> extractMethodParameterPositionMap(Method method)
Extracts the assignment ofinput parameters
to themethod parameters
- Parameters:
method
- the method from which to extract the map from- Returns:
- the map, which assigns every
input parameter
an position in theParameter
list of aMethod
according to the names of theinput parameters
-
extractMethodParameterNames
public static List<String> extractMethodParameterNames(Method method)
Extracts the parameter names from allParameter
of aMethod
according toextractMethodParameterName(Parameter)
- Parameters:
method
- the method whose parameter names should be extracts- Returns:
- the names of the parameters
-
extractMethodParameterName
public static String extractMethodParameterName(Parameter parameter)
Extracts the name of theinput parameter
from amethod parameter
- Parameters:
parameter
- the method parameter- Returns:
- the name of the
input parameter
- See Also:
InputParameter
-
createNewInstance
public static <T> T createNewInstance(Class<T> clazz, Object... constructorArgs)
Creates a new instance of a provided class.Wrapper around
ReflectionSupport.newInstance(Class, Object...)
for more meaningful exceptions in case of failure- Type Parameters:
T
- the type of the class to instantiate- Parameters:
clazz
- the class to instantiateconstructorArgs
- the arguments of the constructor- Returns:
- the instantiated class
- See Also:
ReflectionSupport.newInstance(Class, Object...)
-
invokeMethod
public static Object invokeMethod(Method method, Object target, Object... args)
Invokes the method on the target with the provided argumentsWrapper around
ReflectionSupport.invokeMethod(Method, Object, Object...)
for more meaningful exceptions in case of failure- Parameters:
method
- the method to invokedtarget
- the instance on which to invoke the methodargs
- the arguments of the method- Returns:
- the value the method returns
- See Also:
ReflectionSupport.invokeMethod(Method, Object, Object...)
-
findRequiredMethod
public static Method findRequiredMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes)
Finds a method with provided parameter type in a classWrapper around
ReflectionSupport.findMethod(Class, String, Class[])
for more meaningful exceptions in case of failure- Parameters:
clazz
- the class where to find the methodmethodName
- the name of the methodparameterTypes
- the type of the parameters of the method- Returns:
- the searched method
-
getObjectReturnedByMethod
public static Object getObjectReturnedByMethod(Method method)
Gets the object returned by a method by potentially instantiating its declaring class in case the method is notstatic
- Parameters:
method
- the method from which to get the object from- Returns:
- the object return by the method
- See Also:
createNewInstance(Class, Object...)
,invokeMethod(Method, Object, Object...)
-
findQualifiedMethod
public static Method findQualifiedMethod(Class<?> clazz, String methodName)
Attempts to find the method defined by the given name. If the methodName is blank, a method in the class with the same name as the test method is returned. Otherwise, either the method with the given name in the class (if the methodName is not fully qualified) or the method in another class (if the method name is fully qualified) is returned. To fully qualify a method name, put the class name before the method name and separate them with a"#".- Parameters:
clazz
- the class where a non-qualified method is foundmethodName
- the name of the method which should be returned- Returns:
- the method defined by the name with the rules explained above
-
-