janino.net

org.codehaus.janino
Class ExpressionEvaluator

  extended by org.codehaus.commons.compiler.Cookable
      extended by org.codehaus.janino.SimpleCompiler
          extended by org.codehaus.janino.ClassBodyEvaluator
              extended by org.codehaus.janino.ScriptEvaluator
                  extended by org.codehaus.janino.ExpressionEvaluator
All Implemented Interfaces:
IClassBodyEvaluator, ICookable, IExpressionEvaluator, IScriptEvaluator, ISimpleCompiler

public class ExpressionEvaluator
extends ScriptEvaluator
implements IExpressionEvaluator

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 parameter and return types of the expression are known at compile time, then a "fast" expression evaluator can be instantiated through createFastExpressionEvaluator(String, Class, String[], ClassLoader). Expression evaluation is faster than through ScriptEvaluator.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
(How can it be that interface method invocation is slower than reflection for the server JVM?)


Field Summary
 
Fields inherited from class org.codehaus.janino.ScriptEvaluator
optionalOverrideMethod, optionalReturnTypes, optionalStaticMethod
 
Fields inherited from interface org.codehaus.commons.compiler.IExpressionEvaluator
ANY_TYPE
 
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
ExpressionEvaluator()
           
ExpressionEvaluator(Scanner scanner,  className,  optionalExtendedType, [] implementedTypes, boolean staticMethod,  expressionType,  methodName, [] parameterNames, [] parameterTypes, [] thrownExceptions,  optionalParentClassLoader)
          Equivalent to ExpressionEvaluator ee = new ExpressionEvaluator(); ee.setClassName(className); ee.setExtendedType(optionalExtendedType); ee.setImplementedTypes(implementedTypes); ee.setStaticMethod(staticMethod); ee.setExpressionType(expressionType); ee.setMethodName(methodName); ee.setParameters(parameterNames, parameterTypes); ee.setThrownExceptions(thrownExceptions); ee.setParentClassLoader(optionalParentClassLoader); ee.cook(scanner);
ExpressionEvaluator( expression,  expressionType, [] parameterNames, [] parameterTypes)
          Equivalent to ExpressionEvaluator ee = new ExpressionEvaluator(); ee.setExpressionType(expressionType); ee.setParameters(parameterNames, parameterTypes); ee.cook(expression);
ExpressionEvaluator( expression,  expressionType, [] parameterNames, [] parameterTypes, [] thrownExceptions,  optionalExtendedType, [] implementedTypes,  optionalParentClassLoader)
          Equivalent to ExpressionEvaluator ee = new ExpressionEvaluator(); ee.setExpressionType(expressionType); ee.setParameters(parameterNames, parameterTypes); ee.setThrownExceptions(thrownExceptions); ee.setExtendedType(optionalExtendedType); ee.setImplementedTypes(implementedTypes); ee.setParentClassLoader(optionalParentClassLoader); ee.cook(expression);
ExpressionEvaluator( expression,  expressionType, [] parameterNames, [] parameterTypes, [] thrownExceptions,  optionalParentClassLoader)
          Equivalent to ExpressionEvaluator ee = new ExpressionEvaluator(); ee.setExpressionType(expressionType); ee.setParameters(parameterNames, parameterTypes); ee.setThrownExceptions(thrownExceptions); ee.setParentClassLoader(optionalParentClassLoader); ee.cook(expression);
 
Method Summary
static  createFastExpressionEvaluator(Scanner scanner, [] optionalDefaultImports,  className,  optionalExtendedType,  interfaceToImplement, [] parameterNames,  optionalParentClassLoader)
          Deprecated. Use ScriptEvaluator.createFastEvaluator(Reader, Class, String[]) instead
static  createFastExpressionEvaluator(Scanner scanner,  className,  optionalExtendedType,  interfaceToImplement, [] parameterNames,  optionalParentClassLoader)
          Deprecated. Use ScriptEvaluator.createFastEvaluator(Reader, Class, String[]) instead
