Skip navigation links

Package org.apache.commons.jexl3

Provides a framework for evaluating JEXL expressions.

See: Description

Package org.apache.commons.jexl3 Description

Provides a framework for evaluating JEXL expressions.

Introduction

JEXL is a library intended to facilitate the implementation of dynamic and scripting features in applications and frameworks.

A Brief Example

In its simplest form, JEXL merges an JexlExpression with a JexlContext when evaluating expressions. An Expression is created using JexlEngine.createExpression(String), passing a String containing valid JEXL syntax. A simple JexlContext can be created using a MapContext instance; a map of variables that will be internally wrapped can be optionally provided through its constructor. The following example, takes a variable named 'car', and invokes the checkStatus() method on the property 'engine'

 // Create a JexlEngine (could reuse one instead)
 JexlEngine jexl = new JexlBuilder().create();
 // Create an expression object equivalent to 'car.getEngine().checkStatus()':
 String jexlExp = "car.engine.checkStatus()";
 Expression e = jexl.createExpression( jexlExp );
 // The car we have to handle coming as an argument...
 Car car = theCarThatWeHandle;
 // Create a context and add data
 JexlContext jc = new MapContext();
 jc.set("car", car );
 // Now evaluate the expression, getting the result
 Object o = e.evaluate(jc);
 

Using JEXL

The API is composed of three levels addressing different functional needs:

Important note

The public API classes reside in the 2 packages:

The following packages follow a "use at your own maintenance cost" policy; these are only intended to be used for extending JEXL. Their classes and methods are not guaranteed to remain compatible in subsequent versions. If you think you need to use directly some of their features or methods, it might be a good idea to check with the community through the mailing list first.

Dynamic Invocation

These functionalities are close to the core level utilities found in BeanUtils. For basic dynamic property manipulations and method invocation, you can use the following set of methods:

The following example illustrate their usage:
 // test outer class
 public static class Froboz {
 int value;
 public Froboz(int v) { value = v; }
 public void setValue(int v) { value = v; }
 public int getValue() { return value; }
 }
 // test inner class
 public static class Quux {
 String str;
 Froboz froboz;
 public Quux(String str, int fro) {
 this.str = str;
 froboz = new Froboz(fro);
 }
 public Froboz getFroboz() { return froboz; }
 public void setFroboz(Froboz froboz) { this.froboz = froboz; }
 public String getStr() { return str; }
 public void setStr(String str) { this.str = str; }
 }
 // test API
 JexlEngine jexl = new JexlBuilder().create();
 Quux quux = jexl.newInstance(Quux.class, "xuuq", 100);
 jexl.setProperty(quux, "froboz.value", Integer.valueOf(100));
 Object o = jexl.getProperty(quux, "froboz.value");
 assertEquals("Result is not 100", new Integer(100), o);
 jexl.setProperty(quux, "['froboz'].value", Integer.valueOf(1000));
 o = jexl.getProperty(quux, "['froboz']['value']");
 assertEquals("Result is not 1000", new Integer(1000), o);
 

Expressions and Scripts

If your needs require simple expression evaluation capabilities, the core JEXL features will most likely fit. The main methods are:

The following example illustrates their usage:
 JexlEngine jexl = new JexlBuilder().create();
 JexlContext jc = new MapContext();
 jc.set("quuxClass", quux.class);
 JexlExpression create = jexl.createExpression("quux = new(quuxClass, 'xuuq', 100)");
 JelxExpression assign = jexl.createExpression("quux.froboz.value = 10");
 JexlExpression check = jexl.createExpression("quux[\"froboz\"].value");
 Quux quux = (Quux) create.evaluate(jc);
 Object o = assign.evaluate(jc);
 assertEquals("Result is not 10", new Integer(10), o);
 o = check.evaluate(jc);
 assertEquals("Result is not 10", new Integer(10), o);
 

Unified Expressions and Templates

If you are looking for JSP-EL like and basic templating features, you can use Expression from a JxltEngine.

