Search in sources :

Example 21 with SpelExpression

use of org.springframework.expression.spel.standard.SpelExpression in project spring-framework by spring-projects.

the class SpelReproTests method reservedWordProperties_SPR9862.

@Test
void reservedWordProperties_SPR9862() {
    StandardEvaluationContext context = new StandardEvaluationContext();
    SpelExpressionParser parser = new SpelExpressionParser();
    SpelExpression expression = parser.parseRaw("T(org.springframework.expression.spel.testresources.le.div.mod.reserved.Reserver).CONST");
    Object value = expression.getValue(context);
    assertThat(Reserver.CONST).isEqualTo(value);
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Test(org.junit.jupiter.api.Test)

Example 22 with SpelExpression

use of org.springframework.expression.spel.standard.SpelExpression in project spring-framework by spring-projects.

the class SpelCompilationCoverageTests method nullsafeMethodChaining_SPR16489.

@Test
public void nullsafeMethodChaining_SPR16489() throws Exception {
    FooObjectHolder foh = new FooObjectHolder();
    StandardEvaluationContext context = new StandardEvaluationContext();
    context.setRootObject(foh);
    // First non compiled:
    SpelExpression expression = (SpelExpression) parser.parseExpression("getFoo()?.getObject()");
    assertThat(expression.getValue(context)).isEqualTo("hello");
    foh.foo = null;
    assertThat(expression.getValue(context)).isNull();
    assertCanCompile(expression);
    foh.foo = new FooObject();
    assertThat(expression.getValue(context)).isEqualTo("hello");
    foh.foo = null;
    assertThat(expression.getValue(context)).isNull();
    // Static method references
    expression = (SpelExpression) parser.parseExpression("#var?.methoda()");
    context.setVariable("var", StaticsHelper.class);
    assertThat(expression.getValue(context).toString()).isEqualTo("sh");
    context.setVariable("var", null);
    assertThat(expression.getValue(context)).isNull();
    assertCanCompile(expression);
    context.setVariable("var", StaticsHelper.class);
    assertThat(expression.getValue(context).toString()).isEqualTo("sh");
    context.setVariable("var", null);
    assertThat(expression.getValue(context)).isNull();
    // Nullsafe guard on expression element evaluating to primitive/null
    expression = (SpelExpression) parser.parseExpression("#var?.intValue()");
    context.setVariable("var", 4);
    assertThat(expression.getValue(context).toString()).isEqualTo("4");
    context.setVariable("var", null);
    assertThat(expression.getValue(context)).isNull();
    assertCanCompile(expression);
    context.setVariable("var", 4);
    assertThat(expression.getValue(context).toString()).isEqualTo("4");
    context.setVariable("var", null);
    assertThat(expression.getValue(context)).isNull();
    // Nullsafe guard on expression element evaluating to primitive/null
    expression = (SpelExpression) parser.parseExpression("#var?.booleanValue()");
    context.setVariable("var", false);
    assertThat(expression.getValue(context).toString()).isEqualTo("false");
    context.setVariable("var", null);
    assertThat(expression.getValue(context)).isNull();
    assertCanCompile(expression);
    context.setVariable("var", false);
    assertThat(expression.getValue(context).toString()).isEqualTo("false");
    context.setVariable("var", null);
    assertThat(expression.getValue(context)).isNull();
    // Nullsafe guard on expression element evaluating to primitive/null
    expression = (SpelExpression) parser.parseExpression("#var?.booleanValue()");
    context.setVariable("var", true);
    assertThat(expression.getValue(context).toString()).isEqualTo("true");
    context.setVariable("var", null);
    assertThat(expression.getValue(context)).isNull();
    assertCanCompile(expression);
    context.setVariable("var", true);
    assertThat(expression.getValue(context).toString()).isEqualTo("true");
    context.setVariable("var", null);
    assertThat(expression.getValue(context)).isNull();
    // Nullsafe guard on expression element evaluating to primitive/null
    expression = (SpelExpression) parser.parseExpression("#var?.longValue()");
    context.setVariable("var", 5L);
    assertThat(expression.getValue(context).toString()).isEqualTo("5");
    context.setVariable("var", null);
    assertThat(expression.getValue(context)).isNull();
    assertCanCompile(expression);
    context.setVariable("var", 5L);
    assertThat(expression.getValue(context).toString()).isEqualTo("5");
    context.setVariable("var", null);
    assertThat(expression.getValue(context)).isNull();
    // Nullsafe guard on expression element evaluating to primitive/null
    expression = (SpelExpression) parser.parseExpression("#var?.floatValue()");
    context.setVariable("var", 3f);
    assertThat(expression.getValue(context).toString()).isEqualTo("3.0");
    context.setVariable("var", null);
    assertThat(expression.getValue(context)).isNull();
    assertCanCompile(expression);
    context.setVariable("var", 3f);
    assertThat(expression.getValue(context).toString()).isEqualTo("3.0");
    context.setVariable("var", null);
    assertThat(expression.getValue(context)).isNull();
    // Nullsafe guard on expression element evaluating to primitive/null
    expression = (SpelExpression) parser.parseExpression("#var?.shortValue()");
    context.setVariable("var", (short) 8);
    assertThat(expression.getValue(context).toString()).isEqualTo("8");
    context.setVariable("var", null);
    assertThat(expression.getValue(context)).isNull();
    assertCanCompile(expression);
    context.setVariable("var", (short) 8);
    assertThat(expression.getValue(context).toString()).isEqualTo("8");
    context.setVariable("var", null);
    assertThat(expression.getValue(context)).isNull();
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Test(org.junit.jupiter.api.Test)

Example 23 with SpelExpression

use of org.springframework.expression.spel.standard.SpelExpression in project spring-framework by spring-projects.

the class SpelReproTests method reservedWords_8228.

@Test
public void reservedWords_8228() throws Exception {
    // "DIV","EQ","GE","GT","LE","LT","MOD","NE","NOT"
    @SuppressWarnings("unused")
    class Reserver {

        public Reserver getReserver() {
            return this;
        }

        public String NE = "abc";

        public String ne = "def";

        public int DIV = 1;

        public int div = 3;

        public Map<String, String> m = new HashMap<>();

        Reserver() {
            m.put("NE", "xyz");
        }
    }
    StandardEvaluationContext ctx = new StandardEvaluationContext(new Reserver());
    SpelExpressionParser parser = new SpelExpressionParser();
    String ex = "getReserver().NE";
    SpelExpression exp = parser.parseRaw(ex);
    String value = (String) exp.getValue(ctx);
    assertEquals("abc", value);
    ex = "getReserver().ne";
    exp = parser.parseRaw(ex);
    value = (String) exp.getValue(ctx);
    assertEquals("def", value);
    ex = "getReserver().m[NE]";
    exp = parser.parseRaw(ex);
    value = (String) exp.getValue(ctx);
    assertEquals("xyz", value);
    ex = "getReserver().DIV";
    exp = parser.parseRaw(ex);
    assertEquals(1, exp.getValue(ctx));
    ex = "getReserver().div";
    exp = parser.parseRaw(ex);
    assertEquals(3, exp.getValue(ctx));
    exp = parser.parseRaw("NE");
    assertEquals("abc", exp.getValue(ctx));
}
Also used : Reserver(org.springframework.expression.spel.testresources.le.div.mod.reserved.Reserver) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.Test)

Example 24 with SpelExpression

use of org.springframework.expression.spel.standard.SpelExpression in project spring-data-mongodb by spring-projects.

the class SpelExpressionTransformer method transform.

/**
 * Transforms the given SpEL expression to a corresponding MongoDB expression against the given
 * {@link AggregationOperationContext} {@code context}.
 * <p>
 * Exposes the given @{code params} as <code>[0] ... [n]</code>.
 *
 * @param expression must not be {@literal null}
 * @param context must not be {@literal null}
 * @param params must not be {@literal null}
 * @return
 */
public Object transform(String expression, AggregationOperationContext context, Object... params) {
    Assert.notNull(expression, "Expression must not be null!");
    Assert.notNull(context, "AggregationOperationContext must not be null!");
    Assert.notNull(params, "Parameters must not be null!");
    SpelExpression spelExpression = (SpelExpression) PARSER.parseExpression(expression);
    ExpressionState state = new ExpressionState(new StandardEvaluationContext(params), CONFIG);
    ExpressionNode node = ExpressionNode.from(spelExpression.getAST(), state);
    return transform(new AggregationExpressionTransformationContext<ExpressionNode>(node, null, null, context));
}
Also used : ExpressionState(org.springframework.expression.spel.ExpressionState) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) ExpressionNode(org.springframework.data.mongodb.core.spel.ExpressionNode)

