Search in sources :

Example 46 with StandardEvaluationContext

use of org.springframework.expression.spel.support.StandardEvaluationContext in project spring-framework by spring-projects.

the class MethodInvocationTests method testMethodFiltering_SPR6764.

@Test
public void testMethodFiltering_SPR6764() {
    SpelExpressionParser parser = new SpelExpressionParser();
    StandardEvaluationContext context = new StandardEvaluationContext();
    context.setRootObject(new TestObject());
    LocalFilter filter = new LocalFilter();
    context.registerMethodFilter(TestObject.class, filter);
    // Filter will be called but not do anything, so first doit() will be invoked
    SpelExpression expr = (SpelExpression) parser.parseExpression("doit(1)");
    String result = expr.getValue(context, String.class);
    assertEquals("1", result);
    assertTrue(filter.filterCalled);
    // Filter will now remove non @Anno annotated methods
    filter.removeIfNotAnnotated = true;
    filter.filterCalled = false;
    expr = (SpelExpression) parser.parseExpression("doit(1)");
    result = expr.getValue(context, String.class);
    assertEquals("double 1.0", result);
    assertTrue(filter.filterCalled);
    // check not called for other types
    filter.filterCalled = false;
    context.setRootObject(new String("abc"));
    expr = (SpelExpression) parser.parseExpression("charAt(0)");
    result = expr.getValue(context, String.class);
    assertEquals("a", result);
    assertFalse(filter.filterCalled);
    // check de-registration works
    filter.filterCalled = false;
    //clear filter
    context.registerMethodFilter(TestObject.class, null);
    context.setRootObject(new TestObject());
    expr = (SpelExpression) parser.parseExpression("doit(1)");
    result = expr.getValue(context, String.class);
    assertEquals("1", result);
    assertFalse(filter.filterCalled);
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Test(org.junit.Test)

Example 47 with StandardEvaluationContext

use of org.springframework.expression.spel.support.StandardEvaluationContext in project spring-framework by spring-projects.

the class OperatorOverloaderTests method testSimpleOperations.

@Test
public void testSimpleOperations() throws Exception {
    // no built in support for this:
    evaluateAndCheckError("'abc'-true", SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES);
    StandardEvaluationContext eContext = TestScenarioCreator.getTestEvaluationContext();
    eContext.setOperatorOverloader(new StringAndBooleanAddition());
    SpelExpression expr = (SpelExpression) parser.parseExpression("'abc'+true");
    assertEquals("abctrue", expr.getValue(eContext));
    expr = (SpelExpression) parser.parseExpression("'abc'-true");
    assertEquals("abc", expr.getValue(eContext));
    expr = (SpelExpression) parser.parseExpression("'abc'+null");
    assertEquals("abcnull", expr.getValue(eContext));
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Test(org.junit.Test)

Example 48 with StandardEvaluationContext

use of org.springframework.expression.spel.support.StandardEvaluationContext in project spring-framework by spring-projects.

the class SpelCompilationCoverageTests method compiledExpressionShouldWorkWhenUsingCustomFunctionWithVarargs.

@Test
public void compiledExpressionShouldWorkWhenUsingCustomFunctionWithVarargs() throws Exception {
    StandardEvaluationContext context = null;
    // Here the target method takes Object... and we are passing a string
    expression = parser.parseExpression("#doFormat('hey %s', 'there')");
    context = new StandardEvaluationContext();
    context.registerFunction("doFormat", DelegatingStringFormat.class.getDeclaredMethod("format", String.class, Object[].class));
    ((SpelExpression) expression).setEvaluationContext(context);
    assertEquals("hey there", expression.getValue(String.class));
    assertTrue(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable());
    assertCanCompile(expression);
    assertEquals("hey there", expression.getValue(String.class));
    expression = parser.parseExpression("#doFormat([0], 'there')");
    context = new StandardEvaluationContext(new Object[] { "hey %s" });
    context.registerFunction("doFormat", DelegatingStringFormat.class.getDeclaredMethod("format", String.class, Object[].class));
    ((SpelExpression) expression).setEvaluationContext(context);
    assertEquals("hey there", expression.getValue(String.class));
    assertTrue(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable());
    assertCanCompile(expression);
    assertEquals("hey there", expression.getValue(String.class));
    expression = parser.parseExpression("#doFormat([0], #arg)");
    context = new StandardEvaluationContext(new Object[] { "hey %s" });
    context.registerFunction("doFormat", DelegatingStringFormat.class.getDeclaredMethod("format", String.class, Object[].class));
    context.setVariable("arg", "there");
    ((SpelExpression) expression).setEvaluationContext(context);
    assertEquals("hey there", expression.getValue(String.class));
    assertTrue(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable());
    assertCanCompile(expression);
    assertEquals("hey there", expression.getValue(String.class));
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Test(org.junit.Test)

Example 49 with StandardEvaluationContext

use of org.springframework.expression.spel.support.StandardEvaluationContext in project spring-framework by spring-projects.

the class SpelCompilationCoverageTests method constructorReference_SPR13781.

/**
	 * Test variants of using T(...) and static/non-static method/property/field references.
	 */
@Test
public void constructorReference_SPR13781() {
    // Static field access on a T() referenced type
    expression = parser.parseExpression("T(java.util.Locale).ENGLISH");
    assertEquals("en", expression.getValue().toString());
    assertCanCompile(expression);
    assertEquals("en", expression.getValue().toString());
    // The actual expression from the bug report. It fails if the ENGLISH reference fails
    // to pop the type reference for Locale off the stack (if it isn't popped then
    // toLowerCase() will be called with a Locale parameter). In this situation the
    // code generation for ENGLISH should notice there is something on the stack that
    // is not required and pop it off.
    expression = parser.parseExpression("#userId.toString().toLowerCase(T(java.util.Locale).ENGLISH)");
    StandardEvaluationContext context = new StandardEvaluationContext();
    context.setVariable("userId", "RoDnEy");
    assertEquals("rodney", expression.getValue(context));
    assertCanCompile(expression);
    assertEquals("rodney", expression.getValue(context));
    // Property access on a class object
    expression = parser.parseExpression("T(String).name");
    assertEquals("java.lang.String", expression.getValue());
    assertCanCompile(expression);
    assertEquals("java.lang.String", expression.getValue());
    // Now the type reference isn't on the stack, and needs loading
    context = new StandardEvaluationContext(String.class);
    expression = parser.parseExpression("name");
    assertEquals("java.lang.String", expression.getValue(context));
    assertCanCompile(expression);
    assertEquals("java.lang.String", expression.getValue(context));
    expression = parser.parseExpression("T(String).getName()");
    assertEquals("java.lang.String", expression.getValue());
    assertCanCompile(expression);
    assertEquals("java.lang.String", expression.getValue());
    // These tests below verify that the chain of static accesses (either method/property or field)
    // leave the right thing on top of the stack for processing by any outer consuming code.
    // Here the consuming code is the String.valueOf() function.  If the wrong thing were on
    // the stack (for example if the compiled code for static methods wasn't popping the
    // previous thing off the stack) the valueOf() would operate on the wrong value.
    String shclass = StaticsHelper.class.getName();
    // Basic chain: property access then method access
    expression = parser.parseExpression("T(String).valueOf(T(String).name.valueOf(1))");
    assertEquals("1", expression.getValue());
    assertCanCompile(expression);
    assertEquals("1", expression.getValue());
    // chain of statics ending with static method
    expression = parser.parseExpression("T(String).valueOf(T(" + shclass + ").methoda().methoda().methodb())");
    assertEquals("mb", expression.getValue());
    assertCanCompile(expression);
    assertEquals("mb", expression.getValue());
    // chain of statics ending with static field
    expression = parser.parseExpression("T(String).valueOf(T(" + shclass + ").fielda.fielda.fieldb)");
    assertEquals("fb", expression.getValue());
    assertCanCompile(expression);
    assertEquals("fb", expression.getValue());
    // chain of statics ending with static property access
    expression = parser.parseExpression("T(String).valueOf(T(" + shclass + ").propertya.propertya.propertyb)");
    assertEquals("pb", expression.getValue());
    assertCanCompile(expression);
    assertEquals("pb", expression.getValue());
    // variety chain
    expression = parser.parseExpression("T(String).valueOf(T(" + shclass + ").fielda.methoda().propertya.fieldb)");
    assertEquals("fb", expression.getValue());
    assertCanCompile(expression);
    assertEquals("fb", expression.getValue());
    expression = parser.parseExpression("T(String).valueOf(fielda.fieldb)");
    assertEquals("fb", expression.getValue(StaticsHelper.sh));
    assertCanCompile(expression);
    assertEquals("fb", expression.getValue(StaticsHelper.sh));
    expression = parser.parseExpression("T(String).valueOf(propertya.propertyb)");
    assertEquals("pb", expression.getValue(StaticsHelper.sh));
    assertCanCompile(expression);
    assertEquals("pb", expression.getValue(StaticsHelper.sh));
    expression = parser.parseExpression("T(String).valueOf(methoda().methodb())");
    assertEquals("mb", expression.getValue(StaticsHelper.sh));
    assertCanCompile(expression);
    assertEquals("mb", expression.getValue(StaticsHelper.sh));
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Test(org.junit.Test)

Example 50 with StandardEvaluationContext

use of org.springframework.expression.spel.support.StandardEvaluationContext in project spring-framework by spring-projects.

the class SpelCompilationCoverageTests method operatorInstanceOf_SPR14250.

@Test
public void operatorInstanceOf_SPR14250() throws Exception {
    // primitive left operand - should get boxed, return true
    expression = parse("3 instanceof T(Integer)");
    assertEquals(true, expression.getValue());
    assertCanCompile(expression);
    assertEquals(true, expression.getValue());
    // primitive left operand - should get boxed, return false
    expression = parse("3 instanceof T(String)");
    assertEquals(false, expression.getValue());
    assertCanCompile(expression);
    assertEquals(false, expression.getValue());
    // double slot left operand - should get boxed, return false
    expression = parse("3.0d instanceof T(Integer)");
    assertEquals(false, expression.getValue());
    assertCanCompile(expression);
    assertEquals(false, expression.getValue());
    // double slot left operand - should get boxed, return true
    expression = parse("3.0d instanceof T(Double)");
    assertEquals(true, expression.getValue());
    assertCanCompile(expression);
    assertEquals(true, expression.getValue());
    // Only when the right hand operand is a direct type reference
    // will it be compilable.
    StandardEvaluationContext ctx = new StandardEvaluationContext();
    ctx.setVariable("foo", String.class);
    expression = parse("3 instanceof #foo");
    assertEquals(false, expression.getValue(ctx));
    assertCantCompile(expression);
    // use of primitive as type for instanceof check - compilable
    // but always false
    expression = parse("3 instanceof T(int)");
    assertEquals(false, expression.getValue());
    assertCanCompile(expression);
    assertEquals(false, expression.getValue());
    expression = parse("3 instanceof T(long)");
    assertEquals(false, expression.getValue());
    assertCanCompile(expression);
    assertEquals(false, expression.getValue());
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Test(org.junit.Test)

Aggregations

StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)251 Test (org.junit.Test)211 Expression (org.springframework.expression.Expression)157 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)155 SpelExpression (org.springframework.expression.spel.standard.SpelExpression)104 ExpressionParser (org.springframework.expression.ExpressionParser)81 EvaluationContext (org.springframework.expression.EvaluationContext)43 ArrayList (java.util.ArrayList)27 List (java.util.List)14 HashMap (java.util.HashMap)12 EvaluationException (org.springframework.expression.EvaluationException)12 TypedValue (org.springframework.expression.TypedValue)12 Map (java.util.Map)11 TypeDescriptor (org.springframework.core.convert.TypeDescriptor)8 BigInteger (java.math.BigInteger)7 LinkedHashMap (java.util.LinkedHashMap)7 ExpressionState (org.springframework.expression.spel.ExpressionState)7 AccessException (org.springframework.expression.AccessException)6 Inventor (org.springframework.expression.spel.testresources.Inventor)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5