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);
}
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();
}
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));
}
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));
}
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;
}
Aggregations