“Java Expression Language (JEXL) is an expression language engine which can be embedded in applications and frameworks”.
Although the last time that the official site was generated was on march 08 (for version 1.1), there is recent activity on JIRA regarding the release of version 2.
import org.apache.commons.jexl.Expression;
import org.apache.commons.jexl.ExpressionFactory;
import org.apache.commons.jexl.JexlContext;
import org.apache.commons.jexl.JexlHelper;
public class JEXLExample {
public static void main(String[] args) {
MyObject myObject = new MyObject();
String expr;
expr = "(myObject.number1 + myObject.number2) / myObject.number3";
processExpression(myObject, expr);
expr = "(N1 + N2) / N3";
processExpression(myObject, expr);
expr = "myObject.itsTrue";
processExpression(myObject, expr);
expr = "myObject.itsFalse";
processExpression(myObject, expr);
expr = "myObject.getThePresident().equals('Barack Obama')";
processExpression(myObject, expr);
expr = "myObject.thePresident.equals(\"George Bush\")";
processExpression(myObject, expr);
}
/**
* @param containerObject
* @param expression
*/
private static void processExpression(MyObject containerObject, String expression) {
try {
Expression e = ExpressionFactory.createExpression(expression);
JexlContext jc = JexlHelper.createContext();
//"myObject" is the alias to be used for expression evaluation
jc.getVars().put("myObject", containerObject);
jc.getVars().put("N1", containerObject.getNumber1());
jc.getVars().put("N2", containerObject.getNumber2());
jc.getVars().put("N3", containerObject.getNumber3());
System.out.println("Evaluating: " + expression );
System.out.println(" \\--------> " + e.evaluate(jc) );
} catch (Exception e) {
e.printStackTrace();
}
}
}
Notice the method processExpression, inside is where our expression is evaluated and the result is printed in the console.
First, we create an Expression object using the ExpressionFactory provided by Jexl, passing our expression as parameter.
Expression e = ExpressionFactory.createExpression(expression);
Then, we create the context in which our expression is going to be evaluated:
JexlContext jc = JexlHelper.createContext();
And finally, we add the variables into our context. These are the ones that can be used inside the expression.
jc.getVars().put("myObject", containerObject);
jc.getVars().put("N1", containerObject.getNumber1());
jc.getVars().put("N2", containerObject.getNumber2());
jc.getVars().put("N3", containerObject.getNumber3());
In the first line, I’m adding my containerObject object using the alias myObject.
The last 3 lines just add a different alias for each one of the numbers stored in the containerObject.
Below is the (short) code for the class MyObject:
public class MyObject {
private String thePresident = "Barack Obama";
private Boolean itsTrue = true;
private Boolean itsFalse = false;
private Integer number1 = 15;
private Integer number2 = 13;
private Integer number3 = 2;
...Getters & Setters...
}
This should be the output once you execute the example from above:
Evaluating: (myObject.number1 + myObject.number2) / myObject.number3
\--------> 14.0
Evaluating: (N1 + N2) / N3
\--------> 14.0
Evaluating: myObject.itsTrue
\--------> true
Evaluating: myObject.itsFalse
\--------> false
Evaluating: myObject.getThePresident().equals('Barack Obama')
\--------> true
Evaluating: myObject.thePresident.equals("George Bush")
\--------> false
I used Jexl in a previous project that I was involved. It’s very useful when you need to let the users enter expressions for filtering and calculation purposes.