static  createFastExpressionEvaluator( expression,  interfaceToImplement, [] parameterNames,  optionalParentClassLoader)
          Deprecated. Use ScriptEvaluator.createFastEvaluator(String, Class, String[]) instead:
protected   getDefaultReturnType()
           
static [] guessParameterNames(Scanner scanner)
          Guess the names of the parameters used in the given expression.
protected  <Java.BlockStatement> makeStatements(int idx, Parser parser)
          Fills the given block by parsing statements until EOF and adding them to the block.
 void setExpressionType( expressionType)
          Define the type of the expression.
 void setExpressionTypes([] expressionTypes)
          Same as IExpressionEvaluator.setExpressionType(Class), but for multiple expressions.
 void setReturnType( returnType)
          Deprecated. setExpressionType(Class) should be called instead.
 void setReturnTypes([] returnTypes)
          Deprecated. setExpressionTypes(Class[]) should be called instead.
 
Methods inherited from class org.codehaus.janino.ScriptEvaluator
cook, cook, cook, cook, cook, cook, cook, createFastEvaluator, createFastEvaluator, createFastEvaluator, createFastScriptEvaluator, createFastScriptEvaluator, createFastScriptEvaluator, createFastScriptEvaluator, createInstance, evaluate, evaluate, getMethod, getMethod, makeMethodDeclaration, setMethodName, setMethodNames, setOverrideMethod, setOverrideMethod, setParameters, setParameters, setStaticMethod, setStaticMethod, setThrownExceptions, setThrownExceptions
 
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, setCompileErrorHandler, setDebuggingInformation, 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.
, , , , , , , ,
 
Methods inherited from interface org.codehaus.commons.compiler.IExpressionEvaluator
createFastEvaluator, createFastEvaluator, evaluate
 
Methods inherited from interface org.codehaus.commons.compiler.IScriptEvaluator
cook, cook, cook, cook, evaluate, getMethod, getMethod, setMethodName, setMethodNames, setOverrideMethod, setOverrideMethod, setParameters, setParameters, setStaticMethod, setStaticMethod, setThrownExceptions, setThrownExceptions
 
Methods inherited from interface org.codehaus.commons.compiler.IClassBodyEvaluator
createInstance, getClazz, setClassName, setDefaultImports, setExtendedClass, setExtendedType, setImplementedInterfaces, setImplementedTypes
 
Methods inherited from interface org.codehaus.commons.compiler.ICookable
cook, cook, cook, cook, cook, cook, cook, cook, cookFile, cookFile, cookFile, cookFile, setCompileErrorHandler, setDebuggingInformation, setParentClassLoader, setWarningHandler
 

Constructor Detail

ExpressionEvaluator

public ExpressionEvaluator( expression,
                            expressionType,
                           [] parameterNames,
                           [] parameterTypes)
                    throws CompileException
Equivalent to
 ExpressionEvaluator ee = new ExpressionEvaluator();
 ee.setExpressionType(expressionType);
 ee.setParameters(parameterNames, parameterTypes);
 ee.cook(expression);

Throws:
CompileException
See Also:
ExpressionEvaluator(), setExpressionType(Class), ScriptEvaluator.setParameters(String[], Class[]), Cookable.cook(String)

ExpressionEvaluator

public ExpressionEvaluator( expression,
                            expressionType,
                           [] parameterNames,
                           [] parameterTypes,
                           [] thrownExceptions,
                            optionalParentClassLoader)
                    throws CompileException
Equivalent to
 ExpressionEvaluator ee = new ExpressionEvaluator();
 ee.setExpressionType(expressionType);
 ee.setParameters(parameterNames, parameterTypes);
 ee.setThrownExceptions(thrownExceptions);
 ee.setParentClassLoader(optionalParentClassLoader);
 ee.cook(expression);

