76 lines
2.2 KiB
Plaintext
76 lines
2.2 KiB
Plaintext
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
|
|
|
|
[source,java,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
@Value("#{systemProperties['user.region'}") // Noncompliant, unclosed "["
|
|
private String region;
|
|
----
|
|
|
|
[source,java,diff-id=2,diff-type=noncompliant]
|
|
----
|
|
@Value("#{'${listOfValues}' split(',')}") // Noncompliant, missing operator
|
|
private List<String> valuesList;
|
|
----
|
|
|
|
[source,java,diff-id=3,diff-type=noncompliant]
|
|
----
|
|
@Value("#{T(java.lang.Math).random() * 64h}") // Noncompliant, invalid number
|
|
private Double randPercent;
|
|
----
|
|
|
|
[source,java,diff-id=4,diff-type=noncompliant]
|
|
----
|
|
@Query("SELECT u FROM User u WHERE u.status = :#{#status+}") // Noncompliant, missing operand for "+"
|
|
List<User> findUsersByStatus(@Param("status") String status);
|
|
----
|
|
|
|
==== Compliant solution
|
|
|
|
[source,java,diff-id=1,diff-type=compliant]
|
|
----
|
|
@Value("#{systemProperties['user.region']}") // Compliant
|
|
private String region;
|
|
----
|
|
|
|
[source,java,diff-id=2,diff-type=compliant]
|
|
----
|
|
@Value("#{'${listOfValues}'.split(',')}") // Compliant
|
|
private List<String> valuesList;
|
|
----
|
|
|
|
[source,java,diff-id=3,diff-type=compliant]
|
|
----
|
|
@Value("#{T(java.lang.Math).random() * 100.0}") // Compliant
|
|
private Double randPercent;
|
|
----
|
|
|
|
[source,java,diff-id=4,diff-type=compliant]
|
|
----
|
|
@Query("SELECT u FROM User u WHERE u.status = :#{#status+42}") // Compliant
|
|
List<User> findUsersByStatus(@Param("status") String status);
|
|
----
|
|
|
|
== Resources
|
|
|
|
=== Documentation
|
|
|
|
- https://docs.spring.io/spring-framework/reference/core/expressions.html[Spring Framework - Spring Expression Language (SpEL)]
|