public class SingleMethodExecutor extends java.lang.Object implements Executable
Executable
method.
Invokes a single method on given object when executed
.Modifier and Type | Field and Description |
---|---|
protected java.lang.Object[] |
arguments
Arguments for prepare/finalize method
|
protected java.lang.reflect.Method |
method
Method to call before/after execution
|
protected java.lang.Object |
object
Object on which invoke the prepare/finalize method
|
protected java.lang.Object |
returnedValue
Returned value from the executed method
|
Constructor and Description |
---|
SingleMethodExecutor(java.lang.reflect.Method method,
java.lang.Object[] possibleObjects,
java.lang.Object[] arguments)
Creates a new instance of SingleMethodExecutor.
|
SingleMethodExecutor(java.lang.reflect.Method method,
java.lang.Object object,
java.lang.Object[] arguments)
Creates a new instance of SingleMethodExecutor.
|
SingleMethodExecutor(java.lang.Object object,
java.lang.String methodName,
java.lang.Object... arguments)
Creates a new instance of SingleMethodExecutor.
|
Modifier and Type | Method and Description |
---|---|
void |
execute()
Invoke the method represented by this object
|
<E> E |
getArgument(java.lang.Class<E> argClass)
Returns executed argument that has a specified class.
|
java.lang.Object |
getArgument(int index)
Returns the index-th argument of the executed method.
|
int |
getArgumentCount()
Returns the number of executed arguments.
|
java.lang.Object[] |
getArguments()
Returns the array of all arguments of the executed method.
|
static java.lang.reflect.Method |
getDeclaredMethod(java.lang.Class<?> classType,
java.lang.String name,
java.lang.Class[] parameterTypes)
Returns a declared method (i.e. public, protected or private) that matches
the given name and parameter types.
|
java.lang.Object |
getReturnedValue()
Returns value returned by the executed method.
|
static java.lang.Object |
chooseExecutionObject(java.lang.reflect.Method method,
java.lang.Object[] objects)
Searches a list of instances for an instance that is compatible with
the given method.
|
protected final java.lang.reflect.Method method
protected final java.lang.Object object
protected final java.lang.Object[] arguments
protected java.lang.Object returnedValue
public SingleMethodExecutor(java.lang.reflect.Method method, java.lang.Object object, java.lang.Object[] arguments)
method
- the method to executeobject
- the instance on which to execute the methodarguments
- the arguments for the methodpublic SingleMethodExecutor(java.lang.reflect.Method method, java.lang.Object[] possibleObjects, java.lang.Object[] arguments) throws java.lang.NoSuchMethodException
method
- the method to executepossibleObjects
- the list of instances on which to execute the method
(the correct one is selected using chooseExecutionObject(java.lang.reflect.Method, java.lang.Object[])
)arguments
- the arguments for the methodjava.lang.NoSuchMethodException
- if there was no instance compatible with the given methodpublic SingleMethodExecutor(java.lang.Object object, java.lang.String methodName, java.lang.Object... arguments) throws java.lang.NoSuchMethodException
object
- the instance on which to execute the methodmethodName
- the name of the method to executearguments
- the arguments for the methodjava.lang.NoSuchMethodException
- if there was no method compatible with the given parameterspublic static java.lang.Object chooseExecutionObject(java.lang.reflect.Method method, java.lang.Object[] objects) throws java.lang.NoSuchMethodException
method
- the method for which to search a compatible instanceobjects
- the list of instances to searchjava.lang.NoSuchMethodException
- if there was no instance compatible with the given methodpublic static java.lang.reflect.Method getDeclaredMethod(java.lang.Class<?> classType, java.lang.String name, java.lang.Class[] parameterTypes) throws java.lang.NoSuchMethodException
classType
- the class in which to start the searchname
- the name of the methodparameterTypes
- the types of parameters of the methodjava.lang.NoSuchMethodException
- if there was no method that matches the given parameterspublic int getArgumentCount()
public java.lang.Object getArgument(int index)
index
- the index (zero-based) of the argument to returnpublic java.lang.Object[] getArguments()
public <E> E getArgument(java.lang.Class<E> argClass) throws java.util.NoSuchElementException
E
- the distinguishing class of the argumentargClass
- the distinguishing class of the argumentjava.util.NoSuchElementException
- if a parameter with the argClass class was not foundpublic java.lang.Object getReturnedValue()
public void execute() throws java.lang.NoSuchMethodException, java.lang.reflect.InvocationTargetException
execute
in interface Executable
java.lang.NoSuchMethodException
- if there is no code to executejava.lang.reflect.InvocationTargetException
- if there was an exception during executing the code