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, cookFiles
cook, cook, cook, cook, cook, cook, cook, cookFile, cookFile, cookFile, cookFile
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
cook, cook, cook
public ScriptEvaluator(String script) throws CompileException
ScriptEvaluator se = new ScriptEvaluator(); se.cook(script);
CompileException
ScriptEvaluator()
,
Cookable.cook(String)
public ScriptEvaluator(String script, Class<?> returnType) throws CompileException
ScriptEvaluator se = new ScriptEvaluator(); se.setReturnType(returnType); se.cook(script);
CompileException
ScriptEvaluator()
,
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);
CompileException
IOException
ScriptEvaluator()
,
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);
CompileException
IOException
ScriptEvaluator()
,
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 IScriptEvaluator
public void setDebuggingInformation(boolean debugSource, boolean debugLines, boolean debugVars)
setDebuggingInformation
in interface IScriptEvaluator
public void setSourceVersion(int version)
setSourceVersion
in interface ICookable
public void setTargetVersion(int version)
setTargetVersion
in interface ICookable
public void setCompileErrorHandler(@Nullable ErrorHandler compileErrorHandler)
setCompileErrorHandler
in interface IScriptEvaluator
public void setWarningHandler(@Nullable WarningHandler warningHandler)
setWarningHandler
in interface IScriptEvaluator
public 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 IScriptEvaluator
public void setImplementedInterfaces(Class<?>[] implementedTypes)
setImplementedInterfaces
in interface IScriptEvaluator
public void setExtendedClass(@Nullable Class<?> extendedType)
setExtendedClass
in interface IScriptEvaluator
public void setDefaultReturnType(Class<?> defaultReturnType)
setDefaultReturnType
in interface IScriptEvaluator
public Class<?> getDefaultReturnType()
getDefaultReturnType
in interface IScriptEvaluator
public void setOverrideMethod(boolean overrideMethod)
setOverrideMethod
in interface IScriptEvaluator
public void setStaticMethod(boolean staticMethod)
setStaticMethod
in interface IScriptEvaluator
public void setReturnType(@Nullable Class<?> returnType)
setReturnType
in interface IScriptEvaluator
public void setMethodName(@Nullable String methodName)
setMethodName
in interface IScriptEvaluator
public void setParameters(String[] parameterNames, Class<?>[] parameterTypes)
setParameters
in interface IScriptEvaluator
public void setThrownExceptions(Class<?>[] thrownExceptions)
setThrownExceptions
in interface IScriptEvaluator
public void setOverrideMethod(boolean[] overrideMethod)
setOverrideMethod
in interface IScriptEvaluator
public void setStaticMethod(boolean[] staticMethod)
setStaticMethod
in interface IScriptEvaluator
public void setReturnTypes(Class<?>[] returnTypes)
setReturnTypes
in interface IScriptEvaluator
public void setMethodNames(String[] methodNames)
setMethodNames
in interface IScriptEvaluator
public void setParameters(String[][] parameterNames, Class<?>[][] parameterTypes)
setParameters
in interface IScriptEvaluator
public void setThrownExceptions(Class<?>[][] thrownExceptions)
setThrownExceptions
in interface IScriptEvaluator
public void cook(@Nullable String fileName, Reader reader) throws CompileException, IOException
cook
in interface ICookable
cook
in class Cookable
CompileException
IOException
public 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 IMultiCookable
cook
in interface IScriptEvaluator
cook
in class MultiCookable
CompileException
IOException
public 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
scanners
CompileException
IOException
public final void cook(Parser[] parsers) throws CompileException, IOException
CompileException
IOException
cook(Scanner[])
public final void cook(Java.CompilationUnit compilationUnit, Collection<Java.MethodDeclarator> methodDeclarators) throws CompileException
CompileException
public 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 IScriptEvaluator
InvocationTargetException
@Nullable public Object evaluate(int idx, @Nullable Object[] arguments) throws InvocationTargetException
evaluate
in interface IScriptEvaluator
InvocationTargetException
public Method getMethod()
getMethod
in interface IScriptEvaluator
public Method getMethod(int idx)
getMethod
in interface IScriptEvaluator
public Class<?> getClazz()
getClazz
in interface IScriptEvaluator
public 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 declaresCompileException
IOException
@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[])
insteadCompileException
IOException
@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[])
insteadCompileException
IOException
@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 = newScriptEvaluator
(); se.setDefaultImports
.(defaultImports); se.setClassName
.(className); se.setExtendedClass
.(extendedClass); se.setParentClassLoader
(parentClassLoader); return se.createFastEvaluator
(scanner, interfaceToImplement, parameterNames);
CompileException
IOException
public void setDefaultImports(String... defaultImports)
setDefaultImports
in interface IScriptEvaluator
public String[] getDefaultImports()
getDefaultImports
in interface IScriptEvaluator
public <T> T createFastEvaluator(Reader reader, Class<T> interfaceToImplement, String[] parameterNames) throws CompileException, IOException
createFastEvaluator
in interface IScriptEvaluator
CompileException
IOException
public <T> T createFastEvaluator(String script, Class<T> interfaceToImplement, String[] parameterNames) throws CompileException
createFastEvaluator
in interface IScriptEvaluator
CompileException
public <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 readCompileException
IOException
createFastEvaluator(Reader, Class, String[])
public static String[] guessParameterNames(Scanner scanner) throws CompileException, IOException
CompileException
IOException
Scanner(String, Reader)
Copyright © 2021. All rights reserved.