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 :
Linux 2.4.22-1.2199.nptl #1 Wed Aug 4 12:25:07 EDT 2004 i686 athlon i386 GNU/Linux
Microsoft Windows XP [Version 5.1.2600]
A DESCRIPTION OF THE PROBLEM :
As mentioned on the forums:
http://forum.java.sun.com/thread.jsp?forum=316&thread=563260
The type-safety provided by compile-time warnings can be circumvented through the use of a parameterised method, where the parameter is bound to a raw type.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile the provided code, note the lack of any warnings. A String is added to a Collection<Integer>, resulting in a ClassCastException at runtime - exactly the sort of behaviour that the compiler is supposed to catch.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Warnings will always be emitted when type-safety is compromised when no explicit casting is done.
ACTUAL -
There are no warnings for the type-unsafe behaviour described in this report.
ERROR MESSAGES/STACK TRACES THAT OCCUR :
Paradoxically, if there were any, this wouldn't be an error.
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.util.*;
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 :
Don't use methods with parameters bound to raw types, or don't trust that you will be warned about type-unsafe behaviour if you do.
###@###.### 10/21/04 17:15 GMT