public class ELProcessor extends Object
This class provides a direct and simple interface for
ValueExpression
to a Jakarta Expression Language variable.This API is not a replacement for the APIs in Jakarta Expression Language 2.2. Containers that maintain Jakarta Expression Language environments can continue to do so, without using this API.
For Jakarta Expression Language users who want to manipulate Jakarta Expression Language environments, like adding
custom ELResolver
s, ELManager
can be used.
Since it maintains the state of the Jakarta Expression Language environments, ELProcessor
is not thread
safe. In the simplest case, an instance can be created and destroyed before and after evaluating Jakarta Expression
Language expressions. A more general usage is to use an instance of ELProcessor
for a session, so that
the user can configure the Jakarta Expression Language evaluation environment for that session.
A note about the Jakarta Expression Language expressions strings used in the class. The strings allowed in the methods
getValue(java.lang.String, java.lang.Class<?>)
, setValue(java.lang.String, java.lang.Object)
, and setVariable(java.lang.String, java.lang.String)
are limited to
non-composite expressions, i.e. expressions of the form ${...} or #{...} only. Also, it is not necessary (in fact not
allowed) to bracket the expression strings with ${ or #{ and } in these methods: they will be automatically
bracketed. This reduces the visual cluster, without any lost of functionalities (thanks to the addition of the
concatenation operator).
ELProcessor elp = new ELProcessor(); elp.defineBean("employee", new Employee("Charlie Brown")); String name = elp.eval("employee.name");
Constructor and Description |
---|
ELProcessor() |
Modifier and Type | Method and Description |
---|---|
void |
defineBean(String name,
Object bean)
Define a bean in a local bean repository, hiding other beans of the same name.
|
void |
defineFunction(String prefix,
String function,
Method method)
Define a Jakarta Expression Language function in the local function mapper.
|
void |
defineFunction(String prefix,
String function,
String className,
String method)
Define a Jakarta Expression Language function in the local function mapper.
|
Object |
eval(String expression)
Evaluates an Jakarta Expression Language expression.
|
ELManager |
getELManager()
Return the ELManager used for Jakarta Expression Language processing.
|
Object |
getValue(String expression,
Class<?> expectedType)
Evaluates an Jakarta Expression Language expression, and coerces the result to the specified type.
|
void |
setValue(String expression,
Object value)
Sets an expression with a new value.
|
void |
setVariable(String var,
String expression)
Assign a Jakarta Expression Language expression to a Jakarta Expression Language variable.
|
public ELManager getELManager()
public Object eval(String expression)
expression
- The Jakarta Expression Language expression to be evaluated.public Object getValue(String expression, Class<?> expectedType)
expression
- The Jakarta Expression Language expression to be evaluated.expectedType
- Specifies the type that the resultant evaluation will be coerced to.public void setValue(String expression, Object value)
expression
- The target expressionvalue
- The new value to set.PropertyNotFoundException
- if one of the property resolutions failed because a specified variable or property
does not exist or is not readable.PropertyNotWritableException
- if the final variable or property resolution failed because the specified
variable or property is not writable.ELException
- if an exception was thrown while attempting to set the property or variable. The thrown exception
must be included as the cause property of this exception, if available.public void setVariable(String var, String expression)
null
, the variable will be removed.var
- The name of the variable.expression
- The Jakarta Expression Language expression to be assigned to the variable.public void defineFunction(String prefix, String function, String className, String method) throws ClassNotFoundException, NoSuchMethodException
prefix
- The namespace for the function or "" for no namesapce.function
- The name of the function. If empty (""), the method name is used as the function name.className
- The full Java class name that implements the function.method
- The name (specified without parenthesis) or the signature (as in the Java Language Spec) of the static
method that implements the function. If the name (e.g. "sum") is given, the first declared method in class that
matches the name is selected. If the signature (e.g. "int sum(int, int)" ) is given, then the declared method with
the signature is selected.NullPointerException
- if any of the arguments is null.ClassNotFoundException
- if the specified class does not exists.NoSuchMethodException
- if the method (with or without the signature) is not a declared method of the class, or
if the method signature is not valid, or if the method is not a static method.public void defineFunction(String prefix, String function, Method method) throws NoSuchMethodException
prefix
- The namespace for the function or "" for no namesapce.function
- The name of the function. If empty (""), the method name is used as the function name.method
- The java.lang.reflect.Method
instance of the method that implements the function.NullPointerException
- if any of the arguments is null.NoSuchMethodException
- if the method is not a static methodCopyright © 2018,2020 Eclipse Foundation.
Use is subject to license terms.