Throws:
CompileException
See Also:
ExpressionEvaluator(), setExpressionType(Class), ScriptEvaluator.setParameters(String[], Class[]), ScriptEvaluator.setThrownExceptions(Class[]), SimpleCompiler.setParentClassLoader(ClassLoader), Cookable.cook(String)

ExpressionEvaluator

public ExpressionEvaluator( expression,
                            expressionType,
                           [] parameterNames,
                           [] parameterTypes,
                           [] thrownExceptions,
                            optionalExtendedType,
                           [] implementedTypes,
                            optionalParentClassLoader)
                    throws CompileException
Equivalent to
 ExpressionEvaluator ee = new ExpressionEvaluator();
 ee.setExpressionType(expressionType);
 ee.setParameters(parameterNames, parameterTypes);
 ee.setThrownExceptions(thrownExceptions);
 ee.setExtendedType(optionalExtendedType);
 ee.setImplementedTypes(implementedTypes);
 ee.setParentClassLoader(optionalParentClassLoader);
 ee.cook(expression);

Throws:
CompileException
See Also:
ExpressionEvaluator(), setExpressionType(Class), ScriptEvaluator.setParameters(String[], Class[]), ScriptEvaluator.setThrownExceptions(Class[]), ClassBodyEvaluator.setExtendedClass(Class), ClassBodyEvaluator.setImplementedInterfaces(Class[]), SimpleCompiler.setParentClassLoader(ClassLoader), Cookable.cook(String)

ExpressionEvaluator

public ExpressionEvaluator(Scanner scanner,
                            className,
                            optionalExtendedType,
                           [] implementedTypes,
                           boolean staticMethod,
                            expressionType,
                            methodName,
                           [] parameterNames,
                           [] parameterTypes,
                           [] thrownExceptions,
                            optionalParentClassLoader)
                    throws CompileException,
                           
Equivalent to
 ExpressionEvaluator ee = new ExpressionEvaluator();
 ee.setClassName(className);
 ee.setExtendedType(optionalExtendedType);
 ee.setImplementedTypes(implementedTypes);
 ee.setStaticMethod(staticMethod);
 ee.setExpressionType(expressionType);
 ee.setMethodName(methodName);
 ee.setParameters(parameterNames, parameterTypes);
 ee.setThrownExceptions(thrownExceptions);
 ee.setParentClassLoader(optionalParentClassLoader);
 ee.cook(scanner);

Throws:
CompileException
See Also:
IExpressionEvaluator, IClassBodyEvaluator.setClassName(String), IClassBodyEvaluator.setExtendedClass(Class), IClassBodyEvaluator.setImplementedInterfaces(Class[]), IScriptEvaluator.setStaticMethod(boolean), IExpressionEvaluator.setExpressionType(Class), IScriptEvaluator.setMethodName(String), IScriptEvaluator.setParameters(String[], Class[]), IScriptEvaluator.setThrownExceptions(Class[]), ICookable.setParentClassLoader(ClassLoader), ICookable.cook(Reader)

ExpressionEvaluator

public ExpressionEvaluator()
Method Detail

setExpressionType

public void setExpressionType( expressionType)
Description copied from interface: IExpressionEvaluator
Define the type of the expression. The special type IExpressionEvaluator.ANY_TYPE allows the expression to return any type (primitive or reference).

If expressionType is , then the expression must be an invocation of a void method.

Defaults to IExpressionEvaluator.ANY_TYPE.

Specified by:
setExpressionType in interface IExpressionEvaluator

setExpressionTypes

public void setExpressionTypes([] expressionTypes)
Description copied from interface: IExpressionEvaluator
Same as IExpressionEvaluator.setExpressionType(Class), but for multiple expressions.

Specified by:
setExpressionTypes in interface IExpressionEvaluator

setReturnType


public final void setReturnType( returnType)
Deprecated. setExpressionType(Class) should be called instead.

Description copied from class: ScriptEvaluator
Defines the return types of the generated methods.

