Top Description Inners Fields Constructors Methods
org.apache.commons.jexl2

public Class JexlEngine

extends Object
Class Inheritance
Imports
java.io.BufferedReader, .File, .FileReader, .IOException, .InputStreamReader, .StringReader, .Reader, java.net.URL, .URLConnection, java.lang.ref.SoftReference, java.util.ArrayList, .Map, .Set, .Collections, .LinkedHashMap, .LinkedHashSet, .List, .Map.Entry, org.apache.commons.logging.Log, .LogFactory, org.apache.commons.jexl2.parser.ParseException, .Parser, .JexlNode, .TokenMgrError, .ASTJexlScript, .ASTArrayAccess, .ASTIdentifier, .ASTReference, org.apache.commons.jexl2.introspection.Uberspect, .UberspectImpl, .JexlMethod

Creates and evaluates Expression and Script objects. Determines the behavior of Expressions & Scripts during their evaluation with respect to:

The setSilent and setLenient methods allow to fine-tune an engine instance behavior according to various error control needs. The lenient/strict flag tells the engine when and if null as operand is considered an error, the silent/verbose flag tells the engine what to do with the error (log as warning or throw exception).

Note that methods that evaluate expressions may throw unchecked exceptions; The JexlException are thrown in "non-silent" mode but since these are RuntimeException, user-code should catch them wherever most appropriate.

Since
2.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
JexlEngine.Frame

A call frame, created from a scope, stores the arguments and local variables as "registers".

public static class
JexlEngine.Scope

A script scope, stores the declaration of parameters and local variables.

protected class
JexlEngine.SoftCache<
the cache key entry type
K
,
the cache key value type
V
>

A soft reference on cache.

private static class
JexlEngine.UberspectHolder

Gets the default instance of Uberspect.

Field Summary

Modifier and TypeField and Description
protected final JexlArithmetic
arithmetic

The JexlArithmetic instance.

protected JexlEngine.SoftCache<String, ASTJexlScript>
cache

The expression cache.

protected volatile boolean
debug

Whether error messages will carry debugging information.

public static final JexlContext
EMPTY_CONTEXT

An empty/static/non-mutable JexlContext used instead of null context.

protected Map<String, Object>
functions

The map of 'prefix:function' to object implementing the functions.

private static final float
LOAD_FACTOR

The default cache load factor.

protected final Log
logger

The Log to which all JexlEngine messages will be logged.

protected final Parser
parser

The singleton ExpressionFactory also holds a single instance of Parser.

protected volatile boolean
silent

Whether expressions evaluated by this engine will throw exceptions (false) or return null (true) on errors.

protected final Uberspect
uberspect

The Uberspect instance.

Constructor Summary

AccessConstructor and Description
public
JexlEngine()

Creates an engine with default arguments.

public
JexlEngine(Uberspect
to allow different introspection behaviour
anUberspect
,
JexlArithmetic
to allow different arithmetic behaviour
anArithmetic
,
Map<String, Object>
an optional map of functions (@link setFunctions)
theFunctions
,
Log
the logger for various messages
log
)

Creates a JEXL engine using the provided Uberspect, (@link JexlArithmetic), a function map and logger.

Method Summary

Modifier and TypeMethod and Description
public static String

Returns:

trimmed expression ending in a semi-colon
cleanExpression
(CharSequence
expression to clean
str
)

Trims the expression from front & ending spaces.

public void
clearCache()

Clears the expression cache.

protected <
the key type
K
,
the value type
V
>
Map<K, V>

Returns:

a Map usable as a cache bounded to the given size
createCache
(final int
the cache size, must be > 0
cacheSize
)

Creates a cache.

protected Expression

Returns:

the script instance
createExpression
(ASTJexlScript
the parse AST tree
tree
,
String
the script text
text
)

An overridable through covariant return Expression creator.

public Expression

Returns:

An Expression object which can be evaluated with a JexlContext
createExpression
(String
A String containing valid JEXL syntax
expression
)

Creates an Expression from a String containing valid JEXL syntax.

