Skip to content

Commit e7cf54d

Browse files
committed
Revise SpEL inline collection caching improvements
This commit revises the contribution for gh-25921 in the following ways. - Use instanceof pattern matching - Use List.of() and Map.of() - Add missing @⁠since tags - Polish Javadoc - Rename isNegativeNumber() to isNegativeNumberLiteral() - Restructure InlineCollectionTests using @⁠Nested, etc. - Fix testListWithVariableNotCached() test: it previously set a SpEL "variable" but tested a "property" in the root context object, which effectively did not test anything. - Introduce additional tests: listWithPropertyAccessIsNotCached(), mapWithVariableIsNotCached(), and mapWithPropertyAccessIsNotCached().
1 parent 2cc1ee7 commit e7cf54d

File tree

5 files changed

+204
-159
lines changed

5 files changed

+204
-159
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ private TypedValue computeConstantValue() {
6868
return null;
6969
}
7070
}
71-
else if (!(child instanceof OpMinus) || !((OpMinus) child).isNegativeNumber()) {
71+
else if (!(child instanceof OpMinus opMinus) || !opMinus.isNegativeNumberLiteral()) {
7272
return null;
7373
}
7474
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ else if (child instanceof InlineMap inlineMap) {
7171
}
7272
}
7373
else if (!(c % 2 == 0 && child instanceof PropertyOrFieldReference)) {
74-
if (!(child instanceof OpMinus) || !((OpMinus) child).isNegativeNumber()) {
74+
if (!(child instanceof OpMinus opMinus) || !opMinus.isNegativeNumberLiteral()) {
7575
return null;
7676
}
7777
}

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@ public final TypedValue getValueInternal(ExpressionState state) throws SpelEvalu
5353
return getLiteralValue();
5454
}
5555

56+
/**
57+
* Determine if this literal represents a number.
58+
* @return {@code true} if this literal represents a number
59+
* @since 6.1
60+
*/
61+
public boolean isNumberLiteral() {
62+
return (this instanceof IntLiteral ||
63+
this instanceof LongLiteral ||
64+
this instanceof FloatLiteral ||
65+
this instanceof RealLiteral);
66+
}
67+
5668
@Override
5769
public String toString() {
5870
return String.valueOf(getLiteralValue().getValue());
@@ -111,15 +123,4 @@ public static Literal getRealLiteral(String numberToken, int startPos, int endPo
111123
}
112124
}
113125

114-
/**
115-
* Check whether this literal is a number.
116-
* @return true if this is a number
117-
*/
118-
public boolean isNumberLiteral() {
119-
return this instanceof IntLiteral ||
120-
this instanceof LongLiteral ||
121-
this instanceof FloatLiteral ||
122-
this instanceof RealLiteral;
123-
}
124-
125126
}

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,17 @@ public OpMinus(int startPos, int endPos, SpelNodeImpl... operands) {
5454
}
5555

5656

57+
/**
58+
* Determine if this operator is a unary minus and its child is a
59+
* {@linkplain Literal#isNumberLiteral() number literal}.
60+
* @return {@code true} if it is a negative number literal
61+
* @since 6.1
62+
*/
63+
public boolean isNegativeNumberLiteral() {
64+
return (this.children.length == 1 && this.children[0] instanceof Literal literal &&
65+
literal.isNumberLiteral());
66+
}
67+
5768
@Override
5869
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
5970
SpelNodeImpl leftOp = getLeftOperand();
@@ -206,15 +217,4 @@ public void generateCode(MethodVisitor mv, CodeFlow cf) {
206217
cf.pushDescriptor(this.exitTypeDescriptor);
207218
}
208219

209-
/**
210-
* Check whether this operator is an unary minus and it's child is a number.
211-
* @return true if it is a negative number
212-
*/
213-
public boolean isNegativeNumber() {
214-
if (children.length == 1 && children[0] instanceof Literal) {
215-
return ((Literal) children[0]).isNumberLiteral();
216-
}
217-
return false;
218-
}
219-
220220
}

0 commit comments

Comments
 (0)