public class ScriptEvaluator extends ClassBodyEvaluator implements IScriptEvaluator
| Modifier and Type | Field and Description |
|---|---|
protected String[] |
optionalMethodNames |
protected String[][] |
optionalParameterNames |
protected Class[][] |
optionalParameterTypes |
protected Class[] |
optionalReturnTypes |
protected boolean[] |
optionalStaticMethod |
protected Class[][] |
optionalThrownExceptions |
className, ZERO_CLASSESdebugLines, debugSource, debugVarsDEFAULT_CLASS_NAMEBOOT_CLASS_LOADER, SYSTEM_PROPERTY_SOURCE_DEBUGGING_DIR, SYSTEM_PROPERTY_SOURCE_DEBUGGING_ENABLE| Constructor and Description |
|---|
ScriptEvaluator() |
ScriptEvaluator(Scanner scanner,
Class optionalExtendedType,
Class[] implementedTypes,
Class returnType,
String[] parameterNames,
Class[] parameterTypes,
Class[] thrownExceptions,
ClassLoader optionalParentClassLoader)
Equivalent to
|
ScriptEvaluator(Scanner scanner,
Class returnType,
String[] parameterNames,
Class[] parameterTypes,
Class[] thrownExceptions,
ClassLoader optionalParentClassLoader)
Equivalent to
|
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(String script)
Equivalent to
|
ScriptEvaluator(String script,
Class returnType)
Equivalent to
|
ScriptEvaluator(String script,
Class returnType,
String[] parameterNames,
Class[] parameterTypes)
Equivalent to
|
ScriptEvaluator(String script,
Class returnType,
String[] parameterNames,
Class[] parameterTypes,
Class[] thrownExceptions)
Equivalent to
|
ScriptEvaluator(String optionalFileName,
InputStream is,
Class returnType,
String[] parameterNames,
Class[] parameterTypes,
Class[] thrownExceptions,
ClassLoader optionalParentClassLoader)
Equivalent to
|
ScriptEvaluator(String optionalFileName,
Reader reader,
Class returnType,
String[] parameterNames,
Class[] parameterTypes,
Class[] thrownExceptions,
ClassLoader optionalParentClassLoader)
Equivalent to
|
| Modifier and Type | Method and Description |
|---|---|
protected void |
compileToMethods(Java.CompilationUnit compilationUnit,
String[] methodNames,
Class[][] parameterTypes) |
void |
cook(Reader[] readers)
Same as
ICookable.cook(Reader), but for multiple scripts. |
void |
cook(Scanner scanner) |
void |
cook(Scanner[] scanners)
Like
cook(Scanner), but cooks a set of scripts into one class. |
void |
cook(String[] strings)
Same as
ICookable.cook(String), but for multiple scripts. |
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)
Same as
ICookable.cook(String, String), but for multiple scripts. |
Object |
createFastEvaluator(Reader reader,
Class interfaceToImplement,
String[] parameterNames)
If the parameter and return types of the expression are known at compile time, then a "fast"
script evaluator can be instantiated through this method.
|
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. |
Object |
createFastEvaluator(String script,
Class 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.
|
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)
Same as
IScriptEvaluator.evaluate(Object[]), but for multiple scripts. |
Object |
evaluate(Object[] arguments)
Calls the script with concrete parameter values.
|
protected Class |
getDefaultReturnType() |
Method |
getMethod()
Returns the loaded
Method. |
Method |
getMethod(int idx)
Same as
IScriptEvaluator.getMethod(), but for multiple scripts. |
static String[] |
guessParameterNames(Scanner scanner)
Guess the names of the parameters used in the given expression.
|
protected Java.MethodDeclarator |
makeMethodDeclaration(Location location,
boolean staticMethod,
Class returnType,
String methodName,
Class[] parameterTypes,
String[] parameterNames,
Class[] thrownExceptions,
List statements)
To the given
Java.ClassDeclaration, add
A public method declaration with the given return type, name, parameter
names and values and thrown exceptions
A block
|
protected List |
makeStatements(int idx,
Scanner scanner)
Fill the given
block by parsing statements until EOF and adding
them to the block. |
void |
setMethodName(String methodName)
Define the name of the generated method.
|
void |
setMethodNames(String[] methodNames)
Same as
IScriptEvaluator.setMethodName(String), but for multiple scripts. |
void |
setParameters(String[][] parameterNames,
Class[][] parameterTypes)
Same as
IScriptEvaluator.setParameters(String[], Class[]), but for multiple scripts. |
void |
setParameters(String[] parameterNames,
Class[] parameterTypes)
Define the names and types of the parameters of the generated method.
|
void |
setReturnType(Class returnType)
Define the return type of the generated method.
|
void |
setReturnTypes(Class[] returnTypes)
Same as
IScriptEvaluator.setReturnType(Class), but for multiple scripts. |
void |
setStaticMethod(boolean staticMethod)
Define whether the generated method should be STATIC or not.
|
void |
setStaticMethod(boolean[] staticMethod)
Same as
IScriptEvaluator.setStaticMethod(boolean), but for multiple scripts. |
void |
setThrownExceptions(Class[] thrownExceptions)
Define the exceptions that the generated method may throw.
|
void |
setThrownExceptions(Class[][] thrownExceptions)
Same as
IScriptEvaluator.setThrownExceptions(Class[]), but for multiple scripts. |
addPackageMemberClassDeclaration, compileToClass, createFastClassBodyEvaluator, createFastClassBodyEvaluator, getClazz, makeCompilationUnit, setClassName, setDefaultImports, setExtendedClass, setExtendedType, setImplementedInterfaces, setImplementedTypesassertNotCooked, classesToTypes, classToType, compileToClassLoader, cook, cook, equals, getClassLoader, hashCode, main, setDebuggingInformation, setParentClassLoader, setParentClassLoader, setUpClassLoaderscook, cook, cook, cook, cook, cook, cook, cookFile, cookFile, cookFile, cookFile, readStringclone, finalize, getClass, notify, notifyAll, toString, wait, wait, waitgetClazz, setClassName, setDefaultImports, setExtendedClass, setExtendedType, setImplementedInterfaces, setImplementedTypesprotected boolean[] optionalStaticMethod
protected Class[] optionalReturnTypes
protected String[] optionalMethodNames
protected String[][] optionalParameterNames
protected Class[][] optionalParameterTypes
protected Class[][] optionalThrownExceptions
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(String optionalFileName, InputStream is, Class returnType, String[] parameterNames, Class[] parameterTypes, Class[] thrownExceptions, 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(String optionalFileName, Reader reader, Class returnType, String[] parameterNames, Class[] parameterTypes, Class[] thrownExceptions, 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, 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, Class optionalExtendedType, Class[] implementedTypes, Class returnType, String[] parameterNames, Class[] parameterTypes, Class[] thrownExceptions, 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, Class optionalExtendedType, Class[] implementedTypes, boolean staticMethod, Class returnType, String methodName, String[] parameterNames, Class[] parameterTypes, Class[] thrownExceptions, 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()
public void setStaticMethod(boolean staticMethod)
IScriptEvaluatortrue.setStaticMethod in interface IScriptEvaluatorpublic void setReturnType(Class returnType)
IScriptEvaluatorvoid.class.setReturnType in interface IScriptEvaluatorpublic void setMethodName(String methodName)
IScriptEvaluatorsetMethodName in interface IScriptEvaluatorpublic void setParameters(String[] parameterNames, Class[] parameterTypes)
IScriptEvaluator
names and types must have the same number of elements.
The parameters can be of primitive type, e.g. double.class.
setParameters in interface IScriptEvaluatorpublic void setThrownExceptions(Class[] thrownExceptions)
IScriptEvaluatorsetThrownExceptions in interface IScriptEvaluatorpublic final void cook(Scanner scanner) throws CompileException, IOException
cook in class ClassBodyEvaluatorCompileExceptionIOExceptionpublic Object evaluate(Object[] arguments) throws InvocationTargetException
IScriptEvaluator
Each argument must have the same type as specified through the parameterTypes parameter of IScriptEvaluator.setParameters(String[], Class[]).
Arguments of primitive type must passed with their wrapper class objects.
The object returned has the class as specified through IScriptEvaluator.setReturnType(Class).
This method is thread-safe.
evaluate in interface IScriptEvaluatorarguments - The actual parameter valuesInvocationTargetExceptionpublic Method getMethod()
IScriptEvaluatorMethod.
This method must only be called after exactly one of the ICookable.cook(String, Reader)
methods was called.
getMethod in interface IScriptEvaluatorpublic void setStaticMethod(boolean[] staticMethod)
IScriptEvaluatorIScriptEvaluator.setStaticMethod(boolean), but for multiple scripts.setStaticMethod in interface IScriptEvaluatorpublic void setReturnTypes(Class[] returnTypes)
IScriptEvaluatorIScriptEvaluator.setReturnType(Class), but for multiple scripts.setReturnTypes in interface IScriptEvaluatorpublic void setMethodNames(String[] methodNames)
IScriptEvaluatorIScriptEvaluator.setMethodName(String), but for multiple scripts.
Define the names of the generated methods. By default the methods have distinct and implementation-specific names.
If two scripts have the same name, then they must have different parameter types
(see IScriptEvaluator.setParameters(String[][], Class[][])).
setMethodNames in interface IScriptEvaluatorpublic void setParameters(String[][] parameterNames, Class[][] parameterTypes)
IScriptEvaluatorIScriptEvaluator.setParameters(String[], Class[]), but for multiple scripts.setParameters in interface IScriptEvaluatorpublic void setThrownExceptions(Class[][] thrownExceptions)
IScriptEvaluatorIScriptEvaluator.setThrownExceptions(Class[]), but for multiple scripts.setThrownExceptions in interface IScriptEvaluatorpublic final void cook(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 preceeding set...() had an array size different from that
of scannersCompileExceptionIOExceptionpublic final void cook(Reader[] readers) throws CompileException, IOException
IScriptEvaluatorICookable.cook(Reader), but for multiple scripts.cook in interface IScriptEvaluatorCompileExceptionIOExceptionpublic final void cook(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(String[] strings) throws CompileException
IScriptEvaluatorICookable.cook(String), but for multiple scripts.cook in interface IScriptEvaluatorCompileExceptionpublic final void cook(String[] optionalFileNames, String[] strings) throws CompileException
IScriptEvaluatorICookable.cook(String, String), but for multiple scripts.cook in interface IScriptEvaluatorCompileExceptionprotected Class getDefaultReturnType()
protected List makeStatements(int idx, Scanner scanner) throws CompileException, IOException
block by parsing statements until EOF and adding
them to the block.CompileExceptionIOExceptionprotected void compileToMethods(Java.CompilationUnit compilationUnit, String[] methodNames, Class[][] parameterTypes) throws CompileException
CompileExceptionprotected Java.MethodDeclarator makeMethodDeclaration(Location location, boolean staticMethod, Class returnType, String methodName, Class[] parameterTypes, String[] parameterNames, Class[] thrownExceptions, List statements)
Java.ClassDeclaration, add
returnType - Return type of the declared methodpublic static Object createFastScriptEvaluator(String script, Class interfaceToImplement, String[] parameterNames) throws CompileException
public static Object createFastScriptEvaluator(Scanner scanner, Class interfaceToImplement, String[] parameterNames, ClassLoader optionalParentClassLoader) throws CompileException, IOException
public static Object createFastScriptEvaluator(Scanner scanner, String className, Class optionalExtendedType, Class interfaceToImplement, String[] parameterNames, ClassLoader optionalParentClassLoader) throws CompileException, IOException
public static Object createFastScriptEvaluator(Scanner scanner, String[] optionalDefaultImports, String className, Class optionalExtendedClass, Class interfaceToImplement, String[] parameterNames, ClassLoader optionalParentClassLoader) throws CompileException, IOException
createFastEvaluator(Scanner,Class,String[]) instead:
ScriptEvaluatorse = newScriptEvaluator(); 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 ClassBodyEvaluatorreader - Source of class body tokensoptionalExtendedType and implements the given
implementedTypespublic Object createFastEvaluator(Reader reader, Class interfaceToImplement, String[] parameterNames) throws CompileException, IOException
IScriptEvaluator
Script evaluation is faster than through IScriptEvaluator.evaluate(Object[]), because it is not done
through reflection but through direct method invocation.
Example:
public interface Foo {
int bar(int a, int b);
}
...
IScriptEvaluator se = CompilerFactoryFactory.getDefaultCompilerFactory().newScriptEvaluator();
// Optionally configure the SE her:
se.setClassName("Bar");
se.setDefaultImports(new String[] { "java.util.*" });
se.setExtendedClass(SomeOtherClass.class);
se.setParentClassLoader(someClassLoader);
Foo f = (Foo) se.createFastScriptEvaluator(
"return a - b;",
Foo.class,
new String[] { "a", "b" }
);
System.out.println("1 - 2 = " + f.bar(1, 2));
All other configuration (implemented type, static method, return type, method name,
parameter names and types, thrown exceptions) are predetermined by the
interfaceToImplement.
Notice: The interfaceToImplement must either be declared public,
or with package scope in the same package as the generated class (see IClassBodyEvaluator.setClassName(String)).createFastEvaluator in interface IScriptEvaluatorreader - Produces the stream of script tokensinterfaceToImplement - Must declare exactly one methodparameterNames - The names of the parameters of that methodCompileExceptionIOExceptionpublic Object createFastEvaluator(String script, Class interfaceToImplement, String[] parameterNames) throws CompileException
createFastEvaluator in interface IScriptEvaluatorscript - Contains the sequence of script tokensCompileExceptionIScriptEvaluator.createFastEvaluator(Reader, Class, String[])public 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)public Object evaluate(int idx, Object[] arguments) throws InvocationTargetException
IScriptEvaluatorIScriptEvaluator.evaluate(Object[]), but for multiple scripts.evaluate in interface IScriptEvaluatorInvocationTargetExceptionpublic Method getMethod(int idx)
IScriptEvaluatorIScriptEvaluator.getMethod(), but for multiple scripts.getMethod in interface IScriptEvaluatorCopyright © 2001–2013. All rights reserved.