Skip to content

Commit

Permalink
GROOVY-11467: SC: super interface method reference from an abstract type
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Sep 4, 2024
1 parent 4695a35 commit af29aed
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -308,10 +309,14 @@ private MethodNode chooseMethodRefMethod(final List<MethodNode> methods, final E

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)) {
// GROOVY-10791, GROOVY-11467: include non-static interface methods
Set<ClassNode> 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));
Expand Down
20 changes: 20 additions & 0 deletions src/test/groovy/transform/stc/MethodReferenceTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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()
'''
}
}

0 comments on commit af29aed

Please sign in to comment.