public final class ExpressionEvaluator extends Object
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("intValue", 5);
parameters.put("stringValue", "foobar");
ExpressionEvaluator ee = new ExpressionEvaluator(parameters);
org.junit.Assert.assertEquals("foobar", ee.evaluate("${stringValue}", String.class));
org.junit.Assert.assertEquals(Integer.valueOf(5), ee.evaluate("${intValue}", Integer.class));
org.junit.Assert.assertTrue(ee.evaluate("${intValue eq (3+2)}"));
org.junit.Assert.assertTrue(ee.evaluate("${(1 eq (2/2)) && (2<3)}"));
org.junit.Assert.assertTrue(ee.evaluate("${stringValue.length() == 6}"));
| Constructor and Description |
|---|
ExpressionEvaluator(Map<String,?> variables)
Creates a new
ExpressionEvaluator using the given variables. |
ExpressionEvaluator(javax.el.VariableMapper variableMapper)
Creates a new
ExpressionEvaluator using the given VariableMapper. |
| Modifier and Type | Method and Description |
|---|---|
void |
addFunction(String name,
Method method)
Adds a function to this
ExpressionEvaluator. |
void |
addFunction(String prefix,
String name,
Method method)
Adds a prefixed function to this
ExpressionEvaluator. |
boolean |
evaluate(String expression)
Evaluates the given expression to a
boolean. |
<T> T |
evaluate(String expression,
Class<T> targetType)
Evaluates the given expression to an object of the given targetType
|
Boolean |
getBoolean(String expression)
Evaluates the given expression to a
Boolean, if possible. |
String |
getString(String expression)
Evaluates the given expression to a
String, if possible. |
boolean |
isExpression(String value)
Checks whether the given
String is a valid expression (starts with ${ and ends with }). |
void |
setVariable(String name,
Object value)
Sets the variable with the given name to the given value.
|
void |
setVariables(Map<String,?> variables)
Calls
setVariable(String, Object) for each entry of the given map. |
public ExpressionEvaluator(Map<String,?> variables)
ExpressionEvaluator using the given variables.variables - a Map of variables to usepublic ExpressionEvaluator(javax.el.VariableMapper variableMapper)
ExpressionEvaluator using the given VariableMapper.variableMapper - the VariableMapper to usepublic final <T> T evaluate(String expression, Class<T> targetType)
expression - the expression to parsetargetType - the expected target typeNullPointerException - if targetType is nulljavax.el.ELException - if the expression has syntactical errorspublic final boolean evaluate(String expression)
boolean.expression - the expression to evaluatepublic final void addFunction(String prefix, String name, Method method)
ExpressionEvaluator. The given Method must be
static.
ExpressionEvaluator ee = ...;
ee.addFunction("math","max", Math.class.getMethod("max", int.class, int.class));
org.junit.Assert.assertEquals(Integer.valueOf(47), ee.evaluate("${math:max(47,11)}", Integer.class));
prefix - the prefix for the methodname - the name of the methodmethod - the Method to add as a functionpublic final void addFunction(String name, Method method)
ExpressionEvaluator. The given Method must be static.static.
ExpressionEvaluator ee = ...;
ee.addFunction("max", Math.class.getMethod("max", int.class, int.class));
org.junit.Assert.assertEquals(Integer.valueOf(47), ee.evaluate("${max(47,11)}", Integer.class));
name - the name of the methodmethod - the Method to add as a functionpublic final void setVariable(String name, Object value)
name - the name of the variablevalue - the value of the variable (must not be null)public final void setVariables(Map<String,?> variables)
setVariable(String, Object) for each entry of the given map.variables - a Map of variablespublic boolean isExpression(String value)
String is a valid expression (starts with ${ and ends with }).value - the String to checktrue if the given value is a valid expression, false otherwisepublic String getString(String expression)
String, if possible.expression - the expression to evaluateString evaluated from the expression, in cases it's a valid expression
isExpression(String)public Boolean getBoolean(String expression)
Boolean, if possible.expression - the expression to evaluateBoolean evaluated from the expression, in cases it's a valid expression
Boolean.valueOf(expression) if the expression is not null but is not a valid
expression
null if the expression is null
isExpression(String)Copyright © 2011–2022 aiticon GmbH. All rights reserved.