Class ScriptEvaluator
- java.lang.Object
-
- org.codehaus.commons.compiler.Cookable
-
- org.codehaus.janino.SimpleCompiler
-
- org.codehaus.janino.ClassBodyEvaluator
-
- org.codehaus.janino.ScriptEvaluator
-
- All Implemented Interfaces:
IClassBodyEvaluator,ICookable,IScriptEvaluator,ISimpleCompiler
- Direct Known Subclasses:
ExpressionEvaluator
public class ScriptEvaluator extends ClassBodyEvaluator implements IScriptEvaluator
A number of "convenience constructors" exist that execute the setup steps instantly. Their use is discouraged.
-
-
Field Summary
Fields Modifier and Type Field Description protected java.lang.String[]optionalMethodNamesprotected boolean[]optionalOverrideMethodWhether methods override a method declared by a supertype;nullmeans "none".protected java.lang.String[][]optionalParameterNamesprotected java.lang.Class[][]optionalParameterTypesprotected java.lang.Class[]optionalReturnTypesThe methods' return types;nullmeans "none".protected boolean[]optionalStaticMethodWhether methods are static;nullmeans "all".protected java.lang.Class[][]optionalThrownExceptions-
Fields inherited from class org.codehaus.janino.ClassBodyEvaluator
className, ZERO_CLASSES
-
Fields inherited from class org.codehaus.janino.SimpleCompiler
debugLines, debugSource, debugVars
-
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 toScriptEvaluator(Scanner scanner, java.lang.Class optionalExtendedType, java.lang.Class[] implementedTypes, java.lang.Class returnType, java.lang.String[] parameterNames, java.lang.Class[] parameterTypes, java.lang.Class[] thrownExceptions, java.lang.ClassLoader optionalParentClassLoader)Equivalent toScriptEvaluator(Scanner scanner, java.lang.Class returnType, java.lang.String[] parameterNames, java.lang.Class[] parameterTypes, java.lang.Class[] thrownExceptions, java.lang.ClassLoader optionalParentClassLoader)Equivalent toScriptEvaluator(Scanner scanner, java.lang.String className, java.lang.Class optionalExtendedType, 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 optionalParentClassLoader)Equivalent to
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected voidcompileToMethods(Java.CompilationUnit compilationUnit, java.lang.String[] methodNames, java.lang.Class[][] parameterTypes)voidcook(java.io.Reader[] readers)Same asICookable.cook(Reader), but for multiple scripts.voidcook(java.lang.String[] strings)Same asICookable.cook(String), but for multiple scripts.voidcook(java.lang.String[] optionalFileNames, 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.voidcook(java.lang.String[] optionalFileNames, java.lang.String[] strings)Same asICookable.cook(String, String), but for multiple scripts.voidcook(Parser[] parsers)voidcook(Scanner scanner)Scans, parses and ompiles a given compilation unit from the given scanner.voidcook(Scanner[] scanners)Likecook(Scanner), but cooks a set of scripts into one class.java.lang.ObjectcreateFastEvaluator(java.io.Reader reader, 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.ObjectcreateFastEvaluator(java.lang.String script, java.lang.Class interfaceToImplement, java.lang.String[] parameterNames)java.lang.ObjectcreateFastEvaluator(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.ObjectcreateFastScriptEvaluator(java.lang.String script, java.lang.Class interfaceToImplement, java.lang.String[] parameterNames)Deprecated.static java.lang.ObjectcreateFastScriptEvaluator(Scanner scanner, java.lang.Class interfaceToImplement, java.lang.String[] parameterNames, java.lang.ClassLoader optionalParentClassLoader)Deprecated.static java.lang.ObjectcreateFastScriptEvaluator(Scanner scanner, java.lang.String[] optionalDefaultImports, java.lang.String className, java.lang.Class optionalExtendedClass, java.lang.Class interfaceToImplement, java.lang.String[] parameterNames, java.lang.ClassLoader optionalParentClassLoader)Deprecated.UsecreateFastEvaluator(Scanner,Class,String[])instead:static java.lang.ObjectcreateFastScriptEvaluator(Scanner scanner, java.lang.String className, java.lang.Class optionalExtendedType, java.lang.Class interfaceToImplement, java.lang.String[] parameterNames, java.lang.ClassLoader optionalParentClassLoader)Deprecated.java.lang.ObjectcreateInstance(java.io.Reader reader)Don't use.java.lang.Objectevaluate(int idx, java.lang.Object[] arguments)Same asIScriptEvaluator.evaluate(Object[]), but for multiple scripts.java.lang.Objectevaluate(java.lang.Object[] arguments)Calls the script with concrete parameter values.protected java.lang.ClassgetDefaultReturnType()java.lang.reflect.MethodgetMethod()Returns the loadedMethod.java.lang.reflect.MethodgetMethod(int idx)Same asIScriptEvaluator.getMethod(), but for multiple scripts.static java.lang.String[]guessParameterNames(Scanner scanner)Guess the names of the parameters used in the given expression.protected Java.MethodDeclaratormakeMethodDeclaration(Location location, Java.Annotation[] annotations, boolean staticMethod, java.lang.Class returnType, java.lang.String methodName, java.lang.Class[] parameterTypes, java.lang.String[] parameterNames, java.lang.Class[] thrownExceptions, java.util.List statements)To the givenJava.ClassDeclaration, add A public method declaration with the given return type, name, parameter names and values and thrown exceptions A blockprotected java.util.ListmakeStatements(int idx, Parser parser)Fill the givenblockby parsing statements until EOF and adding them to the block.voidsetMethodName(java.lang.String methodName)Define the name of the generated method.voidsetMethodNames(java.lang.String[] methodNames)Same asIScriptEvaluator.setMethodName(String), but for multiple scripts.voidsetOverrideMethod(boolean overrideMethod)Defines whether the generated method overrides a methods declared in a supertype.voidsetOverrideMethod(boolean[] overrideMethod)Same asIScriptEvaluator.setOverrideMethod(boolean), but for multiple scripts.voidsetParameters(java.lang.String[][] parameterNames, java.lang.Class[][] parameterTypes)Same asIScriptEvaluator.setParameters(String[], Class[]), but for multiple scripts.voidsetParameters(java.lang.String[] parameterNames, java.lang.Class[] parameterTypes)Define the names and types of the parameters of the generated method.voidsetReturnType(java.lang.Class returnType)Defines the return type of the generated method.voidsetReturnTypes(java.lang.Class[] returnTypes)Defines the return types of the generated methods.voidsetStaticMethod(boolean staticMethod)Define whether the generated method should be STATIC or not.voidsetStaticMethod(boolean[] staticMethod)Same asIScriptEvaluator.setStaticMethod(boolean), but for multiple scripts.voidsetThrownExceptions(java.lang.Class[] thrownExceptions)Define the exceptions that the generated method may throw.voidsetThrownExceptions(java.lang.Class[][] thrownExceptions)Same asIScriptEvaluator.setThrownExceptions(Class[]), but for multiple scripts.-
Methods inherited from class org.codehaus.janino.ClassBodyEvaluator
addPackageMemberClassDeclaration, compileToClass, createFastClassBodyEvaluator, createFastClassBodyEvaluator, getClazz, makeCompilationUnit, setClassName, setDefaultImports, setExtendedClass, setExtendedType, setImplementedInterfaces, setImplementedTypes
-
Methods inherited from class org.codehaus.janino.SimpleCompiler
assertNotCooked, classesToTypes, classToType, compileToClassLoader, cook, cook, equals, getClassLoader, hashCode, main, setDebuggingInformation, setParentClassLoader
-
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, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.codehaus.commons.compiler.IClassBodyEvaluator
getClazz, setClassName, setDefaultImports, setExtendedClass, setExtendedType, setImplementedInterfaces, setImplementedTypes
-
-
-
-
Field Detail
-
optionalOverrideMethod
protected boolean[] optionalOverrideMethod
Whether methods override a method declared by a supertype;nullmeans "none".
-
optionalStaticMethod
protected boolean[] optionalStaticMethod
Whether methods are static;nullmeans "all".
-
optionalReturnTypes
protected java.lang.Class[] optionalReturnTypes
The methods' return types;nullmeans "none".
-
optionalMethodNames
protected java.lang.String[] optionalMethodNames
-
optionalParameterNames
protected java.lang.String[][] optionalParameterNames
-
optionalParameterTypes
protected java.lang.Class[][] optionalParameterTypes
-
optionalThrownExceptions
protected java.lang.Class[][] optionalThrownExceptions
-
-
Constructor Detail
-
ScriptEvaluator
public ScriptEvaluator(java.lang.String script) throws CompileExceptionEquivalent 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 CompileExceptionEquivalent 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 CompileExceptionEquivalent 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 CompileExceptionEquivalent 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.IOExceptionEquivalent toScriptEvaluator se = new ScriptEvaluator(); se.setReturnType(returnType); se.setParameters(parameterNames, parameterTypes); se.setThrownExceptions(thrownExceptions); se.setParentClassLoader(optionalParentClassLoader); se.cook(optionalFileName, is);
- Throws:
CompileExceptionjava.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.IOExceptionEquivalent toScriptEvaluator se = new ScriptEvaluator(); se.setReturnType(returnType); se.setParameters(parameterNames, parameterTypes); se.setThrownExceptions(thrownExceptions); se.setParentClassLoader(optionalParentClassLoader); se.cook(reader);
- Throws:
CompileExceptionjava.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, 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(scanner);
- Throws:
CompileExceptionjava.io.IOException- See Also:
ScriptEvaluator(),setReturnType(Class),setParameters(String[], Class[]),setThrownExceptions(Class[]),SimpleCompiler.setParentClassLoader(ClassLoader),Cookable.cook(Reader)
-
ScriptEvaluator
public ScriptEvaluator(Scanner scanner, java.lang.Class optionalExtendedType, java.lang.Class[] implementedTypes, 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.setExtendedType(optionalExtendedType); se.setImplementedTypes(implementedTypes); se.setReturnType(returnType); se.setParameters(parameterNames, parameterTypes); se.setThrownExceptions(thrownExceptions); se.setParentClassLoader(optionalParentClassLoader); se.cook(scanner);
- Throws:
CompileExceptionjava.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, java.lang.Class optionalExtendedType, 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 optionalParentClassLoader) throws CompileException, java.io.IOException
Equivalent toScriptEvaluator 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);
- Throws:
CompileExceptionjava.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()
-
-
Method Detail
-
setOverrideMethod
public void setOverrideMethod(boolean overrideMethod)
Description copied from interface:IScriptEvaluatorDefines whether the generated method overrides a methods declared in a supertype.- Specified by:
setOverrideMethodin interfaceIScriptEvaluator
-
setStaticMethod
public void setStaticMethod(boolean staticMethod)
Description copied from interface:IScriptEvaluatorDefine whether the generated method should be STATIC or not. Defaults totrue.- Specified by:
setStaticMethodin interfaceIScriptEvaluator
-
setReturnType
public void setReturnType(java.lang.Class returnType)
Description copied from interface:IScriptEvaluatorDefines the return type of the generated method. The meaning of anullvalue is implementation-dependent.- Specified by:
setReturnTypein interfaceIScriptEvaluator
-
setMethodName
public void setMethodName(java.lang.String methodName)
Description copied from interface:IScriptEvaluatorDefine the name of the generated method. Defaults to an unspecified name.- Specified by:
setMethodNamein interfaceIScriptEvaluator
-
setParameters
public void setParameters(java.lang.String[] parameterNames, java.lang.Class[] parameterTypes)Description copied from interface:IScriptEvaluatorDefine the names and types of the parameters of the generated method.namesandtypesmust have the same number of elements.The parameters can be of primitive type, e.g.
double.class.- Specified by:
setParametersin interfaceIScriptEvaluator
-
setThrownExceptions
public void setThrownExceptions(java.lang.Class[] thrownExceptions)
Description copied from interface:IScriptEvaluatorDefine the exceptions that the generated method may throw.- Specified by:
setThrownExceptionsin interfaceIScriptEvaluator
-
cook
public final void cook(Scanner scanner) throws CompileException, java.io.IOException
Description copied from class:SimpleCompilerScans, parses and ompiles a given compilation unit from the given scanner. After completion,SimpleCompiler.getClassLoader()returns aClassLoaderthat allows for access to the compiled classes.- Overrides:
cookin classClassBodyEvaluator- Throws:
CompileExceptionjava.io.IOException
-
evaluate
public java.lang.Object evaluate(java.lang.Object[] arguments) throws java.lang.reflect.InvocationTargetExceptionDescription copied from interface:IScriptEvaluatorCalls the script with concrete parameter values.Each argument must have the same type as specified through the
parameterTypesparameter 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:
evaluatein interfaceIScriptEvaluator- Parameters:
arguments- The actual parameter values- Throws:
java.lang.reflect.InvocationTargetException
-
getMethod
public java.lang.reflect.Method getMethod()
Description copied from interface:IScriptEvaluatorReturns the loadedMethod.This method must only be called after exactly one of the
ICookable.cook(String, Reader)methods was called.- Specified by:
getMethodin interfaceIScriptEvaluator
-
setOverrideMethod
public void setOverrideMethod(boolean[] overrideMethod)
Description copied from interface:IScriptEvaluatorSame asIScriptEvaluator.setOverrideMethod(boolean), but for multiple scripts.- Specified by:
setOverrideMethodin interfaceIScriptEvaluator
-
setStaticMethod
public void setStaticMethod(boolean[] staticMethod)
Description copied from interface:IScriptEvaluatorSame asIScriptEvaluator.setStaticMethod(boolean), but for multiple scripts.- Specified by:
setStaticMethodin interfaceIScriptEvaluator
-
setReturnTypes
public void setReturnTypes(java.lang.Class[] returnTypes)
Defines the return types of the generated methods.- Specified by:
setReturnTypesin interfaceIScriptEvaluator- Parameters:
returnTypes- The methods' return types;nullvalues mean the "default return type", which is the type returned bygetDefaultReturnType()(void.classforScriptEvaluatorandObject.classforExpressionEvaluator)- See Also:
getDefaultReturnType(),ExpressionEvaluator.getDefaultReturnType()
-
setMethodNames
public void setMethodNames(java.lang.String[] methodNames)
Description copied from interface:IScriptEvaluatorSame 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:
setMethodNamesin interfaceIScriptEvaluator
-
setParameters
public void setParameters(java.lang.String[][] parameterNames, java.lang.Class[][] parameterTypes)Description copied from interface:IScriptEvaluatorSame asIScriptEvaluator.setParameters(String[], Class[]), but for multiple scripts.- Specified by:
setParametersin interfaceIScriptEvaluator
-
setThrownExceptions
public void setThrownExceptions(java.lang.Class[][] thrownExceptions)
Description copied from interface:IScriptEvaluatorSame asIScriptEvaluator.setThrownExceptions(Class[]), but for multiple scripts.- Specified by:
setThrownExceptionsin interfaceIScriptEvaluator
-
cook
public final void cook(Scanner[] scanners) throws CompileException, java.io.IOException
Likecook(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 theoptionalFileNameargument 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 preceedingset...()had an array size different from that ofscannersCompileExceptionjava.io.IOException
-
cook
public final void cook(Parser[] parsers) throws CompileException, java.io.IOException
- Throws:
CompileExceptionjava.io.IOException
-
cook
public final void cook(java.io.Reader[] readers) throws CompileException, java.io.IOExceptionDescription copied from interface:IScriptEvaluatorSame asICookable.cook(Reader), but for multiple scripts.- Specified by:
cookin interfaceIScriptEvaluator- Throws:
CompileExceptionjava.io.IOException
-
cook
public final void cook(java.lang.String[] optionalFileNames, java.io.Reader[] readers) throws CompileException, java.io.IOExceptionOn 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:
cookin interfaceIScriptEvaluator- Throws:
CompileExceptionjava.io.IOException
-
cook
public final void cook(java.lang.String[] strings) throws CompileExceptionDescription copied from interface:IScriptEvaluatorSame asICookable.cook(String), but for multiple scripts.- Specified by:
cookin interfaceIScriptEvaluator- Throws:
CompileException
-
cook
public final void cook(java.lang.String[] optionalFileNames, java.lang.String[] strings) throws CompileExceptionDescription copied from interface:IScriptEvaluatorSame asICookable.cook(String, String), but for multiple scripts.- Specified by:
cookin interfaceIScriptEvaluator- Throws:
CompileException
-
getDefaultReturnType
protected java.lang.Class getDefaultReturnType()
- Returns:
void.class- See Also:
setReturnTypes(Class[])
-
makeStatements
protected java.util.List makeStatements(int idx, Parser parser) throws CompileException, java.io.IOExceptionFill the givenblockby parsing statements until EOF and adding them to the block.- Throws:
CompileExceptionjava.io.IOException
-
compileToMethods
protected void compileToMethods(Java.CompilationUnit compilationUnit, java.lang.String[] methodNames, java.lang.Class[][] parameterTypes) throws CompileException
- Throws:
CompileException
-
makeMethodDeclaration
protected Java.MethodDeclarator makeMethodDeclaration(Location location, Java.Annotation[] annotations, boolean staticMethod, java.lang.Class returnType, java.lang.String methodName, java.lang.Class[] parameterTypes, java.lang.String[] parameterNames, java.lang.Class[] thrownExceptions, java.util.List statements)
To the givenJava.ClassDeclaration, add- A public method declaration with the given return type, name, parameter names and values and thrown exceptions
- A block
- Parameters:
annotations- TODOreturnType- Return type of the declared method
-
createFastScriptEvaluator
@Deprecated public static java.lang.Object createFastScriptEvaluator(java.lang.String script, java.lang.Class interfaceToImplement, java.lang.String[] parameterNames) throws CompileExceptionDeprecated.- Throws:
CompileException
-
createFastScriptEvaluator
@Deprecated public static java.lang.Object createFastScriptEvaluator(Scanner scanner, java.lang.Class interfaceToImplement, java.lang.String[] parameterNames, java.lang.ClassLoader optionalParentClassLoader) throws CompileException, java.io.IOException
Deprecated.- Throws:
CompileExceptionjava.io.IOException
-
createFastScriptEvaluator
@Deprecated public static java.lang.Object createFastScriptEvaluator(Scanner scanner, java.lang.String className, java.lang.Class optionalExtendedType, java.lang.Class interfaceToImplement, java.lang.String[] parameterNames, java.lang.ClassLoader optionalParentClassLoader) throws CompileException, java.io.IOException
Deprecated.- Throws:
CompileExceptionjava.io.IOException
-
createFastScriptEvaluator
@Deprecated public static java.lang.Object createFastScriptEvaluator(Scanner scanner, java.lang.String[] optionalDefaultImports, java.lang.String className, java.lang.Class optionalExtendedClass, java.lang.Class interfaceToImplement, java.lang.String[] parameterNames, java.lang.ClassLoader optionalParentClassLoader) throws CompileException, java.io.IOException
Deprecated.UsecreateFastEvaluator(Scanner,Class,String[])instead:ScriptEvaluatorse = newScriptEvaluator(); se.setDefaultImports.(optionalDefaultImports); se.setClassName.(className); se.setExtendedClass.(optionalExtendedClass); se.setParentClassLoader(optionalParentClassLoader); return se.createFastEvaluator(scanner, interfaceToImplement, parameterNames);- Throws:
CompileExceptionjava.io.IOException
-
createInstance
public final java.lang.Object createInstance(java.io.Reader reader)
Don't use.- Specified by:
createInstancein interfaceIClassBodyEvaluator- Overrides:
createInstancein classClassBodyEvaluator- Parameters:
reader- Source of class body tokens- Returns:
- An object that extends the
optionalExtendedTypeand implements the givenimplementedTypes
-
createFastEvaluator
public java.lang.Object createFastEvaluator(java.io.Reader reader, java.lang.Class interfaceToImplement, java.lang.String[] parameterNames) throws CompileException, java.io.IOExceptionDescription copied from interface:IScriptEvaluatorIf 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: TheinterfaceToImplementmust either be declaredpublic, or with package scope in the same package as the generated class (seeIClassBodyEvaluator.setClassName(String)).- Specified by:
createFastEvaluatorin 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:
CompileExceptionjava.io.IOException
-
createFastEvaluator
public java.lang.Object createFastEvaluator(java.lang.String script, java.lang.Class interfaceToImplement, java.lang.String[] parameterNames) throws CompileException- Specified by:
createFastEvaluatorin interfaceIScriptEvaluator- Parameters:
script- Contains the sequence of script tokens- Throws:
CompileException- See Also:
IScriptEvaluator.createFastEvaluator(Reader, Class, String[])
-
createFastEvaluator
public java.lang.Object 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:
CompileExceptionjava.io.IOException- See Also:
createFastEvaluator(Reader, Class, String[])
-
guessParameterNames
public static java.lang.String[] guessParameterNames(Scanner scanner) throws CompileException, java.io.IOException
Guess 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:
CompileExceptionjava.io.IOException- See Also:
Scanner(String, Reader)
-
evaluate
public java.lang.Object evaluate(int idx, java.lang.Object[] arguments) throws java.lang.reflect.InvocationTargetExceptionDescription copied from interface:IScriptEvaluatorSame asIScriptEvaluator.evaluate(Object[]), but for multiple scripts.- Specified by:
evaluatein interfaceIScriptEvaluator- Throws:
java.lang.reflect.InvocationTargetException
-
getMethod
public java.lang.reflect.Method getMethod(int idx)
Description copied from interface:IScriptEvaluatorSame asIScriptEvaluator.getMethod(), but for multiple scripts.- Specified by:
getMethodin interfaceIScriptEvaluator
-
-