Browse Source

Merge branch '6.0.x'

pull/31328/head
Sam Brannen 3 years ago
parent
commit
515d8aae94
  1. 8
      spring-expression/src/main/java/org/springframework/expression/spel/ast/CompoundExpression.java
  2. 16
      spring-expression/src/main/java/org/springframework/expression/spel/ast/MethodReference.java
  3. 6
      spring-expression/src/main/java/org/springframework/expression/spel/ast/PropertyOrFieldReference.java
  4. 1
      spring-expression/src/test/java/org/springframework/expression/spel/ParsingTests.java

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

@ -28,7 +28,9 @@ import org.springframework.expression.spel.SpelNode; @@ -28,7 +28,9 @@ import org.springframework.expression.spel.SpelNode;
/**
* Represents a DOT separated expression sequence, such as
* {@code property1.property2.methodOne()}.
* {@code property1.property2.methodOne()} or
* {@code property1?.property2?.methodOne()} when the null-safe navigation
* operator is used.
*
* <p>May also contain array/collection/map indexers, such as
* {@code property1[0].property2['key']}.
@ -122,6 +124,10 @@ public class CompoundExpression extends SpelNodeImpl { @@ -122,6 +124,10 @@ public class CompoundExpression extends SpelNodeImpl {
// Don't append a '.' if the next child is an Indexer.
// For example, we want 'myVar[0]' instead of 'myVar.[0]'.
if (!(nextChild instanceof Indexer)) {
if ((nextChild instanceof MethodReference methodRef && methodRef.isNullSafe()) ||
(nextChild instanceof PropertyOrFieldReference pofRef && pofRef.isNullSafe())) {
sb.append('?');
}
sb.append('.');
}
}

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

@ -50,14 +50,15 @@ import org.springframework.util.ObjectUtils; @@ -50,14 +50,15 @@ import org.springframework.util.ObjectUtils;
*
* @author Andy Clement
* @author Juergen Hoeller
* @author Sam Brannen
* @since 3.0
*/
public class MethodReference extends SpelNodeImpl {
private final String name;
private final boolean nullSafe;
private final String name;
@Nullable
private String originalPrimitiveExitTypeDescriptor;
@ -72,6 +73,17 @@ public class MethodReference extends SpelNodeImpl { @@ -72,6 +73,17 @@ public class MethodReference extends SpelNodeImpl {
}
/**
* Does this node represent a null-safe method reference?
* @since 6.0.13
*/
public final boolean isNullSafe() {
return this.nullSafe;
}
/**
* Get the name of the referenced method.
*/
public final String getName() {
return this.name;
}

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

@ -73,10 +73,16 @@ public class PropertyOrFieldReference extends SpelNodeImpl { @@ -73,10 +73,16 @@ public class PropertyOrFieldReference extends SpelNodeImpl {
}
/**
* Does this node represent a null-safe property or field reference?
*/
public boolean isNullSafe() {
return this.nullSafe;
}
/**
* Get the name of the referenced property or field.
*/
public String getName() {
return this.name;
}

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

@ -47,6 +47,7 @@ class ParsingTests { @@ -47,6 +47,7 @@ class ParsingTests {
void compoundExpressions() {
parseCheck("property1.property2.methodOne()");
parseCheck("property1[0].property2['key'].methodOne()");
parseCheck("property1?.methodOne()?.property2?.methodTwo()");
}
@Test

Loading…
Cancel
Save