Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mutually self-referencing type parameters compiling under JDK6 but not 7?

The following code compiles using JDK6 (I tried 1.6.0_24)

class XY<A extends XY<A, B>, B extends XY<B, A>> { } 

But compiling under JDK7 (e.g. 1.7.0), I get this error:

XY.java:1: error: type argument B is not within bounds of type-variable A
class XY<A extends XY<A, B>, B extends XY<B, A>> {
                                      ^
  where B,A are type-variables:
    B extends XY<B,A> declared in class XY
    A extends XY<A,B> declared in class XY
1 error

Can anyone point as to whether this was an intentional change to Java's generics?

like image 926
oxbow_lakes Avatar asked Apr 02 '12 13:04

oxbow_lakes


1 Answers

It seems that's a bug that comes from OpenJDK and is caused by a bug in type-variable substitution.

It appears when you switch generic variables for examples:

class XY<A extends XY<A, B>, B extends XY<B, A>> { } 
class XY<A extends XY<B, A>, B extends XY<A, B>> { } 

It doesn't appear in this:

class XY<A extends XY<A, B>, B extends XY<A, B>> { } 
class XY<A extends XY<B, A>, B extends XY<B, A>> { } 

you can see another example here: http://old.nabble.com/Apparent-generics-compilation-bug-new-to-Java-1.7-td33378164.html

like image 111
Luca Avatar answered Oct 13 '22 00:10

Luca