OverviewIn this reference you will find the following topics to help with your use of JEXL. You can find two sample programs in JEXL's source repository: As well, JEXL's Unit Tests provide handy examples of expressions. The test code also contains a simple class that evaluates its command line arguments as JEXL expressions when run. The package javadoc also contains useful information. Evaluating ExpressionsTo evaluate expressions using JEXL, you need three things:
The common way of using a JEXL engine is to create it as a singleton and use this opportunity to tailor its behavior and cache through the jexlBuilder options. private static final JexlEngine jexl = new JexlBuilder().cache(512).strict(true).silent(false).create(); The easiest way of obtaining a context is to use the new MapContext() statement. This creates a context implemented using an underlying HashMap Expressions are created using the JexlEngine.createExpression(String) method. Once you have your expression, you can then use the evaluate to execute it and obtain a result. Here's a typical scenario: // Assuming we have a JexlEngine instance initialized in our class named 'jexl': // Create an expression object for our calculation String calculateTax = taxManager.getTaxCalc(); //e.g. "((G1 + G2 + G3) * 0.1) + G4"; Expression e = jexl.createExpression( calculateTax ); // populate the context JexlContext context = new MapContext(); context.set("G1", businessObject.getTotalSales()); context.set("G2", taxManager.getTaxCredit(businessObject.getYear())); context.set("G3", businessObject.getIntercompanyPayments()); context.set("G4", -taxManager.getAllowances()); // ... // work it out Number result = (Number) e.evaluate(context); Custom ContextsOften you have the objects and values that are needed in the context available elsewhere. If those are already in a Map, instead of creating the default context and populating it manually in the code, you can wrap a MapContext around your own map using new MapContext(yourOwnMap) In edge cases, it may be simpler to create a context implementation of your own. The JexlContext interface is very simple with only three methods, to set, get and check the existence of variables. Example ExpressionsArithmeticMost valid arithmetic expressions in Java are also valid in Jexl. 1 + 2 12.0 - 5.2 6 * 12 + 5 / 2.6 12 % 2 6 / 4 -12 + 77.2 x * 1.1 + y Arithmetic expressions can use variables. Calling methods
JEXL allows you to call any method on a Java object using the same syntax.
If you have a string in the context under the name aString.length() aString.substring(6)
Often the values you want to pass to a method are other variables or expressions.
If you have a number in the context, named aString.substring(i) Accessing propertiesJEXL provides a shorthand syntax to access methods that follow the JavaBean naming convention for properties, i.e. setters and getters.
If you have some object foo in the context and it has a
method foo.bar
Since foo.class.name Arrays, Lists and MapsArray elements can be accessed using either square brackets or a dotted index notation, e.g. the following are equivalent arr[0] arr.0 For a map, the syntax is very similar, except the 'index' is an object, e.g. the following are equivalent. aMap['key'] aMap.get('key') |