Class MethodExecutionStrategy
- java.lang.Object
-
- ch.obermuhlner.scriptengine.java.execution.MethodExecutionStrategy
-
- All Implemented Interfaces:
ExecutionStrategy
public class MethodExecutionStrategy extends java.lang.Object implements ExecutionStrategy
TheExecutionStrategythat executes a specific method. This implementation has three static constructor methods to define the method that should be called:byMethod(Method, Object...)to call the specified method and pass it the specified arguments.byArgumentTypes(Class, String, Class[], Object...)to call the public method with the specified argument types and pass it the specified arguments.byMatchingArguments(Class, String, Object...)to call a public method that matches the specified arguments.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static MethodExecutionStrategybyArgumentTypes(java.lang.Class<?> clazz, java.lang.String methodName, java.lang.Class<?>[] argumentTypes, java.lang.Object... arguments)Creates aMethodExecutionStrategythat will call the public method with the specified argument types and passes the specified argument list.static MethodExecutionStrategybyMatchingArguments(java.lang.Class<?> clazz, java.lang.String methodName, java.lang.Object... arguments)Creates aMethodExecutionStrategythat will call a public method that matches the specified arguments.static MethodExecutionStrategybyMethod(java.lang.reflect.Method method, java.lang.Object... arguments)Creates aMethodExecutionStrategythat will call the specifiedMethod.java.lang.Objectexecute(java.lang.Object instance)Executes a method on an object instance, or a static method if the specified instance isnull.
-
-
-
Method Detail
-
execute
public java.lang.Object execute(java.lang.Object instance) throws javax.script.ScriptExceptionDescription copied from interface:ExecutionStrategyExecutes a method on an object instance, or a static method if the specified instance isnull.- Specified by:
executein interfaceExecutionStrategy- Parameters:
instance- the object instance to be executed ornullto execute a static method- Returns:
- the return value of the method, or
null - Throws:
javax.script.ScriptException- if no method to execute was found
-
byMethod
public static MethodExecutionStrategy byMethod(java.lang.reflect.Method method, java.lang.Object... arguments)
Creates aMethodExecutionStrategythat will call the specifiedMethod.- Parameters:
method- theMethodto executearguments- the arguments to be passed to the method- Returns:
- the value returned by the method, or
null
-
byArgumentTypes
public static MethodExecutionStrategy byArgumentTypes(java.lang.Class<?> clazz, java.lang.String methodName, java.lang.Class<?>[] argumentTypes, java.lang.Object... arguments) throws javax.script.ScriptException
Creates aMethodExecutionStrategythat will call the public method with the specified argument types and passes the specified argument list.- Parameters:
clazz- theClassmethodName- the method nameargumentTypes- the argument types defining the constructor to callarguments- the arguments to pass to the constructor (may containnull)- Returns:
- the created
DefaultConstructorStrategy - Throws:
javax.script.ScriptException- if no matching public method was found
-
byMatchingArguments
public static MethodExecutionStrategy byMatchingArguments(java.lang.Class<?> clazz, java.lang.String methodName, java.lang.Object... arguments) throws javax.script.ScriptException
Creates aMethodExecutionStrategythat will call a public method that matches the specified arguments. A method must match all specified arguments, exceptnullvalues which match any non-primitive type. The conversion from object types into corresponding primitive types (for exampleIntegerintoint) is handled automatically.- Parameters:
clazz- theClassmethodName- the method namearguments- the arguments to be passed to the method- Returns:
- the value returned by the method, or
null - Throws:
javax.script.ScriptException- if no matching public method was found
-
-