Class ExpressionEvaluator

  • All Implemented Interfaces:
    ICookable, IExpressionEvaluator, IMultiCookable

    public class ExpressionEvaluator
    extends MultiCookable
    implements IExpressionEvaluator
    This 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 JVMClient JVM
    Normal EE23.7 ns64.0 ns
    Fast EE31.2 ns42.2 ns