Browse Source

Support prefix notation for SpEL increment/decrement in AST representation

Closes gh-32144
pull/32150/head
Sam Brannen 2 years ago
parent
commit
e34ad6bf5f
  1. 9
      spring-expression/src/main/java/org/springframework/expression/spel/ast/OpDec.java
  2. 9
      spring-expression/src/main/java/org/springframework/expression/spel/ast/OpInc.java
  3. 3
      spring-expression/src/test/java/org/springframework/expression/spel/ParsingTests.java

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -39,11 +39,13 @@ import org.springframework.util.Assert; @@ -39,11 +39,13 @@ import org.springframework.util.Assert;
*/
public class OpDec extends Operator {
private static final String DEC = "--";
private final boolean postfix; // false means prefix
public OpDec(int startPos, int endPos, boolean postfix, SpelNodeImpl... operands) {
super("--", startPos, endPos, operands);
super(DEC, startPos, endPos, operands);
this.postfix = postfix;
Assert.notEmpty(operands, "Operands must not be empty");
}
@ -133,7 +135,8 @@ public class OpDec extends Operator { @@ -133,7 +135,8 @@ public class OpDec extends Operator {
@Override
public String toStringAST() {
return getLeftOperand().toStringAST() + "--";
String ast = getLeftOperand().toStringAST();
return (this.postfix ? ast + DEC : DEC + ast);
}
@Override

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -39,11 +39,13 @@ import org.springframework.util.Assert; @@ -39,11 +39,13 @@ import org.springframework.util.Assert;
*/
public class OpInc extends Operator {
private static final String INC = "++";
private final boolean postfix; // false means prefix
public OpInc(int startPos, int endPos, boolean postfix, SpelNodeImpl... operands) {
super("++", startPos, endPos, operands);
super(INC, startPos, endPos, operands);
this.postfix = postfix;
Assert.notEmpty(operands, "Operands must not be empty");
}
@ -128,7 +130,8 @@ public class OpInc extends Operator { @@ -128,7 +130,8 @@ public class OpInc extends Operator {
@Override
public String toStringAST() {
return getLeftOperand().toStringAST() + "++";
String ast = getLeftOperand().toStringAST();
return (this.postfix ? ast + INC : INC + ast);
}
@Override

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

@ -16,7 +16,6 @@ @@ -16,7 +16,6 @@
package org.springframework.expression.spel;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
@ -370,7 +369,6 @@ class ParsingTests { @@ -370,7 +369,6 @@ class ParsingTests {
parseCheck("7 % 4", "(7 % 4)");
}
@Disabled("Disabled due to a bug in OpInc.toStringAST()")
@Test
void mathOperatorIncrementPrefix() {
parseCheck("++7", "++7");
@ -383,7 +381,6 @@ class ParsingTests { @@ -383,7 +381,6 @@ class ParsingTests {
parseCheck("foo++", "foo++");
}
@Disabled("Disabled due to a bug in OpDec.toStringAST()")
@Test
void mathOperatorDecrementPrefix() {
parseCheck("--7", "--7");

Loading…
Cancel
Save