public class ExpressionEvaluator extends MultiCookable 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 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 |
ANY_TYPE, DEFAULT_CLASS_NAME, DEFAULT_EXPRESSION_TYPE
Constructor and Description |
---|
ExpressionEvaluator() |
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.
|
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);
|
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);
|
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);
|
Modifier and Type | Method and Description |
---|---|
protected Java.Type[] |
classesToTypes(Location location,
Class<?>... classes)
|
protected Java.Type |
classToType(Location location,
Class<?> clazz)
|
void |
cook(Parser... parsers) |
void |
cook(Scanner... scanners)
Like
cook(Scanner) , but cooks a set of scripts into one class. |
void |
cook(Scanner scanner) |
void |
cook(String[] fileNames,
Reader[] readers) |
void |
cook(String fileName,
Reader reader) |
<T> T |
createFastEvaluator(Reader reader,
Class<? extends T> interfaceToImplement,
String... parameterNames) |
<T> T |
createFastEvaluator(Scanner scanner,
Class<T> interfaceToImplement,
String... parameterNames)
Notice: This method is not declared in
IScriptEvaluator , and is hence only available in this
implementation of org.codehaus.commons.compiler . |
<T> T |
createFastEvaluator(String script,
Class<? extends T> interfaceToImplement,
String... parameterNames) |
static Object |
createFastExpressionEvaluator(Scanner scanner,
String[] defaultImports,
String className,
Class<?> extendedType,
Class<?> interfaceToImplement,
String[] parameterNames,
ClassLoader parentClassLoader)
Deprecated.
Use
createFastEvaluator(Reader, Class, String[]) instead |
static Object |
createFastExpressionEvaluator(Scanner scanner,
String className,
Class<?> extendedType,
Class<?> interfaceToImplement,
String[] parameterNames,
ClassLoader parentClassLoader)
Deprecated.
Use
createFastEvaluator(Reader, Class, String[]) instead |
static Object |
createFastExpressionEvaluator(String expression,
Class<?> interfaceToImplement,
String[] parameterNames,
ClassLoader parentClassLoader)
Deprecated.
Use
createFastEvaluator(String, Class, String[]) instead: |
Object |
evaluate(int idx,
Object... arguments) |
Object |
evaluate(Object... arguments) |
Map<String,byte[]> |
getBytecodes() |
Class<?> |
getClazz() |
Class<?> |
getDefaultExpressionType() |
String[] |
getDefaultImports() |
Method |
getMethod() |
Method |
getMethod(int idx) |
Method[] |
getResult() |
static String[] |
guessParameterNames(Scanner scanner)
Guess the names of the parameters used in the given expression.
|
protected Java.Type |
optionalClassToType(Location location,
Class<?> clazz)
|
EnumSet<JaninoOption> |
options() |
ExpressionEvaluator |
options(EnumSet<JaninoOption> options)
Sets the options for all future compilations.
|
void |
setClassName(String className) |
void |
setCompileErrorHandler(ErrorHandler compileErrorHandler) |
void |
setDebuggingInformation(boolean debugSource,
boolean debugLines,
boolean debugVars) |
void |
setDefaultExpressionType(Class<?> defaultExpressionType) |
void |
setDefaultImports(String... defaultImports) |
void |
setExpressionType(Class<?> expressionType) |
void |
setExpressionTypes(Class<?>[] expressionTypes) |
void |
setExtendedClass(Class<?> extendedType) |
void |
setImplementedInterfaces(Class<?>[] implementedTypes) |
void |
setMethodName(String methodName) |
void |
setMethodNames(String[] methodNames) |
void |
setOverrideMethod(boolean overrideMethod) |
void |
setOverrideMethod(boolean[] overrideMethod) |
void |
setParameters(String[][] parameterNames,
Class<?>[][] parameterTypes) |
void |
setParameters(String[] parameterNames,
Class<?>[] parameterTypes) |
void |
setParentClassLoader(ClassLoader parentClassLoader) |
void |
setReturnType(Class<?> returnType) |
void |
setSourceVersion(int version) |
void |
setStaticMethod(boolean staticMethod) |
void |
setStaticMethod(boolean[] staticMethod) |
void |
setTargetVersion(int version) |
void |
setThrownExceptions(Class<?>[] thrownExceptions) |
void |
setThrownExceptions(Class<?>[][] thrownExceptions) |
void |
setWarningHandler(WarningHandler warningHandler) |
cook, cook, cook, cook, cook, cook, cook, cookFiles, cookFiles, cookFiles, cookFiles
cook, cook, cook, cook, cook, cook, cook, cookFile, cookFile, cookFile, cookFile
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
public 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, @Nullable ClassLoader parentClassLoader) throws CompileException
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, @Nullable ClassLoader parentClassLoader) throws CompileException
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);
CompileException
ExpressionEvaluator()
,
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, @Nullable Class<?> extendedType, Class<?>[] implementedTypes, boolean staticMethod, Class<?> expressionType, String methodName, String[] parameterNames, Class<?>[] parameterTypes, Class<?>[] thrownExceptions, @Nullable ClassLoader parentClassLoader) throws CompileException, IOException
Equivalent to:
ExpressionEvaluator ee = new ExpressionEvaluator(); ee.setClassName(className); ee.setExtendedType(extendedType); ee.setImplementedTypes(implementedTypes); ee.setStaticMethod(staticMethod); ee.setExpressionType(expressionType); ee.setMethodName(methodName); ee.setParameters(parameterNames, parameterTypes); ee.setThrownExceptions(thrownExceptions); ee.setParentClassLoader(parentClassLoader); ee.cook(scanner);
CompileException
IOException
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[])
,
ISimpleCompiler.setParentClassLoader(ClassLoader)
,
ICookable.cook(Reader)
public ExpressionEvaluator()
public void setParentClassLoader(@Nullable ClassLoader parentClassLoader)
setParentClassLoader
in interface IExpressionEvaluator
public void setDebuggingInformation(boolean debugSource, boolean debugLines, boolean debugVars)
setDebuggingInformation
in interface IExpressionEvaluator
public void setSourceVersion(int version)
setSourceVersion
in interface ICookable
public void setTargetVersion(int version)
setTargetVersion
in interface ICookable
public void setCompileErrorHandler(@Nullable ErrorHandler compileErrorHandler)
setCompileErrorHandler
in interface IExpressionEvaluator
public void setWarningHandler(@Nullable WarningHandler warningHandler)
setWarningHandler
in interface IExpressionEvaluator
public void setDefaultImports(String... defaultImports)
setDefaultImports
in interface IExpressionEvaluator
public String[] getDefaultImports()
getDefaultImports
in interface IExpressionEvaluator
public EnumSet<JaninoOption> options()
public ExpressionEvaluator options(EnumSet<JaninoOption> options)
public void setDefaultExpressionType(Class<?> defaultExpressionType)
setDefaultExpressionType
in interface IExpressionEvaluator
public Class<?> getDefaultExpressionType()
getDefaultExpressionType
in interface IExpressionEvaluator
public void setImplementedInterfaces(Class<?>[] implementedTypes)
setImplementedInterfaces
in interface IExpressionEvaluator
public void setReturnType(Class<?> returnType)
setReturnType
in interface IExpressionEvaluator
public void setExpressionType(Class<?> expressionType)
setExpressionType
in interface IExpressionEvaluator
public void setExpressionTypes(Class<?>[] expressionTypes)
setExpressionTypes
in interface IExpressionEvaluator
public void setOverrideMethod(boolean overrideMethod)
setOverrideMethod
in interface IExpressionEvaluator
public void setOverrideMethod(boolean[] overrideMethod)
setOverrideMethod
in interface IExpressionEvaluator
public void setParameters(String[] parameterNames, Class<?>[] parameterTypes)
setParameters
in interface IExpressionEvaluator
public void setParameters(String[][] parameterNames, Class<?>[][] parameterTypes)
setParameters
in interface IExpressionEvaluator
public void setClassName(String className)
setClassName
in interface IExpressionEvaluator
public void setExtendedClass(@Nullable Class<?> extendedType)
setExtendedClass
in interface IExpressionEvaluator
public void setStaticMethod(boolean staticMethod)
setStaticMethod
in interface IExpressionEvaluator
public void setStaticMethod(boolean[] staticMethod)
setStaticMethod
in interface IExpressionEvaluator
public void setMethodName(String methodName)
setMethodName
in interface IExpressionEvaluator
public void setMethodNames(String[] methodNames)
setMethodNames
in interface IExpressionEvaluator
public void setThrownExceptions(Class<?>[] thrownExceptions)
setThrownExceptions
in interface IExpressionEvaluator
public void setThrownExceptions(Class<?>[][] thrownExceptions)
setThrownExceptions
in interface IExpressionEvaluator
public Method getMethod()
getMethod
in interface IExpressionEvaluator
public Method getMethod(int idx)
getMethod
in interface IExpressionEvaluator
public Method[] getResult()
getResult
in interface IExpressionEvaluator
IllegalStateException
- The ScriptEvaluator
has not yet be cookedpublic void cook(@Nullable String fileName, Reader reader) throws CompileException, IOException
cook
in interface ICookable
cook
in class Cookable
CompileException
IOException
public void cook(String[] fileNames, Reader[] readers) throws CompileException, IOException
cook
in interface IMultiCookable
cook
in class MultiCookable
CompileException
IOException
public final void cook(Scanner scanner) throws CompileException, IOException
CompileException
IOException
public final void cook(Scanner... scanners) throws CompileException, IOException
cook(Scanner)
, 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 fileName
argument of Scanner(String, Reader)
to distinguish between the individual token sources.
On a 2 GHz Intel Pentium Core Duo under Windows XP with an IBM 1.4.2 JDK, compiling 10000 expressions "a + b" (integer) takes about 4 seconds and 56 MB of main memory. The generated class file is 639203 bytes large.
The number and the complexity of the scripts is restricted by the Limitations of the Java Virtual Machine, where the most limiting factor is the 64K entries limit of the constant pool. Since every method with a distinct name requires one entry there, you can define at best 32K (very simple) scripts.
If and only if the number of scanners is one, then that single script may contain leading IMPORT directives.
IllegalStateException
- Any of the preceding set...()
had an array size different from that of
scanners
CompileException
IOException
public final void cook(Parser... parsers) throws CompileException, IOException
CompileException
IOException
cook(Scanner[])
protected Java.Type[] classesToTypes(Location location, @Nullable Class<?>... classes)
@Nullable protected Java.Type optionalClassToType(Location location, @Nullable Class<?> clazz)
@Nullable public Object evaluate(@Nullable Object... arguments) throws InvocationTargetException
evaluate
in interface IExpressionEvaluator
InvocationTargetException
@Nullable public Object evaluate(int idx, @Nullable Object... arguments) throws InvocationTargetException
evaluate
in interface IExpressionEvaluator
InvocationTargetException
public Class<?> getClazz()
getClazz
in interface IExpressionEvaluator
public Map<String,byte[]> getBytecodes()
getBytecodes
in interface ICookable
@Deprecated public static Object createFastExpressionEvaluator(String expression, Class<?> interfaceToImplement, String[] parameterNames, @Nullable ClassLoader parentClassLoader) throws CompileException
createFastEvaluator(String, Class, String[])
instead:CompileException
@Deprecated public static Object createFastExpressionEvaluator(Scanner scanner, String className, @Nullable Class<?> extendedType, Class<?> interfaceToImplement, String[] parameterNames, @Nullable ClassLoader parentClassLoader) throws CompileException, IOException
createFastEvaluator(Reader, Class, String[])
insteadCompileException
IOException
@Deprecated public static Object createFastExpressionEvaluator(Scanner scanner, String[] defaultImports, String className, @Nullable Class<?> extendedType, Class<?> interfaceToImplement, String[] parameterNames, @Nullable ClassLoader parentClassLoader) throws CompileException, IOException
createFastEvaluator(Reader, Class, String[])
insteadCompileException
IOException
public <T> T createFastEvaluator(Reader reader, Class<? extends T> interfaceToImplement, String... parameterNames) throws CompileException, IOException
createFastEvaluator
in interface IExpressionEvaluator
CompileException
IOException
public <T> T createFastEvaluator(String script, Class<? extends T> interfaceToImplement, String... parameterNames) throws CompileException
createFastEvaluator
in interface IExpressionEvaluator
CompileException
public <T> T createFastEvaluator(Scanner scanner, Class<T> interfaceToImplement, String... parameterNames) throws CompileException, IOException
IScriptEvaluator
, and is hence only available in this
implementation of org.codehaus.commons.compiler
. To be independent from this particular
implementation, try to switch to createFastEvaluator(Reader, Class, String[])
.scanner
- Source of tokens to readCompileException
IOException
createFastEvaluator(Reader, Class, String[])
public static String[] guessParameterNames(Scanner scanner) throws CompileException, IOException
CompileException
IOException
Scanner(String, Reader)
Copyright © 2021. All rights reserved.