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

cast to parameterized type is accepted although it should be rejected

XMLWordPrintable

    • b24
    • x86
    • windows_xp



      Name: rmT116609 Date: 07/29/2003


      FULL PRODUCT VERSION :
      java version "1.4.2"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2-b28)
      Java HotSpot(TM) Client VM (build 1.4.2-b28, mixed mode)

      generics prototype compiler 2.2-ea

      FULL OS VERSION :
      Windows XP

      A DESCRIPTION OF THE PROBLEM :
      I am using the generics prototype compiler version 2.2-ea and the specification that comes with it.

      According to section 5.4 of the spec the following code should give an error message, because I cast to a parameterized subtype of Object and there are other subtypes of Object with the same erasure. Such a cast is illegal according to the spec and I had expected a compiletime error message.



      public class Test{

      private static class GenericWrapper<Elem> {
      private Elem theObject;
      public GenericWrapper(Elem arg) {
      theObject = arg;
      }
      public <T extends Elem> GenericWrapper (GenericWrapper<T> other) {
      this.theObject = other.theObject;
      }
      public String toString() {
      return theObject.toString();
      }
      }
      private static GenericWrapper<String> method (Object wrappedString) {
      return (GenericWrapper<String>) wrappedString;
      }

      public static void main(String[] args) {
      System.out.println(method(new GenericWrapper<String>("abc")));
      System.out.println(method(new GenericWrapper<Exception>(new Exception())));
      }
      }



      By the way, I do not get a runtime error either.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      compile the code

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The compiler is supposed to reject the following cast from Object to a parameterized type:

      (GenericWrapper<String>) wrappedString
      ACTUAL -
      no error messages whatsoever, neither at compiletime nor at runtime

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class Test{

      private static class GenericWrapper<Elem> {
      private Elem theObject;
      public GenericWrapper(Elem arg) {
      theObject = arg;
      }
      public <T extends Elem> GenericWrapper (GenericWrapper<T> other) {
      this.theObject = other.theObject;
      }
      public String toString() {
      return theObject.toString();
      }
      }
      private static GenericWrapper<String> method (Object wrappedString) {
      return (GenericWrapper<String>) wrappedString;
      }

      public static void main(String[] args) {
      System.out.println(method(new GenericWrapper<String>("abc")));
      System.out.println(method(new GenericWrapper<Exception>(new Exception())));
      }
      }
      ---------- END SOURCE ----------
      (Incident Review ID: 192975)
      ======================================================================

            gafter Neal Gafter (Inactive)
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: