Logo Search packages:      
Sourcecode: janino version File versions  Download package

org::codehaus::janino::ExpressionEvaluator Class Reference

Inheritance diagram for org::codehaus::janino::ExpressionEvaluator:

org::codehaus::janino::ScriptEvaluator org::codehaus::janino::ClassBodyEvaluator org::codehaus::janino::SimpleCompiler org::codehaus::janino::Cookable

List of all members.


Detailed Description

An engine 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.) This feature is not available if you compile many expressions at a time (see below).

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:

  1. Create the ExpressionEvaluator using ExpressionEvaluator()
  2. Configure the ExpressionEvaluator by calling any of the following methods:
  3. Call any of the org.codehaus.janino.Cookable#cook(Scanner) methods to scan, parse, compile and load the expression into the JVM.
After the ExpressionEvaluator object is set up, the expression can be evaluated as often with different parameter values (see evaluate(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.

Alternatively, a number of "convenience constructors" exist that execute the steps described above instantly.

If you want to compile many expressions at the same time, you have the option to cook an array of expressions in one ExpressionEvaluator by using the following methods:

Notice that these methods have array parameters in contrast to their one-expression brethren.

Notice that for functionally identical ExpressionEvaluators, 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 createFastExpressionEvaluator(String, Class, String[], ClassLoader). 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 JVMClient JVM
Normal EE23.7 ns64.0 ns
Fast EE31.2 ns42.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:

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 Objects, 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.

Definition at line 181 of file ExpressionEvaluator.java.


Public Member Functions

final void cook (String s) throws CompileException, Parser.ParseException, Scanner.ScanException
final void cook (String optionalFileName, InputStream is, String optionalEncoding) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException
final void cook (InputStream is, String optionalEncoding) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException
final void cook (String optionalFileName, InputStream is) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException
final void cook (InputStream is) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException
final void cook (String optionalFileName, Reader r) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException
final void cook (Reader r) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException
final void cook (String[] strings) throws CompileException, Parser.ParseException, Scanner.ScanException
final void cook (String[] optionalFileNames, Reader[] readers) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException
final void cook (Reader[] readers) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException
final void cook (Scanner[] scanners) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException
final void cook (Scanner scanner) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException
final void cookFile (String fileName, String optionalEncoding) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException
final void cookFile (String fileName) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException
final void cookFile (File file, String optionalEncoding) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException
final void cookFile (File file) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException
boolean equals (Object o)
Object evaluate (int idx, Object[] parameterValues) throws InvocationTargetException
Object evaluate (Object[] parameterValues) throws InvocationTargetException
 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
 ExpressionEvaluator (String expression, Class expressionType, String[] parameterNames, Class[] parameterTypes, Class[] thrownExceptions, Class optionalExtendedType, Class[] implementedTypes, ClassLoader optionalParentClassLoader) throws CompileException, Parser.ParseException, Scanner.ScanException
 ExpressionEvaluator (String expression, Class expressionType, String[] parameterNames, Class[] parameterTypes, Class[] thrownExceptions, ClassLoader optionalParentClassLoader) throws CompileException, Parser.ParseException, Scanner.ScanException
 ExpressionEvaluator (String expression, Class expressionType, String[] parameterNames, Class[] parameterTypes) throws CompileException, Parser.ParseException, Scanner.ScanException
ClassLoader getClassLoader ()
Class getClazz ()
Method getMethod (int idx)
Method getMethod ()
int hashCode ()
void setClassName (String className)
void setDefaultImports (String[] optionalDefaultImports)
void setExpressionType (Class expressionType)
void setExpressionTypes (Class[] expressionTypes)
void setExtendedType (Class optionalExtendedType)
void setImplementedTypes (Class[] implementedTypes)
void setMethodName (String methodName)
void setMethodNames (String[] methodNames)
void setParameters (String[][] parameterNames, Class[][] parameterTypes)
void setParameters (String[] parameterNames, Class[] parameterTypes)
void setParentClassLoader (ClassLoader optionalParentClassLoader, Class[] auxiliaryClasses)
void setParentClassLoader (ClassLoader optionalParentClassLoader)
void setReturnType (Class returnType)
void setReturnTypes (Class[] returnTypes)
void setStaticMethod (boolean[] staticMethod)
void setStaticMethod (boolean staticMethod)
void setThrownExceptions (Class[][] thrownExceptions)
void setThrownExceptions (Class[] thrownExceptions)

Static Public Member Functions

static Object createFastClassBodyEvaluator (Scanner scanner, String className, Class optionalExtendedType, Class[] implementedTypes, ClassLoader optionalParentClassLoader) throws CompileException, ParseException, ScanException, IOException
static Object createFastClassBodyEvaluator (Scanner scanner, Class optionalBaseType, ClassLoader optionalParentClassLoader) throws CompileException, ParseException, ScanException, IOException
static Object createFastEvaluator (ScriptEvaluator se, Scanner scanner, String[] parameterNames, Class interfaceToImplement) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException
static Object createFastEvaluator (ScriptEvaluator se, String s, String[] parameterNames, Class interfaceToImplement) throws CompileException, Parser.ParseException, Scanner.ScanException
static Object createFastExpressionEvaluator (Scanner scanner, String[] optionalDefaultImports, String className, Class optionalExtendedType, Class interfaceToImplement, String[] parameterNames, ClassLoader optionalParentClassLoader) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException
static Object createFastExpressionEvaluator (Scanner scanner, String className, Class optionalExtendedType, Class interfaceToImplement, String[] parameterNames, ClassLoader optionalParentClassLoader) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException
static Object createFastExpressionEvaluator (String expression, Class interfaceToImplement, String[] parameterNames, ClassLoader optionalParentClassLoader) throws CompileException, Parser.ParseException, Scanner.ScanException
static Object createFastScriptEvaluator (Scanner scanner, String[] optionalDefaultImports, String className, Class optionalExtendedType, Class interfaceToImplement, String[] parameterNames, ClassLoader optionalParentClassLoader) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException
static Object createFastScriptEvaluator (Scanner scanner, String className, Class optionalExtendedType, Class interfaceToImplement, String[] parameterNames, ClassLoader optionalParentClassLoader) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException
static Object createFastScriptEvaluator (Scanner scanner, Class interfaceToImplement, String[] parameterNames, ClassLoader optionalParentClassLoader) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException
static Object createFastScriptEvaluator (String script, Class interfaceToImplement, String[] parameterNames) throws CompileException, Parser.ParseException, Scanner.ScanException
static void main (String[] args) throws Exception

Static Public Attributes

static final Class ANY_TYPE = null
static final ClassLoader BOOT_CLASS_LOADER = new ClassLoader(null) {}
static final String DEFAULT_CLASS_NAME = "SC"

Protected Member Functions

Java.PackageMemberClassDeclaration addPackageMemberClassDeclaration (Location location, Java.CompilationUnit compilationUnit) throws ParseException
Java.Type[] classesToTypes (Location location, Class[] classes)
Java.Type classToType (Location location, final Class optionalClass)
final Class compileToClass (Java.CompilationUnit compilationUnit, EnumeratorSet debuggingInformation, String newClassName) throws CompileException
final ClassLoader compileToClassLoader (Java.CompilationUnit compilationUnit, EnumeratorSet debuggingInformation) throws CompileException
void compileToMethods (Java.CompilationUnit compilationUnit, String[] methodNames, Class[][] parameterTypes) throws CompileException
Class getDefaultReturnType ()
Java.Block makeBlock (int idx, Scanner scanner) throws ParseException, ScanException, IOException
final Java.CompilationUnit makeCompilationUnit (Scanner optionalScanner) throws ParseException, ScanException, IOException
Java.MethodDeclarator makeMethodDeclaration (Location location, boolean staticMethod, Class returnType, String methodName, Class[] parameterTypes, String[] parameterNames, Class[] thrownExceptions, Java.Block optionalBody)
final void setUpClassLoaders ()

Protected Attributes

String className = ClassBodyEvaluator.DEFAULT_CLASS_NAME
String[] optionalMethodNames = null
String[][] optionalParameterNames = null
Class[][] optionalParameterTypes = null
Class[] optionalReturnTypes = null
boolean[] optionalStaticMethod = null
Class[][] optionalThrownExceptions = null

Static Protected Attributes

static final Class[] ZERO_CLASSES = new Class[0]

Private Attributes

Class[] optionalExpressionTypes = null

The documentation for this class was generated from the following file:

Generated by  Doxygen 1.6.0   Back to index