|
1 | 1 | [[expressions-operator-safe-navigation]]
|
2 | 2 | = Safe Navigation Operator
|
3 | 3 |
|
4 |
| -The safe navigation operator (`?`) is used to avoid a `NullPointerException` and comes |
| 4 | +The safe navigation operator (`?.`) is used to avoid a `NullPointerException` and comes |
5 | 5 | from the https://www.groovy-lang.org/operators.html#_safe_navigation_operator[Groovy]
|
6 | 6 | language. Typically, when you have a reference to an object, you might need to verify
|
7 | 7 | that it is not `null` before accessing methods or properties of the object. To avoid
|
@@ -81,6 +81,64 @@ For example, the expression `#calculator?.max(4, 2)` evaluates to `null` if the
|
81 | 81 | `max(int, int)` method will be invoked on the `#calculator`.
|
82 | 82 | ====
|
83 | 83 |
|
| 84 | +[[expressions-operator-safe-navigation-indexing]] |
| 85 | +== Safe Index Access |
| 86 | + |
| 87 | +Since Spring Framework 6.2, the Spring Expression Language supports safe navigation for |
| 88 | +indexing into the following types of structures. |
| 89 | + |
| 90 | +* xref:core/expressions/language-ref/properties-arrays.adoc#expressions-indexing-arrays-and-collections[arrays and collections] |
| 91 | +* xref:core/expressions/language-ref/properties-arrays.adoc#expressions-indexing-strings[strings] |
| 92 | +* xref:core/expressions/language-ref/properties-arrays.adoc#expressions-indexing-maps[maps] |
| 93 | +* xref:core/expressions/language-ref/properties-arrays.adoc#expressions-indexing-objects[objects] |
| 94 | + |
| 95 | +The following example shows how to use the safe navigation operator for indexing into |
| 96 | +a list (`?.[]`). |
| 97 | + |
| 98 | +[tabs] |
| 99 | +====== |
| 100 | +Java:: |
| 101 | ++ |
| 102 | +[source,java,indent=0,subs="verbatim,quotes",role="primary"] |
| 103 | +---- |
| 104 | + ExpressionParser parser = new SpelExpressionParser(); |
| 105 | + IEEE society = new IEEE(); |
| 106 | + EvaluationContext context = new StandardEvaluationContext(society); |
| 107 | +
|
| 108 | + // evaluates to Inventor("Nikola Tesla") |
| 109 | + Inventor inventor = parser.parseExpression("members?.[0]") // <1> |
| 110 | + .getValue(context, Inventor.class); |
| 111 | +
|
| 112 | + society.members = null; |
| 113 | +
|
| 114 | + // evaluates to null - does not throw an exception |
| 115 | + inventor = parser.parseExpression("members?.[0]") // <2> |
| 116 | + .getValue(context, Inventor.class); |
| 117 | +---- |
| 118 | +<1> Use null-safe index operator on a non-null `members` list |
| 119 | +<2> Use null-safe index operator on a null `members` list |
| 120 | +
|
| 121 | +Kotlin:: |
| 122 | ++ |
| 123 | +[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"] |
| 124 | +---- |
| 125 | + val parser = SpelExpressionParser() |
| 126 | + val society = IEEE() |
| 127 | + val context = StandardEvaluationContext(society) |
| 128 | +
|
| 129 | + // evaluates to Inventor("Nikola Tesla") |
| 130 | + var inventor = parser.parseExpression("members?.[0]") // <1> |
| 131 | + .getValue(context, Inventor::class.java) |
| 132 | +
|
| 133 | + society.members = null |
| 134 | +
|
| 135 | + // evaluates to null - does not throw an exception |
| 136 | + inventor = parser.parseExpression("members?.[0]") // <2> |
| 137 | + .getValue(context, Inventor::class.java) |
| 138 | +---- |
| 139 | +<1> Use null-safe index operator on a non-null `members` list |
| 140 | +<2> Use null-safe index operator on a null `members` list |
| 141 | +====== |
84 | 142 |
|
85 | 143 | [[expressions-operator-safe-navigation-selection-and-projection]]
|
86 | 144 | == Safe Collection Selection and Projection
|
|
0 commit comments