Is Eclipse Juno wrong with this ambiguous method error?
Asked Answered
U

3

11

Today I've been playing around with Eclipse Juno. Coming from Helios it is a great upgrade. Everything is working fine, except one new compile error.

We are using the java.net framework 'Fuse' and we call the following method:

ResourceInjector.get().inject(true, this);

Eclipse tells us:

The method inject(Object[]) is ambiguous for the type ResourceInjector

The following methods collide:

inject(Object... components);
inject(boolean arg0, Object... arg1);

It worked fine (with Java 1.6.0.25) in Eclipse Helios, but now it gives a compile error and doesn't want to run anymore. It seems to us that this is a bug in Eclipse Juno, if we build using Maven is builds fine... Does anybody know a work-around for this?

Utley answered 4/7, 2012 at 6:18 Comment(0)
U
9

This is actually a bug in Java 5 and Java 6. This has been fixed in Java 7, and Eclipse now checks for this 'bug'.

More about the bug here: https://bugs.java.com/bugdatabase/view_bug?bug_id=6886431

What method should be called in the following case?

inject(true);

Both can apply, sure, but the primitive boolean surely is a better match? Not according to the Java Language Specification (JLS):

  • Per 15.12.2.5, neither is more specific than the other (int <: Object is not true)

A work-around (we aren't able to change Fuse itself):

ResourceInjector.get().inject(true, new Object[] {this});

Utley answered 5/7, 2012 at 9:55 Comment(0)
V
2

Related question here: "method is ambiguous for the type" but the types are NOT ambiguous (and the error comes by upgrade from eclipse 3.7.2 to eclipse 4.2), copying my answer:

A bug has been filed and fixed for this problem 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 Juno release build(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
Varicolored answered 13/11, 2012 at 1:0 Comment(2)
"tolerate" should indicate that this was intentional... it was a bug in Java 5 and 6, apparently, that was fixed in Java 7: bugs.sun.com/bugdatabase/view_bug.do?bug_id=6886431 expected behavior: compiler error.Anachronous
@NicholasDiPiazza I have Neon 1.a and this does not work for me either.Javelin
L
0

it is really weird that, if you change the API

inject(boolean arg0, Object... arg1);

to

inject(Boolean arg0, Object... arg1);

then inject(true, this) not ambiguous any more

I thinks the bug fix https://bugs.java.com/bugdatabase/view_bug?bug_id=6199075 is definitely a BAD idea

Laughingstock answered 6/2, 2013 at 10:20 Comment(1)
I noticed this myself. The moral seems to be, don't precede a variable argument parameter with a primitive type. I had this issue when trying to upgrade to Juno and changing method(int, Object[]) to method(Integer, Object[]) works fine. Since the compiler is going to be enforcing this, there seems to be little alternative to getting with the program.Hagerman

© 2022 - 2024 — McMap. All rights reserved.