SimpleEvaluationContext disables array allocations

This rule flags usage of org.springframework.expression.spel.support.SimpleEvaluationContext with Expression.getValue(...).

In Spring Framework 6.0, SimpleEvaluationContext was changed to fully disable array allocations in SpEL expressions, aligning with standard constructor resolution behavior. This change affects code that uses SimpleEvaluationContext to evaluate SpEL expressions that either create arrays explicitly or call methods with varargs parameters.

When migrating to Spring 6, review all calls to Expression.getValue(...) where a SimpleEvaluationContext is used. Ensure the evaluated SpEL expressions do not rely on features that are unsupported in SimpleEvaluationContext, such as array creation or varargs method calls.

If your use case requires array creation or varargs method calls in a SpEL expression, you can either switch from SimpleEvaluationContext to StandardEvaluationContext, or refactor your SpEL expressions to avoid such constructs entirely.

The following example shows usage that will be flagged, because SimpleEvaluationContext is passed to Expression.getValue(...). While the expressions shown may fail at runtime in Spring 6 due to unsupported features like array creation or varargs, this rule flags the context usage itself, regardless of the expression.


import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.SimpleEvaluationContext;
public class InvalidSpelArrayUsage {
    public void failAtRuntime() {
        SimpleEvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build();
        ExpressionParser parser = new SpelExpressionParser();
        parser.parseExpression("new String[1]").getValue(context); // Should be flagged
        parser.parseExpression("join('a', 'b', 'c')").getValue(context); // Should be flagged
        }
    }

The following example shows a valid usage where StandardEvaluationContext is used, which supports all SpEL features.


import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
public class ValidSpelUsage {
    public void worksCorrectly() {
        StandardEvaluationContext context = new StandardEvaluationContext();
        ExpressionParser parser = new SpelExpressionParser();
        parser.parseExpression("new String[1]").getValue(context); // allowed
        parser.parseExpression("join('a', 'b', 'c')").getValue(context); //allowed
        }
    }

For more information, see the following resources: