File tree Expand file tree Collapse file tree
main/java/org/codehaus/groovy/runtime/metaclass
test/groovy/gls/invocation Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -145,8 +145,6 @@ private static class NormalMethodChooser implements MethodChooser {
145145 public Object chooseMethod (final Class <?>[] arguments , final boolean coerce ) {
146146 if (arguments .length == 0 ) {
147147 return MetaClassHelper .chooseEmptyMethodParams (methods );
148- } else if (arguments .length == 1 && arguments [0 ] == null ) {
149- return MetaClassHelper .chooseMostGeneralMethodWith1NullParam (methods );
150148 } else {
151149 int methodCount = methods .size ();
152150 List <Object > matchingMethods = new ArrayList <>(methodCount );
Original file line number Diff line number Diff line change @@ -102,6 +102,33 @@ final class MethodSelectionTest extends CompilableTestSupport {
102102 '''
103103 }
104104
105+ @Test // GROOVY-6289: closure doCall with null selects most specific applicable method
106+ void testClosureCallWithNullSelectsMethod () {
107+ assertScript '''
108+ class MyClosure extends Closure {
109+ MyClosure(owner) { super(owner) }
110+ def doCall(String s) { 'string' }
111+ def doCall(Integer i) { 'integer' }
112+ }
113+ // String is more specific than Integer for null (both reference types)
114+ assert new MyClosure(this)(null) == 'string'
115+ '''
116+ }
117+
118+ @Test // GROOVY-6289: closure doCall with null and unrelated types should be ambiguous
119+ void testClosureCallWithNullAmbiguous () {
120+ shouldFail '''
121+ class A {}
122+ class B {}
123+ class MyClosure extends Closure {
124+ MyClosure(owner) { super(owner) }
125+ def doCall(A a) { 'a' }
126+ def doCall(B b) { 'b' }
127+ }
128+ new MyClosure(this)(null)
129+ '''
130+ }
131+
105132 @Test
106133 void testMethodSelectionException () {
107134 assertScript '''
You can’t perform that action at this time.
0 commit comments