From af29aedf8fe7c166b28a69dcb157a69c02a820de Mon Sep 17 00:00:00 2001 From: Eric Milles Date: Wed, 4 Sep 2024 09:14:56 -0500 Subject: [PATCH] GROOVY-11467: SC: super interface method reference from an abstract type --- ...cTypesMethodReferenceExpressionWriter.java | 11 +++++++--- .../transform/stc/MethodReferenceTest.groovy | 20 +++++++++++++++++++ 2 files changed, 28 insertions(+), 3 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 2cb4b2a88ee..122c4a7af13 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 @@ -45,6 +45,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Set; import static java.util.Comparator.comparingInt; import static java.util.stream.Collectors.joining; @@ -308,10 +309,14 @@ private MethodNode chooseMethodRefMethod(final List methods, final E private List findVisibleMethods(final String name, final ClassNode type) { List methods = type.getMethods(name); - // GROOVY-10791: include interface default methods in search - for (ClassNode cn : getInterfacesAndSuperInterfaces(type)) { + // GROOVY-10791, GROOVY-11467: include non-static interface methods + Set implemented = getInterfacesAndSuperInterfaces(type); + implemented.remove(type); + for (ClassNode cn : implemented) { for (MethodNode mn : cn.getDeclaredMethods(name)) { - if (mn.isDefault()) methods.add(mn); + if (mn.isDefault() || (mn.isPublic() && !mn.isStatic() && type.isAbstract())) { + methods.add(mn); + } } } methods.addAll(findDGMMethodsForClassNode(controller.getSourceUnit().getClassLoader(), type, name)); diff --git a/src/test/groovy/transform/stc/MethodReferenceTest.groovy b/src/test/groovy/transform/stc/MethodReferenceTest.groovy index f9c8735187a..b99bee86186 100644 --- a/src/test/groovy/transform/stc/MethodReferenceTest.groovy +++ b/src/test/groovy/transform/stc/MethodReferenceTest.groovy @@ -1531,4 +1531,24 @@ final class MethodReferenceTest { assert result == 'something' ''' } + + @Test // GROOVY-11467 + void testSuperInterfaceMethodReference() { + assertScript imports + ''' + interface A { int m() } + interface B extends A { } + class C implements B { int m() { 42 } } + + @CompileStatic + class D { + B b = new C() + void test() { + IntSupplier s = b::m + assert s.getAsInt() == 42 + } + } + + new D().test() + ''' + } }