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/06/25 09:48:44 UTC

[groovy] 02/02: GROOVY-9606: Traits using generics generate incorrect stub for Methods (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

commit e7ab52ee9ca9a38ae2f13289f02c412b0b386d98
Author: Paul King <pa...@asert.com.au>
AuthorDate: Wed Jun 24 22:25:47 2020 +1000

    GROOVY-9606: Traits using generics generate incorrect stub for Methods (port to 2_5_X)
---
 .../codehaus/groovy/ast/tools/GenericsUtils.java   |  2 +
 .../groovy/tools/javac/JavaStubGenerator.java      | 35 +++++-----
 src/test/groovy/bugs/Groovy9606.groovy             | 74 ++++++++++++++++++++++
 3 files changed, 95 insertions(+), 16 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 38049ae..6abc821 100644
--- a/src/main/java/org/codehaus/groovy/ast/tools/GenericsUtils.java
+++ b/src/main/java/org/codehaus/groovy/ast/tools/GenericsUtils.java
@@ -324,6 +324,8 @@ public class GenericsUtils {
     }
 
     public static MethodNode correctToGenericsSpec(Map<String, ClassNode> genericsSpec, MethodNode mn) {
+        if (genericsSpec == null) return mn;
+        if (mn.getGenericsTypes() != null) genericsSpec = addMethodGenerics(mn, genericsSpec);
         ClassNode correctedType = correctToGenericsSpecRecurse(genericsSpec, mn.getReturnType());
         Parameter[] origParameters = mn.getParameters();
         Parameter[] newParameters = new Parameter[origParameters.length];
diff --git a/src/main/java/org/codehaus/groovy/tools/javac/JavaStubGenerator.java b/src/main/java/org/codehaus/groovy/tools/javac/JavaStubGenerator.java
index e38cc64..ba6e1af 100644
--- a/src/main/java/org/codehaus/groovy/tools/javac/JavaStubGenerator.java
+++ b/src/main/java/org/codehaus/groovy/tools/javac/JavaStubGenerator.java
@@ -146,6 +146,20 @@ public class JavaStubGenerator {
         }
     }
 
+    private static Iterable<ClassNode> findTraits(ClassNode node) {
+        Set<ClassNode> traits = new LinkedHashSet<>();
+
+        LinkedList<ClassNode> todo = new LinkedList<>();
+        Collections.addAll(todo, node.getInterfaces());
+        while (!todo.isEmpty()) {
+            ClassNode next = todo.removeLast();
+            if (Traits.isTrait(next)) traits.add(next);
+            Collections.addAll(todo, next.getInterfaces());
+        }
+
+        return traits;
+    }
+
     private void printClassContents(PrintWriter out, ClassNode classNode) throws FileNotFoundException {
         if (classNode instanceof InnerClassNode && ((InnerClassNode) classNode).isAnonymous()) {
             // if it is an anonymous inner class, don't generate the stub code for it.
@@ -171,20 +185,6 @@ public class JavaStubGenerator {
                     }
                 }
 
-                private Iterable<ClassNode> findTraits(ClassNode node) {
-                    Set<ClassNode> traits = new LinkedHashSet<>();
-
-                    LinkedList<ClassNode> todo = new LinkedList<>();
-                    Collections.addAll(todo, node.getInterfaces());
-                    while (!todo.isEmpty()) {
-                        ClassNode next = todo.removeLast();
-                        if (Traits.isTrait(next)) traits.add(next);
-                        Collections.addAll(todo, next.getInterfaces());
-                    }
-
-                    return traits;
-                }
-
                 @Override
                 protected FinalVariableAnalyzer.VariableNotFinalCallback getFinalVariablesCallback() {
                     return null;
@@ -348,9 +348,12 @@ public class JavaStubGenerator {
         }
 
         // print the methods from traits
-        for (ClassNode trait : Traits.findTraits(classNode)) {
+        for (ClassNode trait : findTraits(classNode)) {
+            Map<String, ClassNode> generics = trait.isUsingGenerics() ? createGenericsSpec(trait) : null;
             List<MethodNode> traitMethods = trait.getMethods();
-            for (MethodNode traitMethod : traitMethods) {
+            for (MethodNode traitOrigMethod : traitMethods) {
+                // GROOVY-9606: replace method return type and parameter type placeholder with resolved type from trait generics
+                MethodNode traitMethod = correctToGenericsSpec(generics, traitOrigMethod);
                 MethodNode existingMethod = classNode.getMethod(traitMethod.getName(), traitMethod.getParameters());
                 if (existingMethod != null) continue;
                 for (MethodNode propertyMethod : propertyMethods) {
diff --git a/src/test/groovy/bugs/Groovy9606.groovy b/src/test/groovy/bugs/Groovy9606.groovy
new file mode 100644
index 0000000..b158310
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy9606.groovy
@@ -0,0 +1,74 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package groovy.bugs
+
+import org.codehaus.groovy.control.CompilerConfiguration
+import org.codehaus.groovy.tools.javac.JavaAwareCompilationUnit
+import org.junit.Test
+
+final class Groovy9606 {
+
+    @Test
+    void testGenerics() {
+        def config = new CompilerConfiguration(
+            targetDirectory: File.createTempDir(),
+            jointCompilationOptions: [
+                    stubDir: File.createTempDir(),
+                    keepStubs: true   // keep stubs to read the stubs later in this test
+            ]
+        )
+
+        def parentDir = File.createTempDir()
+        try {
+            def a = new File(parentDir, 'SomeGroovyTrait.groovy')
+            a.write '''
+                trait SomeGroovyTrait<D> {
+                    List<D> list(List<D> front, D last) {
+                        front + [last]
+                    }
+                }
+            '''
+            def b = new File(parentDir, 'SomeGroovyClass.groovy')
+            b.write '''
+                class SomeGroovyClass implements SomeGroovyTrait<String> { }
+            '''
+            def c = new File(parentDir, 'SomeJavaClass.java')
+            c.write '''
+                public class SomeJavaClass {
+                    SomeGroovyClass sgc;
+                }
+            '''
+
+            def loader = new GroovyClassLoader(this.class.classLoader)
+            def cu = new JavaAwareCompilationUnit(config, loader)
+            cu.addSources(a, b, c)
+            cu.compile()
+
+            def stub = new File(config.jointCompilationOptions.stubDir, 'SomeGroovyClass.java')
+            def text = stub.text
+
+            assert text.contains('java.util.List<java.lang.String> list')
+            assert text.contains('java.util.List<java.lang.String> front, java.lang.String last')
+        } finally {
+            parentDir.deleteDir()
+            config.targetDirectory.deleteDir()
+            config.jointCompilationOptions.stubDir.deleteDir()
+        }
+    }
+}