FULL PRODUCT VERSION :
java version "1.5.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-b64)
Java HotSpot(TM) Client VM (build 1.5.0-b64, mixed mode, sharing)
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [version 5.1.2600]
A DESCRIPTION OF THE PROBLEM :
The JLS3 permits to declare a type variable with a raw type as its bound but don't report it as an unsafe operation.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile the class TypeUnsafe with javac. It don't produce unsafe warning.
run it, it fails on a ClassCastException.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
javac should raise an unsafe operation because T use a raw type (Collection) as bound.
ACTUAL -
ClassCastException at the last line
ERROR MESSAGES/STACK TRACES THAT OCCUR :
Exception in thread "main" java.lang.ClassCastException: java.lang.String
at TypeUnsafe.main(TypeUnsafe.java:26)
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
public class TypeUnsafe {
static public <T extends Collection> void workaround(T a, T b) {
a.addAll(b);
}
static public void main(String[] args) {
Collection<Integer> a = new ArrayList<Integer>();
Collection<String> b = new ArrayList<String>();
b.add("Bwahahaha");
workaround(a, b);
System.out.println(a.iterator().next().intValue()); // ClassCastException
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
change the workaround method by this one:
static public <E,T extends Collection<E>> void workaround(T a, T b) {
a.addAll(b);
}
it produces a warning
###@###.### 10/21/04 21:04 GMT