Search in sources :

Example 51 with EvaluationContext

use of org.springframework.expression.EvaluationContext in project spring-framework by spring-projects.

the class MethodReference method getValueInternal.

@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
    EvaluationContext evaluationContext = state.getEvaluationContext();
    Object value = state.getActiveContextObject().getValue();
    TypeDescriptor targetType = state.getActiveContextObject().getTypeDescriptor();
    Object[] arguments = getArguments(state);
    TypedValue result = getValueInternal(evaluationContext, value, targetType, arguments);
    updateExitTypeDescriptor();
    return result;
}
Also used : TypeDescriptor(org.springframework.core.convert.TypeDescriptor) EvaluationContext(org.springframework.expression.EvaluationContext) TypedValue(org.springframework.expression.TypedValue)

Example 52 with EvaluationContext

use of org.springframework.expression.EvaluationContext in project spring-framework by spring-projects.

the class SpelReproTests method SPR10417_maps.

@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void SPR10417_maps() {
    Map map1 = new HashMap();
    map1.put("A", 65);
    map1.put("B", 66);
    map1.put("X", 66);
    Map map2 = new HashMap();
    map2.put("X", 66);
    EvaluationContext context = new StandardEvaluationContext();
    context.setVariable("map1", map1);
    context.setVariable("map2", map2);
    // #this should be the element from list1
    Expression ex = parser.parseExpression("#map1.?[#map2.containsKey(#this.getKey())]");
    Object result = ex.getValue(context);
    assertEquals("{X=66}", result.toString());
    ex = parser.parseExpression("#map1.?[#map2.containsKey(key)]");
    result = ex.getValue(context);
    assertEquals("{X=66}", result.toString());
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Expression(org.springframework.expression.Expression) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.Test)

Example 53 with EvaluationContext

use of org.springframework.expression.EvaluationContext in project spring-framework by spring-projects.

the class SpelReproTests method conversionPriority_8224.

/**
	 * Test whether {@link ReflectiveMethodResolver} follows Java Method Invocation
	 * Conversion order. And more precisely that widening reference conversion is 'higher'
	 * than a unboxing conversion.
	 */
