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

[groovy] branch master updated: GROOVY-9760: parameterizeType for raw type should be T not T (closes #1381)
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/master by this push:
     new 1eaea44  GROOVY-9760: parameterizeType for raw type should be T<Object> not T<X> (closes #1381)
1eaea44 is described below

commit 1eaea44384a5b06012c34d5e341c4ba5a0f4f959
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> (closes #1381)
    
    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 '''