[
https://issues.apache.org/jira/browse/GROOVY-8660?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17363712#comment-17363712
]
Eric Milles commented on GROOVY-8660:
-------------------------------------
The implicit null selection only works when there is a single method with one
non-primitive parameter. It is the last case in
{{org.codehaus.groovy.reflection.ParameterTypes#isValidMethod}}:
{code:java}
public boolean isValidMethod(Class[] arguments) {
if (arguments == null) return true;
final int size = arguments.length;
CachedClass[] pt = getParameterTypes();
final int paramMinus1 = pt.length - 1;
if (isVargsMethod && size >= paramMinus1)
return isValidVarargsMethod(arguments, size, pt, paramMinus1);
else if (pt.length == size)
return isValidExactMethod(arguments, pt);
else if (pt.length == 1 && size == 0 && !pt[0].isPrimitive) // here
return true;
return false;
}
{code}
When there are multiple methods available and no arguments, selection goes
through {{MetaClassHelper.chooseEmptyMethodParams}} in
{{groovy.lang.MetaClassImpl#chooseMethodInternal}}, which just tries to execute
a no-arg method instead of a varargs method if both are declared.
> Unexpected MethodSelectionException with implicit null argument
> ---------------------------------------------------------------
>
> Key: GROOVY-8660
> URL: https://issues.apache.org/jira/browse/GROOVY-8660
> Project: Groovy
> Issue Type: Bug
> Affects Versions: 3.0.0-alpha-2, 2.4.15, 2.5.0
> Reporter: Daniil Ovchinnikov
> Priority: Major
>
> {code:groovy}
> class OnlySingle {
> def foo(a) { "single param: $a" }
> }
> println new OnlySingle().foo()
> // as expected: 'single param: null'
> class OnlyVararg {
> def foo(a, ... b) { "vararg param: $a, $b" }
> }
> println new OnlyVararg().foo()
> // as expected: 'MME: No signature of method: OnlyVararg.foo() is applicable
> for argument types: () values: []'
> class Both {
> def foo(a) { "single param: $a" }
> def foo(a, ... b) { "vararg param: $a, $b" }
> }
> println new Both().foo()
> // unexpected:
> // MethodSelectionException: Could not find which method foo() to invoke from
> this list:
> // public java.lang.Object Both#foo(java.lang.Object)
> // public transient java.lang.Object Both#foo(java.lang.Object,
> [Ljava.lang.Object;)
> {code}
> If the exception is expected then {{OnlyVararg}} case should work too.
> If the exception is unexpected then {{Both#foo(Object)}} should be selected.
--
This message was sent by Atlassian Jira
(v8.3.4#803005)