Interface IExpressionEvaluator
-
- All Superinterfaces:
IClassBodyEvaluator
,ICookable
,IScriptEvaluator
- All Known Implementing Classes:
ExpressionEvaluator
,ExpressionEvaluator
public interface IExpressionEvaluator extends IScriptEvaluator
An engine that evaluates expressions in Java™ bytecode.The syntax of the expression to compile is that of a Java™ expression, as defined in the Java Language Specification, 2nd edition, section 15. Notice that a Java™ expression does not have a concluding semicolon.
Example:
a + 7 * b
(Notice that this expression refers to two parameters "a" and "b", as explained below.)The expression may optionally be preceeded with a sequence of import directives like
import java.text.*; new DecimalFormat("####,###.##").format(10200020.345345)
(Notice that the import directive is concluded with a semicolon, while the expression is not.) This feature is not available if you compile many expressions at a time (see below).To set up an
IExpressionEvaluator
object, proceed as follows:-
Create an
IExpressionEvaluator
-derived class -
Configure the
IExpressionEvaluator
by calling any of the following methods: -
Call any of the
ICookable.cook(String, java.io.Reader)
methods to scan, parse, compile and load the expression into the JVM.
IExpressionEvaluator
object is set up, the expression can be evaluated as often with different parameter values (seeevaluate(Object[])
). This evaluation is very fast, compared to the compilation.Less common methods exist that allow for the specification of the name of the generated class, the class it extends, the interfaces it implements, the name of the method that executes the expression, the exceptions that this method (i.e. the expression) is allowed to throw, and the
ClassLoader
that is used to define the generated class and to load classes referenced by the expression.If you want to compile many expressions at the same time, you have the option to cook an array of expressions in one
IExpressionEvaluator
by using the following methods:IScriptEvaluator.setMethodNames(String[])
IScriptEvaluator.setParameters(String[][], Class[][])
setExpressionTypes(Class[])
IScriptEvaluator.setStaticMethod(boolean[])
IScriptEvaluator.setThrownExceptions(Class[][])
ICookable.cook(String, java.io.Reader)
IScriptEvaluator.evaluate(int, Object[])
Notice that for functionally identical
IExpressionEvaluator
s,Object.equals(java.lang.Object)
will returntrue
. E.g. "a+b" and "c + d" are functionally identical if "a" and "c" have the same type, and so do "b" and "d".
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.Class
ANY_TYPE
-
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
-
-
Method Summary
All Methods Instance Methods Abstract Methods Deprecated Methods Modifier and Type Method Description java.lang.Object
createFastEvaluator(java.io.Reader reader, java.lang.Class interfaceToImplement, java.lang.String[] parameterNames)
If the parameter and return types of the expression are known at compile time, then a "fast" script evaluator can be instantiated through this method.java.lang.Object
createFastEvaluator(java.lang.String expression, java.lang.Class interfaceToImplement, java.lang.String[] parameterNames)
If the parameter and return types of the expression are known at compile time, then a "fast" expression evaluator can be instantiated throughcreateFastEvaluator(String, Class, String[])
.java.lang.Object
evaluate(java.lang.Object[] arguments)
Evaluates the expression with concrete parameter values.void
setExpressionType(java.lang.Class expressionType)
Define the type of the expression.void
setExpressionTypes(java.lang.Class[] expressionTypes)
Same assetExpressionType(Class)
, but for multiple expressions.void
setReturnType(java.lang.Class returnType)
Deprecated.Must not be used on anIExpressionEvaluator
; usesetExpressionType(Class)
instead.void
setReturnTypes(java.lang.Class[] returnTypes)
Deprecated.Must not be used on anIExpressionEvaluator
; usesetExpressionTypes(Class[])
instead.-
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.IScriptEvaluator
cook, cook, cook, cook, evaluate, getMethod, getMethod, setMethodName, setMethodNames, setOverrideMethod, setOverrideMethod, setParameters, setParameters, setStaticMethod, setStaticMethod, setThrownExceptions, setThrownExceptions
-
-
-
-
Method Detail
-
setExpressionType
void setExpressionType(java.lang.Class expressionType)
-
setExpressionTypes
void setExpressionTypes(java.lang.Class[] expressionTypes)
Same assetExpressionType(Class)
, but for multiple expressions.
-
setReturnType
@Deprecated void setReturnType(java.lang.Class returnType)
Deprecated.Must not be used on anIExpressionEvaluator
; usesetExpressionType(Class)
instead.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 interfaceIScriptEvaluator
-
setReturnTypes
@Deprecated void setReturnTypes(java.lang.Class[] returnTypes)
Deprecated.Must not be used on anIExpressionEvaluator
; usesetExpressionTypes(Class[])
instead.Description copied from interface:IScriptEvaluator
Defines the return types of the generated methods. The meaning ofnull
values is implementation-dependent.- Specified by:
setReturnTypes
in interfaceIScriptEvaluator
-
evaluate
java.lang.Object evaluate(java.lang.Object[] arguments) throws java.lang.reflect.InvocationTargetException
Evaluates the expression with concrete parameter values.Each argument value must have the same type as specified through the "parameterTypes" parameter of
IScriptEvaluator.setParameters(String[], Class[])
.Arguments of primitive type must passed with their wrapper class objects.
The object returned has the class as specified through
setExpressionType(Class)
.This method is thread-safe.
- Specified by:
evaluate
in interfaceIScriptEvaluator
- Parameters:
arguments
- The actual parameter values.- Throws:
java.lang.reflect.InvocationTargetException
-
createFastEvaluator
java.lang.Object createFastEvaluator(java.lang.String expression, java.lang.Class interfaceToImplement, java.lang.String[] parameterNames) throws CompileException
If the parameter and return types of the expression are known at compile time, then a "fast" expression evaluator can be instantiated throughcreateFastEvaluator(String, Class, String[])
. Expression evaluation is faster than throughevaluate(Object[])
, because it is not done through reflection but through direct method invocation.Example:
public interface Foo { int bar(int a, int b); } ... ExpressionEvaluator ee = CompilerFactoryFactory.getDefaultCompilerFactory().newExpressionEvaluator(); // Optionally configure the EE here... ee.
All other configuration (implemented type, static method, return type, method name, parameter names and types, thrown exceptions) are predetermined by thesetClassName
("Bar"); ee.setDefaultImports
(new String[] { "java.util.*" }); ee.setExtendedClass
(SomeOtherClass.class); ee.setParentClassLoader
(someClassLoader); // Optionally configure the EE here... Foo f = (Foo) ee.createFastEvaluator( "a + b", // expression to evaluate Foo.class, // interface that describes the expression's signature new String[] { "a", "b" } // the parameters' names ); System.out.println("1 + 2 = " + f.bar(1, 2)); // Evaluate the expressioninterfaceToImplement
. Notice: TheinterfaceToImplement
must be accessible by the compiled class, i.e. either be declaredpublic
, or withprotected
or default access in the package of the compiled class (seeIClassBodyEvaluator.setClassName(String)
.- Specified by:
createFastEvaluator
in interfaceIScriptEvaluator
- Parameters:
expression
- Contains the sequence of script tokens- Throws:
CompileException
- See Also:
IScriptEvaluator.createFastEvaluator(Reader, Class, String[])
-
createFastEvaluator
java.lang.Object createFastEvaluator(java.io.Reader reader, java.lang.Class interfaceToImplement, java.lang.String[] parameterNames) throws CompileException, java.io.IOException
Description copied from interface:IScriptEvaluator
If the parameter and return types of the expression are known at compile time, then a "fast" script evaluator can be instantiated through this method.Script evaluation is faster than through
IScriptEvaluator.evaluate(Object[])
, because it is not done through reflection but through direct method invocation.Example:
public interface Foo { int bar(int a, int b); } ... IScriptEvaluator se =
All other configuration (implemented type, static method, return type, method name, parameter names and types, thrown exceptions) are predetermined by theCompilerFactoryFactory
.getDefaultCompilerFactory
().newScriptEvaluator
(); // Optionally configure the SE her: se.setClassName
("Bar"); se.setDefaultImports
(new String[] { "java.util.*" }); se.setExtendedClass
(SomeOtherClass.class); se.setParentClassLoader
(someClassLoader); Foo f = (Foo) se.createFastScriptEvaluator
( "return a - b;", Foo.class, new String[] { "a", "b" } ); System.out.println("1 - 2 = " + f.bar(1, 2));interfaceToImplement
. Notice: TheinterfaceToImplement
must either be declaredpublic
, or with package scope in the same package as the generated class (seeIClassBodyEvaluator.setClassName(String)
).- Specified by:
createFastEvaluator
in interfaceIScriptEvaluator
- Parameters:
reader
- Produces the stream of script tokensinterfaceToImplement
- Must declare exactly one methodparameterNames
- The names of the parameters of that method- Returns:
- An object that implements the given interface
- Throws:
CompileException
java.io.IOException
- See Also:
createFastEvaluator(String, Class, String[])
-
-