Value Expressions

Value expressions are expressions that are evaluated in the "classical sense". There are two kinds of value expressions: those created by parsing an expression string and those simply wrapping an object.

A javax.el.ValueExpression is evaluated by calling its getValue(ELContext) method. Value expressions can also be writable and provide methods isReadOnly(ELContext), getType(ELContext) and setValue(ELContext, Object).

A value expression is called an lvalue expression if its expression string is an eval expression (#{...} or ${...}) consisting of a single identifier or a nonliteral prefix (function, identifier or nested expression), followed by a sequence of property operators (. or []). All other value expressions are called non-lvalue expressions.

For non-lvalue expressions

  • getType(ELContext) method will always return null.
  • isReadOnly(ELContext) method will always return true.
  • setValue(ELContext, Object) method will always throw an exception.

Tree Value Expressions

Creating a tree value expression involves

  1. parsing an expression string and building an abstract syntax tree,
  2. binding functions and variables using the mappers provided by the context.

Once created, a tree value expression can be evaluated using the getValue(ELContext) method. The result is automatically coerced to the expected type given at creation time.

Class de.odysseus.el.TreeValueExpression is a subclass of javax.el.ValueExpression, which is used by JUEL to represent a value expression, that has been created from an expression string. It is the return type of

ExpressionFactoryImpl.createValueExpression(ELContext, String, Class<?>)

In addition to the methods available for javax.el.ValueExpression, it provides methods

  • void dump(java.io.PrintWriter writer) – dump parse tree
  • boolean isDeferred()true if expression is deferred (contains eval expressions #{...})
  • boolean isLeftValue()true if expression is an lvalue expression.
import java.io.*;
import de.odysseus.el.*;
import de.odysseus.el.util.*;
...
ExpressionFactoryImpl factory = new ExpressionFactoryImpl();
SimpleContext context = new SimpleContext(); // more on this here...
TreeValueExpression e = factory.createValueExpression(context, "#{pi/2}", Object.class);
PrintWriter out = new PrintWriter(System.out);
e.dump(out);
// +- #{...}
//    |
//    +- '/'
//       |
//       +- pi
//       |
//       +- 2
out.flush();
System.out.println(e.isDeferred()); // true
System.out.println(e.isLeftValue()); // false
...

Object Value Expressions

An object value expression simply wraps an object giving it an "expression facade". At the first place, object expressions are used to define variables.

Once created, an object value expression can be evaluated using the getValue(ELContext) method, which simply returns the wrapped object, coerced to the expected type provided at creation time.

Class de.odysseus.el.ObjectValueExpression is a subclass of javax.el.ValueExpression, which is used by JUEL to represent a value expression, that has been created from an object. It is the return type of

ExpressionFactoryImpl.createValueExpression(Object, Class<?>)

This class provides no extra methods to those available for javax.el.ValueExpression.

import java.io.*;
import de.odysseus.el.*;
import de.odysseus.el.util.*;
...
ExpressionFactoryImpl factory = new ExpressionFactoryImpl();
SimpleContext context = new SimpleContext(); // more on this here...
ObjectValueExpression e = factory.createValueExpression(Math.PI, Double.class);
context.setVariable("pi", e);
...