Class MethodExecutionStrategy
java.lang.Object
ch.obermuhlner.scriptengine.java.execution.MethodExecutionStrategy
- All Implemented Interfaces:
ExecutionStrategy
The
ExecutionStrategy
that 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.
-
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
MethodExecutionStrategy
(Method method, Object... arguments) -
Method Summary
Modifier and TypeMethodDescriptionstatic MethodExecutionStrategy
byArgumentTypes
(Class<?> clazz, String methodName, Class<?>[] argumentTypes, Object... arguments) Creates aMethodExecutionStrategy
that will call the public method with the specified argument types and passes the specified argument list.static MethodExecutionStrategy
byMainMethod
(Class<?> clazz, String... arguments) Creates aMethodExecutionStrategy
that will call thepublic static void main(String[] args)
with the specified arguments.static MethodExecutionStrategy
byMatchingArguments
(Class<?> clazz, String methodName, Object... arguments) Creates aMethodExecutionStrategy
that will call a public method that matches the specified arguments.static MethodExecutionStrategy
Creates aMethodExecutionStrategy
that will call the specifiedMethod
.Executes a method on an object instance, or a static method if the specified instance isnull
.
-
Field Details
-
method
-
arguments
-
-
Constructor Details
-
MethodExecutionStrategy
-
-
Method Details
-
execute
Description copied from interface:ExecutionStrategy
Executes a method on an object instance, or a static method if the specified instance isnull
.- Specified by:
execute
in interfaceExecutionStrategy
- Parameters:
instance
- the object instance to be executed ornull
to execute a static method- Returns:
- the return value of the method, or
null
- Throws:
ScriptException
- if no method to execute was found
-
byMethod
Creates aMethodExecutionStrategy
that will call the specifiedMethod
.- Parameters:
method
- theMethod
to executearguments
- the arguments to be passed to the method- Returns:
- the value returned by the method, or
null
-
byMainMethod
public static MethodExecutionStrategy byMainMethod(Class<?> clazz, String... arguments) throws ScriptException Creates aMethodExecutionStrategy
that will call thepublic static void main(String[] args)
with the specified arguments.- Parameters:
clazz
- theClass
arguments
- the arguments to pass to the main method- Returns:
- the created
MethodExecutionStrategy
- Throws:
ScriptException
- if nopublic static void main(String[] args)
method was found
-
byArgumentTypes
public static MethodExecutionStrategy byArgumentTypes(Class<?> clazz, String methodName, Class<?>[] argumentTypes, Object... arguments) throws ScriptException Creates aMethodExecutionStrategy
that will call the public method with the specified argument types and passes the specified argument list.- Parameters:
clazz
- theClass
methodName
- the method nameargumentTypes
- the argument types defining the constructor to callarguments
- the arguments to pass to the constructor (may containnull
)- Returns:
- the created
MethodExecutionStrategy
- Throws:
ScriptException
- if no matching public method was found
-
byMatchingArguments
public static MethodExecutionStrategy byMatchingArguments(Class<?> clazz, String methodName, Object... arguments) throws ScriptException Creates aMethodExecutionStrategy
that will call a public method that matches the specified arguments. A method must match all specified arguments, exceptnull
values which match any non-primitive type. The conversion from object types into corresponding primitive types (for exampleInteger
intoint
) is handled automatically.- Parameters:
clazz
- theClass
methodName
- the method namearguments
- the arguments to be passed to the method- Returns:
- the created
MethodExecutionStrategy
- Throws:
ScriptException
- if no matching public method was found
-