Search in sources :

Example 81 with EvaluationContext

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

the class SpelReproTests method SPR13918.

@Test
public void SPR13918() {
    EvaluationContext context = new StandardEvaluationContext();
    context.setVariable("encoding", "UTF-8");
    Expression ex = parser.parseExpression("T(java.nio.charset.Charset).forName(#encoding)");
    Object result = ex.getValue(context);
    assertEquals(StandardCharsets.UTF_8, result);
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Expression(org.springframework.expression.Expression) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Test(org.junit.Test)

Example 82 with EvaluationContext

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

the class SpelReproTests method varargsAndPrimitives_SPR8174.

@Test
public void varargsAndPrimitives_SPR8174() throws Exception {
    EvaluationContext emptyEvalContext = new StandardEvaluationContext();
    List<TypeDescriptor> args = new ArrayList<>();
    args.add(TypeDescriptor.forObject(34L));
    ReflectionUtil<Integer> ru = new ReflectionUtil<>();
    MethodExecutor me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "methodToCall", args);
    args.set(0, TypeDescriptor.forObject(23));
    me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args);
    me.execute(emptyEvalContext, ru, 45);
    args.set(0, TypeDescriptor.forObject(23f));
    me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args);
    me.execute(emptyEvalContext, ru, 45f);
    args.set(0, TypeDescriptor.forObject(23d));
    me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args);
    me.execute(emptyEvalContext, ru, 23d);
    args.set(0, TypeDescriptor.forObject((short) 23));
    me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args);
    me.execute(emptyEvalContext, ru, (short) 23);
    args.set(0, TypeDescriptor.forObject(23L));
    me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args);
    me.execute(emptyEvalContext, ru, 23L);
    args.set(0, TypeDescriptor.forObject((char) 65));
    me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args);
    me.execute(emptyEvalContext, ru, (char) 65);
    args.set(0, TypeDescriptor.forObject((byte) 23));
    me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args);
    me.execute(emptyEvalContext, ru, (byte) 23);
    args.set(0, TypeDescriptor.forObject(true));
    me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args);
    me.execute(emptyEvalContext, ru, true);
    // trickier:
    args.set(0, TypeDescriptor.forObject(12));
    args.add(TypeDescriptor.forObject(23f));
    me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "bar", args);
    me.execute(emptyEvalContext, ru, 12, 23f);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) TypeDescriptor(org.springframework.core.convert.TypeDescriptor) ReflectiveMethodResolver(org.springframework.expression.spel.support.ReflectiveMethodResolver) MethodExecutor(org.springframework.expression.MethodExecutor) ArrayList(java.util.ArrayList) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Test(org.junit.Test)

Example 83 with EvaluationContext

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

the class SpelReproTests method wideningPrimitiveConversion_8224.

/**
	 * Test whether {@link ReflectiveMethodResolver} handles Widening Primitive Conversion. That's passing an 'int' to a
	 * method accepting 'long' is ok.
	 */
@Test
public void wideningPrimitiveConversion_8224() throws Exception {
    class WideningPrimitiveConversion {

        public int getX(long i) {
            return 10;
        }
    }
    final Integer INTEGER_VALUE = Integer.valueOf(7);
    WideningPrimitiveConversion target = new WideningPrimitiveConversion();
    EvaluationContext emptyEvalContext = new StandardEvaluationContext();
    List<TypeDescriptor> args = new ArrayList<>();
    args.add(TypeDescriptor.forObject(INTEGER_VALUE));
    MethodExecutor me = new ReflectiveMethodResolver(true).resolve(emptyEvalContext, target, "getX", args);
    final int actual = (Integer) me.execute(emptyEvalContext, target, INTEGER_VALUE).getValue();
    final int compiler = target.getX(INTEGER_VALUE);
    assertEquals(compiler, actual);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) TypeDescriptor(org.springframework.core.convert.TypeDescriptor) ReflectiveMethodResolver(org.springframework.expression.spel.support.ReflectiveMethodResolver) MethodExecutor(org.springframework.expression.MethodExecutor) ArrayList(java.util.ArrayList) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Test(org.junit.Test)

Example 84 with EvaluationContext

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

the class SpelCompilationCoverageTests method variableReference_userDefined.

@Test
public void variableReference_userDefined() throws Exception {
    EvaluationContext ctx = new StandardEvaluationContext();
    ctx.setVariable("target", "abc");
    expression = parser.parseExpression("#target");
    assertEquals("abc", expression.getValue(ctx));
    assertCanCompile(expression);
    assertEquals("abc", expression.getValue(ctx));
    ctx.setVariable("target", "123");
    assertEquals("123", expression.getValue(ctx));
    ctx.setVariable("target", 42);
    try {
        assertEquals(42, expression.getValue(ctx));
        fail();
    } catch (SpelEvaluationException see) {
        assertTrue(see.getCause() instanceof ClassCastException);
    }
    ctx.setVariable("target", "abc");
    expression = parser.parseExpression("#target.charAt(0)");
    assertEquals('a', expression.getValue(ctx));
    assertCanCompile(expression);
    assertEquals('a', expression.getValue(ctx));
    ctx.setVariable("target", "1");
    assertEquals('1', expression.getValue(ctx));
    ctx.setVariable("target", 42);
    try {
        assertEquals('4', expression.getValue(ctx));
        fail();
    } catch (SpelEvaluationException see) {
        assertTrue(see.getCause() instanceof ClassCastException);
    }
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Test(org.junit.Test)

Example 85 with EvaluationContext

use of org.springframework.expression.EvaluationContext in project uPortal by Jasig.

the class PortletSpELServiceImpl method getValue.

@Override
public <T> T getValue(String expressionString, PortletRequest request, Class<T> desiredResultType) {
    final Expression expression = parseExpression(expressionString);
    final EvaluationContext evaluationContext = getEvaluationContext(request);
    return expression.getValue(evaluationContext, desiredResultType);
}
Also used : Expression(org.springframework.expression.Expression) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext)

Aggregations

EvaluationContext (org.springframework.expression.EvaluationContext)85 Test (org.junit.Test)72 Expression (org.springframework.expression.Expression)52 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