[groovy] branch GROOVY_2_5_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_2_5_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_2_5_X
in repository https://gitbox.apache.org/repos/asf/groovy.git


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

commit cc17342108fe16a98d684ef7b7128794bb6a40c6
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      | 16 +++++++++++++---
 .../groovy/transform/DelegateASTTransformation.java      |  2 +-
 .../groovy/transform/DelegateTransformTest.groovy        | 16 ++++++++++++++++
 4 files changed, 45 insertions(+), 4 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 1446762..9c5a72e 100644
--- a/src/main/java/org/codehaus/groovy/ast/tools/GeneralUtils.java
+++ b/src/main/java/org/codehaus/groovy/ast/tools/GeneralUtils.java
@@ -251,6 +251,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)  {
 
@@ -262,6 +273,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 4fc13d2..c33591d 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) {
-        final List<AnnotationNode> copiedAnnotations = new ArrayList<AnnotationNode>();
-        final List<AnnotationNode> notCopied = new ArrayList<AnnotationNode>();
-        GeneralUtils.copyAnnotatedNodeAnnotations(annotatedNode, copiedAnnotations, notCopied);
+        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, 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 6da77ed..355a81e 100644
--- a/src/main/java/org/codehaus/groovy/transform/DelegateASTTransformation.java
+++ b/src/main/java/org/codehaus/groovy/transform/DelegateASTTransformation.java
@@ -337,7 +337,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 b7ac685..9759d35 100644
--- a/src/test/org/codehaus/groovy/transform/DelegateTransformTest.groovy
+++ b/src/test/org/codehaus/groovy/transform/DelegateTransformTest.groovy
@@ -828,6 +828,18 @@ assert foo.dm.x == '123'
             assert new BugsMe().length == 2
         '''
     }
+
+    // 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 {
@@ -904,6 +916,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 {