public class ScriptEvaluator extends ClassBodyEvaluator implements IScriptEvaluator
IScriptEvaluator that utilizes the JANINO Java compiler.
This implementation implements the concept of "Local methods", i.e. statements may be freely intermixed with
method declarations. These methods are typically called by the "main code" of the script evaluator. One limitation
exists: When cooking multiple scripts in one ScriptEvaluator, then local method signatures
(names and parameter types) must not collide between scripts.
A plethora of "convenience constructors" exist that execute the setup steps instantly. Their use is discouraged,
in favor of using the default constructor, plus calling a number of setters, and then one of the cook()
methods.
| Modifier and Type | Field and Description |
|---|---|
static String |
DEFAULT_METHOD_NAME
The name of the generated method(s), if no custom method name is configured with
setMethodNames(String[]). |
DEFAULT_CLASS_NAMEBOOT_CLASS_LOADER, SYSTEM_PROPERTY_SOURCE_DEBUGGING_DIR, SYSTEM_PROPERTY_SOURCE_DEBUGGING_ENABLE| Constructor and Description |
|---|
ScriptEvaluator()
Constructs a script evaluator with all the default settings (return type
void |
ScriptEvaluator(int count)
Constructs a script evaluator with the given number of scripts.
|
ScriptEvaluator(Scanner scanner,
Class<?> optionalExtendedType,
Class<?>[] implementedTypes,
Class<?> returnType,
String[] parameterNames,
Class<?>[] parameterTypes,
Class<?>[] thrownExceptions,
ClassLoader optionalParentClassLoader)
Equivalent to
ScriptEvaluator se = new ScriptEvaluator();
se.setExtendedType(optionalExtendedType);
se.setImplementedTypes(implementedTypes);
se.setReturnType(returnType);
se.setParameters(parameterNames, parameterTypes);
se.setThrownExceptions(thrownExceptions);
se.setParentClassLoader(optionalParentClassLoader);
se.cook(scanner);
|
ScriptEvaluator(Scanner scanner,
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(scanner);
|
ScriptEvaluator(Scanner scanner,
String className,
Class<?> optionalExtendedType,
Class<?>[] implementedTypes,
boolean staticMethod,
Class<?> returnType,
String methodName,
String[] parameterNames,
Class<?>[] parameterTypes,
Class<?>[] thrownExceptions,
ClassLoader optionalParentClassLoader)
Equivalent to
ScriptEvaluator se = new ScriptEvaluator();
se.setClassName(className);
se.setExtendedType(optionalExtendedType);
se.setImplementedTypes(implementedTypes);
se.setStaticMethod(staticMethod);
se.setReturnType(returnType);
se.setMethodName(methodName);
se.setParameters(parameterNames, parameterTypes);
se.setThrownExceptions(thrownExceptions);
se.setParentClassLoader(optionalParentClassLoader);
se.cook(scanner);
|
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 |
|---|---|
void |
cook(Parser[] parsers) |
void |
cook(Reader[] readers) |
void |
cook(Scanner scanner)
Scans, parses and compiles a given compilation unit from the given scanner.
|
void |
cook(Scanner[] scanners)
Like
cook(Scanner), but cooks a set of scripts into one class. |
void |
cook(String[] strings) |
void |
cook(String[] optionalFileNames,
Reader[] readers)
On a 2 GHz Intel Pentium Core Duo under Windows XP with an IBM 1.4.2 JDK, compiling 10000 expressions "a + b"
(integer) takes about 4 seconds and 56 MB of main memory.
|
void |
cook(String[] optionalFileNames,
String[] strings) |
protected void |
cook2(Java.CompilationUnit compilationUnit)
Compiles the given compilationUnit, defines it into a
ClassLoader, loads the generated class,
gets the script methods from that class, and makes them available through getMethod(int). |
<T> Object |
createFastEvaluator(Reader reader,
Class<T> interfaceToImplement,
String[] parameterNames) |
Object |
createFastEvaluator(Scanner scanner,
Class<?> interfaceToImplement,
String[] parameterNames)
Notice: This method is not declared in
IScriptEvaluator, and is hence only available in this
implementation of org.codehaus.commons.compiler. |
<T> Object |
createFastEvaluator(String script,
Class<T> interfaceToImplement,
String[] parameterNames) |
static Object |
createFastScriptEvaluator(Scanner scanner,
Class<?> interfaceToImplement,
String[] parameterNames,
ClassLoader optionalParentClassLoader)
Deprecated.
|
static Object |
createFastScriptEvaluator(Scanner scanner,
String[] optionalDefaultImports,
String className,
Class<?> optionalExtendedClass,
Class<?> interfaceToImplement,
String[] parameterNames,
ClassLoader optionalParentClassLoader)
Deprecated.
Use
createFastEvaluator(Scanner,Class,String[]) instead: |
static Object |
createFastScriptEvaluator(Scanner scanner,
String className,
Class<?> optionalExtendedType,
Class<?> interfaceToImplement,
String[] parameterNames,
ClassLoader optionalParentClassLoader)
Deprecated.
|
static Object |
createFastScriptEvaluator(String script,
Class<?> interfaceToImplement,
String[] parameterNames)
Deprecated.
|
Object |
createInstance(Reader reader)
Don't use.
|
Object |
evaluate(int idx,
Object[] arguments) |
Object |
evaluate(Object[] arguments) |
protected Class<?> |
getDefaultReturnType() |
Method |
getMethod() |
Method |
getMethod(int idx) |
protected Class<?> |
getReturnType(int index) |
static String[] |
guessParameterNames(Scanner scanner)
Guesses the names of the parameters used in the given expression.
|
protected void |
makeStatements(int idx,
Parser parser,
List<Java.BlockStatement> resultStatements,
List<Java.MethodDeclarator> resultMethods)
Parses statements from the parser until end-of-input.
|
void |
setMethodName(String methodName) |
void |
setMethodNames(String[] methodNames) |
void |
setOverrideMethod(boolean overrideMethod) |
void |
setOverrideMethod(boolean[] overrideMethod) |
void |
setParameters(String[][] parameterNames,
Class<?>[][] parameterTypes) |
void |
setParameters(String[] parameterNames,
Class<?>[] parameterTypes) |
void |
setReturnType(Class<?> returnType)
Defines the return types of the generated methods.
|
void |
setReturnTypes(Class<?>[] returnTypes)
Defines the return types of the generated methods.
|
void |
setScriptCount(int count) |
void |
setStaticMethod(boolean staticMethod) |
void |
setStaticMethod(boolean[] staticMethod) |
void |
setThrownExceptions(Class<?>[] thrownExceptions) |
void |
setThrownExceptions(Class<?>[][] thrownExceptions) |
addPackageMemberClassDeclaration, compileToClass, createFastClassBodyEvaluator, createFastClassBodyEvaluator, getClazz, makeCompilationUnit, setClassName, setDefaultImports, setExtendedClass, setExtendedType, setImplementedInterfaces, setImplementedTypesclassesToTypes, classToType, compileToClassLoader, cook, cook, cook, cook, equals, getClassLoader, hashCode, main, optionalClassToType, options, options, setCompileErrorHandler, setDebuggingInformation, setNoPermissions, setParentClassLoader, setPermissions, setWarningHandlercook, cook, cook, cook, cook, cook, cook, cookFile, cookFile, cookFile, cookFile, readStringclone, finalize, getClass, notify, notifyAll, toString, wait, wait, waitgetClazz, setClassName, setDefaultImports, setExtendedClass, setExtendedType, setImplementedInterfaces, setImplementedTypesgetClassLoader, setNoPermissions, setPermissionscook, cook, cook, cook, cook, cook, cook, cook, cookFile, cookFile, cookFile, cookFile, setCompileErrorHandler, setDebuggingInformation, setParentClassLoader, setWarningHandlerpublic static final String DEFAULT_METHOD_NAME
setMethodNames(String[]).
The '*' in this string is replaced with the method index, starting at 0.
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);
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);
public ScriptEvaluator(Scanner scanner, 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(scanner);
public ScriptEvaluator(Scanner scanner, @Nullable Class<?> optionalExtendedType, Class<?>[] implementedTypes, Class<?> returnType, String[] parameterNames, Class<?>[] parameterTypes, Class<?>[] thrownExceptions, @Nullable ClassLoader optionalParentClassLoader) throws CompileException, IOException
ScriptEvaluator se = new ScriptEvaluator();
se.setExtendedType(optionalExtendedType);
se.setImplementedTypes(implementedTypes);
se.setReturnType(returnType);
se.setParameters(parameterNames, parameterTypes);
se.setThrownExceptions(thrownExceptions);
se.setParentClassLoader(optionalParentClassLoader);
se.cook(scanner);
CompileExceptionIOExceptionScriptEvaluator(),
ClassBodyEvaluator.setExtendedClass(Class),
ClassBodyEvaluator.setImplementedInterfaces(Class[]),
setReturnType(Class),
setParameters(String[], Class[]),
setThrownExceptions(Class[]),
SimpleCompiler.setParentClassLoader(ClassLoader),
Cookable.cook(Reader)public ScriptEvaluator(Scanner scanner, String className, @Nullable Class<?> optionalExtendedType, Class<?>[] implementedTypes, boolean staticMethod, Class<?> returnType, String methodName, String[] parameterNames, Class<?>[] parameterTypes, Class<?>[] thrownExceptions, @Nullable ClassLoader optionalParentClassLoader) throws CompileException, IOException
ScriptEvaluator se = new ScriptEvaluator();
se.setClassName(className);
se.setExtendedType(optionalExtendedType);
se.setImplementedTypes(implementedTypes);
se.setStaticMethod(staticMethod);
se.setReturnType(returnType);
se.setMethodName(methodName);
se.setParameters(parameterNames, parameterTypes);
se.setThrownExceptions(thrownExceptions);
se.setParentClassLoader(optionalParentClassLoader);
se.cook(scanner);
CompileExceptionIOExceptionScriptEvaluator(),
ClassBodyEvaluator.setClassName(String),
ClassBodyEvaluator.setExtendedClass(Class),
ClassBodyEvaluator.setImplementedInterfaces(Class[]),
setStaticMethod(boolean),
setReturnType(Class),
setMethodName(String),
setParameters(String[], Class[]),
setThrownExceptions(Class[]),
SimpleCompiler.setParentClassLoader(ClassLoader),
Cookable.cook(Reader)public ScriptEvaluator()
voidpublic ScriptEvaluator(int count)
The argument of all following invocations of
setMethodNames(String[]),
setOverrideMethod(boolean[]),
setParameters(String[][], Class[][]),
setReturnTypes(Class[]),
setStaticMethod(boolean[]),
setThrownExceptions(Class[][]),
SimpleCompiler.cook(org.codehaus.janino.util.ClassFile[]),
cook(Parser[]),
cook(Reader[]),
cook(Scanner[]),
cook(String[]),
cook(String[], Reader[]) and
cook(String[], String[])
must be arrays with exactly that length.
If a different constructor is used, then the first invocation of one of the above method implicitly sets the script count.
public void setScriptCount(int count)
IllegalArgumentException - count is different from previous invocations of
this methodpublic void setOverrideMethod(boolean overrideMethod)
setOverrideMethod in interface IScriptEvaluatorpublic void setStaticMethod(boolean staticMethod)
setStaticMethod in interface IScriptEvaluatorpublic void setReturnType(Class<?> returnType)
setReturnType in interface IScriptEvaluatorreturnType - The method's return type; null means the "default return type", which is the type
returned by getDefaultReturnType() (void.class for ScriptEvaluator
and Object.class for ExpressionEvaluator)getDefaultReturnType(),
ExpressionEvaluator.getDefaultReturnType()public void setMethodName(String methodName)
setMethodName in interface IScriptEvaluatorpublic void setParameters(String[] parameterNames, Class<?>[] parameterTypes)
setParameters in interface IScriptEvaluatorpublic void setThrownExceptions(Class<?>[] thrownExceptions)
setThrownExceptions in interface IScriptEvaluatorpublic void setOverrideMethod(boolean[] overrideMethod)
setOverrideMethod in interface IScriptEvaluatorpublic void setStaticMethod(boolean[] staticMethod)
setStaticMethod in interface IScriptEvaluatorpublic void setReturnTypes(Class<?>[] returnTypes)
setReturnTypes in interface IScriptEvaluatorreturnTypes - The methods' return types; null elements mean the "default return type", which is the
type returned by getDefaultReturnType() (void.class for ScriptEvaluator and Object.class for ExpressionEvaluator)getDefaultReturnType(),
ExpressionEvaluator.getDefaultReturnType()public void setMethodNames(String[] methodNames)
setMethodNames in interface IScriptEvaluatorpublic void setParameters(String[][] parameterNames, Class<?>[][] parameterTypes)
setParameters in interface IScriptEvaluatorpublic void setThrownExceptions(Class<?>[][] thrownExceptions)
setThrownExceptions in interface IScriptEvaluatorpublic final void cook(String[] strings) throws CompileException
cook in interface IScriptEvaluatorCompileExceptionpublic final void cook(@Nullable String[] optionalFileNames, String[] strings) throws CompileException
cook in interface IScriptEvaluatorCompileExceptionpublic final void cook(Reader[] readers) throws CompileException, IOException
cook in interface IScriptEvaluatorCompileExceptionIOExceptionpublic final void cook(@Nullable String[] optionalFileNames, Reader[] readers) throws CompileException, IOException
The number and the complexity of the scripts is restricted by the Limitations of the Java Virtual Machine, where the most limiting factor is the 64K entries limit of the constant pool. Since every method with a distinct name requires one entry there, you can define at best 32K (very simple) scripts.
cook in interface IScriptEvaluatorCompileExceptionIOExceptionpublic final void cook(Scanner scanner) throws CompileException, IOException
SimpleCompilerSimpleCompiler.getClassLoader() returns a ClassLoader that allows for access to the compiled classes.cook in class ClassBodyEvaluatorCompileExceptionIOExceptionpublic final void cook(Scanner[] scanners) throws CompileException, IOException
cook(Scanner), 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 optionalFileName argument of Scanner.Scanner(String, Reader) to distinguish between the individual token sources.
On a 2 GHz Intel Pentium Core Duo under Windows XP with an IBM 1.4.2 JDK, compiling 10000 expressions "a + b" (integer) takes about 4 seconds and 56 MB of main memory. The generated class file is 639203 bytes large.
The number and the complexity of the scripts is restricted by the Limitations of the Java Virtual Machine, where the most limiting factor is the 64K entries limit of the constant pool. Since every method with a distinct name requires one entry there, you can define at best 32K (very simple) scripts.
If and only if the number of scanners is one, then that single script may contain leading IMPORT directives.
IllegalStateException - Any of the preceding set...() had an array size different from that of
scannersCompileExceptionIOExceptionpublic final void cook(Parser[] parsers) throws CompileException, IOException
CompileExceptionIOExceptioncook(Scanner[])protected void cook2(Java.CompilationUnit compilationUnit) throws CompileException
ClassLoader, loads the generated class,
gets the script methods from that class, and makes them available through getMethod(int).CompileException@Nullable public Object evaluate(@Nullable Object[] arguments) throws InvocationTargetException
evaluate in interface IScriptEvaluatorInvocationTargetException@Nullable public Object evaluate(int idx, @Nullable Object[] arguments) throws InvocationTargetException
evaluate in interface IScriptEvaluatorInvocationTargetExceptionpublic Method getMethod()
getMethod in interface IScriptEvaluatorpublic Method getMethod(int idx)
getMethod in interface IScriptEvaluatorprotected Class<?> getDefaultReturnType()
void.classsetReturnTypes(Class[])protected final Class<?> getReturnType(int index)
protected void makeStatements(int idx,
Parser parser,
List<Java.BlockStatement> resultStatements,
List<Java.MethodDeclarator> resultMethods)
throws CompileException,
IOException
resultStatements - Is filled with the generated statementsresultMethods - Is filled with any local methods that the script declaresCompileExceptionIOException@Deprecated public static Object createFastScriptEvaluator(String script, Class<?> interfaceToImplement, String[] parameterNames) throws CompileException
createFastScriptEvaluator(Scanner, String[], String, Class, Class, String[],
ClassLoader) insteadCompileException@Deprecated public static Object createFastScriptEvaluator(Scanner scanner, Class<?> interfaceToImplement, String[] parameterNames, @Nullable ClassLoader optionalParentClassLoader) throws CompileException, IOException
createFastScriptEvaluator(Scanner, String[], String, Class, Class, String[],
ClassLoader) insteadCompileExceptionIOException@Deprecated public static Object createFastScriptEvaluator(Scanner scanner, String className, @Nullable Class<?> optionalExtendedType, Class<?> interfaceToImplement, String[] parameterNames, @Nullable ClassLoader optionalParentClassLoader) throws CompileException, IOException
createFastScriptEvaluator(Scanner, String[], String, Class, Class, String[],
ClassLoader) insteadCompileExceptionIOException@Deprecated public static Object createFastScriptEvaluator(Scanner scanner, @Nullable String[] optionalDefaultImports, String className, @Nullable Class<?> optionalExtendedClass, Class<?> interfaceToImplement, String[] parameterNames, @Nullable ClassLoader optionalParentClassLoader) throws CompileException, IOException
createFastEvaluator(Scanner,Class,String[]) instead:
ScriptEvaluator se = new ScriptEvaluator();
se.setDefaultImports.(optionalDefaultImports);
se.setClassName.(className);
se.setExtendedClass.(optionalExtendedClass);
se.setParentClassLoader(optionalParentClassLoader);
return se.createFastEvaluator(scanner,
interfaceToImplement, parameterNames);
CompileExceptionIOExceptionpublic final Object createInstance(Reader reader)
createInstance in interface IClassBodyEvaluatorcreateInstance in class ClassBodyEvaluatorpublic <T> Object createFastEvaluator(Reader reader, Class<T> interfaceToImplement, String[] parameterNames) throws CompileException, IOException
createFastEvaluator in interface IScriptEvaluatorCompileExceptionIOExceptionpublic <T> Object createFastEvaluator(String script, Class<T> interfaceToImplement, String[] parameterNames) throws CompileException
createFastEvaluator in interface IScriptEvaluatorCompileExceptionpublic Object createFastEvaluator(Scanner scanner, Class<?> interfaceToImplement, String[] parameterNames) throws CompileException, IOException
IScriptEvaluator, and is hence only available in this
implementation of org.codehaus.commons.compiler. To be independent from this particular
implementation, try to switch to createFastEvaluator(Reader, Class, String[]).scanner - Source of tokens to readCompileExceptionIOExceptioncreateFastEvaluator(Reader, Class, String[])public static String[] guessParameterNames(Scanner scanner) throws CompileException, IOException
CompileExceptionIOExceptionScanner.Scanner(String, Reader)Copyright © 2019. All rights reserved.