public class ExpressionEvaluator extends ScriptEvaluator implements IExpressionEvaluator
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 JVM | Client JVM | |
|---|---|---|
| Normal EE | 23.7 ns | 64.0 ns |
| Fast EE | 31.2 ns | 42.2 ns |
optionalMethodNames, optionalParameterNames, optionalParameterTypes, optionalReturnTypes, optionalStaticMethod, optionalThrownExceptionsclassName, ZERO_CLASSESdebugLines, debugSource, debugVarsANY_TYPEDEFAULT_CLASS_NAMEBOOT_CLASS_LOADER, SYSTEM_PROPERTY_SOURCE_DEBUGGING_DIR, SYSTEM_PROPERTY_SOURCE_DEBUGGING_ENABLE| Constructor and Description |
|---|
ExpressionEvaluator() |
ExpressionEvaluator(Scanner scanner,
String className,
Class optionalExtendedType,
Class[] implementedTypes,
boolean staticMethod,
Class expressionType,
String methodName,
String[] parameterNames,
Class[] parameterTypes,
Class[] thrownExceptions,
ClassLoader optionalParentClassLoader)
Equivalent to
|
ExpressionEvaluator(String expression,
Class expressionType,
String[] parameterNames,
Class[] parameterTypes)
Equivalent to
|
ExpressionEvaluator(String expression,
Class expressionType,
String[] parameterNames,
Class[] parameterTypes,
Class[] thrownExceptions,
Class optionalExtendedType,
Class[] implementedTypes,
ClassLoader optionalParentClassLoader)
Equivalent to
|
ExpressionEvaluator(String expression,
Class expressionType,
String[] parameterNames,
Class[] parameterTypes,
Class[] thrownExceptions,
ClassLoader optionalParentClassLoader)
Equivalent to
|
| Modifier and Type | Method and Description |
|---|---|
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.
|
static Object |
createFastExpressionEvaluator(String expression,
Class interfaceToImplement,
String[] parameterNames,
ClassLoader optionalParentClassLoader)
Deprecated.
|
protected Class |
getDefaultReturnType() |
static String[] |
guessParameterNames(Scanner scanner)
Guess the names of the parameters used in the given expression.
|
protected List |
makeStatements(int idx,
Scanner scanner)
Fill the given
block 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) |
compileToMethods, cook, cook, cook, cook, cook, cook, createFastEvaluator, createFastEvaluator, createFastEvaluator, createFastScriptEvaluator, createFastScriptEvaluator, createFastScriptEvaluator, createFastScriptEvaluator, createInstance, evaluate, evaluate, getMethod, getMethod, makeMethodDeclaration, setMethodName, setMethodNames, setParameters, setParameters, setReturnType, setReturnTypes, setStaticMethod, setStaticMethod, setThrownExceptions, setThrownExceptionsaddPackageMemberClassDeclaration, compileToClass, createFastClassBodyEvaluator, createFastClassBodyEvaluator, getClazz, makeCompilationUnit, setClassName, setDefaultImports, setExtendedClass, setExtendedType, setImplementedInterfaces, setImplementedTypesassertNotCooked, classesToTypes, classToType, compileToClassLoader, cook, cook, equals, getClassLoader, hashCode, main, setDebuggingInformation, setParentClassLoader, setParentClassLoader, setUpClassLoaderscook, cook, cook, cook, cook, cook, cook, cookFile, cookFile, cookFile, cookFile, readStringclone, finalize, getClass, notify, notifyAll, toString, wait, wait, waitcreateFastEvaluator, createFastEvaluator, evaluatecook, cook, cook, cook, evaluate, getMethod, getMethod, setMethodName, setMethodNames, setParameters, setParameters, setReturnType, setReturnTypes, setStaticMethod, setStaticMethod, setThrownExceptions, setThrownExceptionscreateInstance, getClazz, setClassName, setDefaultImports, setExtendedClass, setExtendedType, setImplementedInterfaces, setImplementedTypespublic ExpressionEvaluator(String expression, Class expressionType, String[] parameterNames, Class[] parameterTypes) throws CompileException
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 optionalParentClassLoader) throws CompileException
ExpressionEvaluator ee = new ExpressionEvaluator(); ee.setExpressionType(expressionType); ee.setParameters(parameterNames, parameterTypes); ee.setThrownExceptions(thrownExceptions); ee.setParentClassLoader(optionalParentClassLoader); ee.cook(expression);
public ExpressionEvaluator(String expression, Class expressionType, String[] parameterNames, Class[] parameterTypes, Class[] thrownExceptions, Class optionalExtendedType, Class[] implementedTypes, ClassLoader optionalParentClassLoader) throws CompileException
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);
CompileExceptionExpressionEvaluator(),
setExpressionType(Class),
ScriptEvaluator.setParameters(String[], Class[]),
ScriptEvaluator.setThrownExceptions(Class[]),
ClassBodyEvaluator.setExtendedClass(Class),
ClassBodyEvaluator.setImplementedInterfaces(Class[]),
SimpleCompiler.setParentClassLoader(ClassLoader),
Cookable.cook(String)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
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);
CompileExceptionIOExceptionIExpressionEvaluator,
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)public ExpressionEvaluator()
public void setExpressionType(Class expressionType)
IExpressionEvaluatorIExpressionEvaluator.ANY_TYPE allows the expression
to return any type (primitive or reference).
If expressionType is Void.TYPE, then the expression must be an
invocation of a void method.
Defaults to IExpressionEvaluator.ANY_TYPE.
setExpressionType in interface IExpressionEvaluatorpublic void setExpressionTypes(Class[] expressionTypes)
setExpressionTypes in interface IExpressionEvaluatorprotected Class getDefaultReturnType()
getDefaultReturnType in class ScriptEvaluatorprotected List makeStatements(int idx, Scanner scanner) throws CompileException, IOException
ScriptEvaluatorblock by parsing statements until EOF and adding
them to the block.makeStatements in class ScriptEvaluatorCompileExceptionIOExceptionpublic static Object createFastExpressionEvaluator(String expression, Class interfaceToImplement, String[] parameterNames, ClassLoader optionalParentClassLoader) throws CompileException
ScriptEvaluator.createFastEvaluator(String, Class, String[]) instead:
IExpressionEvaluatoree =CompilerFactoryFactory.getDefaultCompilerFactory().newExpressionEvaluator(); ee.setParentClassLoader(optionalParentClassLoader); return ee.createFastEvaluator(expression, interfaceToImplement, parameterNames);
public static Object createFastExpressionEvaluator(Scanner scanner, String className, Class optionalExtendedType, Class interfaceToImplement, String[] parameterNames, ClassLoader optionalParentClassLoader) throws CompileException, IOException
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[]).public static Object createFastExpressionEvaluator(Scanner scanner, String[] optionalDefaultImports, String className, Class optionalExtendedType, Class interfaceToImplement, String[] parameterNames, ClassLoader optionalParentClassLoader) throws CompileException, IOException
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[]).public static String[] guessParameterNames(Scanner scanner) throws CompileException, IOException
CompileExceptionIOExceptionScanner.Scanner(String, Reader)Copyright © 2001-2012. All Rights Reserved.