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 2022/10/25 14:31:55 UTC

[groovy] branch master updated: GROOVY-10791: SC: support `NotTheInterface::defaultMethod`

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

emilles 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 f579fa304a GROOVY-10791: SC: support `NotTheInterface::defaultMethod`
f579fa304a is described below

commit f579fa304a23461b7d1c86602eb60c600ace36c6
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Tue Oct 25 08:55:16 2022 -0500

    GROOVY-10791: SC: support `NotTheInterface::defaultMethod`
---
 .../asm/sc/StaticTypesMethodReferenceExpressionWriter.java   | 10 ++++++++--
 src/test/groovy/transform/stc/MethodReferenceTest.groovy     | 12 ++++++++++++
 2 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesMethodReferenceExpressionWriter.java b/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesMethodReferenceExpressionWriter.java
index 5e2470a1ab..c3efd7a793 100644
--- a/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesMethodReferenceExpressionWriter.java
+++ b/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesMethodReferenceExpressionWriter.java
@@ -49,6 +49,7 @@ import java.util.stream.Collectors;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.callX;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.classX;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.ctorX;
+import static org.codehaus.groovy.ast.tools.GeneralUtils.getInterfacesAndSuperInterfaces;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.nullX;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.returnS;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.varX;
@@ -354,9 +355,14 @@ public class StaticTypesMethodReferenceExpressionWriter extends MethodReferenceE
 
     private List<MethodNode> findVisibleMethods(final String name, final ClassNode type) {
         List<MethodNode> methods = type.getMethods(name);
+        // GROOVY-10791: include interface default methods in search
+        for (ClassNode cn : getInterfacesAndSuperInterfaces(type)) {
+            for (MethodNode mn : cn.getDeclaredMethods(name)) {
+                if (mn.isDefault()) methods.add(mn);
+            }
+        }
         methods.addAll(findDGMMethodsForClassNode(controller.getSourceUnit().getClassLoader(), type, name));
-        methods = filterMethodsByVisibility(methods, controller.getClassNode());
-        return methods;
+        return filterMethodsByVisibility(methods, controller.getClassNode());
     }
 
     private void addFatalError(final String msg, final ASTNode node) {
diff --git a/src/test/groovy/transform/stc/MethodReferenceTest.groovy b/src/test/groovy/transform/stc/MethodReferenceTest.groovy
index ef9688f559..4c4011ea24 100644
--- a/src/test/groovy/transform/stc/MethodReferenceTest.groovy
+++ b/src/test/groovy/transform/stc/MethodReferenceTest.groovy
@@ -246,6 +246,18 @@ final class MethodReferenceTest {
         '''
     }
 
+    @Test // class::instanceMethod -- GROOVY-10791
+    void testBiConsumerCI() {
+        assertScript shell, '''
+            @CompileStatic
+            def <T> void test(List<T> list, Consumer<? super T> todo) {
+                BiConsumer<List<T>, Consumer<? super T>> binder = List::forEach // default method of Iterator
+                binder.accept(list, todo)
+            }
+            test(['works']) { assert it == 'works' }
+        '''
+    }
+
     @Test // class::instanceMethod
     void testBinaryOperatorCI() {
         assertScript shell, '''