Example 25 with SpelExpression

use of org.springframework.expression.spel.standard.SpelExpression in project motech by motech.

the class SecurityAnnotationBeanPostProcessor method postProcessAfterInitialization.

/**
 * Searches for {@link org.springframework.security.access.prepost.PreAuthorize}
 * and {@link org.springframework.security.access.prepost.PostAuthorize} annotations
 * representing permissions and parses them. Parsed annotations are used
 * to find permissions. After that those permissions are added to
 * {@link org.motechproject.security.service.MotechPermissionService}
 *
 * @param bean to be processed
 * @param beanName name of the bean
 * @return processed bean
 */
@Override
public Object postProcessAfterInitialization(final Object bean, final String beanName) {
    LOGGER.debug("Searching for security annotations in: {}", beanName);
    doWithMethods(bean.getClass(), new MethodCallback() {

        @Override
        public void doWith(Method method) throws IllegalAccessException {
            Method methodOfOriginalClassIfProxied = findMethod(getTargetClass(bean), method.getName(), method.getParameterTypes());
            if (methodOfOriginalClassIfProxied != null) {
                PreAuthorize preAuthorize = findAnnotation(methodOfOriginalClassIfProxied, PreAuthorize.class);
                PostAuthorize postAuthorize = findAnnotation(methodOfOriginalClassIfProxied, PostAuthorize.class);
                List<String> annotations = new ArrayList<>(2);
                List<String> permissions = new ArrayList<>();
                if (preAuthorize != null) {
                    annotations.add(preAuthorize.value());
                }
                if (postAuthorize != null) {
                    annotations.add(postAuthorize.value());
                }
                for (String annotation : annotations) {
                    SpelExpression expression = (SpelExpression) annotationParser.parseExpression(annotation);
                    permissions.addAll(findPermissions(expression.getAST()));
                }
                addRoleAndPermissions(permissions);
            }
        }
    });
    LOGGER.debug("Searched for security annotations in: {}", beanName);
    return bean;
}
Also used : SpelExpression(org.springframework.expression.spel.standard.SpelExpression) ArrayList(java.util.ArrayList) List(java.util.List) ReflectionUtils.findMethod(org.springframework.util.ReflectionUtils.findMethod) Method(java.lang.reflect.Method) PostAuthorize(org.springframework.security.access.prepost.PostAuthorize) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) MethodCallback(org.springframework.util.ReflectionUtils.MethodCallback)

Aggregations

SpelExpression (org.springframework.expression.spel.standard.SpelExpression)56 Test (org.junit.jupiter.api.Test)30 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)27 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)18 Test (org.junit.Test)15 Expression (org.springframework.expression.Expression)8 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)6 ArrayList (java.util.ArrayList)5 Map (java.util.Map)4 EvaluationContext (org.springframework.expression.EvaluationContext)4 HashMap (java.util.HashMap)3 List (java.util.List)3 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)3 TypeDescriptor (org.springframework.core.convert.TypeDescriptor)3 ClientHttpRequestFactory (org.springframework.http.client.ClientHttpRequestFactory)3 SimpleClientHttpRequestFactory (org.springframework.http.client.SimpleClientHttpRequestFactory)3 HttpRequestExecutingMessageHandler (org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler)3 Method (java.lang.reflect.Method)2 BigDecimal (java.math.BigDecimal)2 LinkedHashMap (java.util.LinkedHashMap)2