Thu
17
Feb '05
Tool of the day: Janino
by Frank Spychalski filed under Java, Work

Janino is a light-weight java compiler written in Java.

Why Janino? Why not use the Sun SDK?

… compiling Java programs with SUN’s JDK is a relatively resource-intensive process (disk access, CPU time, …).

This is where Janino comes into play… a leight-weight, “embedded” Java compiler that compiles simple programs in memory into Java bytecode which executes within the JVM of the running program.

here’s a short example:

String[] names = new String[] { "a", "b", "c", "d", "e" };
Class[] types = new Class[] { Boolean.TYPE, Boolean.TYPE,
                              Boolean.TYPE, Boolean.TYPE,
                              Boolean.TYPE };
String expr = "(a & b || c && d ) & !e";
ExpressionEvaluator ee = new ExpressionEvaluator(expr,
                                                 Boolean.TYPE,
                                                 names,
                                                 types);

Class[] values = new Object[] { Boolean.FALSE, Boolean.TRUE,
                                Boolean.TRUE, Boolean.TRUE,
                                Boolean.FALSE};
System.out.println(ee.evaluate(values));

Interesting fact: Groovy uses Janino, too.


Any comments? Or questions? Just leave a Reply: