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 15:08:10 UTC

[groovy] branch GROOVY_3_0_X 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 GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/GROOVY_3_0_X by this push:
     new 3435983891 GROOVY-10791: SC: support `NotTheInterface::defaultMethod`
3435983891 is described below

commit 343598389175f015759179ff1cf38eadcda1f8f3
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    | 13 +++++++++++++
 2 files changed, 21 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 b9382a9027..6894b5cccb 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
@@ -52,6 +52,7 @@ import static org.codehaus.groovy.ast.tools.GeneralUtils.block;
 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.GenericsUtils.extractPlaceholders;
@@ -281,9 +282,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 57527ef31c..006efbebaf 100644
--- a/src/test/groovy/transform/stc/MethodReferenceTest.groovy
+++ b/src/test/groovy/transform/stc/MethodReferenceTest.groovy
@@ -224,6 +224,19 @@ final class MethodReferenceTest extends GroovyTestCase {
         '''
     }
 
+    // class::instanceMethod -- GROOVY-10791
+    void testBiConsumerCI() {
+        assertScript '''
+            import java.util.function.*
+            @groovy.transform.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' }
+        '''
+    }
+
     // class::instanceMethod
     void testBinaryOperatorCI() {
         assertScript '''