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 ScriptEvaluator.createFastEvaluator(String, Class, String[]). 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, optionalOverrideMethod, optionalParameterNames, optionalParameterTypes, optionalReturnTypes, optionalStaticMethod, optionalThrownExceptionsANY_TYPEDEFAULT_CLASS_NAMEBOOT_CLASS_LOADER, SYSTEM_PROPERTY_SOURCE_DEBUGGING_DIR, SYSTEM_PROPERTY_SOURCE_DEBUGGING_ENABLE| Constructor and Description |
|---|
ExpressionEvaluator() |
ExpressionEvaluator(java.lang.String expression,
java.lang.Class<?> expressionType,
java.lang.String[] parameterNames,
java.lang.Class<?>[] parameterTypes)
Equivalent to
|
ExpressionEvaluator(java.lang.String expression,
java.lang.Class<?> expressionType,
java.lang.String[] parameterNames,
java.lang.Class<?>[] parameterTypes,
java.lang.Class<?>[] thrownExceptions,
java.lang.Class<?> optionalExtendedType,
java.lang.Class<?>[] implementedTypes,
java.lang.ClassLoader optionalParentClassLoader)
Equivalent to
|
ExpressionEvaluator(java.lang.String expression,
java.lang.Class<?> expressionType,
java.lang.String[] parameterNames,
java.lang.Class<?>[] parameterTypes,
java.lang.Class<?>[] thrownExceptions,
java.lang.ClassLoader optionalParentClassLoader)
Equivalent to
|
| Modifier and Type | Method and Description |
|---|---|
void |
cook(java.lang.String[] optionalFileNames,
java.io.Reader[] readers)
Same as
ICookable.cook(String, Reader), but cooks a set of scripts into one class. |
protected java.lang.Class<?> |
getDefaultReturnType() |
void |
setExpressionType(java.lang.Class expressionType)
Define the type of the expression.
|
void |
setExpressionTypes(java.lang.Class[] expressionTypes)
Same as
IExpressionEvaluator.setExpressionType(Class), but for multiple expressions. |
void |
setReturnType(java.lang.Class returnType)
Deprecated.
|
void |
setReturnTypes(java.lang.Class[] returnTypes)
Deprecated.
|
cook, cook, cook, cook, cook, createFastEvaluator, createFastEvaluator, createInstance, evaluate, evaluate, getMethod, getMethod, setMethodName, setMethodNames, setOverrideMethod, setOverrideMethod, setParameters, setParameters, setStaticMethod, setStaticMethod, setThrownExceptions, setThrownExceptionscook, getClazz, parseImportDeclarations, setClassName, setDefaultImports, setExtendedClass, setExtendedType, setImplementedInterfaces, setImplementedTypesassertCooked, assertNotCooked, cook, getClassLoader, setDebuggingInformation, setParentClassLoader, setParentClassLoadercook, cook, cook, cook, cook, cook, cook, cookFile, cookFile, cookFile, cookFile, readStringclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitcreateFastEvaluator, createFastEvaluator, evaluatecook, cook, cook, evaluate, getMethod, getMethod, setMethodName, setMethodNames, setOverrideMethod, setOverrideMethod, setParameters, setParameters, setStaticMethod, setStaticMethod, setThrownExceptions, setThrownExceptionscreateInstance, getClazz, setClassName, setDefaultImports, setExtendedClass, setExtendedType, setImplementedInterfaces, setImplementedTypespublic ExpressionEvaluator(java.lang.String expression,
java.lang.Class<?> expressionType,
java.lang.String[] parameterNames,
java.lang.Class<?>[] parameterTypes)
throws CompileException
ExpressionEvaluator ee = new ExpressionEvaluator(); ee.setExpressionType(expressionType); ee.setParameters(parameterNames, parameterTypes); ee.cook(expression);
public ExpressionEvaluator(java.lang.String expression,
java.lang.Class<?> expressionType,
java.lang.String[] parameterNames,
java.lang.Class<?>[] parameterTypes,
java.lang.Class<?>[] thrownExceptions,
java.lang.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(java.lang.String expression,
java.lang.Class<?> expressionType,
java.lang.String[] parameterNames,
java.lang.Class<?>[] parameterTypes,
java.lang.Class<?>[] thrownExceptions,
java.lang.Class<?> optionalExtendedType,
java.lang.Class<?>[] implementedTypes,
java.lang.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()
public void setExpressionType(java.lang.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(java.lang.Class[] expressionTypes)
IExpressionEvaluatorIExpressionEvaluator.setExpressionType(Class), but for multiple expressions.setExpressionTypes in interface IExpressionEvaluator@Deprecated public final void setReturnType(java.lang.Class returnType)
IScriptEvaluatornull value is implementation-dependent.setReturnType in interface IExpressionEvaluatorsetReturnType in interface IScriptEvaluatorsetReturnType in class ScriptEvaluator@Deprecated public final void setReturnTypes(java.lang.Class[] returnTypes)
IScriptEvaluatornull values is
implementation-dependent.setReturnTypes in interface IExpressionEvaluatorsetReturnTypes in interface IScriptEvaluatorsetReturnTypes in class ScriptEvaluatorprotected java.lang.Class<?> getDefaultReturnType()
getDefaultReturnType in class ScriptEvaluatorpublic void cook(java.lang.String[] optionalFileNames,
java.io.Reader[] readers)
throws CompileException,
java.io.IOException
IScriptEvaluatorICookable.cook(String, Reader), but cooks a set of scripts into one class. Notice that
if any of the scripts causes trouble, the entire compilation will fail. If you
need to report which of the scripts causes the exception, you may want to use the
optionalFileNames parameter to distinguish between the individual token sources.
If and only if the number of scanners is one, then that single script may contain leading IMPORT directives.
cook in interface IScriptEvaluatorcook in class ScriptEvaluatorCompileExceptionjava.io.IOException