Top Description Fields Constructors Methods
org.codehaus.janino

public Class ExpressionEvaluator

extends MultiCookable
implements IExpressionEvaluator
Class Inheritance
All Implemented Interfaces
org.codehaus.commons.compiler.IExpressionEvaluator, org.codehaus.commons.compiler.IMultiCookable, org.codehaus.commons.compiler.ICookable
Imports
java.io.IOException, .Reader, .StringReader, java.lang.reflect.InvocationTargetException, .Method, java.util.EnumSet, .HashSet, .Map, .Set, org.codehaus.commons.compiler.CompileException, .Cookable, .ErrorHandler, .IClassBodyEvaluator, .ICookable, .IExpressionEvaluator, .IScriptEvaluator, .ISimpleCompiler, .InternalCompilerException, .Location, .MultiCookable, .WarningHandler, org.codehaus.commons.nullanalysis.Nullable, org.codehaus.janino.util.AbstractTraverser

This IExpressionEvaluator is implemented by creating and compiling a temporary compilation unit defining one class with one static method with one RETURN statement.

A number of "convenience constructors" exist that execute the set-up steps described for IExpressionEvaluator instantly.

If the expression type and the parameters' types are known at compile time, then a "fast" expression evaluator can be instantiated through createFastEvaluator(String, Class, String...). Expression evaluation is faster than through evaluate(Object[]), because it is not done through reflection but through direct method invocation.

Example:

    public interface Foo {
        int bar(int a, int b);
    }
    ...
    Foo f = (Foo) ExpressionEvaluator.createFastExpressionEvaluator(
        "a + b",                    // expression to evaluate
        Foo.class,                  // interface that describes the expression's signature
        new String[] { "a", "b" },  // the parameters' names
        (ClassLoader) null          // Use current thread's context class loader
    );
    System.out.println("1 + 2 = " + f.bar(1, 2)); // Evaluate the expression

Notice: The interfaceToImplement must either be declared public, or with package scope in the root package (i.e. "no" package).

On my system (Intel P4, 2 GHz, MS Windows XP, JDK 1.4.1), expression "x + 1" evaluates as follows:

Server JVMClient JVM
Normal EE23.7 ns64.0 ns
Fast EE31.2 ns42.2 ns

Field Summary

Modifier and TypeField and Description
private final ScriptEvaluator
private int
private WarningHandler

Constructor Summary

AccessConstructor and Description
public
ExpressionEvaluator(String expression, Class<?> expressionType, String[] parameterNames, Class<?>[] parameterTypes)

Equivalent to

    ExpressionEvaluator ee = new ExpressionEvaluator();
    ee.setExpressionType(expressionType);
    ee.setParameters(parameterNames, parameterTypes);
    ee.cook(expression);
public
ExpressionEvaluator(String expression, Class<?> expressionType, String[] parameterNames, Class<?>[] parameterTypes, Class<?>[] thrownExceptions, ClassLoader parentClassLoader)

Equivalent to

    ExpressionEvaluator ee = new ExpressionEvaluator();
    ee.setExpressionType(expressionType);
    ee.setParameters(parameterNames, parameterTypes);
    ee.setThrownExceptions(thrownExceptions);
    ee.setParentClassLoader(parentClassLoader);
    ee.cook(expression);
public
ExpressionEvaluator(String expression, Class<?> expressionType, String[] parameterNames, Class<?>[] parameterTypes, Class<?>[] thrownExceptions, Class<?> extendedClass, Class<?>[] implementedTypes, ClassLoader parentClassLoader)

Equivalent to

    ExpressionEvaluator ee = new ExpressionEvaluator();
    ee.setExpressionType(expressionType);
    ee.setParameters(parameterNames, parameterTypes);
    ee.setThrownExceptions(thrownExceptions);
    ee.setExtendedClass(extendedClass);
    ee.setImplementedTypes(implementedTypes);
    ee.setParentClassLoader(parentClassLoader);
    ee.cook(expression);
public
ExpressionEvaluator(Scanner scanner, String className, Class<?> extendedType, Class<?>[] implementedTypes, boolean staticMethod, Class<?> expressionType, String methodName, String[] parameterNames, Class<?>[] parameterTypes, Class<?>[] thrownExceptions, ClassLoader parentClassLoader)

Creates an expression evaluator with the full configurability.

public

Method Summary

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

Converts an array of Classes into an array ofJava.Types.

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

Wraps a reflection Class in a Java.Type object.

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 void
cook(String[] fileNames, Reader[] readers)

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

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

