messif.executor
Class MethodNameExecutor

java.lang.Object
  extended by messif.executor.MethodExecutor
      extended by messif.executor.MethodNameExecutor

public class MethodNameExecutor
extends MethodExecutor

This class allows to execute a methods on a specified object. First, methods must be registered. This is done through constructor, where also an instance of the target object must be provided. The second parameter specify the required argument types that the method must have. Then the method execute method can be called. This method invokes the method of the instance (provided in constructor), which is appropriate for the provided arguments. Method backgroundExecute can be called to invoke the method in a new thread. A returned thread can be used for wait calls to test, whether the execution has finished and also to retrieve the data.


Nested Class Summary
 
Nested classes/interfaces inherited from class messif.executor.MethodExecutor
MethodExecutor.ExecutableMethod
 
Field Summary
 
Fields inherited from class messif.executor.MethodExecutor
executionObject
 
Constructor Summary
MethodNameExecutor(java.lang.Object executionObject, java.lang.Class<?>... methodPrototype)
          Create new instance of MethodNameExecutor and search for operation methods.
MethodNameExecutor(java.lang.Object executionObject, int nameArgIndex, java.lang.Class<?>... methodPrototype)
          Create new instance of MethodNameExecutor and search for operation methods.
 
Method Summary
protected  java.util.Map<java.lang.String,java.lang.reflect.Method> createRegisteredMethods(java.lang.Class classToSearch, boolean staticOnly, java.lang.Class<?>[] methodPrototype)
          Search the classToSearch for methods that are matching the given methodPrototype.
 java.util.List<java.lang.String> getDifferentiatingNames(java.lang.String regexp)
          Returns the list of method names that this executor supports and that match the specified regular expression.
static int getFirstStringClass(java.lang.Class<?>[] array)
          Search array for first String class.
protected  java.lang.reflect.Method getMethod(java.lang.Object[] arguments)
          Returns the method that is appropriate for the provided arguments.
protected  java.util.Collection<java.lang.reflect.Method> getRegisteredMethods()
          Returns all methods that are registered within this executor.
static java.lang.String getStringObject(java.lang.Object[] array, int index)
          Get string from array at specified position.
 
Methods inherited from class messif.executor.MethodExecutor
backgroundExecute, backgroundExecute, backgroundExecute, execute, execute, printUsage, printUsage, printUsage
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MethodNameExecutor

public MethodNameExecutor(java.lang.Object executionObject,
                          int nameArgIndex,
                          java.lang.Class<?>... methodPrototype)
                   throws java.lang.IllegalArgumentException
Create new instance of MethodNameExecutor and search for operation methods.

Parameters:
executionObject - an instance of the object to execute the operations on
methodPrototype - list of argument types for the registered methods
nameArgIndex - the index of an argument from methodPrototype, which will hold the method name
Throws:
java.lang.IllegalArgumentException - if either the method prototype or named argument index is invalid or the executionObject is null

MethodNameExecutor

public MethodNameExecutor(java.lang.Object executionObject,
                          java.lang.Class<?>... methodPrototype)
                   throws java.lang.IllegalArgumentException
Create new instance of MethodNameExecutor and search for operation methods. The first string class in the given methodPrototype is expected to hold the method name.

Parameters:
executionObject - an instance of the object to execute the operations on
methodPrototype - list of argument types for the registered methods
Throws:
java.lang.IllegalArgumentException - if either the method prototype or named argument index is invalid or the executionObject is null
Method Detail

createRegisteredMethods

protected java.util.Map<java.lang.String,java.lang.reflect.Method> createRegisteredMethods(java.lang.Class classToSearch,
                                                                                           boolean staticOnly,
                                                                                           java.lang.Class<?>[] methodPrototype)
Search the classToSearch for methods that are matching the given methodPrototype. The search is recursive starting from the top-level (excluding Object) class to classToSearch.

Parameters:
classToSearch - the class to search
staticOnly - the flag if only static methods are added
methodPrototype - the prototype of the methods to search for
Returns:
the map of methods using the respective method's name as a key

getFirstStringClass

public static int getFirstStringClass(java.lang.Class<?>[] array)
Search array for first String class. String[] class is allowed as the last item.

Parameters:
array - the array to search
Returns:
the index of the first String class in the array or -1 if it is not found

getStringObject

public static java.lang.String getStringObject(java.lang.Object[] array,
                                               int index)
                                        throws java.lang.ClassCastException,
                                               java.lang.IndexOutOfBoundsException
Get string from array at specified position. This method handles the encapsulated string arrays of the varargs.

Parameters:
array - the array of arguments
index - the index of the argument to get
Returns:
the string argument at the specified index of the array
Throws:
java.lang.ClassCastException - if the array item at the specified position is not a string
java.lang.IndexOutOfBoundsException - if the specified position is invalid

getMethod

protected java.lang.reflect.Method getMethod(java.lang.Object[] arguments)
                                      throws java.lang.NoSuchMethodException
Description copied from class: MethodExecutor
Returns the method that is appropriate for the provided arguments.

Specified by:
getMethod in class MethodExecutor
Parameters:
arguments - the arguments for the method
Returns:
the method that is appropriate for the provided arguments
Throws:
java.lang.NoSuchMethodException - if there is no method that can process the provided arguments in this executor

getDifferentiatingNames

public java.util.List<java.lang.String> getDifferentiatingNames(java.lang.String regexp)
Returns the list of method names that this executor supports and that match the specified regular expression.

Parameters:
regexp - the regular expression for matching method names
Returns:
the list of method names

getRegisteredMethods

protected java.util.Collection<java.lang.reflect.Method> getRegisteredMethods()
Description copied from class: MethodExecutor
Returns all methods that are registered within this executor.

Specified by:
getRegisteredMethods in class MethodExecutor
Returns:
all methods that are registered within this executor