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 2021/01/20 18:19:55 UTC

[groovy] branch GROOVY-9901 created (now 9a7fba7)

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

emilles pushed a change to branch GROOVY-9901
in repository https://gitbox.apache.org/repos/asf/groovy.git.


      at 9a7fba7  GROOVY-9901: enable semantic analysis transforms for trait class

This branch includes the following new commits:

     new 9a7fba7  GROOVY-9901: enable semantic analysis transforms for trait class

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[groovy] 01/01: GROOVY-9901: enable semantic analysis transforms for trait class

Posted by em...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 9a7fba75e5051e4df56935c714f5edd4800e3885
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Wed Jan 20 12:19:36 2021 -0600

    GROOVY-9901: enable semantic analysis transforms for trait class
---
 .../groovy/transform/ASTTransformationVisitor.java | 24 +++++++++++++++++++++
 .../transform/trait/TraitASTTransformation.java    | 17 ++++++---------
 .../traitx/TraitASTTransformationTest.groovy       | 25 ++++++++++++++++++++++
 3 files changed, 56 insertions(+), 10 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/transform/ASTTransformationVisitor.java b/src/main/java/org/codehaus/groovy/transform/ASTTransformationVisitor.java
index 2f0350b..140858c 100644
--- a/src/main/java/org/codehaus/groovy/transform/ASTTransformationVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/ASTTransformationVisitor.java
@@ -208,6 +208,7 @@ public final class ASTTransformationVisitor extends ClassCodeVisitorSupport {
             GroovyClassVisitor visitor = new ASTTransformationCollectorCodeVisitor(source, compilationUnit.getTransformLoader());
             visitor.visitClass(classNode);
         }, Phases.SEMANTIC_ANALYSIS);
+
         for (CompilePhase phase : CompilePhase.values()) {
             switch (phase) {
                 case INITIALIZATION:
@@ -228,6 +229,29 @@ public final class ASTTransformationVisitor extends ClassCodeVisitorSupport {
         }
     }
 
+    /**
+     * Enables transforms for class that was added during current phase.
+     */
+    public static void addNewPhaseOperation(final CompilationUnit compilationUnit, final SourceUnit sourceUnit, final ClassNode classNode) {
+        int phase = compilationUnit.getPhase();
+        if (phase < Phases.SEMANTIC_ANALYSIS) {
+            return;
+        }
+
+        {
+            GroovyClassVisitor visitor = new ASTTransformationCollectorCodeVisitor(sourceUnit, compilationUnit.getTransformLoader());
+            visitor.visitClass(classNode);
+        }
+
+        compilationUnit.addNewPhaseOperation(source -> {
+            if (source == sourceUnit) {
+                ASTTransformationVisitor visitor = new ASTTransformationVisitor(CompilePhase.fromPhaseNumber(phase), compilationUnit.getASTTransformationsContext());
+                visitor.source = source;
+                visitor.visitClass(classNode);
+            }
+        }, phase);
+    }
+
     public static void addGlobalTransformsAfterGrab(ASTTransformationsContext context) {
         doAddGlobalTransforms(context, false);
     }
diff --git a/src/main/java/org/codehaus/groovy/transform/trait/TraitASTTransformation.java b/src/main/java/org/codehaus/groovy/transform/trait/TraitASTTransformation.java
index 3b314ec..5191cf4 100644
--- a/src/main/java/org/codehaus/groovy/transform/trait/TraitASTTransformation.java
+++ b/src/main/java/org/codehaus/groovy/transform/trait/TraitASTTransformation.java
@@ -48,7 +48,7 @@ import org.codehaus.groovy.control.CompilePhase;
 import org.codehaus.groovy.control.SourceUnit;
 import org.codehaus.groovy.runtime.InvokerHelper;
 import org.codehaus.groovy.syntax.SyntaxException;
-import org.codehaus.groovy.transform.ASTTransformationCollectorCodeVisitor;
+import org.codehaus.groovy.transform.ASTTransformationVisitor;
 import org.codehaus.groovy.transform.AbstractASTTransformation;
 import org.codehaus.groovy.transform.GroovyASTTransformation;
 
@@ -346,16 +346,13 @@ public class TraitASTTransformation extends AbstractASTTransformation implements
     }
 
     private void registerASTTransformations(final ClassNode helper) {
-        {
-            GroovyClassVisitor visitor = new ASTTransformationCollectorCodeVisitor(sourceUnit, compilationUnit.getTransformLoader());
-            visitor.visitClass(helper);
-        }
-        // Perform an additional phase which has to be done *after* type checking
+        ASTTransformationVisitor.addNewPhaseOperation(compilationUnit, sourceUnit, helper);
+        // perform an additional operation which has to be done *after* static type checking
         compilationUnit.addPhaseOperation((final SourceUnit source, final GeneratorContext context, final ClassNode classNode) -> {
-            if (classNode != helper) return;
-
-            GroovyClassVisitor visitor = new PostTypeCheckingExpressionReplacer(source);
-            visitor.visitClass(helper);
+            if (classNode == helper) {
+                GroovyClassVisitor visitor = new PostTypeCheckingExpressionReplacer(source);
+                visitor.visitClass(helper);
+            }
         }, CompilePhase.INSTRUCTION_SELECTION.getPhaseNumber());
     }
 
diff --git a/src/test/org/codehaus/groovy/transform/traitx/TraitASTTransformationTest.groovy b/src/test/org/codehaus/groovy/transform/traitx/TraitASTTransformationTest.groovy
index c11798d..b5f69de 100644
--- a/src/test/org/codehaus/groovy/transform/traitx/TraitASTTransformationTest.groovy
+++ b/src/test/org/codehaus/groovy/transform/traitx/TraitASTTransformationTest.groovy
@@ -1791,6 +1791,31 @@ final class TraitASTTransformationTest {
         '''
     }
 
+    @Test // GROOVY-9901
+    void testTraitWithMemozied() {
+        assertScript '''
+            trait Foo {
+                @groovy.transform.Memoized
+                double method() {
+                    Math.random()
+                }
+            }
+            class Bar implements Foo {}
+            class Baz implements Foo {}
+
+            def bar = new Bar()
+            def a = bar.method()
+            def b = bar.method()
+            def c = new Bar().method()
+            def d = new Baz().method()
+
+            assert a == b
+            assert a != c
+            assert a != d
+            assert c != d
+        '''
+    }
+
     @Test
     void testAnnotationShouldBeCarriedOver() {
         assertScript '''