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 ee764ab commit 77276ef
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 @@ -46,6 +46,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 @@ -399,10 +400,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 @@ -1494,6 +1494,26 @@ final class MethodReferenceTest {
'''
}

@Test // GROOVY-11467
void testSuperInterfaceMethodReference() {
assertScript shell, '''
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()
'''
}

@Test // GROOVY-10635
void testRecordComponentMethodReference() {
assertScript shell, '''
Expand Down

0 comments on commit 77276ef

Please sign in to comment.