public class ReflectionInvoker<T,V>
extends java.lang.Object
Client should specify parameters for a method invocation.
// Using ReflectionUtils
ReflectionUtils.newInstance(Person.class,
new Class[] {String.class, String.class, int.class},
new Object[] {"John", "Smith", 35})
// Using ReflectionInvoker
Person person = ReflectionInvoker.constructor(Person.class)
.parameters(String.class, String.class, int.class)
.invoke("John", "Smith", 35);
// Invoke constructor without parameters
ReflectionInvoker.constructor(Person.class).invoke();
// Invoke method
Person person = ...
ReflectionInvoker.method(person, "setName").invoke("James");
// Create invoker for multiple invocations
ReflectionInvoker invoker = ReflectionInvoker.method(person, "setName");
invoker.invoke("John");
...
invoker.invoke("James");
// Create invoker for multiple constructor invocation
ReflectionInvoker invoker = ReflectionInvoker.constructor(Person.class).parameters(String.class, Integer.class);
Person john = invoker.invoke("John", 24);
Person james = invoker.invoke("James", 33);
| Constructor and Description |
|---|
ReflectionInvoker(java.lang.Class<T> clazz,
java.lang.String methodName,
java.lang.Class<?>... parameters)
Creates builder for specified class.
|
| Modifier and Type | Method and Description |
|---|---|
static <T> ReflectionInvoker<T,T> |
constructor(java.lang.Class<T> clazz)
Create constructor invoker.
|
ReflectionInvoker<T,V> |
errorMessage(java.lang.String pattern,
java.lang.Object... parameters)
An error message to be added to standard exception message.
|
V |
invoke(java.lang.Object... arguments)
Creates new instance.
|
static <T> T |
invokeMethod(java.lang.Object target,
java.lang.reflect.Method method,
java.lang.Object... arguments)
Invokes specified method of specified target using reflection.
|
static <T,V> ReflectionInvoker<T,V> |
method(java.lang.Class<T> clazz,
java.lang.String methodName)
Create method invoker for specified class and method name.
|
static <T,V> ReflectionInvoker<T,V> |
method(T target,
java.lang.String methodName)
Create method invoker for specified method of target object.
|
static <T> T |
newInstance(java.lang.Class<T> clazz,
java.lang.Class[] parameters,
java.lang.Object[] arguments)
Creates instance of specified class and wraps checked exceptions into unchecked ones.
|
ReflectionInvoker<T,V> |
parameters(java.lang.Class<?>... parameters)
Specify parameter parameters as vararg.
|
ReflectionInvoker<T,V> |
parameters(java.util.List<java.lang.Class<?>> types)
Specify parameter parameters as list.
|
ReflectionInvoker<T,V> |
target(T target)
Specify parameter parameters as list.
|
public ReflectionInvoker(java.lang.Class<T> clazz, java.lang.String methodName, java.lang.Class<?>... parameters)
clazz - Class a class to create instance ofpublic static <T> ReflectionInvoker<T,T> constructor(java.lang.Class<T> clazz)
clazz - Class a class to create instance ofpublic static <T,V> ReflectionInvoker<T,V> method(java.lang.Class<T> clazz, java.lang.String methodName)
This method is used if target object isn't known and should be provided later with target method.
clazz - Class a class of target objectmethodName - String name of method to invokepublic static <T,V> ReflectionInvoker<T,V> method(T target, java.lang.String methodName)
target - Class a class to create instance ofmethodName - String name of method to invokepublic ReflectionInvoker<T,V> target(T target)
target - Object set target object to invoke methods onpublic ReflectionInvoker<T,V> parameters(java.util.List<java.lang.Class<?>> types)
types - List constructor parameter parameters.public ReflectionInvoker<T,V> parameters(java.lang.Class<?>... parameters)
parameters - vararg constructor parameter parameters.public ReflectionInvoker<T,V> errorMessage(java.lang.String pattern, java.lang.Object... parameters)
pattern - String error message pattern.parameters - vararg message pattern parameters.public V invoke(java.lang.Object... arguments)
arguments - vararg of actual parameters.java.lang.IllegalArgumentException - if number actual parameters more or less than number of formal parameters (parameters).ReflectionException - if object creation failspublic static <T> T invokeMethod(java.lang.Object target,
java.lang.reflect.Method method,
java.lang.Object... arguments)
Method wraps all checked exceptions into unchecked exceptions.
method - Method to invoketarget - Object the target the underlying method is invoked fromarguments - vararg array of method arguments.public static <T> T newInstance(java.lang.Class<T> clazz,
java.lang.Class[] parameters,
java.lang.Object[] arguments)
Converts checked exceptions to unchecked
clazz - Class a class to create instanceparameters - Class[] array of constructor parameters (parameter types).arguments - Object[] an array of constructor arguments.