Skip to content

Commit 44b79c7

Browse files
committed
Support Optional with null-safe and Elvis operators in SpEL expressions
This commit introduces null-safe support for java.util.Optional in the following SpEL operators: - Elvis - Indexer - MethodReference - PropertyOrFieldReference - Projection - Selection TODO: Update the SpEL chapter of the reference manual. Closes spring-projectsgh-20433
1 parent eb7b26d commit 44b79c7

File tree

8 files changed

+700
-135
lines changed

8 files changed

+700
-135
lines changed

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

+31-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.expression.spel.ast;
1818

19+
import java.util.Optional;
20+
1921
import org.springframework.asm.Label;
2022
import org.springframework.asm.MethodVisitor;
2123
import org.springframework.expression.EvaluationException;
@@ -26,9 +28,13 @@
2628
import org.springframework.util.ObjectUtils;
2729

2830
/**
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>.
31+
* Represents the Elvis operator {@code ?:}.
32+
*
33+
* <p>For the expression "{@code A ?: B}", if {@code A} is neither {@code null},
34+
* an empty {@link Optional}, nor an empty {@link String}, the value of the
35+
* expression is {@code A}, or {@code A.get()} for an {@code Optional}. If
36+
* {@code A} is {@code null}, an empty {@code Optional}, or an
37+
* empty {@code String}, the value of the expression is {@code B}.
3238
*
3339
* @author Andy Clement
3440
* @author Juergen Hoeller
@@ -43,18 +49,32 @@ public Elvis(int startPos, int endPos, SpelNodeImpl... args) {
4349

4450

4551
/**
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.
52+
* If the left-hand operand is neither neither {@code null}, an empty
53+
* {@link Optional}, nor an empty {@link String}, return its value, or the
54+
* value contained in the {@code Optional}. If the left-hand operand is
55+
* {@code null}, an empty {@code Optional}, or an empty {@code String},
56+
* return the other value.
4857
* @param state the expression state
49-
* @throws EvaluationException if the condition does not evaluate correctly
50-
* to a boolean or there is a problem executing the chosen alternative
58+
* @throws EvaluationException if the null/empty check does not evaluate correctly
59+
* or there is a problem evaluating the alternative
5160
*/
5261
@Override
5362
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
54-
TypedValue value = this.children[0].getValueInternal(state);
63+
TypedValue leftHandTypedValue = this.children[0].getValueInternal(state);
64+
Object leftHandValue = leftHandTypedValue.getValue();
65+
66+
if (leftHandValue instanceof Optional<?> optional) {
67+
// Compilation is currently not supported for Optional with the Elvis operator.
68+
this.exitTypeDescriptor = null;
69+
if (optional.isPresent()) {
70+
return new TypedValue(optional.get());
71+
}
72+
return this.children[1].getValueInternal(state);
73+
}
74+
5575
// If this check is changed, the generateCode method will need changing too
56-
if (value.getValue() != null && !"".equals(value.getValue())) {
57-
return value;
76+
if (leftHandValue != null && !"".equals(leftHandValue)) {
77+
return leftHandTypedValue;
5878
}
5979
else {
6080
TypedValue result = this.children[1].getValueInternal(state);

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

+19-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Collection;
2121
import java.util.List;
2222
import java.util.Map;
23+
import java.util.Optional;
2324
import java.util.function.Supplier;
2425

2526
import org.jspecify.annotations.Nullable;
@@ -67,7 +68,12 @@
6768
* <p>As of Spring Framework 6.2, null-safe indexing is supported via the {@code '?.'}
6869
* operator. For example, {@code 'colors?.[0]'} will evaluate to {@code null} if
6970
* {@code colors} is {@code null} and will otherwise evaluate to the 0<sup>th</sup>
70-
* color.
71+
* color. As of Spring Framework 7.0, null-safe indexing also applies when
72+
* indexing into a structure contained in an {@link Optional}. For example, if
73+
* {@code colors} is of type {@code Optional<Colors>}, the expression
74+
* {@code 'colors?.[0]'} will evaluate to {@code null} if {@code colors} is
75+
* {@code null} or {@link Optional#isEmpty() empty} and will otherwise evaluate
76+
* to the 0<sup>th</sup> color, effectively {@code colors.get()[0]}.
7177
*
7278
* @author Andy Clement
7379
* @author Phillip Webb
@@ -165,11 +171,20 @@ private ValueRef getValueRef(ExpressionState state, AccessMode accessMode) throw
165171
TypedValue context = state.getActiveContextObject();
166172
Object target = context.getValue();
167173

168-
if (target == null) {
169-
if (isNullSafe()) {
174+
if (isNullSafe()) {
175+
if (target == null) {
170176
return ValueRef.NullValueRef.INSTANCE;
171177
}
172-
// Raise a proper exception in case of a null target
178+
if (target instanceof Optional<?> optional) {
179+
if (optional.isEmpty()) {
180+
return ValueRef.NullValueRef.INSTANCE;
181+
}
182+
target = optional.get();
183+
}
184+
}
185+
186+
// Raise a proper exception in case of a null target
187+
if (target == null) {
173188
throw new SpelEvaluationException(getStartPosition(), SpelMessage.CANNOT_INDEX_INTO_NULL_VALUE);
174189
}
175190

0 commit comments

Comments
 (0)