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 JVM | Client JVM | |
|---|---|---|
| Normal EE | 23.7 ns | 64.0 ns |
| Fast EE | 31.2 ns | 42.2 ns |
| Modifier and Type | Field and Description |
|---|---|
| private final ScriptEvaluator | |
| private int | |
| private WarningHandler |
| Access | Constructor 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 |
| Modifier and Type | Method and Description |
|---|---|
| protected Java. | classesToTypes(Location location, Class<?>... classes)
|
| protected Java. | classToType(final Location location, final Class<?> clazz)
|
| public void | cook(String
Used when reporting errors and warnings fileName, Reader reader)Implements abstract org. Implements org. Reader.
|
| public void | cook(String[] fileNames, Reader[] readers)
Implements abstract org. Implements org. Cookable#cook(String, Reader), but cooks a set of documents into one class.
|
| public final void | |
| public final void | |
| public final void | |
| private void | |
| public <T> T | createFastEvaluator(Reader reader, Class<? extends T> interfaceToImplement, String... parameterNames)
Implements org. |
| public <T> T | createFastEvaluator(String script, Class<? extends T> interfaceToImplement, String... parameterNames)
Implements org. 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 |
| public static Object | createFastExpressionEvaluator(String expression, Class<?> interfaceToImplement, String[] parameterNames, ClassLoader parentClassLoader)
Deprecated
Use
createFastEvaluator(String, Class, String[]) instead:
|
| public static Object | createFastExpressionEvaluator(Scanner scanner, String className, Class<?> extendedType, Class<?> interfaceToImplement, String[] parameterNames, ClassLoader parentClassLoader)
Deprecated
Use
createFastEvaluator(Reader, Class, String[]) instead
|
| public static Object | createFastExpressionEvaluator(Scanner scanner, String[] defaultImports, String className, Class<?> extendedType, Class<?> interfaceToImplement, String[] parameterNames, ClassLoader parentClassLoader)
Deprecated
Use
createFastEvaluator(Reader, Class, String[]) instead
|
| public Object | evaluate()
Implements org. evaluate(new Object[0]).
|
| public Object | evaluate(Object[]
The actual parameter values arguments)Implements org. |
| public Object | evaluate(int idx, Object[] arguments)
Implements org. evaluate(Object[]), but for multiple expressions.
|
| public Map | |
| public Class | |
| public Class | |
| public String[] | |
| public Method | |
| public Method | getMethod(int idx)
Implements org. getMethod(), but for multiple expressions.
|
| public Method[] | Returns: The generated methodImplements org. |
| public static String[] | |
| private static String[] | |
| protected Java. | optionalClassToType(final Location location, final Class<?> clazz)
|
| public EnumSet | Returns: A reference to the currently effective compilation options; changes to it take effect immediately
|
| public ExpressionEvaluator | |
| public void | setClassName(String className)
Implements org. |
| public void | setCompileErrorHandler(ErrorHandler
null to restore the default behavior (throwing a CompileException)Implements org. ErrorHandler which is invoked during compilation on each error.
|
| public void | setDebuggingInformation(boolean debugSource, boolean debugLines, boolean debugVars)
Implements org. |
| public void | setDefaultExpressionType(Class<?> defaultExpressionType)
Implements org. IExpressionEvaluator, the "default expression type" is used for the expression
|
| public void | setDefaultImports(String... defaultImports)
Implements org. |
| public void | setExpressionType(Class<?> expressionType)
Implements org. |
| public void | setExpressionTypes(Class<?>[] expressionTypes)
Implements org. |
| public void | setExtendedClass(Class<?> extendedType)
Implements org. |
| public void | setImplementedInterfaces(Class<?>[] implementedTypes)
Implements org. |
| public void | setMethodName(String
null means reset to default nameImplements org. |
| public void | setMethodNames(String[] methodNames)
Implements org. setMethodName(String), but for multiple expressions.
|
| public void | setOverrideMethod(boolean overrideMethod)
Implements org. |
| public void | setOverrideMethod(boolean[] overrideMethod)
Implements org. setOverrideMethod(boolean), but for multiple expressions.
|
| public void | setParameters(String[] parameterNames, Class<?>[] parameterTypes)
Implements org. |
| public void | setParameters(String[][] parameterNames, Class<?>[][] parameterTypes)
Implements org. setParameters(String[], Class[]), but for multiple expressions.
|
| public void | setParentClassLoader(ClassLoader parentClassLoader)
Implements org. |
| public void | setReturnType(Class<?> returnType)
Implements org. |
| public void | setSourceVersion(int version)
Implements org. -source command line option.
|
| public void | setStaticMethod(boolean staticMethod)
Implements org. |
| public void | setStaticMethod(boolean[] staticMethod)
Implements org. setStaticMethod(boolean), but for multiple expressions.
|
| public void | setTargetVersion(int version)
Implements org. -target command line option.
|
| public void | setThrownExceptions(Class<?>[] thrownExceptions)
Implements org. |
| public void | setThrownExceptions(Class<?>[][] thrownExceptions)
Implements org. setThrownExceptions(Class[]), but for multiple expressions.
|
| public void | setWarningHandler(WarningHandler
null to indicate that no warnings be issuedImplements org. WarningHandler.
|