Class ScriptEvaluator
- java.lang.Object
-
- org.codehaus.commons.compiler.Cookable
-
- org.codehaus.commons.compiler.jdk.SimpleCompiler
-
- org.codehaus.commons.compiler.jdk.ClassBodyEvaluator
-
- org.codehaus.commons.compiler.jdk.ScriptEvaluator
-
- All Implemented Interfaces:
IClassBodyEvaluator
,ICookable
,IScriptEvaluator
,ISimpleCompiler
- Direct Known Subclasses:
ExpressionEvaluator
public class ScriptEvaluator extends ClassBodyEvaluator implements IScriptEvaluator
To set up aScriptEvaluator
object, proceed as described forIScriptEvaluator
. Alternatively, a number of "convenience constructors" exist that execute the described steps instantly.Alternatively, a number of "convenience constructors" exist that execute the steps described above instantly. Their use is discouraged.
Notice that this implementation of
IClassBodyEvaluator
is prone to "Java injection", i.e. an application could get more than one class body compiled by passing a bogus input document.Also notice that the parsing of leading IMPORT declarations is heuristic and has certain limitations; see
ClassBodyEvaluator.parseImportDeclarations(Reader)
.
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.String[]
optionalMethodNames
protected boolean[]
optionalOverrideMethod
Whether methods override a method declared by a supertype;null
means "none".private java.lang.String[][]
optionalParameterNames
private java.lang.Class<?>[][]
optionalParameterTypes
private java.lang.Class<?>[]
optionalReturnTypes
protected boolean[]
optionalStaticMethod
Whether methods are static;null
means "all".private java.lang.Class<?>[][]
optionalThrownExceptions
private java.lang.reflect.Method[]
result
null=uncooked-
Fields inherited from interface org.codehaus.commons.compiler.IClassBodyEvaluator
DEFAULT_CLASS_NAME
-
Fields inherited from interface org.codehaus.commons.compiler.ICookable
BOOT_CLASS_LOADER, SYSTEM_PROPERTY_SOURCE_DEBUGGING_DIR, SYSTEM_PROPERTY_SOURCE_DEBUGGING_ENABLE
-
-
Constructor Summary
Constructors Constructor Description ScriptEvaluator()
ScriptEvaluator(java.lang.String script)
Equivalent toScriptEvaluator(java.lang.String optionalFileName, java.io.InputStream is, java.lang.Class<?> returnType, java.lang.String[] parameterNames, java.lang.Class<?>[] parameterTypes, java.lang.Class<?>[] thrownExceptions, java.lang.ClassLoader optionalParentClassLoader)
Equivalent toScriptEvaluator(java.lang.String optionalFileName, java.io.Reader reader, java.lang.Class<?> returnType, java.lang.String[] parameterNames, java.lang.Class<?>[] parameterTypes, java.lang.Class<?>[] thrownExceptions, java.lang.ClassLoader optionalParentClassLoader)
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 to
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
cook(java.io.Reader[] readers)
Same asICookable.cook(Reader)
, but for multiple scripts.void
cook(java.lang.String[] strings)
Same asICookable.cook(String)
, but for multiple scripts.void
cook(java.lang.String[] optionalFileNames, java.io.Reader[] readers)
Same asICookable.cook(String, Reader)
, but cooks a set of scripts into one class.protected void
cook(java.lang.String[] optionalFileNames, java.io.Reader[] readers, java.lang.String[] imports)
void
cook(java.lang.String[] optionalFileNames, java.lang.String[] strings)
Same asICookable.cook(String, String)
, but for multiple scripts.void
cook(java.lang.String optionalFileName, java.io.Reader r)
Reads, scans, parses and compiles Java tokens from the givenReader
.java.lang.Object
createFastEvaluator(java.io.Reader r, java.lang.Class 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.java.lang.Object
createFastEvaluator(java.lang.String script, java.lang.Class interfaceToImplement, java.lang.String[] parameterNames)
java.lang.Object
createInstance(java.io.Reader reader)
Don't use.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.protected java.lang.Class<?>
getDefaultReturnType()
The default return type of a script isvoid
.java.lang.reflect.Method
getMethod()
Returns the loadedMethod
.java.lang.reflect.Method
getMethod(int idx)
Same asIScriptEvaluator.getMethod()
, but for multiple scripts.void
setMethodName(java.lang.String methodName)
Define 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[][] names, java.lang.Class[][] types)
Same asIScriptEvaluator.setParameters(String[], Class[])
, but for multiple scripts.void
setParameters(java.lang.String[] names, java.lang.Class[] types)
Define the names and types of the parameters of the generated method.void
setReturnType(java.lang.Class returnType)
Defines the return type of the generated method.void
setReturnTypes(java.lang.Class[] returnTypes)
Defines the return types of the generated methods.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
setThrownExceptions(java.lang.Class[] thrownExceptions)
Define the exceptions that the generated method may throw.void
setThrownExceptions(java.lang.Class[][] thrownExceptions)
Same asIScriptEvaluator.setThrownExceptions(Class[])
, but for multiple scripts.-
Methods inherited from class org.codehaus.commons.compiler.jdk.ClassBodyEvaluator
cook, getClazz, parseImportDeclarations, setClassName, setDefaultImports, setExtendedClass, setExtendedType, setImplementedInterfaces, setImplementedTypes
-
Methods inherited from class org.codehaus.commons.compiler.jdk.SimpleCompiler
assertCooked, assertNotCooked, getClassLoader, setCompileErrorHandler, setDebuggingInformation, setParentClassLoader, setParentClassLoader, setWarningHandler
-
Methods inherited from class org.codehaus.commons.compiler.Cookable
cook, cook, cook, cook, cook, cook, cook, cookFile, cookFile, cookFile, cookFile, readString
-
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.IClassBodyEvaluator
getClazz, setClassName, setDefaultImports, setExtendedClass, setExtendedType, setImplementedInterfaces, setImplementedTypes
-
Methods inherited from interface org.codehaus.commons.compiler.ICookable
cook, cook, cook, cook, cook, cook, cook, cookFile, cookFile, cookFile, cookFile, setCompileErrorHandler, setDebuggingInformation, setParentClassLoader, setWarningHandler
-
-
-
-
Field Detail
-
optionalOverrideMethod
protected boolean[] optionalOverrideMethod
Whether methods override a method declared by a supertype;null
means "none".
-
optionalStaticMethod
protected boolean[] optionalStaticMethod
Whether methods are static;null
means "all".
-
optionalReturnTypes
private java.lang.Class<?>[] optionalReturnTypes
-
optionalMethodNames
private java.lang.String[] optionalMethodNames
-
optionalParameterNames
private java.lang.String[][] optionalParameterNames
-
optionalParameterTypes
private java.lang.Class<?>[][] optionalParameterTypes
-
optionalThrownExceptions
private java.lang.Class<?>[][] optionalThrownExceptions
-
result
private java.lang.reflect.Method[] result
null=uncooked
-
-
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(java.lang.String optionalFileName, java.io.InputStream is, java.lang.Class<?> returnType, java.lang.String[] parameterNames, java.lang.Class<?>[] parameterTypes, java.lang.Class<?>[] thrownExceptions, java.lang.ClassLoader optionalParentClassLoader) throws CompileException, java.io.IOException
Equivalent toScriptEvaluator se = new ScriptEvaluator(); se.setReturnType(returnType); se.setParameters(parameterNames, parameterTypes); se.setThrownExceptions(thrownExceptions); se.setParentClassLoader(optionalParentClassLoader); se.cook(optionalFileName, 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(java.lang.String optionalFileName, java.io.Reader reader, java.lang.Class<?> returnType, java.lang.String[] parameterNames, java.lang.Class<?>[] parameterTypes, java.lang.Class<?>[] thrownExceptions, java.lang.ClassLoader optionalParentClassLoader) throws CompileException, java.io.IOException
Equivalent toScriptEvaluator se = new ScriptEvaluator(); se.setReturnType(returnType); se.setParameters(parameterNames, parameterTypes); se.setThrownExceptions(thrownExceptions); se.setParentClassLoader(optionalParentClassLoader); 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()
-
-
Method Detail
-
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(java.lang.Class returnType)
Description copied from interface:IScriptEvaluator
Defines the return type of the generated method. The meaning of anull
value is implementation-dependent.- Specified by:
setReturnType
in interfaceIScriptEvaluator
-
setMethodName
public void setMethodName(java.lang.String methodName)
Description copied from interface:IScriptEvaluator
Define the name of the generated method. Defaults to an unspecified name.- Specified by:
setMethodName
in interfaceIScriptEvaluator
-
setParameters
public void setParameters(java.lang.String[] names, java.lang.Class[] types)
Description copied from interface:IScriptEvaluator
Define the names and types of the parameters of the generated method.names
andtypes
must have the same number of elements.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
Define the exceptions that the generated method may throw.- Specified by:
setThrownExceptions
in interfaceIScriptEvaluator
-
cook
public void cook(java.lang.String optionalFileName, java.io.Reader r) 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
- Overrides:
cook
in classClassBodyEvaluator
- Parameters:
optionalFileName
- Used when reporting errors and warnings.- Throws:
CompileException
java.io.IOException
- See Also:
ICookable.cook(String, Reader)
-
evaluate
public java.lang.Object evaluate(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.
- Specified by:
evaluate
in interfaceIScriptEvaluator
- Parameters:
arguments
- The actual parameter values- Throws:
java.lang.reflect.InvocationTargetException
-
getMethod
public java.lang.reflect.Method getMethod()
Description copied from interface:IScriptEvaluator
Returns the loadedMethod
.This method must only be called after exactly one of the
ICookable.cook(String, Reader)
methods was called.- Specified by:
getMethod
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
Defines the return types of the generated methods. The meaning ofnull
elements is implementation-dependent.- Specified by:
setReturnTypes
in interfaceIScriptEvaluator
-
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[][] names, java.lang.Class[][] types)
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 final void cook(java.io.Reader[] readers) throws CompileException, java.io.IOException
Description copied from interface:IScriptEvaluator
Same asICookable.cook(Reader)
, but for multiple scripts.- Specified by:
cook
in interfaceIScriptEvaluator
- Throws:
CompileException
java.io.IOException
-
cook
public void cook(java.lang.String[] optionalFileNames, java.io.Reader[] readers) throws CompileException, java.io.IOException
Description copied from interface:IScriptEvaluator
Same asICookable.cook(String, Reader)
, 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 theoptionalFileNames
parameter to distinguish between the individual token sources.If and only if the number of scanners is one, then that single script may contain leading IMPORT directives.
- Specified by:
cook
in interfaceIScriptEvaluator
- Throws:
CompileException
java.io.IOException
-
cook
public final void cook(java.lang.String[] strings) throws CompileException
Description copied from interface:IScriptEvaluator
Same asICookable.cook(String)
, but for multiple scripts.- Specified by:
cook
in interfaceIScriptEvaluator
- Throws:
CompileException
-
cook
public void cook(java.lang.String[] optionalFileNames, java.lang.String[] strings) throws CompileException
Description copied from interface:IScriptEvaluator
Same asICookable.cook(String, String)
, but for multiple scripts.- Specified by:
cook
in interfaceIScriptEvaluator
- Throws:
CompileException
-
cook
protected final void cook(java.lang.String[] optionalFileNames, java.io.Reader[] readers, java.lang.String[] imports) throws CompileException, java.io.IOException
- Parameters:
readers
- The scripts to cook- Throws:
CompileException
java.io.IOException
-
getDefaultReturnType
protected java.lang.Class<?> getDefaultReturnType()
The default return type of a script isvoid
.
-
createFastEvaluator
public java.lang.Object createFastEvaluator(java.lang.String script, java.lang.Class interfaceToImplement, java.lang.String[] parameterNames) throws CompileException
- Specified by:
createFastEvaluator
in interfaceIScriptEvaluator
- Parameters:
script
- Contains the sequence of script tokens- Throws:
CompileException
- See Also:
createFastEvaluator(String, Class, String[])
-
createInstance
public final java.lang.Object createInstance(java.io.Reader reader)
Don't use.- Specified by:
createInstance
in interfaceIClassBodyEvaluator
- Overrides:
createInstance
in classClassBodyEvaluator
- Parameters:
reader
- Source of class body tokens- Returns:
- An object that extends the
optionalExtendedType
and implements the givenimplementedTypes
-
createFastEvaluator
public java.lang.Object createFastEvaluator(java.io.Reader r, java.lang.Class 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 =
All other configuration (implemented type, static method, return type, method name, parameter names and types, thrown exceptions) are predetermined by theCompilerFactoryFactory
.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));interfaceToImplement
. Notice: TheinterfaceToImplement
must either be declaredpublic
, or with package scope in the same package as the generated class (seeIClassBodyEvaluator.setClassName(String)
).- Specified by:
createFastEvaluator
in interfaceIScriptEvaluator
- Parameters:
r
- 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
-
evaluate
public java.lang.Object evaluate(int idx, 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(int idx)
Description copied from interface:IScriptEvaluator
Same asIScriptEvaluator.getMethod()
, but for multiple scripts.- Specified by:
getMethod
in interfaceIScriptEvaluator
-
-