messif.executor
Class SingleMethodExecutor

java.lang.Object
  extended by messif.executor.SingleMethodExecutor
All Implemented Interfaces:
Executable
Direct Known Subclasses:
MethodThread

public class SingleMethodExecutor
extends java.lang.Object
implements Executable

A single Executable method. Invokes a single method on given object when executed.


Field Summary
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 Summary
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.
 
Method Summary
 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

method

protected final java.lang.reflect.Method method
Method to call before/after execution


object

protected final java.lang.Object object
Object on which invoke the prepare/finalize method


arguments

protected final java.lang.Object[] arguments
Arguments for prepare/finalize method


returnedValue

protected java.lang.Object returnedValue
Returned value from the executed method

Constructor Detail

SingleMethodExecutor

public SingleMethodExecutor(java.lang.reflect.Method method,
                            java.lang.Object object,
                            java.lang.Object[] arguments)
Creates a new instance of SingleMethodExecutor.

Parameters:
method - the method to execute
object - the instance on which to execute the method
arguments - the arguments for the method

SingleMethodExecutor

public SingleMethodExecutor(java.lang.reflect.Method method,
                            java.lang.Object[] possibleObjects,
                            java.lang.Object[] arguments)
                     throws java.lang.NoSuchMethodException
Creates a new instance of SingleMethodExecutor.

Parameters:
method - the method to execute
possibleObjects - 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 method
Throws:
java.lang.NoSuchMethodException - if there was no instance compatible with the given method

SingleMethodExecutor

public SingleMethodExecutor(java.lang.Object object,
                            java.lang.String methodName,
                            java.lang.Object... arguments)
                     throws java.lang.NoSuchMethodException
Creates a new instance of SingleMethodExecutor.

Parameters:
object - the instance on which to execute the method
methodName - the name of the method to execute
arguments - the arguments for the method
Throws:
java.lang.NoSuchMethodException - if there was no method compatible with the given parameters
Method Detail

chooseExecutionObject

public static java.lang.Object chooseExecutionObject(java.lang.reflect.Method method,
                                                     java.lang.Object[] objects)
                                              throws java.lang.NoSuchMethodException
Searches a list of instances for an instance that is compatible with the given method. That is, the first object that is instance of the class that declared the method is returned.

Parameters:
method - the method for which to search a compatible instance
objects - the list of instances to search
Returns:
a compatible instance from the list
Throws:
java.lang.NoSuchMethodException - if there was no instance compatible with the given method

getDeclaredMethod

public static java.lang.reflect.Method getDeclaredMethod(java.lang.Class<?> classType,
                                                         java.lang.String name,
                                                         java.lang.Class[] parameterTypes)
                                                  throws java.lang.NoSuchMethodException
Returns a declared method (i.e. public, protected or private) that matches the given name and parameter types.

Parameters:
classType - the class in which to start the search
name - the name of the method
parameterTypes - the types of parameters of the method
Returns:
the method that matches the parameters
Throws:
java.lang.NoSuchMethodException - if there was no method that matches the given parameters

getArgumentCount

public int getArgumentCount()
Returns the number of executed arguments.

Returns:
the number of executed arguments

getArgument

public java.lang.Object getArgument(int index)
Returns the index-th argument of the executed method.

Parameters:
index - the index (zero-based) of the argument to return
Returns:
the argument of the executed method

getArguments

public java.lang.Object[] getArguments()
Returns the array of all arguments of the executed method.

Returns:
the array of all arguments of the executed method

getArgument

public <E> E getArgument(java.lang.Class<E> argClass)
              throws java.util.NoSuchElementException
Returns executed argument that has a specified class.

Type Parameters:
E - the distinguishing class of the argument
Parameters:
argClass - the distinguishing class of the argument
Returns:
executed argument
Throws:
java.util.NoSuchElementException - if a parameter with the argClass class was not found

getReturnedValue

public java.lang.Object getReturnedValue()
Returns value returned by the executed method.

Returns:
value returned by the executed method

execute

public void execute()
             throws java.lang.NoSuchMethodException,
                    java.lang.reflect.InvocationTargetException
Invoke the method represented by this object

Specified by:
execute in interface Executable
Throws:
java.lang.NoSuchMethodException - if there is no code to execute
java.lang.reflect.InvocationTargetException - if there was an exception during executing the code