Spring Expression Language cannot resolve method on class

Viewed 823

I'm trying to evaluate some expression written in SpEL programmatically.

ExpressionParser expressionParser = new SpelExpressionParser();
Expression expression = expressionParser.parseExpression(annotation.filter());

SimpleEvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build();
context.setVariable("authorities", authentication.getAuthorities());

return expression.getValue(context, Boolean.class);

As you can see, I add a variable, called authorities. I'm trying to evaluate this expression: #authorities.isEmpty().

But I get the exception:

Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1004E: Method call: Method isEmpty() cannot be found on type java.util.ArrayList
    at org.springframework.expression.spel.ast.MethodReference.findAccessorForMethod(MethodReference.java:225) ~[spring-expression-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at org.springframework.expression.spel.ast.MethodReference.getValueInternal(MethodReference.java:134) ~[spring-expression-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    ...

Anyone knows a solution to this problem?

1 Answers

The SimpleEvaluationContext does not allow arbitrary method invocation. See its Javadocs.

If you "trust" the SpEL expression, use

StandardEvaluationContext context = new StandardEvaluationContext();
Related