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 2020/09/26 15:57:35 UTC

[groovy] branch GROOVY-9760 created (now 55333b0)

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

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


      at 55333b0  GROOVY-9760: parameterizeType for raw type should be T<Object> not T<X>

This branch includes the following new commits:

     new 55333b0  GROOVY-9760: parameterizeType for raw type should be T<Object> not T<X>

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-9760: parameterizeType for raw type should be T not T Posted by em...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 55333b0c196d7af828478781ab181ebe02721e66
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Sat Sep 26 10:57:20 2020 -0500

    GROOVY-9760: parameterizeType for raw type should be T<Object> not T<X>
    
    class C implements T {
    //Object get() {}
    }
    trait T<X> {
      X get() {}
    }
---
 .../codehaus/groovy/ast/tools/GenericsUtils.java   | 22 ++++++++++++++----
 .../traitx/TraitASTTransformationTest.groovy       | 27 ++++++++++++++++++++++
 2 files changed, 45 insertions(+), 4 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/ast/tools/GenericsUtils.java b/src/main/java/org/codehaus/groovy/ast/tools/GenericsUtils.java
index c06ef0d..969ba47 100644
--- a/src/main/java/org/codehaus/groovy/ast/tools/GenericsUtils.java
+++ b/src/main/java/org/codehaus/groovy/ast/tools/GenericsUtils.java
@@ -535,13 +535,27 @@ public class GenericsUtils {
         return superClass;
     }
 
-    private static void extractSuperClassGenerics(GenericsType[] usage, GenericsType[] declaration, Map<String, ClassNode> spec) {
+    private static void extractSuperClassGenerics(final GenericsType[] usage, final GenericsType[] declaration, final Map<String, ClassNode> spec) {
         // if declaration does not provide generics, there is no connection to make
-        if (usage == null || declaration == null || declaration.length == 0) return;
+        if (declaration == null || declaration.length == 0) return;
+
+        // if usage is a raw type, remove type parameters from spec
+        if (usage == null) {
+            for (GenericsType dt : declaration) {
+                String name = dt.getName();
+                ClassNode type = spec.get(name);
+                if (type != null && type.isGenericsPlaceHolder()
+                        && type.getUnresolvedName().equals(name)) {
+                    type = type.asGenericsType().getUpperBounds()[0];
+                    spec.put(name, type);
+                }
+            }
+            return;
+        }
+
         if (usage.length != declaration.length) return;
 
-        // both have generics
-        for (int i = 0; i < usage.length; i++) {
+        for (int i = 0, n = usage.length; i < n; i += 1) {
             GenericsType ui = usage[i];
             GenericsType di = declaration[i];
             if (di.isPlaceholder()) {
diff --git a/src/test/org/codehaus/groovy/transform/traitx/TraitASTTransformationTest.groovy b/src/test/org/codehaus/groovy/transform/traitx/TraitASTTransformationTest.groovy
index 7a359f4..ef65c22 100644
--- a/src/test/org/codehaus/groovy/transform/traitx/TraitASTTransformationTest.groovy
+++ b/src/test/org/codehaus/groovy/transform/traitx/TraitASTTransformationTest.groovy
@@ -553,6 +553,33 @@ final class TraitASTTransformationTest {
         '''
     }
 
+    @Test // GROOVY-9760
+    void testTraitWithGenerics3() {
+        assertScript '''
+            trait Provider<T> {
+                T get(T ref) {
+                    ref
+                }
+            }
+            class UnspecifiedProvider implements Provider {
+            }
+            assert new UnspecifiedProvider().get('foo') == 'foo'
+        '''
+
+        assertScript '''
+            @groovy.transform.CompileStatic
+            trait Provider<T> {
+                T get(T ref) {
+                    ref
+                }
+            }
+            @groovy.transform.CompileStatic
+            class UnspecifiedProvider implements Provider {
+            }
+            assert new UnspecifiedProvider().get('foo') == 'foo'
+        '''
+    }
+
     @Test
     void testTraitWithGenericProperty() {
         assertScript '''