Class ScriptEvaluator
- java.lang.Object
-
- org.codehaus.commons.compiler.Cookable
-
- org.codehaus.commons.compiler.MultiCookable
-
- org.codehaus.janino.ScriptEvaluator
-
- All Implemented Interfaces:
ICookable
,IMultiCookable
,IScriptEvaluator
public class ScriptEvaluator extends MultiCookable implements IScriptEvaluator
An implementation ofIScriptEvaluator
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.
-
-
Field Summary
-
Fields inherited from interface org.codehaus.commons.compiler.IScriptEvaluator
DEFAULT_METHOD_NAME, DEFAULT_RETURN_TYPE
-
-
Constructor Summary
Constructors Constructor 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(java.lang.String script)
Equivalent toScriptEvaluator(java.lang.String fileName, java.io.InputStream is, java.lang.Class<?> returnType, java.lang.String[] parameterNames, java.lang.Class<?>[] parameterTypes, java.lang.Class<?>[] thrownExceptions, java.lang.ClassLoader parentClassLoader)
Equivalent toScriptEvaluator(java.lang.String fileName, java.io.Reader reader, java.lang.Class<?> returnType, java.lang.String[] parameterNames, java.lang.Class<?>[] parameterTypes, java.lang.Class<?>[] thrownExceptions, java.lang.ClassLoader parentClassLoader)
Equivalent toScriptEvaluator(java.lang.String script, java.lang.Class<?> returnType)
Equivalent toScriptEvaluator(java.lang.String script, java.lang.Class<?> returnType, java.lang.String[] parameterNames, java.lang.Class<?>[] parameterTypes)
Equivalent toScriptEvaluator(java.lang.String script, java.lang.Class<?> returnType, java.lang.String[] parameterNames, java.lang.Class<?>[] parameterTypes, java.lang.Class<?>[] thrownExceptions)
Equivalent toScriptEvaluator(Scanner scanner, java.lang.Class<?> extendedType, java.lang.Class<?>[] implementedTypes, java.lang.Class<?> returnType, java.lang.String[] parameterNames, java.lang.Class<?>[] parameterTypes, java.lang.Class<?>[] thrownExceptions, java.lang.ClassLoader parentClassLoader)
Equivalent toScriptEvaluator(Scanner scanner, java.lang.Class<?> returnType, java.lang.String[] parameterNames, java.lang.Class<?>[] parameterTypes, java.lang.Class<?>[] thrownExceptions, java.lang.ClassLoader parentClassLoader)
Equivalent toScriptEvaluator(Scanner scanner, java.lang.String className, java.lang.Class<?> extendedType, java.lang.Class<?>[] implementedTypes, boolean staticMethod, java.lang.Class<?> returnType, java.lang.String methodName, java.lang.String[] parameterNames, java.lang.Class<?>[] parameterTypes, java.lang.Class<?>[] thrownExceptions, java.lang.ClassLoader parentClassLoader)
Equivalent to
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected Java.Type[]
classesToTypes(Location location, java.lang.Class<?>[] classes)
protected Java.Type
classToType(Location loc, java.lang.Class<?> clazz)
void
cook(java.lang.String[] fileNames, java.io.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(java.lang.String fileName, java.io.Reader reader)
Reads, scans, parses and compiles Java tokens from the givenReader
.protected void
cook(Java.CompilationUnit compilationUnit)
Compiles the given compilationUnit, defines it into aClassLoader
, loads the generated class, gets the script methods from that class, and makes them available throughgetMethod(int)
.void
cook(Java.CompilationUnit compilationUnit, java.util.Collection<Java.MethodDeclarator> methodDeclarators)
void
cook(Parser[] parsers)
void
cook(Scanner... scanners)
Cooks a set of scripts into one class.<T> T
createFastEvaluator(java.io.Reader reader, java.lang.Class<T> interfaceToImplement, java.lang.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> T
createFastEvaluator(java.lang.String script, java.lang.Class<T> interfaceToImplement, java.lang.String[] parameterNames)
<T> T
createFastEvaluator(Scanner scanner, java.lang.Class<?> interfaceToImplement, java.lang.String[] parameterNames)
Notice: This method is not declared inIScriptEvaluator
, and is hence only available in this implementation oforg.codehaus.commons.compiler
.static java.lang.Object
createFastScriptEvaluator(java.lang.String script, java.lang.Class<?> interfaceToImplement, java.lang.String[] parameterNames)
Deprecated.UsecreateFastEvaluator(Scanner, Class, String[])
insteadstatic java.lang.Object
createFastScriptEvaluator(Scanner scanner, java.lang.Class<?> interfaceToImplement, java.lang.String[] parameterNames, java.lang.ClassLoader parentClassLoader)
Deprecated.UsecreateFastEvaluator(Scanner, Class, String[])
insteadstatic java.lang.Object
createFastScriptEvaluator(Scanner scanner, java.lang.String[] defaultImports, java.lang.String className, java.lang.Class<?> extendedClass, java.lang.Class<?> interfaceToImplement, java.lang.String[] parameterNames, java.lang.ClassLoader parentClassLoader)
Deprecated.UsecreateFastEvaluator(Scanner,Class,String[])
instead:static java.lang.Object
createFastScriptEvaluator(Scanner scanner, java.lang.String className, java.lang.Class<?> extendedType, java.lang.Class<?> interfaceToImplement, java.lang.String[] parameterNames, java.lang.ClassLoader parentClassLoader)
Deprecated.UsecreateFastEvaluator(Scanner, Class, String[])
insteadjava.lang.Object
evaluate()
Shorthand forevaluate
(new Object[0])
.java.lang.Object
evaluate(int idx, java.lang.Object[] arguments)
Same asIScriptEvaluator.evaluate(Object[])
, but for multiple scripts.java.lang.Object
evaluate(java.lang.Object[] arguments)
Calls the script with concrete parameter values.java.util.Map<java.lang.String,byte[]>
getBytecodes()
java.lang.Class<?>
getClazz()
java.lang.String[]
getDefaultImports()
java.lang.Class<?>
getDefaultReturnType()
java.lang.reflect.Method
getMethod()
java.lang.reflect.Method
getMethod(int idx)
Same asIScriptEvaluator.getMethod()
, but for multiple scripts.java.lang.reflect.Method[]
getResult()
protected java.lang.Class<?>
getReturnType(int index)
static java.lang.String[]
guessParameterNames(Scanner scanner)
Guesses the names of the parameters used in the given expression.protected void
makeStatements(int idx, Parser parser, java.util.List<Java.BlockStatement> resultStatements, java.util.List<Java.MethodDeclarator> resultMethods)
Parses statements from the parser until end-of-input.protected Java.Type
optionalClassToType(Location loc, java.lang.Class<?> clazz)
java.util.EnumSet<JaninoOption>
options()
ScriptEvaluator
options(java.util.EnumSet<JaninoOption> options)
Sets the options for all future compilations.void
setClassName(java.lang.String className)
void
setCompileErrorHandler(ErrorHandler compileErrorHandler)
Installs anErrorHandler
which is invoked during compilation on each error.void
setDebuggingInformation(boolean debugSource, boolean debugLines, boolean debugVars)
Determines what kind of debugging information is included in the generates classes.void
setDefaultImports(java.lang.String... defaultImports)
void
setDefaultReturnType(java.lang.Class<?> defaultReturnType)
When thisIScriptEvaluator
is coooked, then the defaultReturnType applies to all scripts for which no explicit return type was configured.void
setExtendedClass(java.lang.Class<?> extendedType)
void
setImplementedInterfaces(java.lang.Class<?>[] implementedTypes)
void
setMethodName(java.lang.String methodName)
Defines the name of the generated method.void
setMethodNames(java.lang.String[] methodNames)
Same asIScriptEvaluator.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 asIScriptEvaluator.setOverrideMethod(boolean)
, but for multiple scripts.void
setParameters(java.lang.String[][] parameterNames, java.lang.Class<?>[][] parameterTypes)
Same asIScriptEvaluator.setParameters(String[], Class[])
, but for multiple scripts.void
setParameters(java.lang.String[] parameterNames, java.lang.Class<?>[] parameterTypes)
Defines the names and types of the parameters of the generated method.void
setParentClassLoader(java.lang.ClassLoader parentClassLoader)
The "parent class loader" is used to load referenced classes.void
setReturnType(java.lang.Class<?> returnType)
Defines the return type of the generated method.void
setReturnTypes(java.lang.Class<?>[] returnTypes)
Configures the return types of the generated methods.void
setScriptCount(int count)
void
setSourceVersion(int version)
Specifies the version of source code accepted, in analogy with JAVAC's-source
command line option.void
setStaticMethod(boolean staticMethod)
Defines whether the generated method should be STATIC or not.void
setStaticMethod(boolean[] staticMethod)
Same asIScriptEvaluator.setStaticMethod(boolean)
, but for multiple scripts.void
setTargetVersion(int version)
Generates class files that target a specified release of the virtual machine, in analogy with JAVAC's-target
command line option.void
setThrownExceptions(java.lang.Class<?>[] thrownExceptions)
Defines the exceptions that the generated method may throw.void
setThrownExceptions(java.lang.Class<?>[][] thrownExceptions)
Same asIScriptEvaluator.setThrownExceptions(Class[])
, but for multiple scripts.void
setWarningHandler(WarningHandler warningHandler)
By default, warnings are discarded, but an application my install a customWarningHandler
.-
Methods inherited from class org.codehaus.commons.compiler.MultiCookable
cook, cook, cook, cook, cook, cook, cook, cookFiles, cookFiles, cookFiles, cookFiles
-
Methods inherited from class org.codehaus.commons.compiler.Cookable
cook, cook, cook, cook, cook, cook, cook, cookFile, cookFile, cookFile, cookFile
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.codehaus.commons.compiler.ICookable
cook, cook, cook, cook, cook, cook, cook, cookFile, cookFile, cookFile, cookFile
-
Methods inherited from interface org.codehaus.commons.compiler.IMultiCookable
cook, cook, cook, cook, cookFiles, cookFiles, cookFiles, cookFiles
-
Methods inherited from interface org.codehaus.commons.compiler.IScriptEvaluator
cook, cook, cook
-
-
-
-
Constructor Detail
-
ScriptEvaluator
public ScriptEvaluator(java.lang.String script) throws CompileException
Equivalent toScriptEvaluator se = new ScriptEvaluator(); se.cook(script);
- Throws:
CompileException
- See Also:
ScriptEvaluator()
,Cookable.cook(String)
-
ScriptEvaluator
public ScriptEvaluator(java.lang.String script, java.lang.Class<?> returnType) throws CompileException
Equivalent toScriptEvaluator se = new ScriptEvaluator(); se.setReturnType(returnType); se.cook(script);
- Throws:
CompileException
- See Also:
ScriptEvaluator()
,setReturnType(Class)
,Cookable.cook(String)
-
ScriptEvaluator
public ScriptEvaluator(java.lang.String script, java.lang.Class<?> returnType, java.lang.String[] parameterNames, java.lang.Class<?>[] parameterTypes) throws CompileException
Equivalent toScriptEvaluator se = new ScriptEvaluator(); se.setReturnType(returnType); se.setParameters(parameterNames, parameterTypes); se.cook(script);
-
ScriptEvaluator
public ScriptEvaluator(java.lang.String script, java.lang.Class<?> returnType, java.lang.String[] parameterNames, java.lang.Class<?>[] parameterTypes, java.lang.Class<?>[] thrownExceptions) throws CompileException
Equivalent toScriptEvaluator se = new ScriptEvaluator(); se.setReturnType(returnType); se.setParameters(parameterNames, parameterTypes); se.setThrownExceptions(thrownExceptions); se.cook(script);
-
ScriptEvaluator
public ScriptEvaluator(@Nullable java.lang.String fileName, java.io.InputStream is, java.lang.Class<?> returnType, java.lang.String[] parameterNames, java.lang.Class<?>[] parameterTypes, java.lang.Class<?>[] thrownExceptions, @Nullable java.lang.ClassLoader parentClassLoader) throws CompileException, java.io.IOException
Equivalent toScriptEvaluator se = new ScriptEvaluator(); se.setReturnType(returnType); se.setParameters(parameterNames, parameterTypes); se.setThrownExceptions(thrownExceptions); se.setParentClassLoader(parentClassLoader); se.cook(fileName, is);
- Throws:
CompileException
java.io.IOException
- See Also:
ScriptEvaluator()
,setReturnType(Class)
,setParameters(String[], Class[])
,setThrownExceptions(Class[])
,SimpleCompiler.setParentClassLoader(ClassLoader)
,Cookable.cook(String, InputStream)
-
ScriptEvaluator
public ScriptEvaluator(@Nullable java.lang.String fileName, java.io.Reader reader, java.lang.Class<?> returnType, java.lang.String[] parameterNames, java.lang.Class<?>[] parameterTypes, java.lang.Class<?>[] thrownExceptions, @Nullable java.lang.ClassLoader parentClassLoader) throws CompileException, java.io.IOException
Equivalent toScriptEvaluator se = new ScriptEvaluator(); se.setReturnType(returnType); se.setParameters(parameterNames, parameterTypes); se.setThrownExceptions(thrownExceptions); se.setParentClassLoader(parentClassLoader); se.cook(reader);
- Throws:
CompileException
java.io.IOException
- See Also:
ScriptEvaluator()
,setReturnType(Class)
,setParameters(String[], Class[])
,setThrownExceptions(Class[])
,SimpleCompiler.setParentClassLoader(ClassLoader)
,Cookable.cook(String, Reader)
-
ScriptEvaluator
public ScriptEvaluator(Scanner scanner, java.lang.Class<?> returnType, java.lang.String[] parameterNames, java.lang.Class<?>[] parameterTypes, java.lang.Class<?>[] thrownExceptions, @Nullable java.lang.ClassLoader parentClassLoader) throws CompileException, java.io.IOException
Equivalent toScriptEvaluator se = new ScriptEvaluator(); se.setReturnType(returnType); se.setParameters(parameterNames, parameterTypes); se.setThrownExceptions(thrownExceptions); se.setParentClassLoader(parentClassLoader); se.cook(scanner);
- Throws:
CompileException
java.io.IOException
- See Also:
ScriptEvaluator()
,setReturnType(Class)
,setParameters(String[], Class[])
,setThrownExceptions(Class[])
,SimpleCompiler.setParentClassLoader(ClassLoader)
,Cookable.cook(Reader)
-
ScriptEvaluator
public ScriptEvaluator(Scanner scanner, @Nullable java.lang.Class<?> extendedType, java.lang.Class<?>[] implementedTypes, java.lang.Class<?> returnType, java.lang.String[] parameterNames, java.lang.Class<?>[] parameterTypes, java.lang.Class<?>[] thrownExceptions, @Nullable java.lang.ClassLoader parentClassLoader) throws CompileException, java.io.IOException
Equivalent toScriptEvaluator 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);
- Throws:
CompileException
java.io.IOException
- See Also:
ScriptEvaluator()
,ClassBodyEvaluator.setExtendedClass(Class)
,ClassBodyEvaluator.setImplementedInterfaces(Class[])
,setReturnType(Class)
,setParameters(String[], Class[])
,setThrownExceptions(Class[])
,SimpleCompiler.setParentClassLoader(ClassLoader)
,Cookable.cook(Reader)
-
ScriptEvaluator
public ScriptEvaluator(Scanner scanner, java.lang.String className, @Nullable java.lang.Class<?> extendedType, java.lang.Class<?>[] implementedTypes, boolean staticMethod, java.lang.Class<?> returnType, java.lang.String methodName, java.lang.String[] parameterNames, java.lang.Class<?>[] parameterTypes, java.lang.Class<?>[] thrownExceptions, @Nullable java.lang.ClassLoader parentClassLoader) throws CompileException, java.io.IOException
Equivalent toScriptEvaluator 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);
- Throws:
CompileException
java.io.IOException
- See Also:
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)
-
ScriptEvaluator
public ScriptEvaluator()
Constructs a script evaluator with all the default settings.
-
ScriptEvaluator
public ScriptEvaluator(int count)
Constructs a script evaluator with the given number of scripts.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[])
andMultiCookable.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.
-
-
Method Detail
-
setParentClassLoader
public void setParentClassLoader(@Nullable java.lang.ClassLoader parentClassLoader)
Description copied from interface:IScriptEvaluator
The "parent class loader" is used to load referenced classes. Useful values are:System.getSystemClassLoader()
The running JVM's class path Thread.currentThread().getContextClassLoader()
ornull
The class loader effective for the invoking thread ClassLoaders.BOOTCLASSPATH_CLASS_LOADER
The running JVM's boot class path The parent class loader defaults to the current thread's context class loader.
- Specified by:
setParentClassLoader
in interfaceIScriptEvaluator
-
setDebuggingInformation
public void setDebuggingInformation(boolean debugSource, boolean debugLines, boolean debugVars)
Description copied from interface:IScriptEvaluator
Determines what kind of debugging information is included in the generates classes. The default is typically "-g:none
".- Specified by:
setDebuggingInformation
in interfaceIScriptEvaluator
-
setSourceVersion
public void setSourceVersion(int version)
Description copied from interface:ICookable
Specifies the version of source code accepted, in analogy with JAVAC's-source
command line option. May be ignored by an implementation (e.g. thejanino
implementation always accepts the language features as described on the home page). Allowed values, and the default value, depend on the implementation.-1
means to use a default version.- Specified by:
setSourceVersion
in interfaceICookable
-
setTargetVersion
public void setTargetVersion(int version)
Description copied from interface:ICookable
Generates class files that target a specified release of the virtual machine, in analogy with JAVAC's-target
command line option. Allowed values depend on the implementation. The default value also depends on the implementation. The only invariant is that the generated class files are suitable for the currently executing JVM.-1
means to use a default version.- Specified by:
setTargetVersion
in interfaceICookable
-
setCompileErrorHandler
public void setCompileErrorHandler(@Nullable ErrorHandler compileErrorHandler)
Description copied from interface:IScriptEvaluator
Installs anErrorHandler
which is invoked during compilation on each error. (By default, the compilation throws aCompileException
on the first error and terminates.)If the given
ErrorHandler
throws aCompileException
, then the compilation terminates and the exception is propagated.If the given
ErrorHandler
does not throw aCompileException
but completes normally, then the compilation may or may not continue, depending on the error. Iff the compilation completes normally but errors were reported, then it will throw aCompileException
indicating the number of errors.In other words: The
ErrorHandler
may throw aCompileException
or not, but the compilation will definitely throw aCompileException
if one or more compile errors have occurred.- Specified by:
setCompileErrorHandler
in interfaceIScriptEvaluator
- Parameters:
compileErrorHandler
-null
to restore the default behavior (throwing aCompileException
)
-
setWarningHandler
public void setWarningHandler(@Nullable WarningHandler warningHandler)
Description copied from interface:IScriptEvaluator
By default, warnings are discarded, but an application my install a customWarningHandler
.- Specified by:
setWarningHandler
in interfaceIScriptEvaluator
- Parameters:
warningHandler
-null
to indicate that no warnings be issued
-
options
public java.util.EnumSet<JaninoOption> options()
- Returns:
- A reference to the currently effective compilation options; changes to it take effect immediately
-
options
public ScriptEvaluator options(java.util.EnumSet<JaninoOption> options)
Sets the options for all future compilations.
-
setScriptCount
public void setScriptCount(int count)
- Throws:
java.lang.IllegalArgumentException
- count is different from previous invocations of this method
-
setClassName
public void setClassName(java.lang.String className)
- Specified by:
setClassName
in interfaceIScriptEvaluator
- See Also:
IClassBodyEvaluator.setClassName(String)
-
setImplementedInterfaces
public void setImplementedInterfaces(java.lang.Class<?>[] implementedTypes)
- Specified by:
setImplementedInterfaces
in interfaceIScriptEvaluator
- See Also:
IClassBodyEvaluator.setImplementedInterfaces(Class[])
-
setExtendedClass
public void setExtendedClass(@Nullable java.lang.Class<?> extendedType)
- Specified by:
setExtendedClass
in interfaceIScriptEvaluator
- See Also:
IClassBodyEvaluator.setExtendedClass(Class)
-
setDefaultReturnType
public void setDefaultReturnType(java.lang.Class<?> defaultReturnType)
Description copied from interface:IScriptEvaluator
When thisIScriptEvaluator
is coooked, then the defaultReturnType applies to all scripts for which no explicit return type was configured.The initial default return type (if you want, the "default-default" return type) is
void.class
.- Specified by:
setDefaultReturnType
in interfaceIScriptEvaluator
- See Also:
IScriptEvaluator.setReturnType(Class)
,IScriptEvaluator.setReturnTypes(Class[])
-
getDefaultReturnType
public java.lang.Class<?> getDefaultReturnType()
- Specified by:
getDefaultReturnType
in interfaceIScriptEvaluator
- Returns:
- The default return type that was previously configured with
IScriptEvaluator.setDefaultReturnType(Class)
, orIScriptEvaluator.DEFAULT_RETURN_TYPE
-
setOverrideMethod
public void setOverrideMethod(boolean overrideMethod)
Description copied from interface:IScriptEvaluator
Defines whether the generated method overrides a methods declared in a supertype.- Specified by:
setOverrideMethod
in interfaceIScriptEvaluator
-
setStaticMethod
public void setStaticMethod(boolean staticMethod)
Description copied from interface:IScriptEvaluator
Defines whether the generated method should be STATIC or not. Defaults totrue
.- Specified by:
setStaticMethod
in interfaceIScriptEvaluator
-
setReturnType
public void setReturnType(@Nullable java.lang.Class<?> returnType)
Description copied from interface:IScriptEvaluator
Defines the return type of the generated method. Valuenull
means "use the default return type".- Specified by:
setReturnType
in interfaceIScriptEvaluator
- See Also:
IScriptEvaluator.setDefaultReturnType(Class)
-
setMethodName
public void setMethodName(@Nullable java.lang.String methodName)
Description copied from interface:IScriptEvaluator
Defines the name of the generated method. Initially, the method name is "eval*".- Specified by:
setMethodName
in interfaceIScriptEvaluator
- Parameters:
methodName
-null
means reset to default name- See Also:
IScriptEvaluator.DEFAULT_METHOD_NAME
-
setParameters
public void setParameters(java.lang.String[] parameterNames, java.lang.Class<?>[] parameterTypes)
Description copied from interface:IScriptEvaluator
Defines the names and types of the parameters of the generated method.names
.length
and types.length
must be equal. This invariant may be checked immediately, or later when the script is cooked.The parameters can be of primitive type, e.g.
double.class
.The default is to have zero parameters.
- Specified by:
setParameters
in interfaceIScriptEvaluator
-
setThrownExceptions
public void setThrownExceptions(java.lang.Class<?>[] thrownExceptions)
Description copied from interface:IScriptEvaluator
Defines the exceptions that the generated method may throw.- Specified by:
setThrownExceptions
in interfaceIScriptEvaluator
-
setOverrideMethod
public void setOverrideMethod(boolean[] overrideMethod)
Description copied from interface:IScriptEvaluator
Same asIScriptEvaluator.setOverrideMethod(boolean)
, but for multiple scripts.- Specified by:
setOverrideMethod
in interfaceIScriptEvaluator
-
setStaticMethod
public void setStaticMethod(boolean[] staticMethod)
Description copied from interface:IScriptEvaluator
Same asIScriptEvaluator.setStaticMethod(boolean)
, but for multiple scripts.- Specified by:
setStaticMethod
in interfaceIScriptEvaluator
-
setReturnTypes
public void setReturnTypes(java.lang.Class<?>[] returnTypes)
Description copied from interface:IScriptEvaluator
Configures the return types of the generated methods. If an element of the array isnull
, then use the "default return type" for that script.- Specified by:
setReturnTypes
in interfaceIScriptEvaluator
- Parameters:
returnTypes
- The methods' return types;null
elements mean "use the default return type"- See Also:
IScriptEvaluator.setDefaultReturnType(Class)
,IScriptEvaluator.getDefaultReturnType()
-
setMethodNames
public void setMethodNames(java.lang.String[] methodNames)
Description copied from interface:IScriptEvaluator
Same asIScriptEvaluator.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[][])
).- Specified by:
setMethodNames
in interfaceIScriptEvaluator
-
setParameters
public void setParameters(java.lang.String[][] parameterNames, java.lang.Class<?>[][] parameterTypes)
Description copied from interface:IScriptEvaluator
Same asIScriptEvaluator.setParameters(String[], Class[])
, but for multiple scripts.- Specified by:
setParameters
in interfaceIScriptEvaluator
-
setThrownExceptions
public void setThrownExceptions(java.lang.Class<?>[][] thrownExceptions)
Description copied from interface:IScriptEvaluator
Same asIScriptEvaluator.setThrownExceptions(Class[])
, but for multiple scripts.- Specified by:
setThrownExceptions
in interfaceIScriptEvaluator
-
cook
public void cook(@Nullable java.lang.String fileName, java.io.Reader reader) throws CompileException, java.io.IOException
Description copied from interface:ICookable
Reads, scans, parses and compiles Java tokens from the givenReader
.- Specified by:
cook
in interfaceICookable
- Specified by:
cook
in classCookable
- Parameters:
fileName
- Used when reporting errors and warnings- Throws:
CompileException
java.io.IOException
-
cook
public final void cook(java.lang.String[] fileNames, java.io.Reader[] readers) throws CompileException, java.io.IOException
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.
- Specified by:
cook
in interfaceIMultiCookable
- Specified by:
cook
in interfaceIScriptEvaluator
- Specified by:
cook
in classMultiCookable
- Throws:
CompileException
java.io.IOException
-
cook
public final void cook(Scanner... scanners) throws CompileException, java.io.IOException
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 fileName argument ofScanner(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.
- Throws:
java.lang.IllegalStateException
- Any of the precedingset...()
had an array size different from that ofscanners
CompileException
java.io.IOException
-
cook
public final void cook(Parser[] parsers) throws CompileException, java.io.IOException
- Throws:
CompileException
java.io.IOException
- See Also:
cook(Scanner[])
-
cook
public final void cook(Java.CompilationUnit compilationUnit, java.util.Collection<Java.MethodDeclarator> methodDeclarators) throws CompileException
- Throws:
CompileException
-
getResult
public java.lang.reflect.Method[] getResult()
- Specified by:
getResult
in interfaceIScriptEvaluator
- Returns:
- The generated and loaded methods that implement the cooked scripts
-
optionalClassToType
@Nullable protected Java.Type optionalClassToType(Location loc, @Nullable java.lang.Class<?> clazz)
-
classesToTypes
protected Java.Type[] classesToTypes(Location location, java.lang.Class<?>[] classes)
-
cook
protected void cook(Java.CompilationUnit compilationUnit) throws CompileException
Compiles the given compilationUnit, defines it into aClassLoader
, loads the generated class, gets the script methods from that class, and makes them available throughgetMethod(int)
.- Throws:
CompileException
-
evaluate
@Nullable public java.lang.Object evaluate() throws java.lang.reflect.InvocationTargetException
Description copied from interface:IScriptEvaluator
Shorthand forevaluate
(new Object[0])
.- Specified by:
evaluate
in interfaceIScriptEvaluator
- Throws:
java.lang.reflect.InvocationTargetException
-
evaluate
@Nullable public java.lang.Object evaluate(@Nullable java.lang.Object[] arguments) throws java.lang.reflect.InvocationTargetException
Description copied from interface:IScriptEvaluator
Calls the script with concrete parameter values.Each argument must have the same type as specified through the
parameterTypes
parameter ofIScriptEvaluator.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.
Notice: In version 3.1.8, the arguments parameter was changed from
Object[]
toObject...
, which turned out to be a really bad decision because it caused a very ugly invocation ambiguity withIScriptEvaluator.evaluate(int, Object[])
. Thus, with version 3.1.10, the parameter was changed back toObject[]
.- Specified by:
evaluate
in interfaceIScriptEvaluator
- Parameters:
arguments
- The actual parameter values- Throws:
java.lang.reflect.InvocationTargetException
-
evaluate
@Nullable public java.lang.Object evaluate(int idx, @Nullable java.lang.Object[] arguments) throws java.lang.reflect.InvocationTargetException
Description copied from interface:IScriptEvaluator
Same asIScriptEvaluator.evaluate(Object[])
, but for multiple scripts.- Specified by:
evaluate
in interfaceIScriptEvaluator
- Throws:
java.lang.reflect.InvocationTargetException
-
getMethod
public java.lang.reflect.Method getMethod()
- Specified by:
getMethod
in interfaceIScriptEvaluator
- Returns:
- The generated and loaded
Method
-
getMethod
public java.lang.reflect.Method getMethod(int idx)
Description copied from interface:IScriptEvaluator
Same asIScriptEvaluator.getMethod()
, but for multiple scripts.- Specified by:
getMethod
in interfaceIScriptEvaluator
-
getClazz
public java.lang.Class<?> getClazz()
- Specified by:
getClazz
in interfaceIScriptEvaluator
- See Also:
IClassBodyEvaluator.getClazz()
-
getBytecodes
public java.util.Map<java.lang.String,byte[]> getBytecodes()
- Specified by:
getBytecodes
in interfaceICookable
- Returns:
- The generated Java bytecode; maps class name to bytes
-
getReturnType
@Nullable protected final java.lang.Class<?> getReturnType(int index)
- Returns:
- The return type of the indexed script;
null
means "use thedefault return type
"
-
makeStatements
protected void makeStatements(int idx, Parser parser, java.util.List<Java.BlockStatement> resultStatements, java.util.List<Java.MethodDeclarator> resultMethods) throws CompileException, java.io.IOException
Parses statements from the parser until end-of-input.- Parameters:
resultStatements
- Is filled with the generated statementsresultMethods
- Is filled with any local methods that the script declares- Throws:
CompileException
java.io.IOException
-
createFastScriptEvaluator
@Deprecated public static java.lang.Object createFastScriptEvaluator(java.lang.String script, java.lang.Class<?> interfaceToImplement, java.lang.String[] parameterNames) throws CompileException
Deprecated.UsecreateFastEvaluator(Scanner, Class, String[])
instead- Throws:
CompileException
-
createFastScriptEvaluator
@Deprecated public static java.lang.Object createFastScriptEvaluator(Scanner scanner, java.lang.Class<?> interfaceToImplement, java.lang.String[] parameterNames, @Nullable java.lang.ClassLoader parentClassLoader) throws CompileException, java.io.IOException
Deprecated.UsecreateFastEvaluator(Scanner, Class, String[])
instead- Throws:
CompileException
java.io.IOException
-
createFastScriptEvaluator
@Deprecated public static java.lang.Object createFastScriptEvaluator(Scanner scanner, java.lang.String className, @Nullable java.lang.Class<?> extendedType, java.lang.Class<?> interfaceToImplement, java.lang.String[] parameterNames, @Nullable java.lang.ClassLoader parentClassLoader) throws CompileException, java.io.IOException
Deprecated.UsecreateFastEvaluator(Scanner, Class, String[])
instead- Throws:
CompileException
java.io.IOException
-
createFastScriptEvaluator
@Deprecated public static java.lang.Object createFastScriptEvaluator(Scanner scanner, java.lang.String[] defaultImports, java.lang.String className, @Nullable java.lang.Class<?> extendedClass, java.lang.Class<?> interfaceToImplement, java.lang.String[] parameterNames, @Nullable java.lang.ClassLoader parentClassLoader) throws CompileException, java.io.IOException
Deprecated.UsecreateFastEvaluator(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);- Throws:
CompileException
java.io.IOException
-
setDefaultImports
public void setDefaultImports(java.lang.String... defaultImports)
- Specified by:
setDefaultImports
in interfaceIScriptEvaluator
- See Also:
IClassBodyEvaluator.setDefaultImports(String...)
-
getDefaultImports
public java.lang.String[] getDefaultImports()
- Specified by:
getDefaultImports
in interfaceIScriptEvaluator
- See Also:
IClassBodyEvaluator.getDefaultImports()
-
createFastEvaluator
public <T> T createFastEvaluator(java.io.Reader reader, java.lang.Class<T> interfaceToImplement, java.lang.String[] parameterNames) throws CompileException, java.io.IOException
Description copied from interface:IScriptEvaluator
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
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
sinterfaceToImplement
must either be declaredpublic
, or with package scope in the same package as the generated class (seeIScriptEvaluator.setClassName(String)
).- Specified by:
createFastEvaluator
in interfaceIScriptEvaluator
- Parameters:
reader
- Produces the stream of script tokensinterfaceToImplement
- Must declare exactly one methodparameterNames
- The names of the parameters of that method- Returns:
- An object that implements the given interface
- Throws:
CompileException
java.io.IOException
-
createFastEvaluator
public <T> T createFastEvaluator(java.lang.String script, java.lang.Class<T> interfaceToImplement, java.lang.String[] parameterNames) throws CompileException
- Specified by:
createFastEvaluator
in interfaceIScriptEvaluator
- Parameters:
script
- Contains the sequence of script tokens- Throws:
CompileException
- See Also:
IScriptEvaluator.createFastEvaluator(Reader, Class, String[])
-
createFastEvaluator
public <T> T createFastEvaluator(Scanner scanner, java.lang.Class<?> interfaceToImplement, java.lang.String[] parameterNames) throws CompileException, java.io.IOException
Notice: This method is not declared inIScriptEvaluator
, and is hence only available in this implementation oforg.codehaus.commons.compiler
. To be independent from this particular implementation, try to switch tocreateFastEvaluator(Reader, Class, String[])
.- Parameters:
scanner
- Source of tokens to read- Throws:
CompileException
java.io.IOException
- See Also:
createFastEvaluator(Reader, Class, String[])
-
guessParameterNames
public static java.lang.String[] guessParameterNames(Scanner scanner) throws CompileException, java.io.IOException
Guesses the names of the parameters used in the given expression. The strategy is to look at all "ambiguous names" in the expression (e.g. in "a.b.c.d()", the ambiguous name is "a.b.c"), and then at the components of the ambiguous name.- If any component starts with an upper-case letter, then ambiguous name is assumed to be a type name.
- Otherwise, if the first component of the ambiguous name matches the name of a previously defined local variable, then the first component of the ambiguous name is assumed to be a local variable name. (Notice that this strategy does not consider that the scope of a local variable declaration may end before the end of the script.)
- Otherwise, the first component of the ambiguous name is assumed to be a parameter name.
- Throws:
CompileException
java.io.IOException
- See Also:
Scanner(String, Reader)
-
-