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:37:44 UTC

[groovy] branch GROOVY_2_5_X updated: GROOVY-9760: parameterizeType for raw type should be T not T (port to 2_5_X)
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 4e80157  GROOVY-9760: parameterizeType for raw type should be T<Object> not T<X> (port to 2_5_X)
4e80157 is described below

commit 4e801574fb90956adfe4b75a437339dd2b499e12
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> (port to 2_5_X)
    
    class C implements T {
    //Object get() {}
    }
    trait T<X> {
      X get() {}
    }
---
 .../codehaus/groovy/ast/tools/GenericsUtils.java   | 20 +++++++++++++---
 .../traitx/TraitASTTransformationTest.groovy       | 27 ++++++++++++++++++++++
 2 files changed, 44 insertions(+), 3 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 6abc821..8d27412 100644
--- a/src/main/java/org/codehaus/groovy/ast/tools/GenericsUtils.java
+++ b/src/main/java/org/codehaus/groovy/ast/tools/GenericsUtils.java
@@ -531,11 +531,25 @@ public class GenericsUtils {
 
     private static void extractSuperClassGenerics(GenericsType[] usage, GenericsType[] declaration, 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 2737285..a0da8e7 100644
--- a/src/test/org/codehaus/groovy/transform/traitx/TraitASTTransformationTest.groovy
+++ b/src/test/org/codehaus/groovy/transform/traitx/TraitASTTransformationTest.groovy
@@ -528,6 +528,33 @@ assert MyEnum.X.bar == 123 && MyEnum.Y.bar == 0
         '''
     }
 
+    // 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'
+        '''
+    }
+
     void testTraitWithGenericProperty() {
         assertScript '''
             trait PropertyProvider<T> {