public class ScriptEvaluator extends MultiCookable 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.
DEFAULT_METHOD_NAME, DEFAULT_RETURN_TYPE| Constructor and Description |
|---|
ScriptEvaluator()
Constructs a script evaluator with all the default settings.
|
ScriptEvaluator(int count)
Constructs a script evaluator with the given number of scripts.
|
ScriptEvaluator(Scanner scanner,
Class<?> extendedType,
Class<?>[] implementedTypes,
Class<?> returnType,
String[] parameterNames,
Class<?>[] parameterTypes,
Class<?>[] thrownExceptions,
ClassLoader parentClassLoader)
Equivalent to
ScriptEvaluator se = new ScriptEvaluator();
se.setExtendedType(extendedType);
se.setImplementedTypes(implementedTypes);
se.setReturnType(returnType);
se.setParameters(parameterNames, parameterTypes);
se.setThrownExceptions(thrownExceptions);
se.setParentClassLoader(parentClassLoader);
se.cook(scanner);
|
ScriptEvaluator(Scanner scanner,
Class<?> returnType,
String[] parameterNames,
Class<?>[] parameterTypes,
Class<?>[] thrownExceptions,
ClassLoader parentClassLoader)
Equivalent to
ScriptEvaluator se = new ScriptEvaluator();
se.setReturnType(returnType);
se.setParameters(parameterNames, parameterTypes);
se.setThrownExceptions(thrownExceptions);
se.setParentClassLoader(parentClassLoader);
se.cook(scanner);
|
ScriptEvaluator(Scanner scanner,
String className,
Class<?> extendedType,
Class<?>[] implementedTypes,
boolean staticMethod,
Class<?> returnType,
String methodName,
String[] parameterNames,
Class<?>[] parameterTypes,
Class<?>[] thrownExceptions,
ClassLoader parentClassLoader)
Equivalent to
ScriptEvaluator se = new ScriptEvaluator();
se.setClassName(className);
se.setExtendedType(extendedType);
se.setImplementedTypes(implementedTypes);
se.setStaticMethod(staticMethod);
se.setReturnType(returnType);
se.setMethodName(methodName);
se.setParameters(parameterNames, parameterTypes);
se.setThrownExceptions(thrownExceptions);
se.setParentClassLoader(parentClassLoader);
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 fileName,
InputStream is,
Class<?> returnType,
String[] parameterNames,
Class<?>[] parameterTypes,
Class<?>[] thrownExceptions,
ClassLoader parentClassLoader)
Equivalent to
ScriptEvaluator se = new ScriptEvaluator();
se.setReturnType(returnType);
se.setParameters(parameterNames, parameterTypes);
se.setThrownExceptions(thrownExceptions);
se.setParentClassLoader(parentClassLoader);
se.cook(fileName, is);
|
ScriptEvaluator(String fileName,
Reader reader,
Class<?> returnType,
String[] parameterNames,
Class<?>[] parameterTypes,
Class<?>[] thrownExceptions,
ClassLoader parentClassLoader)
Equivalent to
ScriptEvaluator se = new ScriptEvaluator();
se.setReturnType(returnType);
se.setParameters(parameterNames, parameterTypes);
se.setThrownExceptions(thrownExceptions);
se.setParentClassLoader(parentClassLoader);
se.cook(reader);
|
| Modifier and Type | Method and Description |
|---|---|
protected Java.Type[] |
classesToTypes(Location location,
Class<?>[] classes) |
protected Java.Type |
classToType(Location loc,
Class<?> clazz) |
protected void |
cook(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). |
void |
cook(Java.CompilationUnit compilationUnit,
Collection<Java.MethodDeclarator> methodDeclarators) |
void |
cook(Parser[] parsers) |
void |
cook(Scanner... scanners)
Cooks a set of scripts into one class.
|
void |
cook(String[] fileNames,
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 fileName,
Reader reader) |
<T> T |
createFastEvaluator(Reader reader,
Class<T> interfaceToImplement,
String[] parameterNames) |
<T> T |
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> T |
createFastEvaluator(String script,
Class<T> interfaceToImplement,
String[] parameterNames) |
static Object |
createFastScriptEvaluator(Scanner scanner,
Class<?> interfaceToImplement,
String[] parameterNames,
ClassLoader parentClassLoader)
Deprecated.
Use
createFastEvaluator(Scanner, Class, String[]) instead |
static Object |
createFastScriptEvaluator(Scanner scanner,
String[] defaultImports,
String className,
Class<?> extendedClass,
Class<?> interfaceToImplement,
String[] parameterNames,
ClassLoader parentClassLoader)
Deprecated.
Use
createFastEvaluator(Scanner,Class,String[]) instead: |
static Object |
createFastScriptEvaluator(Scanner scanner,
String className,
Class<?> extendedType,
Class<?> interfaceToImplement,
String[] parameterNames,
ClassLoader parentClassLoader)
Deprecated.
Use
createFastEvaluator(Scanner, Class, String[]) instead |
static Object |
createFastScriptEvaluator(String script,
Class<?> interfaceToImplement,
String[] parameterNames)
Deprecated.
Use
createFastEvaluator(Scanner, Class, String[]) instead |
Object |
evaluate(int idx,
Object[] arguments) |
Object |
evaluate(Object... arguments) |
Map<String,byte[]> |
getBytecodes() |
Class<?> |
getClazz() |
String[] |
getDefaultImports() |
Class<?> |
getDefaultReturnType() |
Method |
getMethod() |
Method |
getMethod(int idx) |
Method[] |
getResult() |
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.
|
protected Java.Type |
optionalClassToType(Location loc,
Class<?> clazz) |
EnumSet<JaninoOption> |
options() |
ScriptEvaluator |
options(EnumSet<JaninoOption> options)
Sets the options for all future compilations.
|
void |
setClassName(String className) |
void |
setCompileErrorHandler(ErrorHandler compileErrorHandler) |
void |
setDebuggingInformation(boolean debugSource,
boolean debugLines,
boolean debugVars) |
void |
setDefaultImports(String... defaultImports) |
void |
setDefaultReturnType(Class<?> defaultReturnType) |
void |
setExtendedClass(Class<?> extendedType) |
void |
setImplementedInterfaces(Class<?>[] implementedTypes) |
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 |
setParentClassLoader(ClassLoader parentClassLoader) |
void |
setReturnType(Class<?> returnType) |
void |
setReturnTypes(Class<?>[] returnTypes) |
void |
setScriptCount(int count) |
void |
setSourceVersion(int version) |
void |
setStaticMethod(boolean staticMethod) |
void |
setStaticMethod(boolean[] staticMethod) |
void |
setTargetVersion(int version) |
void |
setThrownExceptions(Class<?>[] thrownExceptions) |
void |
setThrownExceptions(Class<?>[][] thrownExceptions) |
void |
setWarningHandler(WarningHandler warningHandler) |
cook, cook, cook, cook, cook, cook, cook, cookFiles, cookFiles, cookFiles, cookFilescook, cook, cook, cook, cook, cook, cook, cookFile, cookFile, cookFile, cookFileclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitcook, cook, cookpublic 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 fileName, InputStream is, Class<?> returnType, String[] parameterNames, Class<?>[] parameterTypes, Class<?>[] thrownExceptions, @Nullable ClassLoader parentClassLoader) throws CompileException, IOException
ScriptEvaluator se = new ScriptEvaluator();
se.setReturnType(returnType);
se.setParameters(parameterNames, parameterTypes);
se.setThrownExceptions(thrownExceptions);
se.setParentClassLoader(parentClassLoader);
se.cook(fileName, is);
public ScriptEvaluator(@Nullable String fileName, Reader reader, Class<?> returnType, String[] parameterNames, Class<?>[] parameterTypes, Class<?>[] thrownExceptions, @Nullable ClassLoader parentClassLoader) throws CompileException, IOException
ScriptEvaluator se = new ScriptEvaluator();
se.setReturnType(returnType);
se.setParameters(parameterNames, parameterTypes);
se.setThrownExceptions(thrownExceptions);
se.setParentClassLoader(parentClassLoader);
se.cook(reader);
public ScriptEvaluator(Scanner scanner, Class<?> returnType, String[] parameterNames, Class<?>[] parameterTypes, Class<?>[] thrownExceptions, @Nullable ClassLoader parentClassLoader) throws CompileException, IOException
ScriptEvaluator se = new ScriptEvaluator();
se.setReturnType(returnType);
se.setParameters(parameterNames, parameterTypes);
se.setThrownExceptions(thrownExceptions);
se.setParentClassLoader(parentClassLoader);
se.cook(scanner);
public ScriptEvaluator(Scanner scanner, @Nullable Class<?> extendedType, Class<?>[] implementedTypes, Class<?> returnType, String[] parameterNames, Class<?>[] parameterTypes, Class<?>[] thrownExceptions, @Nullable ClassLoader parentClassLoader) throws CompileException, IOException
ScriptEvaluator se = new ScriptEvaluator();
se.setExtendedType(extendedType);
se.setImplementedTypes(implementedTypes);
se.setReturnType(returnType);
se.setParameters(parameterNames, parameterTypes);
se.setThrownExceptions(thrownExceptions);
se.setParentClassLoader(parentClassLoader);
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<?> extendedType, Class<?>[] implementedTypes, boolean staticMethod, Class<?> returnType, String methodName, String[] parameterNames, Class<?>[] parameterTypes, Class<?>[] thrownExceptions, @Nullable ClassLoader parentClassLoader) throws CompileException, IOException
ScriptEvaluator se = new ScriptEvaluator();
se.setClassName(className);
se.setExtendedType(extendedType);
se.setImplementedTypes(implementedTypes);
se.setStaticMethod(staticMethod);
se.setReturnType(returnType);
se.setMethodName(methodName);
se.setParameters(parameterNames, parameterTypes);
se.setThrownExceptions(thrownExceptions);
se.setParentClassLoader(parentClassLoader);
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()
public ScriptEvaluator(int count)
The argument of all following invocations of
setMethodNames(String[]),
setOverrideMethod(boolean[]),
setParameters(String[][], Class[][]),
setReturnTypes(Class[]),
setStaticMethod(boolean[]),
setThrownExceptions(Class[][]),
cook(Parser[]),
MultiCookable.cook(Reader[]),
cook(Scanner[]),
MultiCookable.cook(String[]),
cook(String[], Reader[]) and
MultiCookable.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 setParentClassLoader(@Nullable ClassLoader parentClassLoader)
setParentClassLoader in interface IScriptEvaluatorpublic void setDebuggingInformation(boolean debugSource,
boolean debugLines,
boolean debugVars)
setDebuggingInformation in interface IScriptEvaluatorpublic void setSourceVersion(int version)
setSourceVersion in interface ICookablepublic void setTargetVersion(int version)
setTargetVersion in interface ICookablepublic void setCompileErrorHandler(@Nullable ErrorHandler compileErrorHandler)
setCompileErrorHandler in interface IScriptEvaluatorpublic void setWarningHandler(@Nullable WarningHandler warningHandler)
setWarningHandler in interface IScriptEvaluatorpublic EnumSet<JaninoOption> options()
public ScriptEvaluator options(EnumSet<JaninoOption> options)
public void setScriptCount(int count)
IllegalArgumentException - count is different from previous invocations of
this methodpublic void setClassName(String className)
setClassName in interface IScriptEvaluatorpublic void setImplementedInterfaces(Class<?>[] implementedTypes)
setImplementedInterfaces in interface IScriptEvaluatorpublic void setExtendedClass(@Nullable Class<?> extendedType)
setExtendedClass in interface IScriptEvaluatorpublic void setDefaultReturnType(Class<?> defaultReturnType)
setDefaultReturnType in interface IScriptEvaluatorpublic Class<?> getDefaultReturnType()
getDefaultReturnType in interface IScriptEvaluatorpublic void setOverrideMethod(boolean overrideMethod)
setOverrideMethod in interface IScriptEvaluatorpublic void setStaticMethod(boolean staticMethod)
setStaticMethod in interface IScriptEvaluatorpublic void setReturnType(@Nullable Class<?> returnType)
setReturnType in interface IScriptEvaluatorpublic void setMethodName(@Nullable 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 IScriptEvaluatorpublic 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 void cook(@Nullable String fileName, Reader reader) throws CompileException, IOException
cook in interface ICookablecook in class CookableCompileExceptionIOExceptionpublic final void cook(String[] fileNames, 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 IMultiCookablecook in interface IScriptEvaluatorcook in class MultiCookableCompileExceptionIOExceptionpublic final void cook(Scanner... scanners) throws CompileException, IOException
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[])public final void cook(Java.CompilationUnit compilationUnit, Collection<Java.MethodDeclarator> methodDeclarators) throws CompileException
CompileExceptionpublic Method[] getResult()
getResult in interface IScriptEvaluator@Nullable protected Java.Type optionalClassToType(Location loc, @Nullable Class<?> clazz)
protected void cook(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 IScriptEvaluatorpublic Class<?> getClazz()
getClazz in interface IScriptEvaluatorpublic Map<String,byte[]> getBytecodes()
getBytecodes in interface ICookable@Nullable protected final Class<?> getReturnType(int index)
null means "use the default return type"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
createFastEvaluator(Scanner, Class, String[]) insteadCompileException@Deprecated public static Object createFastScriptEvaluator(Scanner scanner, Class<?> interfaceToImplement, String[] parameterNames, @Nullable ClassLoader parentClassLoader) throws CompileException, IOException
createFastEvaluator(Scanner, Class, String[]) insteadCompileExceptionIOException@Deprecated public static Object createFastScriptEvaluator(Scanner scanner, String className, @Nullable Class<?> extendedType, Class<?> interfaceToImplement, String[] parameterNames, @Nullable ClassLoader parentClassLoader) throws CompileException, IOException
createFastEvaluator(Scanner, Class, String[]) insteadCompileExceptionIOException@Deprecated public static Object createFastScriptEvaluator(Scanner scanner, String[] defaultImports, String className, @Nullable Class<?> extendedClass, Class<?> interfaceToImplement, String[] parameterNames, @Nullable ClassLoader parentClassLoader) throws CompileException, IOException
createFastEvaluator(Scanner,Class,String[]) instead:
ScriptEvaluator se = new ScriptEvaluator();
se.setDefaultImports.(defaultImports);
se.setClassName.(className);
se.setExtendedClass.(extendedClass);
se.setParentClassLoader(parentClassLoader);
return se.createFastEvaluator(scanner,
interfaceToImplement, parameterNames);
CompileExceptionIOExceptionpublic void setDefaultImports(String... defaultImports)
setDefaultImports in interface IScriptEvaluatorpublic String[] getDefaultImports()
getDefaultImports in interface IScriptEvaluatorpublic <T> T createFastEvaluator(Reader reader, Class<T> interfaceToImplement, String[] parameterNames) throws CompileException, IOException
createFastEvaluator in interface IScriptEvaluatorCompileExceptionIOExceptionpublic <T> T createFastEvaluator(String script, Class<T> interfaceToImplement, String[] parameterNames) throws CompileException
createFastEvaluator in interface IScriptEvaluatorCompileExceptionpublic <T> T 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(String, Reader)Copyright © 2022. All rights reserved.