net.janino
Class ExpressionEvaluator

java.lang.Object
  |
  +--net.janino.EvaluatorBase
        |
        +--net.janino.ExpressionEvaluator

public class ExpressionEvaluator
extends EvaluatorBase

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 , section . 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 is compiled when the ExpressionEvaluator object is instantiated. The expression, its type, and its parameter names and types are specified at compile time.

The expression evaluator is implemented by creating and compiling a temporary compilation unit defining one class with one static method with one return statement.

After the ExpressionEvaluator object is created, the expression can be evaluated as often with different parameter values (see evaluate(Object[])). This evaluation is very fast, compared to the compilation.

The more elaborate constructors of ExpressionEvaluator also 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 evaluates the expression, the exceptions that this method is allowed to throw, and the java.lang.ClassLoader that is used to define the generated class and to load classes referenced by the expression. This degree of flexibility is usually not required; the most commonly used constructor is ExpressionEvaluator(String, Class, String[], Class[]).


Constructor Summary
ExpressionEvaluator(Scanner scanner, java.lang.String className, java.lang.Class extendedType, java.lang.Class[] implementedTypes, java.lang.Class expressionType, java.lang.String methodName, java.lang.String[] parameterNames, java.lang.Class[] parameterTypes, java.lang.Class[] thrownExceptions, java.lang.ClassLoader classLoader)
          Parse an expression from a sequence of Scanner.Tokens delivered by the given scanner and compile it.
ExpressionEvaluator(java.lang.String expression, java.lang.Class expressionType, java.lang.String[] parameterNames, java.lang.Class[] parameterTypes)
          Parse an expression from a String and compile it.
ExpressionEvaluator(java.lang.String expression, java.lang.Class expressionType, java.lang.String[] parameterNames, java.lang.Class[] parameterTypes, java.lang.Class[] thrownExceptions, java.lang.Class extendedType, java.lang.Class[] implementedTypes, java.lang.ClassLoader classLoader)
          Parse an expression from a String and compile it.
ExpressionEvaluator(java.lang.String expression, java.lang.Class expressionType, java.lang.String[] parameterNames, java.lang.Class[] parameterTypes, java.lang.Class[] thrownExceptions, java.lang.ClassLoader classLoader)
          Parse an expression from a String and compile it.
 
Method Summary
 java.lang.Object evaluate(java.lang.Object[] parameterValues)
          Evaluates an expression with concrete parameter values.
 java.lang.reflect.Method getMethod()
          If, for any reason, somebody needs the java.lang.reflect.Method object...
 
Methods inherited from class net.janino.EvaluatorBase
addClassDeclaration, addClassMethodBlockDeclaration, classesToTypes, classToType, compileAndLoad, compileAndLoad, makeFormalParameters, parseImportDeclarations
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ExpressionEvaluator

public ExpressionEvaluator(java.lang.String expression,
                           java.lang.Class expressionType,
                           java.lang.String[] parameterNames,
                           java.lang.Class[] parameterTypes)
                    throws Java.CompileException,
                           Parser.ParseException,
                           Scanner.ScanException,
                           java.io.IOException
Parse an expression from a String and compile it.

The expression must not throw any Exceptions other than RuntimeException.

See Also:
ExpressionEvaluator(Scanner, String, Class, Class[], Class, String, String[], Class[], Class[], ClassLoader)

ExpressionEvaluator

public ExpressionEvaluator(java.lang.String expression,
                           java.lang.Class expressionType,
                           java.lang.String[] parameterNames,
                           java.lang.Class[] parameterTypes,
                           java.lang.Class[] thrownExceptions,
                           java.lang.ClassLoader classLoader)
                    throws Java.CompileException,
                           Parser.ParseException,
                           Scanner.ScanException,
                           java.io.IOException
Parse an expression from a String and compile it.
See Also:
ExpressionEvaluator(Scanner, String, Class, Class[], Class, String, String[], Class[], Class[], ClassLoader)

ExpressionEvaluator

public ExpressionEvaluator(java.lang.String expression,
                           java.lang.Class expressionType,
                           java.lang.String[] parameterNames,
                           java.lang.Class[] parameterTypes,
                           java.lang.Class[] thrownExceptions,
                           java.lang.Class extendedType,
                           java.lang.Class[] implementedTypes,
                           java.lang.ClassLoader classLoader)
                    throws Java.CompileException,
                           Parser.ParseException,
                           Scanner.ScanException,
                           java.io.IOException
Parse an expression from a String and compile it.
See Also:
ExpressionEvaluator(Scanner, String, Class, Class[], Class, String, String[], Class[], Class[], ClassLoader)

ExpressionEvaluator

public ExpressionEvaluator(Scanner scanner,
                           java.lang.String className,
                           java.lang.Class extendedType,
                           java.lang.Class[] implementedTypes,
                           java.lang.Class expressionType,
                           java.lang.String methodName,
                           java.lang.String[] parameterNames,
                           java.lang.Class[] parameterTypes,
                           java.lang.Class[] thrownExceptions,
                           java.lang.ClassLoader classLoader)
                    throws Scanner.ScanException,
                           Parser.ParseException,
                           Java.CompileException,
                           java.io.IOException
Parse an expression from a sequence of Scanner.Tokens delivered by the given scanner and compile it.

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 classLoader serves two purposes:

If the classLoader 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.)

Parameters:
scanner - Source of tokens to parse
className - The name of the temporary class (uncritical)
extendedType - The base class of the generated object or null
implementedTypes - The interfaces that the the generated object implements (all methods must be implemented by the extendedType)
expressionType - The type of the expression, e.g. Double.TYPE, double.class or String.class.
methodName - The name of the temporary method (uncritical)
parameterNames - The names of the expression parameters, e.g. "i" and "j".
parameterTypes - The types of the expression parameters, e.g. Integer.TYPE, Writer.class or Double.TYPE.
thrownExceptions - The exceptions that the expression is allowed to throw, e.g. java.io.IOException.class.
classLoader - loads referenced classes and defines the generated class
Method Detail

evaluate

public java.lang.Object evaluate(java.lang.Object[] parameterValues)
                          throws java.lang.reflect.InvocationTargetException
Evaluates an expression with concrete parameter values.

Each parameter value must have the same type as specified through the "parameterTypes" parameter of ExpressionEvaluator(String, Class, String[], Class[], Class[], ClassLoader).

Parameters of primitive type must passed with their wrapper class objects.

The object returned has the class specified through the "returnType" parameter of ExpressionEvaluator(String, Class, String[], Class[], Class[], ClassLoader). If the "returnType" is primitive (e.g. "int.class"), the return value is returned through a wrapper object ("Integer").

Parameters:
parameterValues - The concrete parameter values.

getMethod

public java.lang.reflect.Method getMethod()
If, for any reason, somebody needs the java.lang.reflect.Method object...