public Expression

Returns:

An Expression object which can be evaluated with a JexlContext
createExpression
(String
A String containing valid JEXL syntax
expression
,
JexlInfo
An info structure to carry debugging information if needed
info
)

Creates an Expression from a String containing valid JEXL syntax.

protected JexlInfo

Returns:

a JexlInfo instance
createInfo
(String
url/file name
fn
,
int
line number
l
,
int
column number
c
)

Creates a JexlInfo instance.

protected Interpreter

Returns:

an Interpreter
createInterpreter
(JexlContext
a JexlContext; if null, the EMPTY_CONTEXT is used instead.
context
)

Creates an interpreter.

protected Interpreter

Returns:

an Interpreter
createInterpreter
(JexlContext
a JexlContext; if null, the EMPTY_CONTEXT is used instead.
context
,
boolean
whether the interpreter runs in strict mode
strictFlag
,
boolean
whether the interpreter runs in silent mode
silentFlag
)

Creates an interpreter.

public Script

Returns:

A Script which can be executed using a JexlContext.
createScript
(String
A String containing valid JEXL syntax
scriptText
)

Creates a Script from a String containing valid JEXL syntax.

public Script

Returns:

A Script which can be executed using a JexlContext.
createScript
(String
A String containing valid JEXL syntax
scriptText
,
JexlInfo
An info structure to carry debugging information if needed
info
)

Creates a Script from a String containing valid JEXL syntax.
public Script

Returns:

A Script which can be executed using a JexlContext.
createScript
(String
A String containing valid JEXL syntax
scriptText
,
String...
the script parameter names
names
)

Creates a Script from a String containing valid JEXL syntax.

public Script

Returns:

A Script which can be executed using a JexlContext.
createScript
(String
A String containing valid JEXL syntax
scriptText
,
JexlInfo
An info structure to carry debugging information if needed
info
,
String[]
the script parameter names
names
)

Creates a Script from a String containing valid JEXL syntax.

protected Script

Returns:

the script instance
createScript
(ASTJexlScript
the parse AST tree
tree
,
String
the script text
text
)

An overridable through covariant return Script creator.

public Script

Returns:

A Script which can be executed with a JexlContext.
createScript
(File
A File containing valid JEXL syntax. Must not be null. Must be a readable file.
scriptFile
)

Creates a Script from a File containing valid JEXL syntax.

public Script

Returns:

A Script which can be executed with a JexlContext.
createScript
(URL
A URL containing valid JEXL syntax. Must not be null. Must be a readable file.
scriptUrl
)

Creates a Script from a URL containing valid JEXL syntax.

protected JexlInfo

Returns:

an Info if debug is set, null otherwise
debugInfo
()

Creates and fills up debugging information.

protected Object

Returns:

the created object instance or null on failure when silent
doCreateInstance
(Object
the class to instantiate
clazz
,
Object...
the constructor arguments
args
)

Creates a new instance of an object using the most appropriate constructor based on the arguments.

public JexlArithmetic

Returns:

the arithmetic
getArithmetic
()

Gets this engine underlying arithmetic.

public Map<String, Object>

Returns:

the map passed in setFunctions or the empty map if the original was null.
getFunctions
()

Retrieves the map of function namespaces.

protected String[]

Returns:

the local variables array which may be empty (but not null) if no local variables were defined
getLocalVariables
(Script
the script
script
)

Gets the array of local variable from a script.

protected String[]

Returns:

the parameters which may be empty (but not null) if no parameters were defined
getParameters
(Script
the script
script
)

Gets the array of parameters from a script.

public Object

Returns:

the value of the property
getProperty
(Object
the bean to get properties from
bean
,
String
the property expression
expr
)

Accesses properties of a bean using an expression.

public Object

Returns:

the value of the property
getProperty
(JexlContext
the evaluation context
context
,
Object
the bean to get properties from
bean
,
String
the property expression
expr
)

Accesses properties of a bean using an expression.

public static Uberspect

Returns:

