From e34ad6bf5f2edf26cf77fb659ff6b86ee9c70e4e Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Sun, 28 Jan 2024 15:20:57 +0100 Subject: [PATCH] Support prefix notation for SpEL increment/decrement in AST representation Closes gh-32144 --- .../org/springframework/expression/spel/ast/OpDec.java | 9 ++++++--- .../org/springframework/expression/spel/ast/OpInc.java | 9 ++++++--- .../springframework/expression/spel/ParsingTests.java | 3 --- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpDec.java b/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpDec.java index d61e8d6410..5152ff6139 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpDec.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpDec.java @@ -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; */ 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 { @Override public String toStringAST() { - return getLeftOperand().toStringAST() + "--"; + String ast = getLeftOperand().toStringAST(); + return (this.postfix ? ast + DEC : DEC + ast); } @Override diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpInc.java b/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpInc.java index f6dc184c0f..89aa7a7390 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpInc.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/ast/OpInc.java @@ -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; */ 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 { @Override public String toStringAST() { - return getLeftOperand().toStringAST() + "++"; + String ast = getLeftOperand().toStringAST(); + return (this.postfix ? ast + INC : INC + ast); } @Override diff --git a/spring-expression/src/test/java/org/springframework/expression/spel/ParsingTests.java b/spring-expression/src/test/java/org/springframework/expression/spel/ParsingTests.java index ca9c6d6df1..042a10c0b7 100644 --- a/spring-expression/src/test/java/org/springframework/expression/spel/ParsingTests.java +++ b/spring-expression/src/test/java/org/springframework/expression/spel/ParsingTests.java @@ -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 { 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 { parseCheck("foo++", "foo++"); } - @Disabled("Disabled due to a bug in OpDec.toStringAST()") @Test void mathOperatorDecrementPrefix() { parseCheck("--7", "--7");