The main methods are: The following example illustrates their usage:
 JexlEngine jexl = new JexlBuilder().create();
 JxltEngine jxlt = jexl.createJxltEngine();
 JxltEngine.Expression expr = jxlt.createExpression("Hello ${user}");
 String hello = expr.evaluate(context).toString();
 

JexlExpression, JexlScript, Expression and Template: summary

JexlExpression

These are the most basic form of JexlEngine expressions and only allow for a single command to be executed and its result returned. If you try to use multiple commands, it ignores everything after the first semi-colon and just returns the result from the first command.

Also note that expressions are not statements (which is what scripts are made of) and do not allow using the flow control (if, while, for), variables or lambdas syntactic elements.

JexlScript

These allow you to use multiple statements and you can use variable assignments, loops, calculations, etc. More or less what can be achieved in Shell or JavaScript at its basic level. The result from the last command is returned from the script.

JxltEngine.Expression

These are ideal to produce "one-liner" text, like a 'toString()' on steroids. To get a calculation you use the EL-like syntax as in ${someVariable}. The expression that goes between the brackets behaves like a JexlScript, not an expression. You can use semi-colons to execute multiple commands and the result from the last command is returned from the script. You also have the ability to use a 2-pass evaluation using the #{someScript} syntax.

JxltEngine.Template

These produce text documents. Each line beginning with '$$' (as a default) is considered JEXL code and all others considered as JxltEngine.Expression. Think of those as simple Velocity templates. A rewritten MudStore initial Velocity sample looks like this:


 <html>
 <body>
 Hello ${customer.name}!
 <table>
 $$      for(var mud : mudsOnSpecial ) {
 $$          if (customer.hasPurchased(mud) ) {
 <tr>
 <td>
 ${flogger.getPromo( mud )}
 </td>
 </tr>
 $$          }
 $$      }
 </table>
 </body>
 </html>
 

JEXL Configuration

The JexlEngine can be configured through a few parameters that will drive how it reacts in case of errors. These configuration methods are embedded through a JexlBuilder.

Static & Shared Configuration

Both JexlEngine and JxltEngine are thread-safe, most of their inner fields are final; the same instance can be shared between different threads and proper synchronization is enforced in critical areas (introspection caches).

Of particular importance is JexlBuilder.loader(java.lang.ClassLoader) which indicates to the JexlEngine being built which class loader to use to solve a class name; this directly affects how JexlEngine.newInstance and the 'new' script method operates.

This can also be very useful in cases where you rely on JEXL to dynamically load and call plugins for your application. To avoid having to restart the server in case of a plugin implementation change, you can call JexlEngine.setClassLoader(java.lang.ClassLoader) and all the scripts created through this engine instance will automatically point to the newly loaded classes.

You can state what can be manipulated through scripting by the NoJexl annotation that completely shield classes and methods from JEXL introspection. The other configurable way to restrict JEXL is by using a JexlSandbox which allows finer control over what is exposed; the sandbox can be set through JexlBuilder.sandbox(org.apache.commons.jexl3.introspection.JexlSandbox).

JexlBuilder.namespaces(java.util.Map<java.lang.String, java.lang.Object>) extends JEXL scripting by registering your own classes as namespaces allowing your own functions to be exposed at will.

This can be used as in:

 public static MyMath {
 public double cos(double x) {
 return Math.cos(x);
 }
 }
 Map<String, Object> funcs = new HashMap<String, Object>();
 funcs.put("math", new MyMath());
 JexlEngine jexl = new JexlBuilder().namespaces(funcs).create();
 JexlContext jc = new MapContext();
 jc.set("pi", Math.PI);
 JexlExpression e = JEXL.createExpression("math:cos(pi)");
 o = e.evaluate(jc);
 assertEquals(Double.valueOf(-1),o);
 

If the namespace is a Class and that class declares a constructor that takes a JexlContext (or a class extending JexlContext), one namespace instance is created on first usage in an expression; this instance lifetime is limited to the expression evaluation.

