The syntax of the script to compile is a sequence of import declarations (not allowed if you compile many scripts at a time, see below) followed by a sequence of statements, as defined in the Java Language Specification, Java SE 7 Edition, sections 7.5 and 14.
An implementation may or may not implement the concept of "local methods", i.e. method declarations being freely intermixed with statements.
Example:
import java.text.*;
System.out.println("HELLO");
System.out.println(new DecimalFormat("####,###.##").format(a));
(Notice that this expression refers to a parameter "a", as explained below.)
The script may complete abnormally, e.g. through a RETURN statement:
if (a == null) {
System.out.println("Oops!");
return;
}
Optionally, the script may be declared with a non-void return type. In this case, the last statement of the script must be a RETURN statement (or a THROW statement), and all RETURN statements in the script must return a value with the given type.
The script evaluator is implemented by creating and compiling a temporary compilation unit defining one class with one method the body of which consists of the statements of the script.
To set up a IScriptEvaluator object, proceed as follows:
IScriptEvaluator-implementing class.IScriptEvaluator by calling any of the following methods:
org.codehaus.commons.compiler.Cookable#cook(Reader) methods to scan, parse, compile and
load the script into the JVM.
After the IScriptEvaluator object is created, the script can be executed as often with different parameter
values (see evaluate(Object[])). This execution is very fast, compared to the compilation.
Less common methods exist that allow for the specification of the name of the generated class, the class it
extends, the interfaces it implements, the name of the method that executes the script, the exceptions that this
method (i.e. the script) is allowed to throw, and the ClassLoader that is used to define the generated
class and to load classes referenced by the script.
If you want to compile many scripts at the same time, you have the option to cook an array of scripts in
one IScriptEvaluator by using the following methods:
setMethodNames(String[])
setParameters(String[][], Class[][])
setReturnTypes(Class[])
setStaticMethod(boolean[])
setThrownExceptions(Class[][])
cook(Reader)
evaluate(int, Object[])
Notice that these methods have array parameters in contrast to their one-script brethren.
| Modifier and Type | Field and Description |
|---|---|
| public static final String | DEFAULT_METHOD_NAME
The name of the generated method(s), if no custom method name is configured with |
| public static final Class | DEFAULT_RETURN_TYPE
The return type for any script for which no return type is explicitly configured. |
| Modifier and Type | Method and Description |
|---|---|
| public void | cook(Reader... readers)
Redeclares org. cook(Reader), but for multiple scripts.
|
| public void | cook(String[] fileNames, Reader[] readers)
Redeclares org. cook(String, Reader), but cooks a set of scripts into one class.
|
| public void | cook(String[] strings)
Redeclares org. cook(String), but for multiple scripts.
|
| public void | cook(String[] fileNames, String[] strings)
Redeclares org. cook(String, String), but for multiple scripts.
|
| public <T> T | createFastEvaluator(String
Contains the sequence of script tokens script, Class<T> interfaceToImplement, String[] parameterNames)
|
| public <T> T | Returns: An object that implements the given interfaceProduces the stream of script tokens reader, Class<T> Must declare exactly one method interfaceToImplement, String[] The names of the parameters of that method parameterNames)If the parameter and return types of the expression are known at compile time, then a "fast" script evaluator can be instantiated through this method. |
| public Object | |
| public Object | |
| public Object | |
| public Class | |
| public String[] | |
| public Class | Returns: The default return type that was previously configured withsetDefaultReturnType(Class), or
DEFAULT_RETURN_TYPE
|
| public Method | |
| public Method | |
| public Method[] | |
| public void | |
| public void | setCompileErrorHandler(ErrorHandler
null to restore the default behavior (throwing a CompileException)Installs an |
| public void | setDebuggingInformation(boolean debugSource, boolean debugLines, boolean debugVars)
Determines what kind of debugging information is included in the generates classes. |
| public void | |
| public void | setDefaultReturnType(Class<?> defaultReturnType)
When this |
| public void | |
| public void | |
| public void | setMethodName(String
null means reset to default nameDefines the name of the generated method. |
| public void | |
| public void | setOverrideMethod(boolean overrideMethod)
Defines whether the generated method overrides a methods declared in a supertype. |
| public void | setOverrideMethod(boolean[] overrideMethod)
Same as |
| public void | setParameters(String[] names, Class<?>[] types)
Defines the names and types of the parameters of the generated method. |
| public void | setParameters(String[][] names, Class<?>[][] types)
Same as |
| public void | setParentClassLoader(ClassLoader parentClassLoader)
The "parent class loader" is used to load referenced classes. |
| public void | |
| public void | setReturnTypes(Class<?>[]
The methods' return types; returnTypes)null elements mean "use the default return
type"Configures the return types of the generated methods. |
| public void | |
| public void | |
| public void | setThrownExceptions(Class<?>[] thrownExceptions)
Defines the exceptions that the generated method may throw. |
| public void | setThrownExceptions(Class<?>[][] thrownExceptions)
Same as |
| public void | setWarningHandler(WarningHandler
null to indicate that no warnings be issuedBy default, warnings are discarded, but an application my install a custom |
| DEFAULT_METHOD_NAME | back to summary |
|---|---|
| public static final String DEFAULT_METHOD_NAME The name of the generated method(s), if no custom method name is configured with
The | |
| DEFAULT_RETURN_TYPE | back to summary |
|---|---|
| public static final Class<?> DEFAULT_RETURN_TYPE The return type for any script for which no return type is explicitly configured. | |
| cook | back to summary |
|---|---|
| public void cook(Reader... readers) throws CompileException, IOException Redeclares org. Same as
| |
| cook | back to summary |
|---|---|
| public void cook(String[] fileNames, Reader[] readers) throws CompileException, IOException Redeclares org. Same as Iff the number of scanners is one, then that single script may contain leading IMPORT directives. s
| |
| cook | back to summary |
|---|---|
| public void cook(String[] strings) throws CompileException Redeclares org. Same as
| |
| cook | back to summary |
|---|---|
| public void cook(String[] fileNames, String[] strings) throws CompileException Redeclares org. Same as
| |
| createFastEvaluator | back to summary |
|---|---|
public <T> T createFastEvaluator(String script, Class<T> interfaceToImplement, String[] parameterNames) throws CompileException
| |
| createFastEvaluator | back to summary |
|---|---|
| public <T> T createFastEvaluator(Reader reader, Class<T> interfaceToImplement, String[] parameterNames) throws CompileException, IOException If the parameter and return types of the expression are known at compile time, then a "fast" script evaluator can be instantiated through this method.
Script evaluation is faster than through Example:
public interface Foo {
int bar(int a, int b);
}
...
IScriptEvaluator se =
All other configuration (implemented type, static method, return type, method name, parameter names and types,
thrown exceptions) are predetermined by the
Notice: The | |
| evaluate | back to summary |
|---|---|
| public Object evaluate() throws InvocationTargetException Shorthand for
| |
| evaluate | back to summary |
|---|---|
| public Object evaluate(Object[] arguments) throws InvocationTargetException Calls the script with concrete parameter values.
Each argument must have the same type as specified through the Arguments of primitive type must passed with their wrapper class objects.
The object returned has the class as specified through This method is thread-safe.
Notice: In version 3.1.8, the arguments parameter was changed from
| |
| evaluate | back to summary |
|---|---|
| public Object evaluate(int idx, Object[] arguments) throws InvocationTargetException Same as
| |
| getClazz | back to summary |
|---|---|
public Class
| |
| getDefaultImports | back to summary |
|---|---|
| public String[] getDefaultImports() | |
| getDefaultReturnType | back to summary |
|---|---|
public Class
| |
| getMethod | back to summary |
|---|---|
public Method getMethod()
| |
| getMethod | back to summary |
|---|---|
| public Method getMethod(int idx) Same as | |
| getResult | back to summary |
|---|---|
public Method[] getResult()
| |
| setClassName | back to summary |
|---|---|
| public void setClassName(String className) | |
| setCompileErrorHandler | back to summary |
|---|---|
| public void setCompileErrorHandler(ErrorHandler compileErrorHandler) Installs an
If the given
If the given
In other words: The
| |
| setDebuggingInformation | back to summary |
|---|---|
| public void setDebuggingInformation(boolean debugSource, boolean debugLines, boolean debugVars) Determines what kind of debugging information is included in the generates classes. The default is typically
" | |
| setDefaultImports | back to summary |
|---|---|
| public void setDefaultImports(String... defaultImports) | |
| setDefaultReturnType | back to summary |
|---|---|
| public void setDefaultReturnType(Class<?> defaultReturnType) When this
The initial default return type (if you want, the "default-default" return type) is
| |
| setExtendedClass | back to summary |
|---|---|
| public void setExtendedClass(Class<?> extendedClass) | |
| setImplementedInterfaces | back to summary |
|---|---|
| public void setImplementedInterfaces(Class<?>[] implementedInterfaces) | |
| setMethodName | back to summary |
|---|---|
| public void setMethodName(String methodName) Defines the name of the generated method. Initially, the method name is eval*.
| |
| setMethodNames | back to summary |
|---|---|
| public void setMethodNames(String[] methodNames) Same as Define the names of the generated methods. By default the methods have distinct and implementation-specific names.
If two scripts have the same name, then they must have different parameter types (see | |
| setOverrideMethod | back to summary |
|---|---|
| public void setOverrideMethod(boolean overrideMethod) Defines whether the generated method overrides a methods declared in a supertype. | |
| setOverrideMethod | back to summary |
|---|---|
| public void setOverrideMethod(boolean[] overrideMethod) Same as | |
| setParameters | back to summary |
|---|---|
| public void setParameters(String[] names, Class<?>[] types) Defines the names and types of the parameters of the generated method.
names
The parameters can be of primitive type, e.g. The default is to have zero parameters. | |
| setParameters | back to summary |
|---|---|
| public void setParameters(String[][] names, Class<?>[][] types) Same as | |
| setParentClassLoader | back to summary | ||||||
|---|---|---|---|---|---|---|---|
| public void setParentClassLoader(ClassLoader parentClassLoader) The "parent class loader" is used to load referenced classes. Useful values are:
The parent class loader defaults to the current thread's context class loader. | |||||||
| setReturnType | back to summary |
|---|---|
| public void setReturnType(Class<?> returnType) Defines the return type of the generated method. Value
| |
| setReturnTypes | back to summary |
|---|---|
| public void setReturnTypes(Class<?>[] returnTypes) Configures the return types of the generated methods. If an element of the array is
| |
| setStaticMethod | back to summary |
|---|---|
| public void setStaticMethod(boolean staticMethod) Defines whether the generated method should be STATIC or not. Defaults to | |
| setStaticMethod | back to summary |
|---|---|
| public void setStaticMethod(boolean[] staticMethod) Same as | |
| setThrownExceptions | back to summary |
|---|---|
| public void setThrownExceptions(Class<?>[] thrownExceptions) Defines the exceptions that the generated method may throw. | |
| setThrownExceptions | back to summary |
|---|---|
| public void setThrownExceptions(Class<?>[][] thrownExceptions) Same as | |
| setWarningHandler | back to summary |
|---|---|
| public void setWarningHandler(WarningHandler warningHandler) By default, warnings are discarded, but an application my install a custom
| |