Same as Cookable#cook(String, Reader), but cooks a set of documents into one class.
public final void
cook(Scanner scanner)

public final void
cook(Scanner... scanners)

Like cook(Scanner), but cooks a set of scripts into one class.

public final void
cook(Parser... parsers)

private void
cook2(Parser... parsers)

public <T> T
createFastEvaluator(Reader reader, Class<? extends T> interfaceToImplement, String... parameterNames)

Implements org.codehaus.commons.compiler.IExpressionEvaluator.createFastEvaluator.

public <T> T
createFastEvaluator(String script, Class<? extends T> interfaceToImplement, String... parameterNames)

Implements org.codehaus.commons.compiler.IExpressionEvaluator.createFastEvaluator.

If the parameter and return types of the expression are known at compile time, then a "fast" expression evaluator can be instantiated through createFastEvaluator(String, Class, String[]).
public <T> T
createFastEvaluator(Scanner
Source of tokens to read
scanner
,
Class<T> 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
createFastExpressionEvaluator(String expression, Class<?> interfaceToImplement, String[] parameterNames, ClassLoader parentClassLoader)

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

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

public Object
public Object
evaluate(Object[]
The actual parameter values
arguments
)

Implements org.codehaus.commons.compiler.IExpressionEvaluator.evaluate.

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

Implements org.codehaus.commons.compiler.IExpressionEvaluator.evaluate.

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

Implements org.codehaus.commons.compiler.IExpressionEvaluator.getMethod.

Same as getMethod(), but for multiple expressions.
public Method[]
public static String[]
guessParameterNames(Scanner scanner)

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

private static String[]
protected Java.Type
optionalClassToType(final Location location, final Class<?> clazz)

Wraps a reflection Class in a Java.Type object.

public EnumSet<JaninoOption>

Returns:

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

public ExpressionEvaluator
options(EnumSet<JaninoOption> options)

Sets the options for all future compilations.

public void
setClassName(String className)

Implements org.codehaus.commons.compiler.IExpressionEvaluator.setClassName.

Sets the name of the generated class.
public void
setCompileErrorHandler(ErrorHandler
null to restore the default behavior (throwing a CompileException)
compileErrorHandler
)

Implements org.codehaus.commons.compiler.IExpressionEvaluator.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.IExpressionEvaluator.setDebuggingInformation.

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

Implements org.codehaus.commons.compiler.IExpressionEvaluator.setDefaultExpressionType.

Reconfigures the "default expression type"; if no expression type is configured for an expression, then, when cooking this IExpressionEvaluator, the "default expression type" is used for the expression
public void
setDefaultImports(String... defaultImports)

Implements org.codehaus.commons.compiler.IExpressionEvaluator.setDefaultImports.

"Default imports" add to the system import "java.lang", i.e. the evaluator may refer to classes imported by default imports without having to explicitly declare IMPORT statements.
public void
setExpressionType(Class<?> expressionType)

Implements org.codehaus.commons.compiler.IExpressionEvaluator.setExpressionType.

Defines the type of the expression.
public void
setExpressionTypes(Class<?>[] expressionTypes)

Implements org.codehaus.commons.compiler.IExpressionEvaluator.setExpressionTypes.

Configures the types of the expressions.
public void
setExtendedClass(Class<?> extendedType)

Implements org.codehaus.commons.compiler.IExpressionEvaluator.setExtendedClass.

Sets a particular superclass that the generated class will extend.
public void
setImplementedInterfaces(Class<?>[] implementedTypes)

Implements org.codehaus.commons.compiler.IExpressionEvaluator.setImplementedInterfaces.

Configures the interfaces that the generated class implements.
public void
setMethodName(String
null means reset to default name
methodName
)

Implements org.codehaus.commons.compiler.IExpressionEvaluator.setMethodName.

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

Implements org.codehaus.commons.compiler.IExpressionEvaluator.setOverrideMethod.

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

Implements org.codehaus.commons.compiler.IExpressionEvaluator.setOverrideMethod.

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

Implements org.codehaus.commons.compiler.IExpressionEvaluator.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.IExpressionEvaluator.setParameters.

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

Implements org.codehaus.commons.compiler.IExpressionEvaluator.setParentClassLoader.

The "parent class loader" is used to load referenced classes.
public void
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.IExpressionEvaluator.setStaticMethod.

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

Implements org.codehaus.commons.compiler.IExpressionEvaluator.setStaticMethod.

Same as setStaticMethod(boolean), but for multiple expressions.
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.IExpressionEvaluator.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.IExpressionEvaluator.setWarningHandler.

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