JDK-6182859 : the JLS3 must not permits to use raw type as bound of type variable
  • Type: Bug
  • Component: specification
  • Sub-Component: language
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2004-10-21
  • Updated: 2004-10-21
  • Resolved: 2004-10-21
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
5.0Resolved
Related Reports
Duplicate :  
Description
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

Comments
EVALUATION This appears to be a plain old compiler bug, not a specification problem. ###@###.### 10/21/04 21:54 GMT
21-10-2004