diff --git a/src/main/java/org/codehaus/groovy/runtime/metaclass/ClosureMetaClass.java b/src/main/java/org/codehaus/groovy/runtime/metaclass/ClosureMetaClass.java index ca7d105187c..5919ca071b1 100644 --- a/src/main/java/org/codehaus/groovy/runtime/metaclass/ClosureMetaClass.java +++ b/src/main/java/org/codehaus/groovy/runtime/metaclass/ClosureMetaClass.java @@ -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 matchingMethods = new ArrayList<>(methodCount); diff --git a/src/test/groovy/gls/invocation/MethodSelectionTest.groovy b/src/test/groovy/gls/invocation/MethodSelectionTest.groovy index eea754d848f..44e0a016906 100644 --- a/src/test/groovy/gls/invocation/MethodSelectionTest.groovy +++ b/src/test/groovy/gls/invocation/MethodSelectionTest.groovy @@ -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 '''