JexlEngine and JxltEngine expression caches can be configured as well. If you intend to use JEXL repeatedly in your application, these are worth configuring since expression parsing is quite heavy. Note that all caches created by JEXL are held through SoftReference; under high memory pressure, the GC will be able to reclaim those caches and JEXL will rebuild them if needed. By default, a JexlEngine does create a cache for "small" expressions and a JxltEngine does create one for Expression .

JexlBuilder.cache(int) will set how many expressions can be simultaneously cached by the JEXL engine. JxltEngine allows to define the cache size through its constructor.

JexlBuilder.debug(boolean) makes stacktraces carried by JExlException more meaningful; in particular, these traces will carry the exact caller location the Expression was created from.

Dynamic Configuration

Those configuration options can be overridden during evaluation by implementing a JexlContext that also implements JexlEngine.Options to carry evaluation options. An example of such a class exists in the test package.

JexlBuilder.strict(boolean) or JexlEngine.Options.isStrict() configures when JEXL considers 'null' as an error or not in various situations; when facing an unreferenceable variable, using null as an argument to an arithmetic operator or failing to call a method or constructor. The lenient mode is close to JEXL-1.1 behavior.

JexlBuilder.silent(boolean) or JexlEngine.Options.isSilent() configures how JEXL reacts to errors; if silent, the engine will not throw exceptions but will warn through loggers and return null in case of errors. Note that when non-silent, JEXL throws JexlException which are unchecked exception.

Implementing a JexlContext.NamespaceResolver through a JexlContext - look at JexlEvalContext in the test directory as an example - allows to override the namespace resolution and the default namespace map defined through JexlBuilder.namespaces(java.util.Map<java.lang.String, java.lang.Object>).

JEXL Customization

The JexlContext, JexlBuilder and JexlEngine.Options are the most likely interfaces you'll want to implement for customization. Since they expose variables and options, they are the primary targets. Before you do so, have a look at JexlEvalContext in the test directory and ObjectContext which may already cover some of your needs.

JexlArithmetic is the class to derive if you need to change how operators behave or add types upon which they operate. There are 3 entry points that allow customizing the type of objects created:

You can also overload operator methods; by convention, each operator has a method name associated to it. If you overload some in your JexlArithmetic derived implementation, these methods will be called when the arguments match your method signature. For example, this would be the case if you wanted '+' to operate on arrays; you'd need to derive JexlArithmetic and implement 'public Object add(Set<?;> x, Set<?;> y)' method. Note however that you can not change the operator precedence. The list of operator / method matches is described in JexlOperator:

You can also add methods to overload property getters and setters operators behaviors. Public methods of the JexlArithmetic instance named propertyGet/propertySet/arrayGet/arraySet are potential overrides that will be called when appropriate. The following table is an overview of the relation between a syntactic form and the method to call where V is the property value class, O the object class and P the property identifier class (usually String or Integer).

Property Accessors
Expression Method Template
foo.property public V propertyGet(O obj, P property);
foo.property = value public V propertySet(O obj, P property, V value);
foo[property] public V arrayGet(O obj, P property, V value);
foo[property] = value public V arraySet(O obj, P property, V value);

You can also override the base operator methods, those whose arguments are Object which gives you total control.

Extending JEXL

If you need to make JEXL treat some objects in a specialized manner or tweak how it reacts to some settings, you can derive most of its inner-workings. The classes and methods are rarely private or final - only when the inner contract really requires it. However, using the protected methods and internal package classes imply you might have to re-adapt your code when new JEXL versions are released.

Engine can be extended to let you capture your own configuration defaults wrt cache sizes and various flags. Implementing your own cache - instead of the basic LinkedHashMap based one - would be another possible extension.

Interpreter is the class to derive if you need to add more features to the evaluation itself; for instance, you want pre- and post- resolvers for variables or nested scopes for for variable contexts.

Uberspect is the class to derive if you need to add introspection or reflection capabilities for some objects, for instance adding factory based support to the 'new' operator. The code already reflects public fields as properties on top of Java-beans conventions.

Skip navigation links

Copyright © 2001–2022 The Apache Software Foundation. All rights reserved.