Java: the relation of a cyclic generic type does not allow to distinguish from a supertype (javac error)

I meet absolutely strange behavior of the Java compiler.
I cannot distinguish between a supertype and a subtype when a cyclic general type is a relation .

JUnit test to reproduce the problem:

public class _SupertypeGenericTest {

    interface ISpace<S extends ISpace<S, A>, A extends IAtom<S, A>> {
    }

    interface IAtom<S extends ISpace<S, A>, A extends IAtom<S, A>> {
    }

    static class Space
            implements ISpace<Space, Atom> {
    }

    static class Atom
            implements IAtom<Space, Atom> {
    }

    public void test() {
        ISpace<?, ?> spaceSupertype = new Space();
        IAtom<?, ?> atomSupertype = new Atom();

        Space space = (Space) spaceSupertype;  // cast error
        Atom atom = (Atom) atomSupertype;  // cast error
    }
}

Compiler Error Output:

_SupertypeGenericTest.java:33: inconvertible types
found   : pinetag.data._SupertypeGenericTest.ISpace<capture#341 of ?,capture#820 of ?>
required: pinetag.data._SupertypeGenericTest.Space
                Space space = (Space) spaceSupertype;
                                    ^

_SupertypeGenericTest.java:34: inconvertible types
found   : pinetag.data._SupertypeGenericTest.IAtom<capture#94 of ?,capture#48 of ?>
required: pinetag.data._SupertypeGenericTest.Atom
                Atom atom = (Atom) atomSupertype;
                                ^
2 errors

Note. I use the latest version of Netbeans, bundled Ant, the latest version of Java 6.
I tried to use Ant from the command line (Netbeans generates a build.xml file), but this leads to the same errors.

What's wrong?
Is there an elegant way to solve the problem?

The strange thing: Netbeans does not put errors (even warnings) in the given code.

EDIT:
, !
Eclipse 3.4.1 , , !!!
? , Ant build.xml, Netbeans ', .
- ?

2:
JDK7 JDK7, netbeans /!
( 1.7.0-ea-b55)

3:
, , javac.

+5
4

, , , javac ecj ( eclipse), Sun Eclipse ( , , URL-, Sun , ).

,

  • , ( , )
  • .

spec, , .

:

javac (javac 1.6.0_13) ecj (Eclipse Java Compiler 0.894_R34x, 3.4.2 release) javac - .class, ecj () .class.

+2

-generics:

    @Test
    public void test() {
            ISpace spaceSupertype = new Space();
            IAtom atomSupertype = new Atom();

            Space space = (Space) spaceSupertype;  // ok
            Atom atom = (Atom) atomSupertype;  // ok
    }
0

?

public void test() {
    ISpace<Space, Atom> spaceSupertype = new Space();
    IAtom<Space, Atom> atomSupertype = new Atom();

    Space space = (Space) spaceSupertype;  // no error
    Atom atom = (Atom) atomSupertype;  // no error
}

, :) , " "

0

, IAtom<?, ?> Atom ( Atom<Atom, Space>). , , , ? Atom Space ?

, , , ,

ISpace spaceSupertype = new Space();

( ), (, , ).

. , , ?, . .

Space Atom, , , . , , , , , if/then- (, ).

, , , . , . , , .

: " ? ?" , . ( , , .) , IAtom ISpace - , test() . test() ISpace/IAtom. , , , test(), IAtom/ISpace, :

public interface IAtom2 extends IAtom
{
    [additional methods]
}

IAtom2 IAtom test(). . , ( ), . , , , , , .

, , instanceof:

if (spaceSupertype instanceof Space)
{
    Space space = (Space)spaceSupertype;
    ...
}
else
...
0

All Articles