Class ExpressionEvaluator
- All Implemented Interfaces:
IClassBodyEvaluator
,ICookable
,IExpressionEvaluator
,IScriptEvaluator
,ISimpleCompiler
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 expressionNotice: 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 JVM | Client JVM | |
---|---|---|
Normal EE | 23.7 ns | 64.0 ns |
Fast EE | 31.2 ns | 42.2 ns |
-
Field Summary
Fields inherited from class org.codehaus.janino.ScriptEvaluator
optionalMethodNames, optionalOverrideMethod, optionalParameterNames, optionalParameterTypes, optionalReturnTypes, optionalStaticMethod, 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
Fields inherited from interface org.codehaus.commons.compiler.IExpressionEvaluator
ANY_TYPE
-
Constructor Summary
ConstructorsConstructorDescriptionExpressionEvaluator
(String expression, Class expressionType, String[] parameterNames, Class[] parameterTypes) Equivalent toExpressionEvaluator
(String expression, Class expressionType, String[] parameterNames, Class[] parameterTypes, Class[] thrownExceptions, ClassLoader optionalParentClassLoader) Equivalent toExpressionEvaluator
(String expression, Class expressionType, String[] parameterNames, Class[] parameterTypes, Class[] thrownExceptions, Class optionalExtendedType, Class[] implementedTypes, ClassLoader optionalParentClassLoader) Equivalent toExpressionEvaluator
(Scanner scanner, String className, Class optionalExtendedType, Class[] implementedTypes, boolean staticMethod, Class expressionType, String methodName, String[] parameterNames, Class[] parameterTypes, Class[] thrownExceptions, ClassLoader optionalParentClassLoader) Equivalent to -
Method Summary
Modifier and TypeMethodDescriptionstatic Object
createFastExpressionEvaluator
(String expression, Class interfaceToImplement, String[] parameterNames, ClassLoader optionalParentClassLoader) Deprecated.static Object
createFastExpressionEvaluator
(Scanner scanner, String[] optionalDefaultImports, String className, Class optionalExtendedType, Class interfaceToImplement, String[] parameterNames, ClassLoader optionalParentClassLoader) Deprecated.static Object
createFastExpressionEvaluator
(Scanner scanner, String className, Class optionalExtendedType, Class interfaceToImplement, String[] parameterNames, ClassLoader optionalParentClassLoader) Deprecated.protected Class
static String[]
guessParameterNames
(Scanner scanner) Guess the names of the parameters used in the given expression.protected List
makeStatements
(int idx, Parser parser) Fill the givenblock
by parsing statements until EOF and adding them to the block.void
setExpressionType
(Class expressionType) Define the type of the expression.void
setExpressionTypes
(Class[] expressionTypes) Same asIExpressionEvaluator.setExpressionType(Class)
, but for multiple expressions.final void
setReturnType
(Class returnType) Deprecated.final void
setReturnTypes
(Class[] returnTypes) Deprecated.Methods inherited from class org.codehaus.janino.ScriptEvaluator
compileToMethods, 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, 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
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, setDebuggingInformation, setParentClassLoader
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
-
Constructor Details
-
ExpressionEvaluator
public ExpressionEvaluator(String expression, Class expressionType, String[] parameterNames, Class[] parameterTypes) throws CompileException Equivalent toExpressionEvaluator ee = new ExpressionEvaluator(); ee.setExpressionType(expressionType); ee.setParameters(parameterNames, parameterTypes); ee.cook(expression);
- Throws:
CompileException
- See Also:
-
ExpressionEvaluator
public ExpressionEvaluator(String expression, Class expressionType, String[] parameterNames, Class[] parameterTypes, Class[] thrownExceptions, ClassLoader optionalParentClassLoader) throws CompileException Equivalent toExpressionEvaluator ee = new ExpressionEvaluator(); ee.setExpressionType(expressionType); ee.setParameters(parameterNames, parameterTypes); ee.setThrownExceptions(thrownExceptions); ee.setParentClassLoader(optionalParentClassLoader); ee.cook(expression);
- Throws:
CompileException
- See Also:
-
ExpressionEvaluator
public ExpressionEvaluator(String expression, Class expressionType, String[] parameterNames, Class[] parameterTypes, Class[] thrownExceptions, Class optionalExtendedType, Class[] implementedTypes, ClassLoader optionalParentClassLoader) throws CompileException Equivalent toExpressionEvaluator 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
public ExpressionEvaluator(Scanner scanner, String className, Class optionalExtendedType, Class[] implementedTypes, boolean staticMethod, Class expressionType, String methodName, String[] parameterNames, Class[] parameterTypes, Class[] thrownExceptions, ClassLoader optionalParentClassLoader) throws CompileException, IOException Equivalent toExpressionEvaluator 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
IOException
- See Also:
-
ExpressionEvaluator
public ExpressionEvaluator()
-
-
Method Details
-
setExpressionType
Description copied from interface:IExpressionEvaluator
Define the type of the expression. The special typeIExpressionEvaluator.ANY_TYPE
allows the expression to return any type (primitive or reference).If
expressionType
isVoid.TYPE
, then the expression must be an invocation of avoid
method.Defaults to
IExpressionEvaluator.ANY_TYPE
.- Specified by:
setExpressionType
in interfaceIExpressionEvaluator
-
setExpressionTypes
Description copied from interface:IExpressionEvaluator
Same asIExpressionEvaluator.setExpressionType(Class)
, but for multiple expressions.- Specified by:
setExpressionTypes
in interfaceIExpressionEvaluator
-
setReturnType
Deprecated.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 interfaceIExpressionEvaluator
- Specified by:
setReturnType
in interfaceIScriptEvaluator
- Overrides:
setReturnType
in classScriptEvaluator
-
setReturnTypes
Deprecated.Description copied from class:ScriptEvaluator
Defines the return types of the generated methods.- Specified by:
setReturnTypes
in interfaceIExpressionEvaluator
- Specified by:
setReturnTypes
in interfaceIScriptEvaluator
- Overrides:
setReturnTypes
in classScriptEvaluator
- Parameters:
returnTypes
- The methods' return types;null
values mean the "default return type", which is the type returned byScriptEvaluator.getDefaultReturnType()
(void.class
forScriptEvaluator
andObject.class
forExpressionEvaluator
)- See Also:
-
getDefaultReturnType
- Overrides:
getDefaultReturnType
in classScriptEvaluator
- Returns:
void.class
- See Also:
-
makeStatements
Description copied from class:ScriptEvaluator
Fill the givenblock
by parsing statements until EOF and adding them to the block.- Overrides:
makeStatements
in classScriptEvaluator
- Throws:
CompileException
IOException
-
createFastExpressionEvaluator
@Deprecated public static Object createFastExpressionEvaluator(String expression, Class interfaceToImplement, String[] parameterNames, ClassLoader optionalParentClassLoader) throws CompileException Deprecated.IExpressionEvaluator
ee =CompilerFactoryFactory
.getDefaultCompilerFactory
().newExpressionEvaluator
(); ee.setParentClassLoader(optionalParentClassLoader); return ee.createFastEvaluator
(expression, interfaceToImplement, parameterNames);- Throws:
CompileException
-
createFastExpressionEvaluator
@Deprecated public static Object createFastExpressionEvaluator(Scanner scanner, String className, Class optionalExtendedType, Class interfaceToImplement, String[] parameterNames, ClassLoader optionalParentClassLoader) throws CompileException, IOException Deprecated.Notice: This method is not declared inIExpressionEvaluator
, and is hence only available in this implementation oforg.codehaus.commons.compiler
. To be independent from this particular implementation, try to switch toScriptEvaluator.createFastEvaluator(Reader, Class, String[])
.- Throws:
CompileException
IOException
-
createFastExpressionEvaluator
@Deprecated public static Object createFastExpressionEvaluator(Scanner scanner, String[] optionalDefaultImports, String className, Class optionalExtendedType, Class interfaceToImplement, String[] parameterNames, ClassLoader optionalParentClassLoader) throws CompileException, IOException Deprecated.Notice: This method is not declared inIExpressionEvaluator
, and is hence only available in this implementation oforg.codehaus.commons.compiler
. To be independent from this particular implementation, try to switch toScriptEvaluator.createFastEvaluator(Reader, Class, String[])
.- Throws:
CompileException
IOException
-
guessParameterNames
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.- If any component starts with an upper-case letter, then ambiguous name is assumed to be a type name.
- Otherwise, it is assumed to be a parameter name.
- Throws:
CompileException
IOException
- See Also:
-
ScriptEvaluator.createFastEvaluator(String, Class, String[])
instead: