public class ScriptEvaluator extends ClassBodyEvaluator implements IScriptEvaluator
ScriptEvaluator object, proceed as described for IScriptEvaluator. Alternatively, a
number of "convenience constructors" exist that execute the described steps instantly.
Alternatively, a number of "convenience constructors" exist that execute the steps described above instantly. Their use is discouraged.
Notice that this implementation of IClassBodyEvaluator is prone to "Java injection", i.e. an
application could get more than one class body compiled by passing a bogus input document.
Also notice that the parsing of leading IMPORT declarations is heuristic and has certain limitations; see
ClassBodyEvaluator.parseImportDeclarations(Reader).
| Modifier and Type | Field and Description |
|---|---|
protected boolean[] |
optionalOverrideMethod
Whether methods override a method declared by a supertype;
null means "none". |
protected boolean[] |
optionalStaticMethod
Whether methods are static;
null means "all". |
DEFAULT_CLASS_NAMEBOOT_CLASS_LOADER, SYSTEM_PROPERTY_SOURCE_DEBUGGING_DIR, SYSTEM_PROPERTY_SOURCE_DEBUGGING_ENABLE| Constructor and Description |
|---|
ScriptEvaluator() |
ScriptEvaluator(String script)
Equivalent to
ScriptEvaluator se = new ScriptEvaluator();
se.cook(script);
|
ScriptEvaluator(String script,
Class<?> returnType)
Equivalent to
ScriptEvaluator se = new ScriptEvaluator();
se.setReturnType(returnType);
se.cook(script);
|
ScriptEvaluator(String script,
Class<?> returnType,
String[] parameterNames,
Class<?>[] parameterTypes)
Equivalent to
ScriptEvaluator se = new ScriptEvaluator();
se.setReturnType(returnType);
se.setParameters(parameterNames, parameterTypes);
se.cook(script);
|
ScriptEvaluator(String script,
Class<?> returnType,
String[] parameterNames,
Class<?>[] parameterTypes,
Class<?>[] thrownExceptions)
Equivalent to
ScriptEvaluator se = new ScriptEvaluator();
se.setReturnType(returnType);
se.setParameters(parameterNames, parameterTypes);
se.setThrownExceptions(thrownExceptions);
se.cook(script);
|
ScriptEvaluator(String optionalFileName,
InputStream is,
Class<?> returnType,
String[] parameterNames,
Class<?>[] parameterTypes,
Class<?>[] thrownExceptions,
ClassLoader optionalParentClassLoader)
Equivalent to
ScriptEvaluator se = new ScriptEvaluator();
se.setReturnType(returnType);
se.setParameters(parameterNames, parameterTypes);
se.setThrownExceptions(thrownExceptions);
se.setParentClassLoader(optionalParentClassLoader);
se.cook(optionalFileName, is);
|
ScriptEvaluator(String optionalFileName,
Reader reader,
Class<?> returnType,
String[] parameterNames,
Class<?>[] parameterTypes,
Class<?>[] thrownExceptions,
ClassLoader optionalParentClassLoader)
Equivalent to
ScriptEvaluator se = new ScriptEvaluator();
se.setReturnType(returnType);
se.setParameters(parameterNames, parameterTypes);
se.setThrownExceptions(thrownExceptions);
se.setParentClassLoader(optionalParentClassLoader);
se.cook(reader);
|
| Modifier and Type | Method and Description |
|---|---|
protected void |
assertNotCooked()
Throws an
IllegalStateException if this Cookable is already cooked. |
void |
cook(Reader[] readers)
Same as
ICookable.cook(Reader), but for multiple scripts. |
void |
cook(String[] strings)
Same as
ICookable.cook(String), but for multiple scripts. |
void |
cook(String[] optionalFileNames,
Reader[] readers)
Same as
ICookable.cook(String, Reader), but cooks a set of scripts into one class. |
protected void |
cook(String[] optionalFileNames,
Reader[] readers,
String[] imports) |
void |
cook(String[] optionalFileNames,
String[] strings)
Same as
ICookable.cook(String, String), but for multiple scripts. |
void |
cook(String optionalFileName,
Reader r)
Reads, scans, parses and compiles Java tokens from the given
Reader. |
<T> Object |
createFastEvaluator(Reader r,
Class<T> interfaceToImplement,
String[] 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.
|
<T> Object |
createFastEvaluator(String script,
Class<T> interfaceToImplement,
String[] parameterNames) |
Object |
createInstance(Reader reader)
Don't use.
|
Object |
evaluate(int idx,
Object[] arguments)
Same as
IScriptEvaluator.evaluate(Object[]), but for multiple scripts. |
Object |
evaluate(Object[] arguments)
Calls the script with concrete parameter values.
|
protected Class<?> |
getDefaultReturnType()
The default return type of a script is
void. |
Method |
getMethod()
Returns the loaded
Method. |
Method |
getMethod(int idx)
Same as
IScriptEvaluator.getMethod(), but for multiple scripts. |
protected Method[] |
getMethods() |
protected Class<?> |
getReturnType(int i) |
void |
setMethodName(String methodName)
Defines the name of the generated method.
|
void |
setMethodNames(String[] methodNames)
Same as
IScriptEvaluator.setMethodName(String), but for multiple scripts. |
void |
setOverrideMethod(boolean overrideMethod)
Defines whether the generated method overrides a methods declared in a supertype.
|
void |
setOverrideMethod(boolean[] overrideMethod)
Same as
IScriptEvaluator.setOverrideMethod(boolean), but for multiple scripts. |
void |
setParameters(String[][] names,
Class<?>[][] types)
Same as
IScriptEvaluator.setParameters(String[], Class[]), but for multiple scripts. |
void |
setParameters(String[] names,
Class<?>[] types)
Defines the names and types of the parameters of the generated method.
|
void |
setReturnType(Class<?> returnType)
Defines the return type of the generated method.
|
void |
setReturnTypes(Class<?>[] returnTypes)
Configures the return types of the generated methods.
|
void |
setStaticMethod(boolean staticMethod)
Defines whether the generated method should be STATIC or not.
|
void |
setStaticMethod(boolean[] staticMethod)
Same as
IScriptEvaluator.setStaticMethod(boolean), but for multiple scripts. |
void |
setThrownExceptions(Class<?>[] thrownExceptions)
Defines sthe exceptions that the generated method may throw.
|
void |
setThrownExceptions(Class<?>[][] thrownExceptions)
Same as
IScriptEvaluator.setThrownExceptions(Class[]), but for multiple scripts. |
cook, getClazz, parseImportDeclarations, setClassName, setDefaultImports, setExtendedClass, setExtendedType, setImplementedInterfaces, setImplementedTypesgetClassLoader, setCompileErrorHandler, setDebuggingInformation, setParentClassLoader, setParentClassLoader, setWarningHandlercook, cook, cook, cook, cook, cook, cook, cookFile, cookFile, cookFile, cookFile, readStringclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitgetClazz, setClassName, setDefaultImports, setExtendedClass, setExtendedType, setImplementedInterfaces, setImplementedTypescook, cook, cook, cook, cook, cook, cook, cookFile, cookFile, cookFile, cookFile, setCompileErrorHandler, setDebuggingInformation, setParentClassLoader, setWarningHandler@Nullable protected boolean[] optionalOverrideMethod
null means "none".@Nullable protected boolean[] optionalStaticMethod
null means "all".public ScriptEvaluator(String script) throws CompileException
ScriptEvaluator se = new ScriptEvaluator();
se.cook(script);
CompileExceptionScriptEvaluator(),
Cookable.cook(String)public ScriptEvaluator(String script, Class<?> returnType) throws CompileException
ScriptEvaluator se = new ScriptEvaluator();
se.setReturnType(returnType);
se.cook(script);
CompileExceptionScriptEvaluator(),
setReturnType(Class),
Cookable.cook(String)public ScriptEvaluator(String script, Class<?> returnType, String[] parameterNames, Class<?>[] parameterTypes) throws CompileException
ScriptEvaluator se = new ScriptEvaluator();
se.setReturnType(returnType);
se.setParameters(parameterNames, parameterTypes);
se.cook(script);
public ScriptEvaluator(String script, Class<?> returnType, String[] parameterNames, Class<?>[] parameterTypes, Class<?>[] thrownExceptions) throws CompileException
ScriptEvaluator se = new ScriptEvaluator();
se.setReturnType(returnType);
se.setParameters(parameterNames, parameterTypes);
se.setThrownExceptions(thrownExceptions);
se.cook(script);
public ScriptEvaluator(@Nullable String optionalFileName, InputStream is, Class<?> returnType, String[] parameterNames, Class<?>[] parameterTypes, Class<?>[] thrownExceptions, @Nullable ClassLoader optionalParentClassLoader) throws CompileException, IOException
ScriptEvaluator se = new ScriptEvaluator();
se.setReturnType(returnType);
se.setParameters(parameterNames, parameterTypes);
se.setThrownExceptions(thrownExceptions);
se.setParentClassLoader(optionalParentClassLoader);
se.cook(optionalFileName, is);
optionalParentClassLoader - null means use current thread's context class loaderCompileExceptionIOExceptionScriptEvaluator(),
setReturnType(Class),
setParameters(String[], Class[]),
setThrownExceptions(Class[]),
SimpleCompiler.setParentClassLoader(ClassLoader),
Cookable.cook(String, InputStream)public ScriptEvaluator(@Nullable String optionalFileName, Reader reader, Class<?> returnType, String[] parameterNames, Class<?>[] parameterTypes, Class<?>[] thrownExceptions, @Nullable ClassLoader optionalParentClassLoader) throws CompileException, IOException
ScriptEvaluator se = new ScriptEvaluator();
se.setReturnType(returnType);
se.setParameters(parameterNames, parameterTypes);
se.setThrownExceptions(thrownExceptions);
se.setParentClassLoader(optionalParentClassLoader);
se.cook(reader);
optionalParentClassLoader - null means use current thread's context class loaderCompileExceptionIOExceptionScriptEvaluator(),
setReturnType(Class),
setParameters(String[], Class[]),
setThrownExceptions(Class[]),
SimpleCompiler.setParentClassLoader(ClassLoader),
ICookable.cook(String, Reader)public ScriptEvaluator()
public void setOverrideMethod(boolean overrideMethod)
IScriptEvaluatorsetOverrideMethod in interface IScriptEvaluatorpublic void setStaticMethod(boolean staticMethod)
IScriptEvaluatortrue.setStaticMethod in interface IScriptEvaluatorpublic void setReturnType(Class<?> returnType)
IScriptEvaluatornull is equivalent with void.class.setReturnType in interface IScriptEvaluatorpublic void setMethodName(String methodName)
IScriptEvaluatorsetMethodName in interface IScriptEvaluatorpublic void setParameters(String[] names, Class<?>[] types)
IScriptEvaluatornames and types must have the same number of elements.
The parameters can be of primitive type, e.g. double.class.
The default is to have zero parameters.
setParameters in interface IScriptEvaluatorpublic void setThrownExceptions(Class<?>[] thrownExceptions)
IScriptEvaluatorsetThrownExceptions in interface IScriptEvaluatorpublic void cook(@Nullable String optionalFileName, Reader r) throws CompileException, IOException
ICookableReader.cook in interface ICookablecook in class ClassBodyEvaluatoroptionalFileName - Used when reporting errors and warningsCompileExceptionIOException@Nullable public Object evaluate(@Nullable Object[] arguments) throws InvocationTargetException
IScriptEvaluator
Each argument must have the same type as specified through the parameterTypes parameter of IScriptEvaluator.setParameters(String[], Class[]).
Arguments of primitive type must passed with their wrapper class objects.
The object returned has the class as specified through IScriptEvaluator.setReturnType(Class).
This method is thread-safe.
evaluate in interface IScriptEvaluatorarguments - The actual parameter valuesInvocationTargetExceptionpublic Method getMethod()
IScriptEvaluatorMethod.
This method must only be called after exactly one of the ICookable.cook(String, Reader) methods was called.
getMethod in interface IScriptEvaluatorpublic void setOverrideMethod(boolean[] overrideMethod)
IScriptEvaluatorIScriptEvaluator.setOverrideMethod(boolean), but for multiple scripts.setOverrideMethod in interface IScriptEvaluatorpublic void setStaticMethod(boolean[] staticMethod)
IScriptEvaluatorIScriptEvaluator.setStaticMethod(boolean), but for multiple scripts.setStaticMethod in interface IScriptEvaluatorpublic void setReturnTypes(Class<?>[] returnTypes)
IScriptEvaluatornull.
Unless this method is invoked, the return type of all script is void.class.
setReturnTypes in interface IScriptEvaluatorpublic void setMethodNames(String[] methodNames)
IScriptEvaluatorIScriptEvaluator.setMethodName(String), but for multiple scripts.
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 IScriptEvaluator.setParameters(String[][], Class[][])).
setMethodNames in interface IScriptEvaluatorpublic void setParameters(String[][] names, Class<?>[][] types)
IScriptEvaluatorIScriptEvaluator.setParameters(String[], Class[]), but for multiple scripts.setParameters in interface IScriptEvaluatorpublic void setThrownExceptions(Class<?>[][] thrownExceptions)
IScriptEvaluatorIScriptEvaluator.setThrownExceptions(Class[]), but for multiple scripts.setThrownExceptions in interface IScriptEvaluatorpublic final void cook(Reader[] readers) throws CompileException, IOException
IScriptEvaluatorICookable.cook(Reader), but for multiple scripts.cook in interface IScriptEvaluatorCompileExceptionIOExceptionpublic void cook(@Nullable String[] optionalFileNames, Reader[] readers) throws CompileException, IOException
IScriptEvaluatorICookable.cook(String, Reader), but cooks a set of scripts into one class. Notice that
if any of the scripts causes trouble, the entire compilation will fail. If you
need to report which of the scripts causes the exception, you may want to use the
optionalFileNames parameter to distinguish between the individual token sources.
Iff the number of scanners is one, then that single script may contain leading IMPORT directives.
scook in interface IScriptEvaluatorCompileExceptionIOExceptionpublic final void cook(String[] strings) throws CompileException
IScriptEvaluatorICookable.cook(String), but for multiple scripts.cook in interface IScriptEvaluatorCompileExceptionpublic void cook(@Nullable String[] optionalFileNames, String[] strings) throws CompileException
IScriptEvaluatorICookable.cook(String, String), but for multiple scripts.cook in interface IScriptEvaluatorCompileExceptionprotected final void cook(@Nullable String[] optionalFileNames, Reader[] readers, String[] imports) throws CompileException, IOException
readers - The scripts to cookCompileExceptionIOExceptionprotected Class<?> getDefaultReturnType()
void.protected final Class<?> getReturnType(int i)
public <T> Object createFastEvaluator(String script, Class<T> interfaceToImplement, String[] parameterNames) throws CompileException
createFastEvaluator in interface IScriptEvaluatorscript - Contains the sequence of script tokensCompileExceptioncreateFastEvaluator(String, Class, String[])public final Object createInstance(Reader reader)
createInstance in interface IClassBodyEvaluatorcreateInstance in class ClassBodyEvaluatorreader - Source of class body tokensoptionalExtendedType and implements the given
implementedTypespublic <T> Object createFastEvaluator(Reader r, Class<T> interfaceToImplement, String[] parameterNames) throws CompileException, IOException
IScriptEvaluator
Script evaluation is faster than through IScriptEvaluator.evaluate(Object[]), because it is not done through
reflection but through direct method invocation.
Example:
public interface Foo {
int bar(int a, int b);
}
...
IScriptEvaluator se = CompilerFactoryFactory.getDefaultCompilerFactory().newScriptEvaluator();
// Optionally configure the SE her:
se.setClassName("Bar");
se.setDefaultImports(new String[] { "java.util.*" });
se.setExtendedClass(SomeOtherClass.class);
se.setParentClassLoader(someClassLoader);
Foo f = (Foo) se.createFastScriptEvaluator(
"return a - b;",
Foo.class,
new String[] { "a", "b" }
);
System.out.println("1 - 2 = " + f.bar(1, 2));
All other configuration (implemented type, static method, return type, method name, parameter names and types,
thrown exceptions) are predetermined by the interfaceToImplement.
Notice: The interfaceToImplement must either be declared public, or with package scope in the
same package as the generated class (see IClassBodyEvaluator.setClassName(String)).
createFastEvaluator in interface IScriptEvaluatorr - Produces the stream of script tokensinterfaceToImplement - Must declare exactly one methodparameterNames - The names of the parameters of that methodCompileExceptionIOException@Nullable public Object evaluate(int idx, @Nullable Object[] arguments) throws InvocationTargetException
IScriptEvaluatorIScriptEvaluator.evaluate(Object[]), but for multiple scripts.evaluate in interface IScriptEvaluatorInvocationTargetExceptionpublic Method getMethod(int idx)
IScriptEvaluatorIScriptEvaluator.getMethod(), but for multiple scripts.getMethod in interface IScriptEvaluatorprotected Method[] getMethods()
IllegalStateException - This ScriptEvaluator has not yet been cook()edprotected void assertNotCooked()
SimpleCompilerIllegalStateException if this Cookable is already cooked.assertNotCooked in class SimpleCompilerIllegalStateException - This ScriptEvaluator has already been cook()ed