[groovy] branch GROOVY_3_0_X updated: GROOVY-8825: Conflict between @Generated and @Delegate (closes #1115)

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[groovy] branch GROOVY_3_0_X updated: GROOVY-8825: Conflict between @Generated and @Delegate (closes #1115)

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

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


The following commit(s) were added to refs/heads/GROOVY_3_0_X by this push:
     new d6220f6  GROOVY-8825: Conflict between @Generated and @Delegate (closes #1115)
d6220f6 is described below

commit d6220f6b0f79070e464fc2c922cea40b78713333
Author: Paul King <[hidden email]>
AuthorDate: Mon Dec 2 22:31:59 2019 +1000

    GROOVY-8825: Conflict between @Generated and @Delegate (closes #1115)
---
 .../java/org/codehaus/groovy/ast/tools/GeneralUtils.java | 15 +++++++++++++++
 .../groovy/transform/AbstractASTTransformation.java      | 12 +++++++++++-
 .../groovy/transform/DelegateASTTransformation.java      |  2 +-
 .../groovy/transform/DelegateTransformTest.groovy        | 16 ++++++++++++++++
 4 files changed, 43 insertions(+), 2 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/ast/tools/GeneralUtils.java b/src/main/java/org/codehaus/groovy/ast/tools/GeneralUtils.java
index 3241767..793769d 100644
--- a/src/main/java/org/codehaus/groovy/ast/tools/GeneralUtils.java
+++ b/src/main/java/org/codehaus/groovy/ast/tools/GeneralUtils.java
@@ -255,6 +255,17 @@ public class GeneralUtils {
      * Annotations with {@link org.codehaus.groovy.runtime.GeneratedClosure} members are not supported at present.
      */
     public static void copyAnnotatedNodeAnnotations(final AnnotatedNode annotatedNode, final List<AnnotationNode> copied, List<AnnotationNode> notCopied) {
+        copyAnnotatedNodeAnnotations(annotatedNode, copied, notCopied, true);
+    }
+
+    /**
+     * Copies all <tt>candidateAnnotations</tt> with retention policy {@link java.lang.annotation.RetentionPolicy#RUNTIME}
+     * and {@link java.lang.annotation.RetentionPolicy#CLASS}.
+     * {@link groovy.transform.Generated} annotations will be copied if {@code includeGenerated} is true.
+     * <p>
+     * Annotations with {@link org.codehaus.groovy.runtime.GeneratedClosure} members are not supported at present.
+     */
+    public static void copyAnnotatedNodeAnnotations(final AnnotatedNode annotatedNode, final List<AnnotationNode> copied, List<AnnotationNode> notCopied, boolean includeGenerated) {
         List<AnnotationNode> annotationList = annotatedNode.getAnnotations();
         for (AnnotationNode annotation : annotationList)  {
 
@@ -266,6 +277,10 @@ public class GeneralUtils {
                 continue;
             }
 
+            if (!includeGenerated && annotation.getClassNode().getName().equals("groovy.transform.Generated")) {
+                continue;
+            }
+
             AnnotationNode retentionPolicyAnnotation = annotations.get(0);
             Expression valueExpression = retentionPolicyAnnotation.getMember("value");
             if (!(valueExpression instanceof PropertyExpression)) continue;
diff --git a/src/main/java/org/codehaus/groovy/transform/AbstractASTTransformation.java b/src/main/java/org/codehaus/groovy/transform/AbstractASTTransformation.java
index bfe25e3..84002f5 100644
--- a/src/main/java/org/codehaus/groovy/transform/AbstractASTTransformation.java
+++ b/src/main/java/org/codehaus/groovy/transform/AbstractASTTransformation.java
@@ -67,9 +67,19 @@ public abstract class AbstractASTTransformation implements Opcodes, ASTTransform
      * Annotations with {@link org.codehaus.groovy.runtime.GeneratedClosure} members are not supported for now.
      */
     protected List<AnnotationNode> copyAnnotatedNodeAnnotations(final AnnotatedNode annotatedNode, String myTypeName) {
+        return copyAnnotatedNodeAnnotations(annotatedNode, myTypeName, true);
+    }
+
+    /**
+     * Copies all <tt>candidateAnnotations</tt> with retention policy {@link java.lang.annotation.RetentionPolicy#RUNTIME}
+     * and {@link java.lang.annotation.RetentionPolicy#CLASS}.
+     * <p>
+     * Annotations with {@link org.codehaus.groovy.runtime.GeneratedClosure} members are not supported for now.
+     */
+    protected List<AnnotationNode> copyAnnotatedNodeAnnotations(final AnnotatedNode annotatedNode, String myTypeName, boolean includeGenerated) {
         final List<AnnotationNode> copiedAnnotations = new ArrayList<>();
         final List<AnnotationNode> notCopied = new ArrayList<>();
-        GeneralUtils.copyAnnotatedNodeAnnotations(annotatedNode, copiedAnnotations, notCopied);
+        GeneralUtils.copyAnnotatedNodeAnnotations(annotatedNode, copiedAnnotations, notCopied, includeGenerated);
         for (AnnotationNode annotation : notCopied) {
             addError(myTypeName + " does not support keeping Closure annotation members.", annotation);
         }
diff --git a/src/main/java/org/codehaus/groovy/transform/DelegateASTTransformation.java b/src/main/java/org/codehaus/groovy/transform/DelegateASTTransformation.java
index a855275..6e6a417 100644
--- a/src/main/java/org/codehaus/groovy/transform/DelegateASTTransformation.java
+++ b/src/main/java/org/codehaus/groovy/transform/DelegateASTTransformation.java
@@ -362,7 +362,7 @@ public class DelegateASTTransformation extends AbstractASTTransformation {
             newMethod.setGenericsTypes(candidate.getGenericsTypes());
 
             if (memberHasValue(delegate.annotation, MEMBER_METHOD_ANNOTATIONS, true)) {
-                newMethod.addAnnotations(copyAnnotatedNodeAnnotations(candidate, MY_TYPE_NAME));
+                newMethod.addAnnotations(copyAnnotatedNodeAnnotations(candidate, MY_TYPE_NAME, false));
             }
         }
     }
diff --git a/src/test/org/codehaus/groovy/transform/DelegateTransformTest.groovy b/src/test/org/codehaus/groovy/transform/DelegateTransformTest.groovy
index bb60c83..f6b99f3 100644
--- a/src/test/org/codehaus/groovy/transform/DelegateTransformTest.groovy
+++ b/src/test/org/codehaus/groovy/transform/DelegateTransformTest.groovy
@@ -847,6 +847,18 @@ assert foo.dm.x == '123'
         """
         assert message.contains("Error during @Delegate processing: 'excludes' property or method 'name' does not exist.")
     }
+
+    // GROOVY-8825
+    void testDelegateToPrecompiledGroovyGeneratedMethod() {
+        assertScript '''
+            import org.codehaus.groovy.transform.CompiledClass8825
+            class B {
+                @Delegate(methodAnnotations = true)
+                private final CompiledClass8825 delegate = new CompiledClass8825()
+            }
+            assert new B().s == '456'
+        '''
+    }
 }
 
 interface DelegateFoo {
@@ -923,6 +935,10 @@ class Bar implements BarInt {
     }
 }
 
+class CompiledClass8825 {
+    final String s = '456'
+}
+
 // DO NOT MOVE INSIDE THE TEST SCRIPT OR IT WILL NOT TEST
 // WHAT IT IS SUPPOSED TO TEST ANYMORE !
 class DelegateMap {