FULL PRODUCT VERSION :
java version "1.5.0_07"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_07-b03)
Java HotSpot(TM) Client VM (build 1.5.0_07-b03, mixed mode, sharing)
A DESCRIPTION OF THE PROBLEM :
In the attached code the capture of ? extends Box<?> is not treated right, allowing you to put a Box<B> in a Box<Box<A>>.
The code compiles without warnings, has no casts, but throws a ClassCastException.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile (and run) the code
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
An error from the compiler
ACTUAL -
Incorrect code compiles, and throws a ClassCastException
ERROR MESSAGES/STACK TRACES THAT OCCUR :
Exception in thread "main" java.lang.ClassCastException: B
at CaptureBug6.main(CaptureBug6.java:9)
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
// this code compiles without warnings, has no casts,
// but throws a ClassCastException
public class CaptureBug6 {
public static void main(String[] args) {
Box<Box<A,A >,Box<A,A>> a = new Box<Box<A,A>,Box<A,A>>(new Box<A,A>(new
A()));
Box<?, ?> b = a;
b.value.same = new Box<B,B>(new B());
A c = a.value.same.value;
}
}
class Box<X extends Box<?,?>, T extends X> {
Box (T v) {
value = v;
}
Box () {}
T value;
Box<X, T> same;
}
class A extends Box<A,A> {}
class B extends Box<B,B> {}
---------- END SOURCE ----------