You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2022/07/06 18:27:37 UTC

[groovy] 03/03: GROOVY-10068: retain field type during constant inlining

This is an automated email from the ASF dual-hosted git repository.

emilles pushed a commit to branch GROOVY_4_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit 8215ed3c389b565dfe8cd01eb4727f018b11a9a7
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Wed Jul 6 11:45:20 2022 -0500

    GROOVY-10068: retain field type during constant inlining
---
 .../java/org/apache/groovy/ast/tools/ExpressionUtils.java |  5 +++++
 src/test/gls/annotations/AnnotationTest.groovy            | 15 +++++++++++++++
 2 files changed, 20 insertions(+)

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 373f8a283a..aaf4d3e7a0 100644
--- a/src/main/java/org/apache/groovy/ast/tools/ExpressionUtils.java
+++ b/src/main/java/org/apache/groovy/ast/tools/ExpressionUtils.java
@@ -230,6 +230,11 @@ public final class ExpressionUtils {
                         && field.getInitialValueExpression() instanceof ConstantExpression) {
                     ConstantExpression value = (ConstantExpression) field.getInitialValueExpression();
                     value = new ConstantExpression(value.getValue());
+                    // GROOVY-10068, et al.: retain field's type
+                    if (!value.getType().equals(field.getType())
+                            && ClassHelper.isNumberType(field.getType()))
+                        value.setType(field.getType());
+                    // TODO: Boolean, Character, String
                     return configure(exp, value);
                 }
             }
diff --git a/src/test/gls/annotations/AnnotationTest.groovy b/src/test/gls/annotations/AnnotationTest.groovy
index b8885c8d46..291ec12af9 100644
--- a/src/test/gls/annotations/AnnotationTest.groovy
+++ b/src/test/gls/annotations/AnnotationTest.groovy
@@ -604,6 +604,21 @@ final class AnnotationTest {
         '''
     }
 
+    @Test // GROOVY-10068
+    void testAttributeValueConstants6() {
+        assertScript shell, '''
+            @interface A {
+                short value()
+            }
+            class C {
+                public static final short ONE = 1
+            }
+
+            @A(C.ONE)
+            def local
+        '''
+    }
+
     @Test
     void testRuntimeRetentionAtAllLevels() {
         assertScript shell, '''