Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6730568

Type erasure affects return types + type parameters

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 7
    • 7
    • specification
    • None
    • rc
    • generic
    • generic
    • Verified

      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..."

            abuckley Alex Buckley
            abuckley Alex Buckley
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: