Top Description Inners Fields Constructors Methods
org.codehaus.janino

public Class ScriptEvaluator

extends MultiCookable
implements IScriptEvaluator
Class Inheritance
All Implemented Interfaces
org.codehaus.commons.compiler.IScriptEvaluator, org.codehaus.commons.compiler.IMultiCookable, org.codehaus.commons.compiler.ICookable
Imports
java.io.IOException, .InputStream, .Reader, .StringReader, java.lang.reflect.Array, .InvocationTargetException, .Method, java.util.ArrayList, .Arrays, .Collection, .EnumSet, .HashMap, .HashSet, .List, .Map, .Set, org.codehaus.commons.compiler.CompileException, .Cookable, .ErrorHandler, .IScriptEvaluator, .InternalCompilerException, .Location, .MultiCookable, .WarningHandler, org.codehaus.commons.nullanalysis.Nullable, org.codehaus.janino.Java.AbstractClassDeclaration, .Java.AbstractCompilationUnit.ImportDeclaration, .Java.Atom, .Java.BlockStatement, .Java.CompilationUnit, .Java.ExpressionStatement, .Java.LocalClassDeclaration, .Java.LocalClassDeclarationStatement, .Java.LocalVariableDeclarationStatement, .Java.MethodDeclarator, .Java.Modifier, .Java.Primitive, .Java.PrimitiveType, .Java.Type, .Java.VariableDeclarator, .Parser.ClassDeclarationContext, .Parser.MethodDeclarationContext, org.codehaus.janino.util.AbstractTraverser

An implementation of IScriptEvaluator that utilizes the JANINO Java compiler.

This implementation implements the concept of "Local methods", i.e. statements may be freely intermixed with method declarations. These methods are typically called by the "main code" of the script evaluator. One limitation exists: When cooking multiple scripts in one ScriptEvaluator, then local method signatures (names and parameter types) must not collide between scripts.

A plethora of "convenience constructors" exist that execute the setup steps instantly. Their use is discouraged, in favor of using the default constructor, plus calling a number of setters, and then one of the cook() methods.

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv class
ScriptEvaluator.Script

Represents one script that this ScriptEvaluator declares.

Field Summary

Modifier and TypeField and Description
private final ClassBodyEvaluator
private Class<?>
private Method[]
private ScriptEvaluator.Script[]
scripts

The scripts to compile.

private int
private WarningHandler

Constructor Summary

AccessConstructor and Description
public
ScriptEvaluator(String script)

Equivalent to

    ScriptEvaluator se = new ScriptEvaluator();
    se.cook(script);
public
ScriptEvaluator(String script, Class<?> returnType)

Equivalent to

    ScriptEvaluator se = new ScriptEvaluator();
    se.setReturnType(returnType);
    se.cook(script);
public
ScriptEvaluator(String script, Class<?> returnType, String[] parameterNames, Class<?>[] parameterTypes)

Equivalent to

    ScriptEvaluator se = new ScriptEvaluator();
    se.setReturnType(returnType);
    se.setParameters(parameterNames, parameterTypes);
    se.cook(script);
public
ScriptEvaluator(String script, Class<?> returnType, String[] parameterNames, Class<?>[] parameterTypes, Class<?>[] thrownExceptions)

Equivalent to

    ScriptEvaluator se = new ScriptEvaluator();
    se.setReturnType(returnType);
    se.setParameters(parameterNames, parameterTypes);
    se.setThrownExceptions(thrownExceptions);
    se.cook(script);
public
ScriptEvaluator(String fileName, InputStream is, Class<?> returnType, String[] parameterNames, Class<?>[] parameterTypes, Class<?>[] thrownExceptions, ClassLoader parentClassLoader)

Equivalent to

    ScriptEvaluator se = new ScriptEvaluator();
    se.setReturnType(returnType);
    se.setParameters(parameterNames, parameterTypes);
    se.setThrownExceptions(thrownExceptions);
    se.setParentClassLoader(parentClassLoader);
    se.cook(fileName, is);
public
ScriptEvaluator(String fileName, Reader reader, Class<?> returnType, String[] parameterNames, Class<?>[] parameterTypes, Class<?>[] thrownExceptions, ClassLoader parentClassLoader)

Equivalent to

    ScriptEvaluator se = new ScriptEvaluator();
    se.setReturnType(returnType);
    se.setParameters(parameterNames, parameterTypes);
    se.setThrownExceptions(thrownExceptions);
    se.setParentClassLoader(parentClassLoader);
    se.cook(reader);
public
ScriptEvaluator(Scanner scanner, Class<?> returnType, String[] parameterNames, Class<?>[] parameterTypes, Class<?>[] thrownExceptions, ClassLoader parentClassLoader)

Equivalent to

    ScriptEvaluator se = new ScriptEvaluator();
    se.setReturnType(returnType);
    se.setParameters(parameterNames, parameterTypes);
    se.setThrownExceptions(thrownExceptions);
    se.setParentClassLoader(parentClassLoader);
    se.cook(scanner);
public
ScriptEvaluator(Scanner scanner, Class<?> extendedType, Class<?>[] implementedTypes, Class<?> returnType, String[] parameterNames, Class<?>[] parameterTypes, Class<?>[] thrownExceptions, ClassLoader parentClassLoader)

Equivalent to

    ScriptEvaluator se = new ScriptEvaluator();
    se.setExtendedType(extendedType);
    se.setImplementedTypes(implementedTypes);
    se.setReturnType(returnType);
    se.setParameters(parameterNames, parameterTypes);
    se.setThrownExceptions(thrownExceptions);
    se.setParentClassLoader(parentClassLoader);
    se.cook(scanner);
public
ScriptEvaluator(Scanner scanner, String className, Class<?> extendedType, Class<?>[] implementedTypes, boolean staticMethod, Class<?> returnType, String methodName, String[] parameterNames, Class<?>[] parameterTypes, Class<?>[] thrownExceptions, ClassLoader parentClassLoader)

Equivalent to

    ScriptEvaluator se = new ScriptEvaluator();
    se.setClassName(className);
    se.setExtendedType(extendedType);
    se.setImplementedTypes(implementedTypes);
    se.setStaticMethod(staticMethod);
    se.setReturnType(returnType);
    se.setMethodName(methodName);
    se.setParameters(parameterNames, parameterTypes);
    se.setThrownExceptions(thrownExceptions);
    se.setParentClassLoader(parentClassLoader);
    se.cook(scanner);
public
ScriptEvaluator()

Constructs a script evaluator with all the default settings.

public
ScriptEvaluator(int count)

Constructs a script evaluator with the given number of scripts.

Method Summary

Modifier and TypeMethod and Description
private static <T> T[]

Returns:

A copy of the followingElements, prepended with the firstElement
cat
(T firstElement, T[] followingElements, Class<T> componentType)

protected Java.Type[]
classesToTypes(Location location, Class<?>[] classes)

protected Java.Type
classToType(Location loc, Class<?> clazz)

public void
cook(String
Used when reporting errors and warnings
fileName
,
Reader reader)

Implements abstract org.codehaus.commons.compiler.Cookable.cook.

Implements org.codehaus.commons.compiler.ICookable.cook.

Reads, scans, parses and compiles Java tokens from the given Reader.
public final void
cook(String[] fileNames, Reader[] readers)

Implements abstract org.codehaus.commons.compiler.MultiCookable.cook.

Implements org.codehaus.commons.compiler.IScriptEvaluator.cook, org.codehaus.commons.compiler.IMultiCookable.cook.

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.
public final void
cook(Scanner... scanners)

Cooks a set of scripts into one class.

public final void
cook(Parser[] parsers)

pack-priv void
cook(String fileName, Java.AbstractCompilationUnit.ImportDeclaration[] importDeclarations, Java.BlockStatement[][] statementss, Java.MethodDeclarator[][] localMethodss)

public final void
cook(Java.CompilationUnit compilationUnit, Collection<Java.MethodDeclarator> methodDeclarators)

protected void
cook(Java.CompilationUnit compilationUnit)

Compiles the given compilationUnit, defines it into a ClassLoader, loads the generated class, gets the script methods from that class, and makes them available through getMethod(int).

private void
cook2(Parser[] parsers)

public <T> T
createFastEvaluator(Reader
Produces the stream of script tokens
reader
,
Class<T>
Must declare exactly one method
interfaceToImplement
,
String[]
The names of the parameters of that method
parameterNames
)

Implements org.codehaus.commons.compiler.IScriptEvaluator.createFastEvaluator.

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.
public <T> T
createFastEvaluator(String
Contains the sequence of script tokens
script
,
Class<T> interfaceToImplement, String[] parameterNames)

Implements org.codehaus.commons.compiler.IScriptEvaluator.createFastEvaluator.

public <T> T
createFastEvaluator(Scanner
Source of tokens to read
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.

public static Object
createFastScriptEvaluator(String script, Class<?> interfaceToImplement, String[] parameterNames)

public static Object
createFastScriptEvaluator(Scanner scanner, Class<?> interfaceToImplement, String[] parameterNames, ClassLoader parentClassLoader)

public static Object
createFastScriptEvaluator(Scanner scanner, String className, Class<?> extendedType, Class<?> interfaceToImplement, String[] parameterNames, ClassLoader parentClassLoader)

public static Object
createFastScriptEvaluator(Scanner scanner, String[] defaultImports, String className, Class<?> extendedClass, Class<?> interfaceToImplement, String[] parameterNames, ClassLoader parentClassLoader)

    ScriptEvaluator se = new ScriptEvaluator();
    se.
public Object
public Object
evaluate(Object[]
The actual parameter values
arguments
)

Implements org.codehaus.commons.compiler.IScriptEvaluator.evaluate.

Calls the script with concrete parameter values.
public Object
evaluate(int idx, Object[] arguments)

Implements org.codehaus.commons.compiler.IScriptEvaluator.evaluate.

Same as evaluate(Object[]), but for multiple scripts.
public Map<String, byte[]>
public Class<?>
public String[]
public Class<?>
public Method
public Method
getMethod(int idx)

Implements org.codehaus.commons.compiler.IScriptEvaluator.getMethod.

Same as getMethod(), but for multiple scripts.
pack-priv Method[]

Returns:

The generated methods
getMethods
()

public Method[]
protected final Class<?>

Returns:

The return type of the indexed script; null means "use the default return type"
getReturnType
(int index)

private ScriptEvaluator.Script
getScript(int index)

public static String[]
guessParameterNames(Scanner scanner)

Guesses the names of the parameters used in the given expression.

private static String[]
private Java.MethodDeclarator
makeMethodDeclaration(Location location, Java.Annotation[] annotations, boolean staticMethod, Class<?> returnType, String methodName, Class<?>[] parameterTypes, String[] parameterNames, Class<?>[] thrownExceptions, Java.BlockStatement[] statements)

protected void
makeStatements(int idx, Parser parser, List<Java.BlockStatement>
Is filled with the generated statements
resultStatements
,
List<Java.MethodDeclarator>
Is filled with any local methods that the script declares
resultMethods
)

Parses statements from the parser until end-of-input.

private static Object
methodKey(String methodName, Class<?>[] parameterTypes)

protected Java.Type
public EnumSet<JaninoOption>

Returns:

A reference to the currently effective compilation options; changes to it take effect immediately
options
()

public ScriptEvaluator
options(EnumSet<JaninoOption> options)

Sets the options for all future compilations.

pack-priv Java.AbstractCompilationUnit.ImportDeclaration[]
private static void
parseScriptStatement(Parser parser, List<Java.BlockStatement> mainStatements, List<Java.MethodDeclarator>
Filled with the methods that the script declares
localMethods
)

  ScriptStatement :=
    Statement                                               (1)
    | 'class' ...
public void
public void
setCompileErrorHandler(ErrorHandler
null to restore the default behavior (throwing a CompileException)
compileErrorHandler
)

Implements org.codehaus.commons.compiler.IScriptEvaluator.setCompileErrorHandler.

Installs an ErrorHandler which is invoked during compilation on each error.
public void
setDebuggingInformation(boolean debugSource, boolean debugLines, boolean debugVars)

Implements org.codehaus.commons.compiler.IScriptEvaluator.setDebuggingInformation.

Determines what kind of debugging information is included in the generates classes.
public void
public void
setDefaultReturnType(Class<?> defaultReturnType)

Implements org.codehaus.commons.compiler.IScriptEvaluator.setDefaultReturnType.

When this IScriptEvaluator is coooked, then the defaultReturnType applies to all scripts for which no explicit return type was configured.
public void
public void
public void
setMethodName(String
null means reset to default name
methodName
)

Implements org.codehaus.commons.compiler.IScriptEvaluator.setMethodName.

Defines the name of the generated method.
public void
public void
setOverrideMethod(boolean overrideMethod)

Implements org.codehaus.commons.compiler.IScriptEvaluator.setOverrideMethod.

Defines whether the generated method overrides a methods declared in a supertype.
public void
setOverrideMethod(boolean[] overrideMethod)

Implements org.codehaus.commons.compiler.IScriptEvaluator.setOverrideMethod.

Same as setOverrideMethod(boolean), but for multiple scripts.
public void
setParameters(String[] parameterNames, Class<?>[] parameterTypes)

Implements org.codehaus.commons.compiler.IScriptEvaluator.setParameters.

Defines the names and types of the parameters of the generated method.
public void
setParameters(String[][] parameterNames, Class<?>[][] parameterTypes)

Implements org.codehaus.commons.compiler.IScriptEvaluator.setParameters.

Same as setParameters(String[], Class[]), but for multiple scripts.
public void
setParentClassLoader(ClassLoader parentClassLoader)

Implements org.codehaus.commons.compiler.IScriptEvaluator.setParentClassLoader.

The "parent class loader" is used to load referenced classes.
public void
setReturnType(Class<?> returnType)

Implements org.codehaus.commons.compiler.IScriptEvaluator.setReturnType.

Defines the return type of the generated method.
public void
setReturnTypes(Class<?>[]
The methods' return types; null elements mean "use the default return type"
returnTypes
)

Implements org.codehaus.commons.compiler.IScriptEvaluator.setReturnTypes.

Configures the return types of the generated methods.
public void
setScriptCount(int count)

public void
setSourceVersion(int version)

Implements org.codehaus.commons.compiler.ICookable.setSourceVersion.

Specifies the version of source code accepted, in analogy with JAVAC's -source command line option.
public void
setStaticMethod(boolean staticMethod)

Implements org.codehaus.commons.compiler.IScriptEvaluator.setStaticMethod.

Defines whether the generated method should be STATIC or not.
public void
setStaticMethod(boolean[] staticMethod)

Implements org.codehaus.commons.compiler.IScriptEvaluator.setStaticMethod.

Same as setStaticMethod(boolean), but for multiple scripts.
public void
setTargetVersion(int version)

Implements org.codehaus.commons.compiler.ICookable.setTargetVersion.

Generates class files that target a specified release of the virtual machine, in analogy with JAVAC's -target command line option.
public void
setThrownExceptions(Class<?>[] thrownExceptions)

Implements org.codehaus.commons.compiler.IScriptEvaluator.setThrownExceptions.

Defines the exceptions that the generated method may throw.
public void
public void
setWarningHandler(WarningHandler
null to indicate that no warnings be issued
warningHandler
)

Implements org.codehaus.commons.compiler.IScriptEvaluator.setWarningHandler.

By default, warnings are discarded, but an application my install a custom WarningHandler.
Inherited from org.codehaus.commons.compiler.MultiCookable:
cookcookcookcookcookcookcookcookFilescookFilescookFilescookFiles