Support prefix notation for SpEL increment/decrement in AST representation

Closes gh-32144
This commit is contained in:
Sam Brannen 2024-01-28 15:20:57 +01:00
parent 179b976964
commit e34ad6bf5f
3 changed files with 12 additions and 9 deletions

View File

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

View File

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

View File

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