| java.lang.Object org.codehaus.janino.Cookable org.codehaus.janino.SimpleCompiler org.codehaus.janino.ClassBodyEvaluator org.codehaus.janino.ScriptEvaluator org.codehaus.janino.ExpressionEvaluator
ExpressionEvaluator | public class ExpressionEvaluator extends ScriptEvaluator (Code) | | An expression evaluator that evaluates expressions in JavaTM bytecode.
The syntax of the expression to compile is that of a JavaTM expression, as defined
in the Java Language Specification,
2nd edition, section
15.
Notice that a JavaTM 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.)
The expression evaluator is implemented by creating and compiling a temporary compilation unit
defining one class with one static method with one return statement.
To set up an
ExpressionEvaluator object, proceed as follows:
-
Create the
ExpressionEvaluator using
ExpressionEvaluator.ExpressionEvaluator()
-
Configure the
ExpressionEvaluator by calling any of the following methods:
-
Call any of the
org.codehaus.janino.Cookable.cook(Scanner) methods to scan,
parse, compile and load the expression into the JVM.
Alternatively, a number of "convenience constructors" exist that execute the steps described
above instantly.
After the
ExpressionEvaluator object is set up, the expression can be evaluated as
often with different parameter values (see
ExpressionEvaluator.evaluate(Object[]) ). This evaluation is
very fast, compared to the setup.
Notice that for functionally identical
ExpressionEvaluator s,
java.lang.Object.equals(java.lang.Object) will return true . E.g. "a+b" and
"c + d" are functionally identical if "a" and "c" have the same type, and so do "b" and "d".
If the parameter and return types of the expression are known at compile time, then a "fast"
expression evaluator can be instantiated through
ExpressionEvaluator.createFastExpressionEvaluator(String,Class,String[],ClassLoader) . Expression
evaluation is faster than through
ExpressionEvaluator.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 |
(How can it be that interface method invocation is slower than reflection for
the server JVM?)
The expression may refer to a set of parameters with the given
parameterNames and parameterTypes .
parameterNames and parameterTypes must have the
same number of elements.
The parameters and/or the return value can be of primitive type, e.g.
Double.TYPE .
The optionalClassLoader serves two purposes:
- It is used to look for classes referenced by the script.
- It is used to load the generated JavaTM class
into the JVM; directly if it is a subclass of
ByteArrayClassLoader , or by creation of a temporary
ByteArrayClassLoader if not.
If the optionalClassLoader is null , then the
current thread's context class loader is used.
A number of constructors exist that provide useful default values for
the various parameters, or parse their script from a
String instead of a
Scanner . (You hardly want to use a scanner other than
the default scanner.)
If the type of the expression is not fixed, you can pass a null
optionalExpressionType argument; in this case, references are
returned as
Object s, and primitive values are wrapped in their
wrapper classes.
If optionalExpressionType is
Void.TYPE , then the
expression must be an invocation of a void method.
|
Constructor Summary | |
public | ExpressionEvaluator(String expression, Class expressionType, String[] parameterNames, Class[] parameterTypes) | public | ExpressionEvaluator(String expression, Class expressionType, String[] parameterNames, Class[] parameterTypes, Class[] thrownExceptions, ClassLoader optionalParentClassLoader) | public | ExpressionEvaluator(String expression, Class expressionType, String[] parameterNames, Class[] parameterTypes, Class[] thrownExceptions, Class optionalExtendedType, Class[] implementedTypes, ClassLoader optionalParentClassLoader) | public | ExpressionEvaluator(Scanner scanner, String className, Class optionalExtendedType, Class[] implementedTypes, boolean staticMethod, Class expressionType, String methodName, String[] parameterNames, Class[] parameterTypes, Class[] thrownExceptions, ClassLoader optionalParentClassLoader) | public | ExpressionEvaluator() |
Method Summary | |
public static Object | createFastExpressionEvaluator(String expression, Class interfaceToImplement, String[] parameterNames, ClassLoader optionalClassLoader) Creates a "fast expression evaluator" from the given
java.lang.String expression , generating a class with the
ExpressionEvaluator.DEFAULT_CLASS_NAME that
extends
Object . | public static Object | createFastExpressionEvaluator(Scanner scanner, String className, Class optionalExtendedType, Class interfaceToImplement, String[] parameterNames, ClassLoader optionalParentClassLoader) Creates a "fast expression evaluator" from the given
Scanner with no default
imports. | public static Object | createFastExpressionEvaluator(Scanner scanner, String[] optionalDefaultImports, String className, Class optionalExtendedType, Class interfaceToImplement, String[] parameterNames, ClassLoader optionalParentClassLoader) Creates a "fast expression evaluator".
See the class description for an explanation of the "fast expression evaluator" concept.
Notice: The interfaceToImplement must either be declared public ,
or with package scope in the same package as className .
Parameters: scanner - Source of expression tokens Parameters: optionalDefaultImports - Default imports, e.g. | protected void | internalCook(Scanner scanner) | public void | setExpressionType(Class expressionType) Define the type of the expression. | final public void | setReturnType(Class returnType) |
ExpressionEvaluator | public ExpressionEvaluator(String expression, Class expressionType, String[] parameterNames, Class[] parameterTypes, Class[] thrownExceptions, Class optionalExtendedType, Class[] implementedTypes, ClassLoader optionalParentClassLoader) throws CompileException, Parser.ParseException, Scanner.ScanException(Code) | | Equivalent to
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);
See Also: ExpressionEvaluator.ExpressionEvaluator() See Also: ExpressionEvaluator.setExpressionType(Class) See Also: ScriptEvaluator.setParameters(String[]Class[]) See Also: ScriptEvaluator.setThrownExceptions(Class[]) See Also: ClassBodyEvaluator.setExtendedType(Class) See Also: ClassBodyEvaluator.setImplementedTypes(Class[]) See Also: SimpleCompiler.setParentClassLoader(ClassLoader) See Also: Cookable.cook(String) |
ExpressionEvaluator | 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 Scanner.ScanException, Parser.ParseException, CompileException, IOException(Code) | | Equivalent to
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);
See Also: ExpressionEvaluator.ExpressionEvaluator() See Also: ClassBodyEvaluator.setClassName(String) See Also: ClassBodyEvaluator.setExtendedType(Class) See Also: ClassBodyEvaluator.setImplementedTypes(Class[]) See Also: ScriptEvaluator.setStaticMethod(boolean) See Also: ExpressionEvaluator.setExpressionType(Class) See Also: ScriptEvaluator.setMethodName(String) See Also: ScriptEvaluator.setParameters(String[]Class[]) See Also: ScriptEvaluator.setThrownExceptions(Class[]) See Also: SimpleCompiler.setParentClassLoader(ClassLoader) See Also: Cookable.cook(Scanner) |
ExpressionEvaluator | public ExpressionEvaluator()(Code) | | |
createFastExpressionEvaluator | public static Object createFastExpressionEvaluator(String expression, Class interfaceToImplement, String[] parameterNames, ClassLoader optionalClassLoader) throws CompileException, Parser.ParseException, Scanner.ScanException(Code) | | Creates a "fast expression evaluator" from the given
java.lang.String expression , generating a class with the
ExpressionEvaluator.DEFAULT_CLASS_NAME that
extends
Object .
See the class description for an explanation of the "fast expression evaluator" concept.
See Also: ExpressionEvaluator.createFastExpressionEvaluator(Scanner,String[],String,Class,Class,String[],ClassLoader) See Also: ExpressionEvaluator |
createFastExpressionEvaluator | public static Object createFastExpressionEvaluator(Scanner scanner, String className, Class optionalExtendedType, Class interfaceToImplement, String[] parameterNames, ClassLoader optionalParentClassLoader) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException(Code) | | Creates a "fast expression evaluator" from the given
Scanner with no default
imports.
See the class description for an explanation of the "fast expression evaluator" concept.
See Also: ExpressionEvaluator.createFastExpressionEvaluator(Scanner,String[],String,Class,Class,String[],ClassLoader) See Also: ExpressionEvaluator |
createFastExpressionEvaluator | public static Object createFastExpressionEvaluator(Scanner scanner, String[] optionalDefaultImports, String className, Class optionalExtendedType, Class interfaceToImplement, String[] parameterNames, ClassLoader optionalParentClassLoader) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException(Code) | | Creates a "fast expression evaluator".
See the class description for an explanation of the "fast expression evaluator" concept.
Notice: The interfaceToImplement must either be declared public ,
or with package scope in the same package as className .
Parameters: scanner - Source of expression tokens Parameters: optionalDefaultImports - Default imports, e.g. { "java.util.Map", "java.io.*" } Parameters: className - Name of generated class Parameters: optionalExtendedType - Class to extend Parameters: interfaceToImplement - Must declare exactly the one method that defines the expression's signature Parameters: parameterNames - The expression references the parameters through these names Parameters: optionalParentClassLoader - Used to load referenced classes, defaults to the current thread's "context class loader" an object that implements the given interface and extends the optionalExtendedType See Also: ExpressionEvaluator |
setReturnType | final public void setReturnType(Class returnType)(Code) | | |
Methods inherited from org.codehaus.janino.ScriptEvaluator | protected Java.Block addClassMethodBlockDeclaration(Location location, Java.CompilationUnit compilationUnit, Class returnType) throws Parser.ParseException(Code)(Java Doc) protected void compileToMethod(Java.CompilationUnit compilationUnit) throws CompileException(Code)(Java Doc) public static Object createFastEvaluator(ScriptEvaluator se, String s, String[] parameterNames, Class interfaceToImplement) throws CompileException, Parser.ParseException, Scanner.ScanException(Code)(Java Doc) public static Object createFastEvaluator(ScriptEvaluator se, Scanner scanner, String[] parameterNames, Class interfaceToImplement) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException(Code)(Java Doc) public static Object createFastScriptEvaluator(String script, Class interfaceToImplement, String[] parameterNames) throws CompileException, Parser.ParseException, Scanner.ScanException(Code)(Java Doc) public static Object createFastScriptEvaluator(Scanner scanner, Class interfaceToImplement, String[] parameterNames, ClassLoader optionalParentClassLoader) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException(Code)(Java Doc) public static Object createFastScriptEvaluator(Scanner scanner, String className, Class optionalExtendedType, Class interfaceToImplement, String[] parameterNames, ClassLoader optionalParentClassLoader) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException(Code)(Java Doc) public static Object createFastScriptEvaluator(Scanner scanner, String[] optionalDefaultImports, String className, Class optionalExtendedType, Class interfaceToImplement, String[] parameterNames, ClassLoader optionalParentClassLoader) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException(Code)(Java Doc) public Object evaluate(Object[] parameterValues) throws InvocationTargetException(Code)(Java Doc) public Method getMethod()(Code)(Java Doc) protected void internalCook(Scanner scanner) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException(Code)(Java Doc) public void setMethodName(String methodName)(Code)(Java Doc) public void setParameters(String[] parameterNames, Class[] parameterTypes)(Code)(Java Doc) public void setReturnType(Class returnType)(Code)(Java Doc) public void setStaticMethod(boolean staticMethod)(Code)(Java Doc) public void setThrownExceptions(Class[] thrownExceptions)(Code)(Java Doc)
|
Methods inherited from org.codehaus.janino.ClassBodyEvaluator | protected Java.PackageMemberClassDeclaration addPackageMemberClassDeclaration(Location location, Java.CompilationUnit compilationUnit) throws Parser.ParseException(Code)(Java Doc) protected Class compileToClass(Java.CompilationUnit compilationUnit, EnumeratorSet debuggingInformation, String newClassName) throws CompileException(Code)(Java Doc) public static Object createFastClassBodyEvaluator(Scanner scanner, Class optionalBaseType, ClassLoader optionalParentClassLoader) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException(Code)(Java Doc) public static Object createFastClassBodyEvaluator(Scanner scanner, String className, Class optionalExtendedType, Class[] implementedTypes, ClassLoader optionalParentClassLoader) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException(Code)(Java Doc) public Class getClazz()(Code)(Java Doc) protected void internalCook(Scanner scanner) throws CompileException, ParseException, ScanException, IOException(Code)(Java Doc) protected Java.CompilationUnit makeCompilationUnit(Scanner scanner) throws Parser.ParseException, Scanner.ScanException, IOException(Code)(Java Doc) public void setClassName(String className)(Code)(Java Doc) public void setDefaultImports(String[] optionalDefaultImports)(Code)(Java Doc) public void setExtendedType(Class optionalExtendedType)(Code)(Java Doc) public void setImplementedTypes(Class[] implementedTypes)(Code)(Java Doc)
|
Methods inherited from org.codehaus.janino.Cookable | final public void cook(Scanner scanner) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException(Code)(Java Doc) final public void cook(Reader r) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException(Code)(Java Doc) final public void cook(String optionalFileName, Reader r) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException(Code)(Java Doc) final public void cook(InputStream is) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException(Code)(Java Doc) final public void cook(String optionalFileName, InputStream is) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException(Code)(Java Doc) final public void cook(InputStream is, String optionalEncoding) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException(Code)(Java Doc) final public void cook(String optionalFileName, InputStream is, String optionalEncoding) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException(Code)(Java Doc) final public void cook(String s) throws CompileException, Parser.ParseException, Scanner.ScanException(Code)(Java Doc) final public void cookFile(File file) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException(Code)(Java Doc) final public void cookFile(File file, String optionalEncoding) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException(Code)(Java Doc) final public void cookFile(String fileName) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException(Code)(Java Doc) final public void cookFile(String fileName, String optionalEncoding) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException(Code)(Java Doc) abstract protected void internalCook(Scanner scanner) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException(Code)(Java Doc)
|
|
|