I have defined:
public static int[] getArray( final int... params ) {
return params;
}
public static <T> T[] getArray( final T... params ) {
return params;
}
And I use this in
getArray( 1, 2 )
and now I get in eclipse 4.2 the compile error:
method is ambiguous for the type
But as you can see this is not ambiguous. What can I do?
This was reported as a bug in eclipse bug 383780.
Here is the documentation of the fix:
https://bugs.eclipse.org/bugs/attachment.cgi?id=218320
Basically, to fix the compiler error, get the latest eclipse release(4.2.1 as of now), add the following line after -vmargs
in eclipse.ini
: (then you might need to restart eclipse and rebuild you projects)
-DtolerateIllegalAmbiguousVarargsInvocation=true
That being said, Samuel is correct: the method invocation is ambiguous. The code example above worked before because there was a bug in JDK before 1.6; and the custom compiler in eclipse successfully mimicked this bug. When developing Juno, they fixed this bug (because the JDK bug was fixed in 1.7) by reporting ambiguous invocation as an error, annoying a lot of people (including me). The fix above asks you to explicitly tell eclipse to "tolerate Illegal Ambiguous Varargs Invocation".
Actually this is ambiguous because Autoboxing in java allows you to call a method that expect an int
with an Integer
and vice versa, so getArray( 1, 2 )
can really be a valid call to any of your methods.
As far as I understand what you are doing, you want to have a utility method to create an Array of whatever. Maybe the simplest thing you can do is rename the method that deals with int
to getIntArray()
. Or just use new int[] {1, 2}
which is very readable if you want an int
array.
You can find this information in the specification of the language at http://docs.oracle.com/javase/specs/jls/se5.0/jls3.pdf (In your case, determining the invoked method will go to step 3 of the process described in section 15.12.2 Compile-Time Step 2: Determine Method Signature, because you use the arity variable, and at step 3, both of the method calls are valid)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With