Note that there are some explanatory texts on larger screens.

plurals
  1. POJava: Cyclic generic type relation doesn't allow cast from supertype (javac bug)
    text
    copied!<p>I encounter a totally strange behavior of the Java compiler.<br> I can't cast a supertype to a subtype when <strong>cyclic generic type relation</strong> is involved.</p> <p><strong>JUnit test case</strong> to reproduce the problem:</p> <pre><code>public class _SupertypeGenericTest { interface ISpace&lt;S extends ISpace&lt;S, A&gt;, A extends IAtom&lt;S, A&gt;&gt; { } interface IAtom&lt;S extends ISpace&lt;S, A&gt;, A extends IAtom&lt;S, A&gt;&gt; { } static class Space implements ISpace&lt;Space, Atom&gt; { } static class Atom implements IAtom&lt;Space, Atom&gt; { } public void test() { ISpace&lt;?, ?&gt; spaceSupertype = new Space(); IAtom&lt;?, ?&gt; atomSupertype = new Atom(); Space space = (Space) spaceSupertype; // cast error Atom atom = (Atom) atomSupertype; // cast error } } </code></pre> <p><strong>Compiler error output:</strong></p> <pre><code>_SupertypeGenericTest.java:33: inconvertible types found : pinetag.data._SupertypeGenericTest.ISpace&lt;capture#341 of ?,capture#820 of ?&gt; required: pinetag.data._SupertypeGenericTest.Space Space space = (Space) spaceSupertype; ^ _SupertypeGenericTest.java:34: inconvertible types found : pinetag.data._SupertypeGenericTest.IAtom&lt;capture#94 of ?,capture#48 of ?&gt; required: pinetag.data._SupertypeGenericTest.Atom Atom atom = (Atom) atomSupertype; ^ 2 errors </code></pre> <p>Note: I'm using Netbeans latest trunk, bundled Ant, latest Java 6 release.<br> I tried using Ant from command line (Netbeans generates a build.xml file) but it results in same errors.</p> <p><strong>What is wrong?</strong><br> Is there an elegant way solve the problem?</p> <p>The strange thing is: Netbeans doesn't mark errors (not even warnings) in given code.</p> <p><strong>EDIT:</strong><br> No, now I understand <strong>nothing</strong>!<br> Eclipse 3.4.1 doesn't mark neither warnings nor errors, and compiles the code without trouble!!!<br> How can this be? I thought, using Ant from command line along with build.xml provided by Netbeans' would be neutral.<br> <strong>Am I missing something?</strong></p> <p><strong>EDIT 2:</strong><br> Using <strong>JDK7</strong> library and JDK7 code format, netbeans compiles without errors/warnings!<br> <strong>(I'm using 1.7.0-ea-b55)</strong></p> <p><strong>EDIT 3:</strong><br> Changed title to indicate that we're dealing with a javac bug.</p>
 

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