You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2020/09/12 16:26:18 UTC

[groovy] 05/19: GROOVY-8000: pass interface with adjusted generics to recursive collect

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

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

commit 1c07ed8d904c3fdf93a60dafd619af33d0a47c48
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Thu Aug 20 17:26:24 2020 -0500

    GROOVY-8000: pass interface with adjusted generics to recursive collect
    
    closes #1347
    
    (cherry picked from commit c514ae0c6889e5edd127f7a890cbbec07f1517c4)
---
 .../codehaus/groovy/transform/trait/Traits.java    | 21 +++++++---------
 .../traitx/TraitASTTransformationTest.groovy       | 29 ++++++++++++++++++++++
 2 files changed, 38 insertions(+), 12 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/transform/trait/Traits.java b/src/main/java/org/codehaus/groovy/transform/trait/Traits.java
index b752ebb..e051ad2 100644
--- a/src/main/java/org/codehaus/groovy/transform/trait/Traits.java
+++ b/src/main/java/org/codehaus/groovy/transform/trait/Traits.java
@@ -284,15 +284,12 @@ public abstract class Traits {
      * @param cNode a class node
      * @param interfaces ordered set of interfaces
      */
-    public static LinkedHashSet<ClassNode> collectAllInterfacesReverseOrder(ClassNode cNode, LinkedHashSet<ClassNode> interfaces) {
-        if (cNode.isInterface())
-            interfaces.add(cNode);
-
+    public static LinkedHashSet<ClassNode> collectAllInterfacesReverseOrder(final ClassNode cNode, final LinkedHashSet<ClassNode> interfaces) {
+        if (cNode.isInterface()) interfaces.add(cNode);
         ClassNode[] directInterfaces = cNode.getInterfaces();
-        for (int i = directInterfaces.length-1; i >=0 ; i--) {
-            final ClassNode anInterface = directInterfaces[i];
-            interfaces.add(GenericsUtils.parameterizeType(cNode,anInterface));
-            collectAllInterfacesReverseOrder(anInterface, interfaces);
+        for (int i = directInterfaces.length - 1; i >= 0; i -= 1) {
+            ClassNode iNode = GenericsUtils.parameterizeType(cNode, directInterfaces[i]);
+            if (interfaces.add(iNode)) collectAllInterfacesReverseOrder(iNode, interfaces);
         }
         return interfaces;
     }
@@ -363,15 +360,15 @@ public abstract class Traits {
     }
 
     /**
-     * Find all traits associated with the given classnode
+     * Find all traits associated with the given type.
      *
      * @param cNode the given classnode
      * @return the list of ordered trait classnodes
      */
-    public static List<ClassNode> findTraits(ClassNode cNode) {
-        LinkedHashSet<ClassNode> interfaces = new LinkedHashSet<ClassNode>();
+    public static List<ClassNode> findTraits(final ClassNode cNode) {
+        LinkedHashSet<ClassNode> interfaces = new LinkedHashSet<>();
         collectAllInterfacesReverseOrder(cNode, interfaces);
-        List<ClassNode> traits = new LinkedList<ClassNode>();
+        List<ClassNode> traits = new LinkedList<>();
         for (ClassNode candidate : interfaces) {
             if (isAnnotatedWithTrait(candidate)) {
                 traits.add(candidate);
diff --git a/src/test/org/codehaus/groovy/transform/traitx/TraitASTTransformationTest.groovy b/src/test/org/codehaus/groovy/transform/traitx/TraitASTTransformationTest.groovy
index e785703..9c1c7c9 100644
--- a/src/test/org/codehaus/groovy/transform/traitx/TraitASTTransformationTest.groovy
+++ b/src/test/org/codehaus/groovy/transform/traitx/TraitASTTransformationTest.groovy
@@ -3206,6 +3206,35 @@ final class TraitASTTransformationTest {
         '''
     }
 
+    @Test // GROOVY-8000
+    void testTraitMultiLevelGenerics() {
+        assertScript '''
+            trait TopTrait<X> { X getSomeThing() {}
+            }
+            trait MiddleTrait<Y> implements TopTrait<Y> {
+            }
+            trait BottomTrait<Z> implements MiddleTrait<Z> {
+            }
+            class Implementation implements BottomTrait<String> {
+            }
+
+            assert new Implementation().getSomeThing() == null
+        '''
+
+        assertScript '''
+            trait TopTrait<T> { T getSomeThing() {}
+            }
+            trait MiddleTrait<T> implements TopTrait<T> {
+            }
+            trait BottomTrait<T> implements MiddleTrait<T> {
+            }
+            class Implementation implements BottomTrait<String> {
+            }
+
+            assert new Implementation().getSomeThing() == null
+        '''
+    }
+
     @Test // GROOVY-9660
     void testAsGenericsParam() {
         assertScript '''