Unicode escape sequences consist of:
a backslash '\'
a 'u'
4 hexadecimal digits ([0-9],[A-H],[a-h]).
Such sequences represent the UTF-16 encoding of a Unicode character,
for example,
'a'
is equivalent to
'\u0061'
.
Start and end with
`
delimiter - back-quote -, e.g.
`Hello world`
The escape character is
\
(backslash); Unicode escape sequences can also be used.
These format literals can span multiple lines and allow Unified JEXL expressions (JSTL like expressions)
to be interpolated. If a variable
user
valued
JEXL
is present in the environment - whether
as a local or global variable -, the format
`Hello ${user}`
will evaluate as
Hello JEXL
.
A
[
followed by zero or more expressions separated by
,
and ending
with
]
, e.g.
[ 1, 2, "three" ]
This syntax creates an
Object[]
.
Empty array literal can be specified as
[]
with result of creating
Object[]
JEXL will attempt to strongly type the array; if all entries are of the same class or if all
entries are Number instance, the array literal will be an
MyClass[]
in the former
case, a
Number[]
in the latter case.
Furthermore, if all entries in the array literal are of the same class
and that class has an equivalent primitive type, the array returned will be a primitive array. e.g.
[1, 2, 3]
will be interpreted as
int[]
.
A
[
followed by zero or more expressions separated by
,
and ending
with
,...]
, e.g.
[ 1, 2, "three",...]
This syntax creates an
ArrayList<Object>
.
Empty list literal can be specified as
[...]
A
{
followed by zero or more expressions separated by
,
and ending
with
}
, e.g.
{ "one" , 2, "more"}
This syntax creates a
HashSet<Object>
.
Empty set literal can be specified as
{}
A
{
followed by zero or more sets of
key : value
pairs separated by
,
and ending
with
}
, e.g.
{ "one" : 1, "two" : 2, "three" : 3, "more": "many more" }
This syntax creates a
HashMap<Object,Object>
.
Empty map literal can be specified as
{:}
1 .. 42
This syntax creates a 'range' object in the form of a java iterable which can be used in for statement, e.g.
for (i : 1..42) a = a + b[i]
An instance of class C and the derived JexlArithmetic overloads a method 'public boolean empty(C arg)'
that returns true when the argument is considered empty
An empty string
An array of length zero
A collection of size zero
An empty map
Defining a method 'public boolean isEmpty()'
that returns true when the instance is considered empty
This is false in other cases (besides errors).
empty(arg)
0 if the argument is null
The result of calling a method from a derived JexlArithmetic overload 'public int size(C arg)',
C being the class of the argument
Length of an array
Length of a string
Size of a Collection
Size of a Map
The result of calling a method 'public int size()' defined by the argument class
This returns 0 in other cases (besides errors).
size("Hello")
returns 5.
Creates a new instance using a fully-qualified class name or Class:
new("java.lang.Double", 10)
returns 10.0.
Note that the first argument of
new
can be a variable or any
expression evaluating as a String or Class; the rest of the arguments are used
as arguments to the constructor for the class considered.
In case of multiple constructors, JEXL will make the best effort to find
the most appropriate non ambiguous constructor to call.
Alternatively, using
#pragma jexl.import java.lang
code>, one can use the
following syntax:
new Double(10)
.
Top level function is a function which can be invoked without specifying a namespace.
Top level function can be defined by the function definition method inside the script
A
JexlContext
can define methods which can be invoked as top level functions.
This can allow expressions like:
string(23.0)
Another way to define top level function is to register to
JexlEngine
objects or classes
with null namespace.
A
JexlEngine
can register objects or classes used as function namespaces.
This can allow expressions like:
math:cosinus(23.0)
The usual
&&
operator can be used as well as the word
and
, e.g.
cond1 and cond2
and
cond1 && cond2
are equivalent.
Note that this operator can not be overloaded
The usual
||
operator can be used as well as the word
or
, e.g.
cond1 or cond2
and
cond1 || cond2
are equivalent.
Note that this operator can not be overloaded
The usual
!
operator can be used as well as the word
not
, e.g.
!cond1
and
not cond1
are equivalent.
Note that this operator can not be overloaded
The left shift operator (<<) shifts the first operand the specified number of bits to the left.
1 << 2
= 4
The right shift operator (>>) shifts the first operand the specified number of bits to the right.
4 >> 2
= 1
(zero-fill right shift) shifts the first operand the specified number of bits to the right.
The sign bit becomes 0, so the result is always non-negative.
The usual ternary conditional operator
condition ? if_true : if_false
operator can be
used as well as the abbreviation
value ?: if_false
which returns the
value
if
its evaluation is defined, non-null and non-false, e.g.
val1 ? val1 : val2
and
val1 ?: val2
are equivalent.
NOTE:
The condition will evaluate to
false
when it
refers to an undefined variable or
null
for all
JexlEngine
flag combinations. This allows explicit syntactic leniency and treats the condition
'if undefined or null or false' the same way in all cases.
Note that this operator can not be overloaded
The null coalescing operator returns the result of its first operand if it is defined and is not null.
When
x
and
y
are null or undefined,
x ?? 'unknown or null x'
evaluates as
'unknown or null x'
y ?? "default"
evaluates as
"default"
.
When
var x = 42
and
var y = "forty-two"
,
x??"other"
evaluates as
42
and
y??"other"
evaluates as
"forty-two"
.
NOTE:
this operator does not behave like the ternary conditional since it
does not coerce the first argument to a boolean to evaluate the condition.
When
var x = false
and
var y = 0
,
x??true
evaluates as
false
and
y??1
evaluates as
0
.
Note that this operator can not be overloaded
The equality
==
operator checks whether its two operands are equal, returning a
Boolean result. Unlike the strict equality operator, it attempts to convert and compare
operands that are of different types.
null
is only ever equal to null, that is if you compare null
to any non-null value, the result is false.
Equality uses the java
equals
method
The inequality
!=
operator checks whether its two operands are not equal,
returning a Boolean result. Unlike the strict inequality operator, it attempts to convert and
compare operands that are of different types.
Strict-Equality
The strict equality
===
operator checks whether its two operands are equal,
returning a Boolean result. Unlike the equality operator, the strict equality operator always
considers operands of different types to be different.
The strict inequality
!==
operator checks whether its two operands are not equal,
returning a Boolean result. Unlike the inequality operator, the strict inequality operator
always considers operands of different types to be different
The usual
<=
operator can be used as well as the abbreviation
le
.
For example
val1 <= val2
and
val1 le val2
are equivalent.
The usual
>=
operator can be used as well as the abbreviation
ge
.
For example
val1 >= val2
and
val1 ge val2
are equivalent.
The syntactically Perl inspired
=~
operator can be used to check that a
string
matches
a regular expression (expressed either a Java String or a java.util.regex.Pattern).
For example
"abcdef" =~ "abc.*
returns
true
.
It also checks whether any collection, set or map (on keys) contains a value or not; in that case, it behaves
as an "in" operator.
Note that arrays and user classes exposing a public 'contains' method will allow their instances
to behave as right-hand side operands of this operator.
"a" =~ ["a","b","c","d","e",f"]
returns
true
.
The syntactically Perl inspired
!~
operator can be used to check that a
string
does not
match a regular expression (expressed either a Java String or a java.util.regex.Pattern).
For example
"abcdef" !~ "abc.*
returns
false
.
It also checks whether any collection, set or map (on keys) does not contain a value; in that case, it behaves
as "not in" operator.
Note that arrays and user classes exposing a public 'contains' method will allow their instances
to behave as right-hand side operands of this operator.
"a" !~ ["a","b","c","d","e",f"]
returns
false
.
The
=^
operator is a short-hand for the 'startsWith' method.
For example,
"abcdef" =^ "abc"
returns
true
.
Note that through duck-typing, user classes exposing a public 'startsWith' method will allow their instances
to behave as left-hand side operands of this operator.
The
=$
operator is a short-hand for the 'endsWith' method.
For example,
"abcdef" =$ "def"
returns
true
.
Note that through duck-typing, user classes exposing an 'endsWith' method will allow their instances
to behave as left-hand side operands of this operator.
Some operators exist in side-effect forms.
Their default behavior is to execute the operator and assign the left-hand side with the result.
For instance
a += 2
is equivalent to
a = a + 2
The list of operators is:
The unary
+
operator is used. It performs an integral promotion meaning
that byte, short, char arguments will be promoted to integer as a result.
For example
+12
or
+(a * b)
instanceof/!instanceof
The instanceof operator allows to check whether an object belongs to a certain class.
It is using Class.isInstance to perform the check. As a convenience, {{ !instanceof }} is
supported as well.
Map elements are accessed using square brackets, e.g.
map[0]; map['name']; map[var];
Note that
map['7']
and
map[7]
refer to different elements.
Map elements with a numeric key may also be accessed using a dotted numeral, e.g.
map[0]
and
map.0
are equivalent.
Note that
map.1
and
map.01
refer to different elements,
while
map.1
and
map[01]
are equivalent.
Properties of JavaBean objects that define appropriate getter methods can be accessed
using either square brackets or a dotted numeral, e.g.
foo['bar']
and
foo.bar
are equivalent.
The appropriate
Foo.getBar()
method will be called.
Note that both
foo.Bar
and
foo.bar
can be used
Indexed properties of JavaBean objects that define appropriate getter methods can be accessed
using either square brackets or a dotted numeral, e.g.
x.attribute['name']
and
x.attribute.name
are equivalent.
The appropriate
Foo.getAttribute(String index)
method will be called
Public fields of java objects can be accessed using either square brackets or a dotted numeral, e.g.
foo['bar']
and
foo.bar
are equivalent.
Properties of Java classes that define public
Object get(String name)
method can be accessed
using either square brackets or a dotted numeral, e.g.
foo['bar']
and
foo.bar
are equivalent.
The appropriate
Foo.get(String index)
method will be called with the argument of
"bar"
String
Loop through items of an Array, Collection, Map, Iterator or Enumeration, e.g.
for (let item : list) {
x = x + item;
Where
list
is a context variable pointing to any iterable structure.
The following syntax using a context variable is also supported:
for (item : list) {
x = x + item;
Note that the loop variable
item
is accessible after loop evaluation
Finally, the conventional syntax using a local variable, initial value, loop condition and
loop step is supported.
for (let i = 0; i < size(list); ++i) {
x = x + list[i];
Where
list
is a local variable pointing to an array-like structure.
The JEXL 1.1 syntax using
foreach(item in list)
is now
unsupported
.
Within loops (do/while/for), terminates execution of the statements in the current iteration
and skips to the next loop iteration.
let text = '';
for (let i : (4..2)) {
if (i === 3) {
continue;
text += i;
text;
Will evaluate as "42".
Within loops (do/while/for), terminates execution of the statements in the current iteration
and exits the loop.
let i = 33;
while (i < 66) {
if (i == 42) {
break;
i += 1;
Will evaluate as 42.
The return statement ends execution and specifies a value to be returned to the script
or lambda caller.
function f(x) {
if (x == 42) {
return "The answer to life, the universe, and everything";
return x
f(41);
Will evaluate as 41.
The throw statement throws an object. Execution of the current script will stop
(the statements after throw won't be executed), and control will be passed to the first catch
block in the call stack. If no catch block exists among caller functions, script execution will
terminate.
try/catch/finally
The try...catch statement is comprised of a try block and either a catch block, a finally block,
or both. The code in the try block is executed first, and if it throws an exception, the code in
the catch block will be executed.
The code in the finally block will always be executed before control flow exits the entire construct.
try {
return 42/0;
} catch (let e) {
// handle the error
The following syntax is also supported:
try {
return 42/0;
} catch (var e) {
// handle the error
try-with-resources
The try-with-resources statement is a try statement that declares one or more resources.
The try-with-resources statement ensures that each resource is closed at
the end of the statement; any object that exposes a
close()
method will see that
method invoked.
let g = open("g");
try(let f = open("f"), g) {
// more code