This rule reports syntax errors in Spring Expression Language (SpEL) expressions.

Why is this an issue?

SpEL is used in Spring annotations and is parsed by the Spring framework, not by the Java compiler. This means that invalid SpEL expressions are not detected during Java compile time. They will cause exceptions during runtime instead, or even fail silently with the expression string interpreted as a simple string literal by Spring.

Exceptions

This rule does report syntactical errors in SpEL expressions but does not consider semantic errors, such as unknown identifiers or incompatible operand data types.

How to fix it

Correct the syntax error in the SpEL expression.

Code examples

Noncompliant code example

@Value("#{systemProperties['user.region'}") // Noncompliant, unclosed "["
private String region;
@Value("#{'${listOfValues}' split(',')}") // Noncompliant, missing operator
private List<String> valuesList;
@Value("#{T(java.lang.Math).random() * 64h}") // Noncompliant, invalid number
private Double randPercent;
@Query("SELECT u FROM User u WHERE u.status = :#{#status+}") // Noncompliant, missing operand for "+"
List<User> findUsersByStatus(@Param("status") String status);

Compliant solution

@Value("#{systemProperties['user.region']}") // Compliant
private String region;
@Value("#{'${listOfValues}'.split(',')}") // Compliant
private List<String> valuesList;
@Value("#{T(java.lang.Math).random() * 100.0}") // Compliant
private Double randPercent;
@Query("SELECT u FROM User u WHERE u.status = :#{#status+42}") // Compliant
List<User> findUsersByStatus(@Param("status") String status);

Resources

Documentation