Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

OK, now you are curious... this is how you use the ExpressionEvaluator:

Code Block

// Compile the expression once; relatively slow.
ExpressionEvaluator ee = new ExpressionEvaluator(
    "c > d ? c : d",                     // expression
    int.class,                           // expressionType
    new String[] { "c", "d" },           // parameterNames
    new Class[] { int.class, int.class } // parameterTypes
);

// Evaluate it with varying parameter values; very fast.
Integer res = (Integer) ee.evaluate(
    new Object[] {          // parameterValues
        new Integer(10),
        new Integer(11),
    }
);
System.out.println("res = " + res);

...

There is a sample program "ExpressionDemo" that you can use to play around with the ExpressionEvaluator, or you can study ExpressionDemo's source code to learn about ExpressionEvaluator's API:

Code Block

$ java org.codehaus.janino.samples.ExpressionDemo -help
Usage:
  ExpressionDemo { <option> } <expression> { <parameter-value> }
Compiles and evaluates the given expression and prints its value.
Valid options are
 -et <expression-type>                        (default: any)
 -pn <comma-separated-parameter-names>        (default: none)
 -pt <comma-separated-parameter-types>        (default: none)
 -te <comma-separated-thrown-exception-types> (default: none)
 -di <comma-separated-default-imports>        (default: none)
 -help
The number of parameter names, types and values must be identical.
Code Block

$ java org.codehaus.janino.samples.ExpressionDemo \
> -pn "a,b" -pt "int,int" "a + b" 11 22
Result = 33
$

...

Analogously to the expression evaluator, a ScriptEvaluator API exists that compiles and processes a JavaTM "block", i.e. the body of a method. If a return value other than "void" is defined, then the block must return a value of that type. Example:

Code Block

System.out.println("Hello world");
return true;

As for the expression compiler, there is a demo program "ScriptDemo" for you to play with the ScriptEvaluator API:

Code Block

$ java org.codehaus.janino.samples.ScriptDemo -help
Usage:
  ScriptDemo { <option> } <script> { <parameter-value> }
Valid options are
 -rt <return-type>                            (default: void)
 -pn <comma-separated-parameter-names>        (default: none)
 -pt <comma-separated-parameter-types>        (default: none)
 -te <comma-separated-thrown-exception-types> (default: none)
 -di <comma-separated-default-imports>        (default: none)
 -help
The number of parameter names, types and values must be identical.
Code Block

$ java org.codehaus.janino.samples.ScriptDemo \
> -rt boolean \
> -pn a,b \
> -pt double,double \
'System.err.println("a + b = " + (a+b));
System.err.println("a - b = " + (a - b));
return true;' 31.765 12.539
a + b = 44.304
a - b = 19.226
Result = true

Check the source code of ScriptDemo to learn more about the ScriptEvaluator API.

...

Analogously to the expression evaluator and the script evaluator, a ClassBodyEvaluator exists that compiles and processes the body of a JavaTM class, i.e. a series of method and variable declarations. If you define a contract that the class body should define a method named "main()", then your script will look almost like a "C" program:

Code Block

import java.util.*;

// Field declaration:
private static final String hello = "World";

// Method declaration:
public static void main(String[] args) {
    System.out.println(hello + args.length);
}

The "ClassBodyDemo" program (source code) demonstrates this:

Code Block

$ java org.codehaus.janino.samples.ClassBodyDemo -help
Usage:
  ClassBodyDemo <class-body> { <argument> }
  ClassBodyDemo -help
If <class-body> starts with a '@', then the class body is read
from the named file.
The <class-body> must declare a method "public static main(String[])"
to which the <argument>s are passed. If the return type of that method is
not VOID, then the returned value is printed to STDOUT.
Code Block

$ java org.codehaus.janino.samples.ClassBodyDemo \
> 'import java.util.*;
>
> // Field declaration:
> private static final String hello = "World";
>
> // Method declaration:
> public static void main(String[] args) {
>     System.out.println(hello + args.length);
> }' alpha beta gamma
World3

...

Janino as a Simple Compiler

The SimpleCompiler compiles a single "compilation unit" (a.k.a. ".java" file). Opposed to to normal JavaTM compilation, that compilation unit may define more than one public class. Example:

Code Block

package my.pkg;

import java.util.*;

public class A {
    public static void main(String[] args) {
        B b = new B();
        b.meth1();
    }
}

public class B {
    void meth1() {
        System.out.println("Hello there.");
    }
}

...