Uberspect the default uberspector instance.
getUberspect
(Log
the logger to use for the underlying Uberspect
logger
)

Gets the default instance of Uberspect.

public Uberspect

Returns:

the uberspect
getUberspect
()

Gets this engine underlying uberspect.

public Set<List<String>>

Returns:

the set of variables, each as a list of strings (ant-ish variables use more than 1 string) or the empty set if no variables are used
getVariables
(Script
the script
script
)

Gets the list of variables accessed by a script.

protected void
getVariables(JexlNode
the node
node
,
Set<List<String>>
the set of variable being filled
refs
,
List<String>
the current variable being filled
ref
)

Fills up the list of variables accessed by a node.

public Object

Returns:

the method returned value or null if it failed and engine is silent
invokeMethod
(Object
the method's invoker object
obj
,
String
the method's name
meth
,
Object...
the method's arguments
args
)

Invokes an object's method by name and arguments.

public boolean

Returns:

true if debug is on, false otherwise
isDebug
()

Checks whether this engine is in debug mode.

public boolean

Returns:

true if lenient, false if strict
isLenient
()

Checks whether this engine considers unknown variables, methods and constructors as errors.

public boolean

Returns:

true if silent, false (default) otherwise
isSilent
()

Checks whether this engine throws JexlException during evaluation.

public final boolean

Returns:

true for strict, false for lenient
isStrict
()

Checks whether this engine behaves in strict or lenient mode.

public <
the type of object
T
>
T

Returns:

the created object instance or null on failure when silent
newInstance
(Class<? extends T>
the class to instantiate
clazz
,
Object...
the constructor arguments
args
)

Creates a new instance of an object using the most appropriate constructor based on the arguments.

public Object

Returns:

the created object instance or null on failure when silent
newInstance
(String
the name of the class to instantiate resolved through this engine's class loader
clazz
,
Object...
the constructor arguments
args
)

Creates a new instance of an object using the most appropriate constructor based on the arguments.

protected ASTJexlScript

Returns:

the parsed tree
parse
(CharSequence
the expression to parse
expression
,
JexlInfo
debug information structure
info
)

Deprecated Use parse(CharSequence, JexlInfo, Scope) instead
Parses an expression.
protected ASTJexlScript

Returns:

the parsed tree
parse
(CharSequence
the expression to parse
expression
,
JexlInfo
debug information structure
info
,
JexlEngine.Scope
the script frame to use
frame
)

Parses an expression.

public static String

Returns:

the contents of the reader as a String.
readerToString
(Reader
to be read.
scriptReader
)

Read from a reader into a local buffer and return a String with the contents of the reader.

public void
setCache(int
if not strictly positive, no cache is used.
size
)

Sets a cache for expressions of the defined size.

public void
setClassLoader(ClassLoader
the class loader to use
loader
)

Sets the class loader used to discover classes in 'new' expressions.

public void
setDebug(boolean
true implies debug is on, false implies debug is off.
flag
)

Sets whether this engine reports debugging information when error occurs.

public void
setFunctions(Map<String, Object>
the map of functions that should not mutate after the call; if null is passed, the empty collection is used.
funcs
)

Sets the map of function namespaces.

public void
setLenient(boolean
true means no JexlException will occur, false allows them
flag
)

Sets whether this engine considers unknown variables, methods and constructors as errors or evaluates them as null or zero.

public void
setProperty(Object
the bean to set properties in
bean
,
String
the property expression
expr
,
Object
the value of the property
value
)

Assign properties of a bean using an expression.

public void
setProperty(JexlContext
the evaluation context
context
,
Object
the bean to set properties in
bean
,
String
the property expression
expr
,
Object
the value of the property
value
)

Assign properties of a bean using an expression.

public void
setSilent(boolean
true means no JexlException will occur, false allows them
flag
)

Sets whether this engine throws JexlException during evaluation when an error is triggered.

public final void
setStrict(boolean
true for strict, false for lenient
flag
)

Sets whether this engine behaves in strict or lenient mode.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait