use of org.springframework.expression.Expression in project spring-framework by spring-projects.
the class CachedExpressionEvaluator method getExpression.
/**
* Return the {@link Expression} for the specified SpEL value
* <p>Parse the expression if it hasn't been already.
* @param cache the cache to use
* @param elementKey the element on which the expression is defined
* @param expression the expression to parse
*/
protected Expression getExpression(Map<ExpressionKey, Expression> cache, AnnotatedElementKey elementKey, String expression) {
ExpressionKey expressionKey = createKey(elementKey, expression);
Expression expr = cache.get(expressionKey);
if (expr == null) {
expr = getParser().parseExpression(expression);
cache.put(expressionKey, expr);
}
return expr;
}
use of org.springframework.expression.Expression in project spring-boot by spring-projects.
the class BindingPreparationTests method testExpressionLists.
@Test
@Ignore("Work in progress")
public void testExpressionLists() throws Exception {
TargetWithNestedMapOfListOfString target = new TargetWithNestedMapOfListOfString();
LinkedHashMap<String, List<String>> map = new LinkedHashMap<>();
// map.put("foo", Arrays.asList("bar"));
target.setNested(map);
SpelExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext(target);
context.addPropertyAccessor(new MapAccessor());
Expression expression = parser.parseExpression("nested.foo");
assertThat(expression.getValue(context)).isNotNull();
}
use of org.springframework.expression.Expression in project spring-framework by spring-projects.
the class MapAccessorTests method mapAccessorCompilable.
@Test
public void mapAccessorCompilable() {
Map<String, Object> testMap = getSimpleTestMap();
StandardEvaluationContext sec = new StandardEvaluationContext();
sec.addPropertyAccessor(new MapAccessor());
SpelExpressionParser sep = new SpelExpressionParser();
// basic
Expression ex = sep.parseExpression("foo");
assertEquals("bar", ex.getValue(sec, testMap));
assertTrue(SpelCompiler.compile(ex));
assertEquals("bar", ex.getValue(sec, testMap));
// compound expression
ex = sep.parseExpression("foo.toUpperCase()");
assertEquals("BAR", ex.getValue(sec, testMap));
assertTrue(SpelCompiler.compile(ex));
assertEquals("BAR", ex.getValue(sec, testMap));
// nested map
Map<String, Map<String, Object>> nestedMap = getNestedTestMap();
ex = sep.parseExpression("aaa.foo.toUpperCase()");
assertEquals("BAR", ex.getValue(sec, nestedMap));
assertTrue(SpelCompiler.compile(ex));
assertEquals("BAR", ex.getValue(sec, nestedMap));
// avoiding inserting checkcast because first part of expression returns a Map
ex = sep.parseExpression("getMap().foo");
MapGetter mapGetter = new MapGetter();
assertEquals("bar", ex.getValue(sec, mapGetter));
assertTrue(SpelCompiler.compile(ex));
assertEquals("bar", ex.getValue(sec, mapGetter));
}
use of org.springframework.expression.Expression in project spring-framework by spring-projects.
the class AbstractExpressionTests method evaluate.
/**
* Evaluate an expression and check that the actual result matches the
* expectedValue and the class of the result matches the expectedClassOfResult.
* This method can also check if the expression is writable (for example,
* it is a variable or property reference).
* @param expression the expression to evaluate
* @param expectedValue the expected result for evaluating the expression
* @param expectedClassOfResult the expected class of the evaluation result
* @param shouldBeWritable should the parsed expression be writable?
*/
public void evaluate(String expression, Object expectedValue, Class<?> expectedClassOfResult, boolean shouldBeWritable) {
Expression expr = parser.parseExpression(expression);
if (expr == null) {
fail("Parser returned null for expression");
}
if (DEBUG) {
SpelUtilities.printAbstractSyntaxTree(System.out, expr);
}
Object value = expr.getValue(eContext);
if (value == null) {
if (expectedValue == null) {
// no point doing other checks
return;
}
assertEquals("Expression returned null value, but expected '" + expectedValue + "'", expectedValue, null);
}
Class<? extends Object> resultType = value.getClass();
if (expectedValue instanceof String) {
assertEquals("Did not get expected value for expression '" + expression + "'.", expectedValue, AbstractExpressionTests.stringValueOf(value));
} else {
assertEquals("Did not get expected value for expression '" + expression + "'.", expectedValue, value);
}
assertEquals("Type of the result was not as expected. Expected '" + expectedClassOfResult + "' but result was of type '" + resultType + "'", expectedClassOfResult.equals(resultType), true);
boolean isWritable = expr.isWritable(eContext);
if (isWritable != shouldBeWritable) {
if (shouldBeWritable)
fail("Expected the expression to be writable but it is not");
else
fail("Expected the expression to be readonly but it is not");
}
}
use of org.springframework.expression.Expression in project spring-framework by spring-projects.
the class AbstractExpressionTests method evaluate.
/**
* Evaluate an expression and check that the actual result matches the
* expectedValue and the class of the result matches the expectedClassOfResult.
* @param expression the expression to evaluate
* @param expectedValue the expected result for evaluating the expression
* @param expectedResultType the expected class of the evaluation result
*/
public void evaluate(String expression, Object expectedValue, Class<?> expectedResultType) {
Expression expr = parser.parseExpression(expression);
if (expr == null) {
fail("Parser returned null for expression");
}
if (DEBUG) {
SpelUtilities.printAbstractSyntaxTree(System.out, expr);
}
Object value = expr.getValue(eContext);
// Check the return value
if (value == null) {
if (expectedValue == null) {
// no point doing other checks
return;
}
assertEquals("Expression returned null value, but expected '" + expectedValue + "'", expectedValue, null);
}
Class<?> resultType = value.getClass();
assertEquals("Type of the actual result was not as expected. Expected '" + expectedResultType + "' but result was of type '" + resultType + "'", expectedResultType, resultType);
if (expectedValue instanceof String) {
assertEquals("Did not get expected value for expression '" + expression + "'.", expectedValue, AbstractExpressionTests.stringValueOf(value));
} else {
assertEquals("Did not get expected value for expression '" + expression + "'.", expectedValue, value);
}
}
Aggregations