Skip to content

Commit d3c3088

Browse files
authored
Clarify behavior of the Elvis SpEL operator in documentation (#30352)
This commit improves both the javadoc and the reference guide section on the Elvis SpEL operator to clarify that in addition to `null` objects, empty Strings also lead the operator to evaluate to its second operand. The reference guide's advanced snippet is modified to use such an empty String instead of `null` to make that behavior prominent with some code. See gh-30318 Closes gh-30352
1 parent f2c0b30 commit d3c3088

File tree

2 files changed

+11
-7
lines changed
  • framework-docs/modules/ROOT/pages/core/expressions/language-ref
  • spring-expression/src/main/java/org/springframework/expression/spel/ast

2 files changed

+11
-7
lines changed

framework-docs/modules/ROOT/pages/core/expressions/language-ref/operator-elvis.adoc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ Kotlin::
3838
----
3939
======
4040

41+
NOTE: The SpEL Elvis operator also checks for _empty_ Strings in addition to `null` objects.
42+
The original snippet is thus only close to emulating the semantics of the operator (it would need an
43+
additional `!name.isEmpty()` check).
44+
4145
The following listing shows a more complex example:
4246

4347
[tabs]
@@ -53,7 +57,7 @@ Java::
5357
String name = parser.parseExpression("name?:'Elvis Presley'").getValue(context, tesla, String.class);
5458
System.out.println(name); // Nikola Tesla
5559
56-
tesla.setName(null);
60+
tesla.setName("");
5761
name = parser.parseExpression("name?:'Elvis Presley'").getValue(context, tesla, String.class);
5862
System.out.println(name); // Elvis Presley
5963
----
@@ -69,7 +73,7 @@ Kotlin::
6973
var name = parser.parseExpression("name?:'Elvis Presley'").getValue(context, tesla, String::class.java)
7074
println(name) // Nikola Tesla
7175
72-
tesla.setName(null)
76+
tesla.setName("")
7377
name = parser.parseExpression("name?:'Elvis Presley'").getValue(context, tesla, String::class.java)
7478
println(name) // Elvis Presley
7579
----

spring-expression/src/main/java/org/springframework/expression/spel/ast/Elvis.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
import org.springframework.util.ObjectUtils;
2727

2828
/**
29-
* Represents the elvis operator <code>?:</code>. For an expression <code>a?:b</code> if <code>a</code> is not null,
30-
* the value of the expression is <code>a</code>, if <code>a</code> is null then the value of the expression is
31-
* <code>b</code>.
29+
* Represents the Elvis operator <code>?:</code>. For an expression <code>a?:b</code> if <code>a</code> is neither null
30+
* nor an empty String, the value of the expression is <code>a</code>.
31+
* If <code>a</code> is null or the empty String, then the value of the expression is <code>b</code>.
3232
*
3333
* @author Andy Clement
3434
* @author Juergen Hoeller
@@ -43,8 +43,8 @@ public Elvis(int startPos, int endPos, SpelNodeImpl... args) {
4343

4444

4545
/**
46-
* Evaluate the condition and if not null, return it.
47-
* If it is null, return the other value.
46+
* Evaluate the condition and if neither null nor an empty String, return it.
47+
* If it is null or an empty String, return the other value.
4848
* @param state the expression state
4949
* @throws EvaluationException if the condition does not evaluate correctly
5050
* to a boolean or there is a problem executing the chosen alternative

0 commit comments

Comments
 (0)