JDK-6730568 : Type erasure affects return types + type parameters
  • Type: Bug
  • Component: specification
  • Sub-Component: language
  • Affected Version: 7
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2008-07-28
  • Updated: 2014-02-26
  • Resolved: 2011-07-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.
JDK 7
7 rcFixed
Related Reports
Relates :  
Relates :  
Description
A common expectation about type erasure is that a method's return type is erased and generic methods have no formal type parameters. This is how javac and Eclipse have always behaved. JLS 4.6 should say:

"...The erasure of every other type is the type itself.

***Type erasure also erases the method signature (8.4.2) of any method which is a member of the type.*** The erasure of a method signature s is a signature consisting of the same name as s and the erasures of all the formal parameter types given in s. ***The return type (8.4.5) and formal type parameters (8.4.4) of a method in an erased type are erased. After erasure, a method in an erased type has no formal type parameters (8.4.2).***"

(The "any method which is a member of the type" is consistent with the rules for the superclasses and methods of a raw type C given in 4.8.)

Example of intended behavior:

class Foo<X extends String> {
                    X m1() {} // erases to String m1
              List<X> m2() {} // erases to List m2
<Z>                 Z m3() {} // erases to Object m3
<Z extends X>       Z m4() {} // erases to String m4
<Z extends List<X>> Z m5() {} // erases to List m5
<Z>         Pair<Z,X> m6() {} // erases to Pair m6
<Z extends Y, Y extends String> Z m7() {}  // erases to String m7
}

JLS 4.8 should be tidied up: "An invocation of a method or constructor of a raw type generates an unchecked warning if erasure changes any of the ***formal parameter types of the method or constructor***. ... No unchecked warning is required for a method call when the ***formal parameter types*** do not change..."

Comments
EVALUATION Yes. Agrees with comments from John S.
28-07-2008