@Test
public void conversionPriority_8224() throws Exception {
    @SuppressWarnings("unused")
    class ConversionPriority1 {

        public int getX(Number i) {
            return 20;
        }

        public int getX(int i) {
            return 10;
        }
    }
    @SuppressWarnings("unused")
    class ConversionPriority2 {

        public int getX(int i) {
            return 10;
        }

        public int getX(Number i) {
            return 20;
        }
    }
    final Integer INTEGER = Integer.valueOf(7);
    EvaluationContext emptyEvalContext = new StandardEvaluationContext();
    List<TypeDescriptor> args = new ArrayList<>();
    args.add(TypeDescriptor.forObject(new Integer(42)));
    ConversionPriority1 target = new ConversionPriority1();
    MethodExecutor me = new ReflectiveMethodResolver(true).resolve(emptyEvalContext, target, "getX", args);
    // MethodInvoker chooses getX(int i) when passing Integer
    final int actual = (Integer) me.execute(emptyEvalContext, target, new Integer(42)).getValue();
    // Compiler chooses getX(Number i) when passing Integer
    final int compiler = target.getX(INTEGER);
    // Fails!
    assertEquals(compiler, actual);
    ConversionPriority2 target2 = new ConversionPriority2();
    MethodExecutor me2 = new ReflectiveMethodResolver(true).resolve(emptyEvalContext, target2, "getX", args);
    // MethodInvoker chooses getX(int i) when passing Integer
    int actual2 = (Integer) me2.execute(emptyEvalContext, target2, new Integer(42)).getValue();
    // Compiler chooses getX(Number i) when passing Integer
    int compiler2 = target2.getX(INTEGER);
    // Fails!
    assertEquals(compiler2, actual2);
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) ArrayList(java.util.ArrayList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TypeDescriptor(org.springframework.core.convert.TypeDescriptor) ReflectiveMethodResolver(org.springframework.expression.spel.support.ReflectiveMethodResolver) MethodExecutor(org.springframework.expression.MethodExecutor) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Test(org.junit.Test)

Example 54 with EvaluationContext

use of org.springframework.expression.EvaluationContext in project spring-framework by spring-projects.

the class TemplateExpressionParsingTests method testCompositeStringExpression.

@Test
public void testCompositeStringExpression() throws Exception {
    SpelExpressionParser parser = new SpelExpressionParser();
    Expression ex = parser.parseExpression("hello ${'world'}", DEFAULT_TEMPLATE_PARSER_CONTEXT);
    checkString("hello world", ex.getValue());
    checkString("hello world", ex.getValue(String.class));
    checkString("hello world", ex.getValue((Object) null, String.class));
    checkString("hello world", ex.getValue(new Rooty()));
    checkString("hello world", ex.getValue(new Rooty(), String.class));
    EvaluationContext ctx = new StandardEvaluationContext();
    checkString("hello world", ex.getValue(ctx));
    checkString("hello world", ex.getValue(ctx, String.class));
    checkString("hello world", ex.getValue(ctx, null, String.class));
    checkString("hello world", ex.getValue(ctx, new Rooty()));
    checkString("hello world", ex.getValue(ctx, new Rooty(), String.class));
    checkString("hello world", ex.getValue(ctx, new Rooty(), String.class));
    assertEquals("hello ${'world'}", ex.getExpressionString());
    assertFalse(ex.isWritable(new StandardEvaluationContext()));
    assertFalse(ex.isWritable(new Rooty()));
    assertFalse(ex.isWritable(new StandardEvaluationContext(), new Rooty()));
    assertEquals(String.class, ex.getValueType());
    assertEquals(String.class, ex.getValueType(ctx));
    assertEquals(String.class, ex.getValueTypeDescriptor().getType());
    assertEquals(String.class, ex.getValueTypeDescriptor(ctx).getType());
    assertEquals(String.class, ex.getValueType(new Rooty()));
    assertEquals(String.class, ex.getValueType(ctx, new Rooty()));
    assertEquals(String.class, ex.getValueTypeDescriptor(new Rooty()).getType());
    assertEquals(String.class, ex.getValueTypeDescriptor(ctx, new Rooty()).getType());
    try {
        ex.setValue(ctx, null);
        fail();
    } catch (EvaluationException ee) {
    // success
    }
    try {
        ex.setValue((Object) null, null);
        fail();
    } catch (EvaluationException ee) {
    // success
    }
    try {
        ex.setValue(ctx, null, null);
        fail();
    } catch (EvaluationException ee) {
    // success
    }
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) CompositeStringExpression(org.springframework.expression.common.CompositeStringExpression) Expression(org.springframework.expression.Expression) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) EvaluationContext(org.springframework.expression.EvaluationContext) EvaluationException(org.springframework.expression.EvaluationException) Test(org.junit.Test)

Example 55 with EvaluationContext

use of org.springframework.expression.EvaluationContext in project spring-framework by spring-projects.

the class SpelReproTests method SPR9735.

@Test
public void SPR9735() {
    Item item = new Item();
    item.setName("parent");
    Item item1 = new Item();
    item1.setName("child1");
    Item item2 = new Item();
    item2.setName("child2");
    item.add(item1);
    item.add(item2);
    ExpressionParser parser = new SpelExpressionParser();
    EvaluationContext context = new StandardEvaluationContext();
    Expression exp = parser.parseExpression("#item[0].name");
    context.setVariable("item", item);
    assertEquals("child1", exp.getValue(context));
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Expression(org.springframework.expression.Expression) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Test(org.junit.Test)

Aggregations

EvaluationContext (org.springframework.expression.EvaluationContext)86 Test (org.junit.Test)72 Expression (org.springframework.expression.Expression)53 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)52 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)32 ArrayList (java.util.ArrayList)12 Authentication (org.springframework.security.core.Authentication)11 ExpressionParser (org.springframework.expression.ExpressionParser)10 SpelExpression (org.springframework.expression.spel.standard.SpelExpression)10 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)10 MethodInvocation (org.aopalliance.intercept.MethodInvocation)9 SimpleMethodInvocation (org.springframework.security.util.SimpleMethodInvocation)9 List (java.util.List)8 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)8 TypedValue (org.springframework.expression.TypedValue)6 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)6 Map (java.util.Map)5 TypeDescriptor (org.springframework.core.convert.TypeDescriptor)5 AccessException (org.springframework.expression.AccessException)5 Method (java.lang.reflect.Method)4