Note that there are some explanatory texts on larger screens.

plurals
  1. POReference is ambiguous with generics
    text
    copied!<p>I'm having quite a tricky case here with generics and method overloading. Check out this example class:</p> <pre><code>public class Test { public &lt;T&gt; void setValue(Parameter&lt;T&gt; parameter, T value) { } public &lt;T&gt; void setValue(Parameter&lt;T&gt; parameter, Field&lt;T&gt; value) { } public void test() { // This works perfectly. &lt;T&gt; is bound to String // ambiguity between setValue(.., String) and setValue(.., Field) // is impossible as String and Field are incompatible Parameter&lt;String&gt; p1 = getP1(); Field&lt;String&gt; f1 = getF1(); setValue(p1, f1); // This causes issues. &lt;T&gt; is bound to Object // ambiguity between setValue(.., Object) and setValue(.., Field) // is possible as Object and Field are compatible Parameter&lt;Object&gt; p2 = getP2(); Field&lt;Object&gt; f2 = getF2(); setValue(p2, f2); } private Parameter&lt;String&gt; getP1() {...} private Parameter&lt;Object&gt; getP2() {...} private Field&lt;String&gt; getF1() {...} private Field&lt;Object&gt; getF2() {...} } </code></pre> <p>The above example compiles perfectly in Eclipse (Java 1.6), but not with the Ant javac command (or with the JDK's javac command), where I get this sort of error message on the second invocation of <code>setValue</code>:</p> <blockquote> <p>reference to setValue is ambiguous, both method setValue(org.jooq.Parameter,T) in Test and method setValue(org.jooq.Parameter,org.jooq.Field) in Test match</p> </blockquote> <p>According to the specification and to my understanding of how the Java compiler works, the most specific method should always be chosen: <a href="http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#20448" rel="noreferrer">http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#20448</a></p> <p>In any case, even if <code>&lt;T&gt;</code> is bound to <code>Object</code>, which makes both <code>setValue</code> methods acceptable candidates for invocation, the one with the <code>Field</code> parameter always seems to be more specific. And it works in Eclipse, just not with the JDK's compiler.</p> <p><strong>UPDATE</strong>:</p> <p>Like this, it would work both in Eclipse and with the JDK compiler (with rawtypes warnings, of course). I understand, that the rules specified in <a href="http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.12.2" rel="noreferrer">the specs</a> are quite special, when generics are involved. But I find this rather confusing:</p> <pre><code> public &lt;T&gt; void setValue(Parameter&lt;T&gt; parameter, Object value) { } // Here, it's easy to see that this method is more specific public &lt;T&gt; void setValue(Parameter&lt;T&gt; parameter, Field value) { } </code></pre> <p><strong>UPDATE 2</strong>:</p> <p>Even with generics, I can create this workaround where I avoid the type <code>&lt;T&gt;</code> being bound to <code>Object</code> at <code>setValue</code> invocation time, by adding an additional, unambiguous indirection called <code>setValue0</code>. This makes me think that the binding of <code>T</code> to <code>Object</code> is really what's causing all the trouble here:</p> <pre><code> public &lt;T&gt; void setValue(Parameter&lt;T&gt; parameter, T value) { } public &lt;T&gt; void setValue(Parameter&lt;T&gt; parameter, Field&lt;T&gt; value) { } public &lt;T&gt; void setValue0(Parameter&lt;T&gt; parameter, Field&lt;T&gt; value) { // This call wasn't ambiguous in Java 7 // It is now ambiguous in Java 8! setValue(parameter, value); } public void test() { Parameter&lt;Object&gt; p2 = p2(); Field&lt;Object&gt; f2 = f2(); setValue0(p2, f2); } </code></pre> <p>Am I misunderstanding something here? Is there a known compiler bug related to this? Or is there a workaround/compiler setting to help me?</p> <h2>Follow-Up:</h2> <p>For those interested, I have filed a bug report both to Oracle and Eclipse. Oracle has accepted the bug, so far, Eclipse has analysed it and rejected it! It looks as though my intuition is right and this is a bug in <code>javac</code></p> <ul> <li><a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7031404" rel="noreferrer">http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7031404</a></li> <li><a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=340506" rel="noreferrer">https://bugs.eclipse.org/bugs/show_bug.cgi?id=340506</a></li> <li><a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=469014" rel="noreferrer">https://bugs.eclipse.org/bugs/show_bug.cgi?id=469014</a> (a new issue in Eclipse Mars)</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload