Appendix A. OGNL Language Reference

Table of Contents

Operators

This section has a fairly detailed treatment of OGNL's syntax and implementation. See below for a complete table of OGNL's operators, a section on how OGNL coerces objects to various types, and a detailed description of OGNL's basic expressions.

Operators

OGNL borrows most of Java's operators, and adds a few new ones. For the most part, OGNL's treatment of a given operator is the same as Java's, with the important caveat that OGNL is essentially a typeless language. What that means is that every value in OGNL is a Java object, and OGNL attempts to coerce from each object a meaning appropriate to the situation it is used in (see the section on coercion).

The following table lists OGNL operators in reverse precedence order. When more than one operator is listed in the same box, these operators have the same precedence and are evaluated in left-to-right order.

Table A.1. OGNL Operators

OperatorgetValue() NotessetValue() Notes
e1, e2

Sequence operator

Both e1 and e2 are evaluated with the same source object, and the result of e2 is returned.getValue is called on e1, and then setValue is called on e2.
e1 = e2

Assignment operator

getValue is called on e2, and then setValue is called on e1 with the result of e2 as the target object.Cannot be the top-level expression for setValue.
e1 ? e2 : e3

Conditional operator

getValue is called on e1 and the result is interpreted as a boolean. getValue is then called on either e2 or e3, depending on whether the result of e1 was true or false respectively, and the result is returned.getValue is called on e1, and then setValue is called on either e2 or e3.
e1 || e2, e1 or e2

Logical or operator

getValue is called on e1 and the result is interpreted as a boolean. If true, that result is returned; if false, getValue is called on e2 and its value is returned.getValue is called on e1; if false, setValue is called on e2. Note that e1 being true prevents any further setting from taking place.
e1 && e2, e1 and e2

Logical and operator

getValue is called on e1 and the result is interpreted as a boolean. If false, that result is returned; if true, getValue is called on e2 and its value is returned.getValue is called on e1; if true, setValue is called on e2. Note that e1 being false prevents any further setting from taking place.
e1 | e2, e1 bor e2

Bitwise or operator

e1 and e2 are interpreted as integers and the result is an integer.Cannot be the top-level expression passed to setValue.
e1 ^ e2, e1 xor e2

Bitwise exclusive-or operator

e1 and e2 are interpreted as integers and the result is an integer.Cannot be the top-level expression passed to setValue.
e1 & e2, e1 band e2

Bitwise and operator

e1 and e2 are interpreted as integers and the result is an integer.Cannot be the top-level expression passed to setValue.
e1 == e2, e1 eq e2

Equality test

e1 != e2, e1 neq e2

Inequality test

Equality is tested for as follows. If either value is null, they are equal if and only if both are null. If they are the same object or the equals() method says they are equal, they are equal. If they are both Numbers, they are equal if their values as double-precision floating point numbers are equal. Otherwise, they are not equal. These rules make numbers compare equal more readily than they would normally, if just using the equals method.Cannot be the top-level expression passed to setValue.
e1 < e2, e1 lt e2

Less than comparison

e1 <= e2, e1 lte e2

Less than or equals comparison

e1 > e2, e1 gt e2

Greater than comparison

e1 >= e2, e1 gte e2

Greater than or equals comparison

e1 in e2

List membership comparison

e1 not in e2

List non-membership comparison

The ordering operators compare with compareTo() if their arguments are non-numeric and implement Comparable; otherwise, the arguments are interpreted as numbers and compared numerically. The in operator is not from Java; it tests for inclusion of e1 in e2, where e2 is interpreted as a collection. This test is not efficient: it iterates the collection. However, it uses the standard OGNL equality test.Cannot be the top-level expression passed to setValue.
e1 << e2, e1 shl e2

Bit shift left

e1 >> e2, e1 shr e2

Bit shift right

e1 >>> e2, e1 ushr e2

Logical shift right

e1 and e2 are interpreted as integers and the result is an integer.Cannot be the top-level expression passed to setValue.
e1 + e2

Addition

e1 - e2

Subtraction

The plus operator concatenates strings if its arguments are non-numeric; otherwise it interprets its arguments as numbers and adds them. The minus operator always works on numbers.Cannot be the top-level expression passed to setValue.
e1* e2

Multiplication

e1 / e2

Division

e1 % e2

Remainder

Multiplication, division, which interpret their arguments as numbers, and remainder, which interprets its arguments as integers.Cannot be the top-level expression passed to setValue.
+ e

Unary plus

- e

Unary minus

! e, not e

Logical not

~ e

Bitwise not

e instanceof class

Class membership

Unary plus is a no-op, it simply returns the value of its argument. Unary minus interprets its argument as a number. Logical not interprets its argument as a boolean. Bitwise not interprets its argument as an integer. The class argument to instanceof is the fully qualified name of a Java class.Cannot be the top-level expression passed to setValue.
e.method(args)

Method call

e.property

Property

e1[ e2 ]

Index

e1.{ e2 }

Projection

e1.{? e2 }

Selection

e1.(e2)

Subexpression evaluation

e1(e2)

Expression evaluation

Generally speaking, navigation chains are evaluated by evaluating the first expression, then evaluating the second one with the result of the first as the source object.Some of these forms can be passed as top-level expressions to setValue and others cannot. Only those chains that end in property references (e.property), indexes (e1[e2]), and subexpressions (e1.(e2)) can be; and expression evaluations can be as well. For the chains, getValue is called on the left-hand expression (e or e1), and then setValue is called on the rest with the result as the target object.
constant

Constant

( e )

Parenthesized expression

method(args)

Method call

property

Property reference

[ e ]

Index reference

{ e, ... }

List creation

#variable

Context variable reference

@class@method(args)

Static method reference

@class@field

Static field reference

new class(args)

Constructor call

new array-component-class[] { e, ... }

Array creation

#{ e1 : e2, ... }

Map creation

#@classname@{ e1 : e2, ... }

Map creation with specific subclass

:[ e ]

Lambda expression definition

Basic expressionsOnly property references (property), indexes ([e]), and variable references (#variable) can be passed as top-level expressions to setValue. For indexes, getValue is called on e, and then the result is used as the property "name" (which might be a String or any other kind of object) to set in the current target object. Variable and property references are set more directly.
[Note]Note

These operators are listed in reverse precedence order