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 2023/07/30 21:39:14 UTC

[groovy] branch master updated: GROOVY-10750: compound constant inlining

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 9f64c9df13 GROOVY-10750: compound constant inlining
9f64c9df13 is described below

commit 9f64c9df13418290b0183a20aabb212aac3b3757
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Sun Jul 30 16:39:01 2023 -0500

    GROOVY-10750: compound constant inlining
---
 .../apache/groovy/ast/tools/ExpressionUtils.java   | 21 +++---
 .../codehaus/groovy/control/ResolveVisitor.java    |  9 ++-
 src/test/gls/annotations/AnnotationTest.groovy     | 83 ++++++++++++++++------
 3 files changed, 82 insertions(+), 31 deletions(-)

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 73fa948f96..11978add4c 100644
--- a/src/main/java/org/apache/groovy/ast/tools/ExpressionUtils.java
+++ b/src/main/java/org/apache/groovy/ast/tools/ExpressionUtils.java
@@ -231,16 +231,17 @@ public final class ExpressionUtils {
             if (pe.getObjectExpression() instanceof ClassExpression) {
                 ClassNode clazz = pe.getObjectExpression().getType();
                 FieldNode field = ClassNodeUtils.getField(clazz, pe.getPropertyAsString());
-                if (field != null && field.isStatic() && field.isFinal() && !field.isEnum()
-                        && 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);
+                if (field != null && !field.isEnum() && field.isFinal() && field.isStatic() && field.hasInitialExpression()) {
+                    Expression value = transformInlineConstants(field.getInitialValueExpression()); // GROOVY-10750
+                    if (value instanceof ConstantExpression) {
+                        value = new ConstantExpression(((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, (ConstantExpression) value);
+                    }
                 }
             }
         } else if (exp instanceof BinaryExpression) {
diff --git a/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java b/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java
index 6ce3aa6a34..3eacaa4383 100644
--- a/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java
+++ b/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java
@@ -1330,8 +1330,15 @@ public class ResolveVisitor extends ClassCodeExpressionTransformer {
             // initialize scopes/variables now that imports and super types are resolved
             new VariableScopeVisitor(source).visitClass(node);
 
+            visitPackage(node.getPackage());
+            visitImports(node.getModule());
+
+            node.visitContents(this);
+            visitObjectInitializerStatements(node);
+
+            // GROOVY-10750: do last for inlining
             visitTypeAnnotations(node);
-            super.visitClass(node);
+            visitAnnotations(node);
         }
         currentClass = oldNode;
     }
diff --git a/src/test/gls/annotations/AnnotationTest.groovy b/src/test/gls/annotations/AnnotationTest.groovy
index d96b1970ab..02c5c80622 100644
--- a/src/test/gls/annotations/AnnotationTest.groovy
+++ b/src/test/gls/annotations/AnnotationTest.groovy
@@ -132,7 +132,8 @@ final class AnnotationTest {
         '''
     }
 
-    @Test // GROOVY-4811
+    // GROOVY-4811
+    @Test
     void testArrayDefault() {
         assertScript shell, '''
             @Retention(RetentionPolicy.RUNTIME)
@@ -416,7 +417,7 @@ final class AnnotationTest {
     }
 
     @Test
-    void testAttributeValueConstants() {
+    void testAttributeValueConstants1() {
         assertScript shell, '''
             import static Constants.*
 
@@ -491,7 +492,8 @@ final class AnnotationTest {
         '''
     }
 
-    @Test // GROOVY-3278
+    // GROOVY-3278
+    @Test
     void testAttributeValueConstants3() {
         assertScript shell, '''
             import gls.annotations.*
@@ -516,7 +518,8 @@ final class AnnotationTest {
         '''
     }
 
-    @Test // GROOVY-8898
+    // GROOVY-8898
+    @Test
     void testAttributeValueConstants4() {
         assertScript shell, '''
             @Retention(RetentionPolicy.RUNTIME)
@@ -605,7 +608,8 @@ final class AnnotationTest {
         '''
     }
 
-    @Test // GROOVY-10068
+    // GROOVY-10068
+    @Test
     void testAttributeValueConstants6() {
         assertScript shell, '''
             @interface A {
@@ -620,7 +624,8 @@ final class AnnotationTest {
         '''
     }
 
-    @Test // GROOVY-7252
+    // GROOVY-7252
+    @Test
     void testAttributeValueConstants7() {
         assertScript shell, '''
             @interface A {
@@ -632,7 +637,8 @@ final class AnnotationTest {
         '''
     }
 
-    @Test // GROOVY-9366
+    // GROOVY-9366
+    @Test
     void testAttributeValueConstants8() {
         assertScript shell, '''
             @interface A {
@@ -644,7 +650,8 @@ final class AnnotationTest {
         '''
     }
 
-    @Test // GROOVY-9206
+    // GROOVY-9206
+    @Test
     void testAttributeValueConstants9() {
         assertScript shell, '''
             @interface A {
@@ -656,6 +663,30 @@ final class AnnotationTest {
         '''
     }
 
+    // GROOVY-10750
+    @Test
+    void testAttributeValueConstants10() {
+        assertScript shell, '''
+            @Retention(RUNTIME)
+            @interface Foo {
+                String value()
+            }
+            @groovy.transform.CompileStatic
+            @Foo(BarController.BASE_URL)
+            class BarController {
+                public static final String BASE_URL = '/bars'
+            }
+            @groovy.transform.CompileStatic
+            @Foo(BazController.BASE_URL)
+            class BazController {
+                public static final String BASE_URL = BarController.BASE_URL + '/{barId}/bazs'
+            }
+
+            def foo = BazController.getAnnotation(Foo)
+            assert foo.value() == '/bars/{barId}/bazs'
+        '''
+    }
+
     @Test
     void testRuntimeRetentionAtAllLevels() {
         assertScript shell, '''
@@ -776,7 +807,8 @@ final class AnnotationTest {
         '''
     }
 
-    @Test // GROOVY-6025
+    // GROOVY-6025
+    @Test
     void testAnnotationDefinitionDefaultValues() {
         assertScript shell, '''
             @Retention(RetentionPolicy.RUNTIME)
@@ -813,7 +845,8 @@ final class AnnotationTest {
         '''
     }
 
-    @NotYetImplemented @Test // GROOVY-6025
+    // GROOVY-6025
+    @NotYetImplemented @Test
     void testAnnotationDefinitionDefaultValues2() {
         assertScript shell, '''
             @interface A {
@@ -828,7 +861,8 @@ final class AnnotationTest {
         '''
     }
 
-    @Test // GROOVY-9205
+    // GROOVY-9205
+    @Test
     void testAnnotationDefinitionDefaultValues3() {
         assertScript shell, '''
             @interface A {
@@ -850,7 +884,8 @@ final class AnnotationTest {
         '''
     }
 
-    @Test // GROOVY-9205
+    // GROOVY-9205
+    @Test
     void testAnnotationDefinitionDefaultValues4() {
         assertScript shell, '''
             @interface A {
@@ -866,7 +901,8 @@ final class AnnotationTest {
         '''
     }
 
-    @Test // GROOVY-6093
+    // GROOVY-6093
+    @Test
     void testAnnotationOnEnumConstant() {
         assertScript shell, '''
             import gls.annotations.XmlEnum
@@ -881,7 +917,8 @@ final class AnnotationTest {
         '''
     }
 
-    @Test // GROOVY-7151
+    // GROOVY-7151
+    @Test
     void testAnnotateAnnotationDefinitionWithAnnotationWithTypeTarget() {
         shell.parse codeWithMetaAnnotationWithTarget('TYPE')
     }
@@ -907,7 +944,8 @@ final class AnnotationTest {
         """
     }
 
-    @Test // GROOVY-7806
+    // GROOVY-7806
+    @Test
     void testNewlinesAllowedBeforeBlock() {
         shell.parse '''
             @interface ANNOTATION_A
@@ -916,7 +954,8 @@ final class AnnotationTest {
         '''
     }
 
-    @Test // GROOVY-8226
+    // GROOVY-8226
+    @Test
     void testAnnotationOnParameterType() {
         assertScript shell, '''
             @Target([PARAMETER, FIELD, METHOD, ANNOTATION_TYPE, TYPE_USE])
@@ -938,7 +977,8 @@ final class AnnotationTest {
         '''
     }
 
-    @Test // GROOVY-8234
+    // GROOVY-8234
+    @Test
     void testAnnotationWithRepeatableSupported() {
         assertScript shell, '''
             class MyClass {
@@ -1021,7 +1061,8 @@ final class AnnotationTest {
         }
     }
 
-    @Test // GROOVY-9452
+    // GROOVY-9452
+    @Test
     void testDuplicationAnnotationOnClassWithParams() {
         def err = shouldFail shell, '''
             @Target(ElementType.TYPE)
@@ -1044,7 +1085,8 @@ final class AnnotationTest {
         assert err =~ /Cannot specify duplicate annotation/
     }
 
-    @Test // GROOVY-7033
+    // GROOVY-7033
+    @Test
     void testClassLiteralsRecognizedForAnonymousInnerClassAnnotationUsage() {
         shell.parse '''
             @interface A {
@@ -1129,7 +1171,8 @@ final class AnnotationTest {
         '''
     }
 
-    @Test // GROOVY-10857
+    // GROOVY-10857
+    @Test
     void testAnnotationWithCircularReference() {
         assertScript shell, '''
             @A @Documented @Retention(RUNTIME) @Target(TYPE)