Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,6 @@ private static class NormalMethodChooser implements MethodChooser {
public Object chooseMethod(final Class<?>[] arguments, final boolean coerce) {
if (arguments.length == 0) {
return MetaClassHelper.chooseEmptyMethodParams(methods);
} else if (arguments.length == 1 && arguments[0] == null) {
return MetaClassHelper.chooseMostGeneralMethodWith1NullParam(methods);
} else {
int methodCount = methods.size();
List<Object> matchingMethods = new ArrayList<>(methodCount);
Expand Down
27 changes: 27 additions & 0 deletions src/test/groovy/gls/invocation/MethodSelectionTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,33 @@ final class MethodSelectionTest extends CompilableTestSupport {
'''
}

@Test // GROOVY-6289: closure doCall with null selects most specific applicable method
void testClosureCallWithNullSelectsMethod() {
assertScript '''
class MyClosure extends Closure {
MyClosure(owner) { super(owner) }
def doCall(String s) { 'string' }
def doCall(Integer i) { 'integer' }
}
// String is more specific than Integer for null (both reference types)
assert new MyClosure(this)(null) == 'string'
'''
}

@Test // GROOVY-6289: closure doCall with null and unrelated types should be ambiguous
void testClosureCallWithNullAmbiguous() {
shouldFail '''
class A {}
class B {}
class MyClosure extends Closure {
MyClosure(owner) { super(owner) }
def doCall(A a) { 'a' }
def doCall(B b) { 'b' }
}
new MyClosure(this)(null)
'''
}

@Test
void testMethodSelectionException() {
assertScript '''
Expand Down
Loading