You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2021/07/01 22:49:41 UTC
[groovy] 01/02: GROOVY-10159: Compilation failure:
ClassCastException (closes #1604)
This is an automated email from the ASF dual-hosted git repository.
sunlan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit 1907fea95b79e523f94cd99eaa601c66666c047e
Author: Dipanjan Bhowmik <di...@lexmark.com>
AuthorDate: Fri Jul 2 06:32:13 2021 +0800
GROOVY-10159: Compilation failure: ClassCastException (closes #1604)
---
.../apache/groovy/ast/tools/ExpressionUtils.java | 2 +-
.../groovy/ast/tools/ExpressionUtilsTest.groovy | 72 ++++++++++++++++++++++
2 files changed, 73 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 aad536c..bb9790c 100644
--- a/src/main/java/org/apache/groovy/ast/tools/ExpressionUtils.java
+++ b/src/main/java/org/apache/groovy/ast/tools/ExpressionUtils.java
@@ -84,7 +84,7 @@ 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) ((ConstantExpression) left).getValue() + ((ConstantExpression) right).getValue()));
+ return configure(be, new ConstantExpression(String.valueOf(((ConstantExpression) left).getValue()) + ((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
new file mode 100644
index 0000000..2f6b5f7
--- /dev/null
+++ b/src/test/org/apache/groovy/ast/tools/ExpressionUtilsTest.groovy
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.groovy.ast.tools
+
+import groovy.test.GroovyTestCase
+import groovy.test.NotYetImplemented
+import groovy.transform.AutoFinal
+import org.apache.groovy.parser.antlr4.TestUtils
+import org.codehaus.groovy.ast.ClassHelper
+import org.codehaus.groovy.ast.ClassNode
+import org.codehaus.groovy.ast.expr.BinaryExpression
+import org.codehaus.groovy.ast.expr.ConstantExpression
+import org.codehaus.groovy.control.CompilationUnit
+import org.codehaus.groovy.control.Phases
+import org.codehaus.groovy.syntax.Token
+import org.codehaus.groovy.syntax.Types
+import org.junit.Assert
+
+import static org.apache.groovy.parser.antlr4.util.ASTComparatorCategory.LOCATION_IGNORE_LIST
+
+/**
+ * Testing expressions of ExpressionUtils.
+ */
+@AutoFinal
+final class ExpressionUtilsTest extends GroovyTestCase {
+
+ void 'test transformBinaryConstantExpression - null'() {
+ try {
+ ExpressionUtils.transformBinaryConstantExpression(null, null)
+ } catch(NullPointerException npe) {
+ // Pass
+ return
+ }
+ Assert.fail('Should throw NullPointerException')
+ }
+
+ void 'test transformBinaryConstantExpression - string PLUS string'() {
+ ConstantExpression left = new ConstantExpression('hello, ')
+ ConstantExpression right = new ConstantExpression('world!')
+ 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)
+ assertEquals('hello, world!', actual.value)
+ }
+
+ void 'test transformBinaryConstantExpression - long PLUS long'() {
+ ConstantExpression left = new ConstantExpression(11111111L)
+ ConstantExpression right = new ConstantExpression(11111111L)
+ Token token = new Token(Types.PLUS, '+', 1, 1)
+ BinaryExpression be = new BinaryExpression(left, token, right)
+ ClassNode targetType = ClassHelper.make(Long)
+ ConstantExpression actual = ExpressionUtils.transformBinaryConstantExpression(be, targetType)
+ assertEquals(22222222L, actual.value)
+ }
+}