Specified by:
setReturnType in interface IExpressionEvaluator
Specified by:
setReturnType in interface IScriptEvaluator
Overrides:
setReturnType in class ScriptEvaluator
Parameters:
returnType - The method's return type; null means the "default return type", which is the type returned by ScriptEvaluator.getDefaultReturnType() (void.class for ScriptEvaluator and Object.class for ExpressionEvaluator)
See Also:
ScriptEvaluator.getDefaultReturnType(), getDefaultReturnType()

setReturnTypes


public final void setReturnTypes([] returnTypes)
Deprecated. setExpressionTypes(Class[]) should be called instead.

Description copied from class: ScriptEvaluator
Defines the return types of the generated methods.

Specified by:
setReturnTypes in interface IExpressionEvaluator
Specified by:
setReturnTypes in interface IScriptEvaluator
Overrides:
setReturnTypes in class ScriptEvaluator
Parameters:
returnTypes - The methods' return types; null elements mean the "default return type", which is the type returned by ScriptEvaluator.getDefaultReturnType() (void.class for ScriptEvaluator and Object.class for ExpressionEvaluator)
See Also:
ScriptEvaluator.getDefaultReturnType(), getDefaultReturnType()

getDefaultReturnType

protected  getDefaultReturnType()
Overrides:
getDefaultReturnType in class ScriptEvaluator
Returns:
void.class
See Also:
ScriptEvaluator.setReturnTypes(Class[])

makeStatements

protected <Java.BlockStatement> makeStatements(int idx,
                                                   Parser parser)
                                            throws CompileException,
                                                   
Description copied from class: ScriptEvaluator
Fills the given block by parsing statements until EOF and adding them to the block.

Overrides:
makeStatements in class ScriptEvaluator
Throws:
CompileException

createFastExpressionEvaluator


public static  createFastExpressionEvaluator( expression,
                                                               interfaceToImplement,
                                                              [] parameterNames,
                                                               optionalParentClassLoader)
                                            throws CompileException
Deprecated. Use ScriptEvaluator.createFastEvaluator(String, Class, String[]) instead:

 IExpressionEvaluator ee = CompilerFactoryFactory.getDefaultCompilerFactory().newExpressionEvaluator();
 ee.setParentClassLoader(optionalParentClassLoader);
 return ee.createFastEvaluator(expression, interfaceToImplement, parameterNames);
 

Throws:
CompileException

createFastExpressionEvaluator


public static  createFastExpressionEvaluator(Scanner scanner,
                                                               className,
                                                               optionalExtendedType,
                                                               interfaceToImplement,
                                                              [] parameterNames,
                                                               optionalParentClassLoader)
                                            throws CompileException,
                                                   
Deprecated. Use ScriptEvaluator.createFastEvaluator(Reader, Class, String[]) instead

Notice: This method is not declared in IExpressionEvaluator, and is hence only available in this implementation of org.codehaus.commons.compiler. To be independent from this particular implementation, try to switch to ScriptEvaluator.createFastEvaluator(Reader, Class, String[]).

Throws:
CompileException

createFastExpressionEvaluator


public static  createFastExpressionEvaluator(Scanner scanner,
                                                              [] optionalDefaultImports,
                                                               className,
                                                               optionalExtendedType,
                                                               interfaceToImplement,
                                                              [] parameterNames,
                                                               optionalParentClassLoader)
                                            throws CompileException,
                                                   
Deprecated. Use ScriptEvaluator.createFastEvaluator(Reader, Class, String[]) instead

Notice: This method is not declared in IExpressionEvaluator, and is hence only available in this implementation of org.codehaus.commons.compiler. To be independent from this particular implementation, try to switch to ScriptEvaluator.createFastEvaluator(Reader, Class, String[]).

Throws:
CompileException

guessParameterNames

public static [] guessParameterNames(Scanner scanner)
                                    throws CompileException,
                                           
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 first components of the ambiguous name.

Throws:
CompileException
See Also:
Scanner.Scanner(String, Reader)

janino.net