Skip to content

Commit 515d8aa

Browse files
committed
Merge branch '6.0.x'
2 parents 47ed4e6 + 6300fb3 commit 515d8aa

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828

2929
/**
3030
* Represents a DOT separated expression sequence, such as
31-
* {@code property1.property2.methodOne()}.
31+
* {@code property1.property2.methodOne()} or
32+
* {@code property1?.property2?.methodOne()} when the null-safe navigation
33+
* operator is used.
3234
*
3335
* <p>May also contain array/collection/map indexers, such as
3436
* {@code property1[0].property2['key']}.
@@ -122,6 +124,10 @@ public String toStringAST() {
122124
// Don't append a '.' if the next child is an Indexer.
123125
// For example, we want 'myVar[0]' instead of 'myVar.[0]'.
124126
if (!(nextChild instanceof Indexer)) {
127+
if ((nextChild instanceof MethodReference methodRef && methodRef.isNullSafe()) ||
128+
(nextChild instanceof PropertyOrFieldReference pofRef && pofRef.isNullSafe())) {
129+
sb.append('?');
130+
}
125131
sb.append('.');
126132
}
127133
}

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,15 @@
5050
*
5151
* @author Andy Clement
5252
* @author Juergen Hoeller
53+
* @author Sam Brannen
5354
* @since 3.0
5455
*/
5556
public class MethodReference extends SpelNodeImpl {
5657

57-
private final String name;
58-
5958
private final boolean nullSafe;
6059

60+
private final String name;
61+
6162
@Nullable
6263
private String originalPrimitiveExitTypeDescriptor;
6364

@@ -72,6 +73,17 @@ public MethodReference(boolean nullSafe, String methodName, int startPos, int en
7273
}
7374

7475

76+
/**
77+
* Does this node represent a null-safe method reference?
78+
* @since 6.0.13
79+
*/
80+
public final boolean isNullSafe() {
81+
return this.nullSafe;
82+
}
83+
84+
/**
85+
* Get the name of the referenced method.
86+
*/
7587
public final String getName() {
7688
return this.name;
7789
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,16 @@ public PropertyOrFieldReference(boolean nullSafe, String propertyOrFieldName, in
7373
}
7474

7575

76+
/**
77+
* Does this node represent a null-safe property or field reference?
78+
*/
7679
public boolean isNullSafe() {
7780
return this.nullSafe;
7881
}
7982

83+
/**
84+
* Get the name of the referenced property or field.
85+
*/
8086
public String getName() {
8187
return this.name;
8288
}

spring-expression/src/test/java/org/springframework/expression/spel/ParsingTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class Miscellaneous {
4747
void compoundExpressions() {
4848
parseCheck("property1.property2.methodOne()");
4949
parseCheck("property1[0].property2['key'].methodOne()");
50+
parseCheck("property1?.methodOne()?.property2?.methodTwo()");
5051
}
5152

5253
@Test

0 commit comments

Comments
 (0)