Search in sources :

Example 1 with Expression

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;
}
Also used : Expression(org.springframework.expression.Expression)

Example 2 with Expression

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();
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Expression(org.springframework.expression.Expression) ArrayList(java.util.ArrayList) List(java.util.List) MapAccessor(org.springframework.context.expression.MapAccessor) LinkedHashMap(java.util.LinkedHashMap) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with Expression

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));
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Expression(org.springframework.expression.Expression) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 4 with Expression

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");
    }
}
Also used : Expression(org.springframework.expression.Expression)

Example 5 with Expression

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);
    }
}
Also used : Expression(org.springframework.expression.Expression)

Aggregations

Expression (org.springframework.expression.Expression)288 Test (org.junit.Test)228 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)172 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)155 SpelExpression (org.springframework.expression.spel.standard.SpelExpression)114 ExpressionParser (org.springframework.expression.ExpressionParser)76 EvaluationContext (org.springframework.expression.EvaluationContext)52 ArrayList (java.util.ArrayList)32 CompoundExpression (org.springframework.expression.spel.ast.CompoundExpression)18 HashMap (java.util.HashMap)17 EvaluationException (org.springframework.expression.EvaluationException)17 Authentication (org.springframework.security.core.Authentication)16 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)14 List (java.util.List)13 Map (java.util.Map)13 ParseException (org.springframework.expression.ParseException)11 LinkedHashMap (java.util.LinkedHashMap)10 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)10 MethodInvocation (org.aopalliance.intercept.MethodInvocation)9 SimpleMethodInvocation (org.springframework.security.util.SimpleMethodInvocation)9