Skip to content

Commit e34ad6b

Browse files
committed
Support prefix notation for SpEL increment/decrement in AST representation
Closes gh-32144
1 parent 179b976 commit e34ad6b

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

Diff for: spring-expression/src/main/java/org/springframework/expression/spel/ast/OpDec.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -39,11 +39,13 @@
3939
*/
4040
public class OpDec extends Operator {
4141

42+
private static final String DEC = "--";
43+
4244
private final boolean postfix; // false means prefix
4345

4446

4547
public OpDec(int startPos, int endPos, boolean postfix, SpelNodeImpl... operands) {
46-
super("--", startPos, endPos, operands);
48+
super(DEC, startPos, endPos, operands);
4749
this.postfix = postfix;
4850
Assert.notEmpty(operands, "Operands must not be empty");
4951
}
@@ -133,7 +135,8 @@ else if (op1 instanceof Byte) {
133135

134136
@Override
135137
public String toStringAST() {
136-
return getLeftOperand().toStringAST() + "--";
138+
String ast = getLeftOperand().toStringAST();
139+
return (this.postfix ? ast + DEC : DEC + ast);
137140
}
138141

139142
@Override

Diff for: spring-expression/src/main/java/org/springframework/expression/spel/ast/OpInc.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -39,11 +39,13 @@
3939
*/
4040
public class OpInc extends Operator {
4141

42+
private static final String INC = "++";
43+
4244
private final boolean postfix; // false means prefix
4345

4446

4547
public OpInc(int startPos, int endPos, boolean postfix, SpelNodeImpl... operands) {
46-
super("++", startPos, endPos, operands);
48+
super(INC, startPos, endPos, operands);
4749
this.postfix = postfix;
4850
Assert.notEmpty(operands, "Operands must not be empty");
4951
}
@@ -128,7 +130,8 @@ else if (op1 instanceof Byte) {
128130

129131
@Override
130132
public String toStringAST() {
131-
return getLeftOperand().toStringAST() + "++";
133+
String ast = getLeftOperand().toStringAST();
134+
return (this.postfix ? ast + INC : INC + ast);
132135
}
133136

134137
@Override

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

-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.expression.spel;
1818

19-
import org.junit.jupiter.api.Disabled;
2019
import org.junit.jupiter.api.Nested;
2120
import org.junit.jupiter.api.Test;
2221

@@ -370,7 +369,6 @@ void mathOperatorModulus() {
370369
parseCheck("7 % 4", "(7 % 4)");
371370
}
372371

373-
@Disabled("Disabled due to a bug in OpInc.toStringAST()")
374372
@Test
375373
void mathOperatorIncrementPrefix() {
376374
parseCheck("++7", "++7");
@@ -383,7 +381,6 @@ void mathOperatorIncrementPostfix() {
383381
parseCheck("foo++", "foo++");
384382
}
385383

386-
@Disabled("Disabled due to a bug in OpDec.toStringAST()")
387384
@Test
388385
void mathOperatorDecrementPrefix() {
389386
parseCheck("--7", "--7");

0 commit comments

Comments
 (0)