You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2021/08/26 06:23:44 UTC
[groovy] branch GROOVY_2_5_X updated: GROOVY-10159: Compilation
failure: ClassCastException (only simplify the simple cases at compile
time)
This is an automated email from the ASF dual-hosted git repository.
paulk pushed a commit to branch GROOVY_2_5_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/GROOVY_2_5_X by this push:
new fb66e60 GROOVY-10159: Compilation failure: ClassCastException (only simplify the simple cases at compile time)
fb66e60 is described below
commit fb66e601412d744b2a6bab028d7fe17bcd53ba74
Author: Paul King <pa...@asert.com.au>
AuthorDate: Wed Aug 25 21:05:43 2021 +1000
GROOVY-10159: Compilation failure: ClassCastException (only simplify the simple cases at compile time)
---
.../org/apache/groovy/ast/tools/ExpressionUtils.java | 6 +++++-
.../groovy/ast/tools/ExpressionUtilsTest.groovy | 19 +++++++++++++++++++
2 files changed, 24 insertions(+), 1 deletion(-)
diff --git a/src/main/java/org/apache/groovy/ast/tools/ExpressionUtils.java b/src/main/java/org/apache/groovy/ast/tools/ExpressionUtils.java
index 8c40a17..8cec61a 100644
--- a/src/main/java/org/apache/groovy/ast/tools/ExpressionUtils.java
+++ b/src/main/java/org/apache/groovy/ast/tools/ExpressionUtils.java
@@ -83,7 +83,11 @@ public final class ExpressionUtils {
Expression left = transformInlineConstants(be.getLeftExpression(), targetType);
Expression right = transformInlineConstants(be.getRightExpression(), targetType);
if (left instanceof ConstantExpression && right instanceof ConstantExpression) {
- return configure(be, new ConstantExpression(String.valueOf(((ConstantExpression) left).getValue()) + ((ConstantExpression) right).getValue()));
+ Object leftV = ((ConstantExpression) left).getValue();
+ if (leftV == null) leftV = "null";
+ if (leftV instanceof String) {
+ return configure(be, new ConstantExpression(((String)leftV) + ((ConstantExpression) right).getValue()));
+ }
}
}
} else if (isNumberOrArrayOfNumber(wrapperType, false)) {
diff --git a/src/test/org/apache/groovy/ast/tools/ExpressionUtilsTest.groovy b/src/test/org/apache/groovy/ast/tools/ExpressionUtilsTest.groovy
index add6f0c..d8336a1 100644
--- a/src/test/org/apache/groovy/ast/tools/ExpressionUtilsTest.groovy
+++ b/src/test/org/apache/groovy/ast/tools/ExpressionUtilsTest.groovy
@@ -26,6 +26,7 @@ import org.codehaus.groovy.syntax.Token
import org.codehaus.groovy.syntax.Types
import org.junit.Test
+import static groovy.test.GroovyAssert.assertScript
import static org.junit.Assert.assertEquals
/**
@@ -66,6 +67,24 @@ final class ExpressionUtilsTest {
}
@Test
+ void 'test transformBinaryConstantExpression, integer + integer with target type string'() {
+ ConstantExpression left = new ConstantExpression(1)
+ ConstantExpression right = new ConstantExpression(1)
+ Token token = new Token(Types.PLUS, '+', 1, 1)
+ BinaryExpression be = new BinaryExpression(left, token, right)
+ ClassNode targetType = ClassHelper.make(String)
+ ConstantExpression actual = ExpressionUtils.transformBinaryConstantExpression(be, targetType)
+ assert !actual // null indicates it could not be transformed (simplified) at compile time
+ // but should still succeed at runtime as per below script
+ assertScript '''
+ class Foo {
+ static final String bar = 1 + 1
+ }
+ assert Foo.bar == '2'
+ '''
+ }
+
+ @Test
void 'test transformBinaryConstantExpression - long + long'() {
ConstantExpression left = new ConstantExpression(11111111L)
ConstantExpression right = new ConstantExpression(11111111L)