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

"after type erasure": MyNode cast should be removed, String cast must be Integer cast

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • None
    • 8u74
    • docs
    • x86
    • windows_8

      A DESCRIPTION OF THE PROBLEM :
      I believe the following changes should be made in the code example following "After type erasure, this code becomes:"

      Node n = (MyNode)mn;
      should be
      Node n = mn;
      because mn is declared as a MyNode, so this is an unneeded cast

      Integer x = (String)mn.data;
      should be
      Integer x = (Integer)mn.data;
      because it doesn't make sense to cast to String when assigning to an Integer: it's already known at compile-time that this will fail

      Note that the proposed changes are in accordance with the actual compiled bytecode.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      After type erasure, this code becomes:

      MyNode mn = new MyNode(5);
      Node n = mn; // A raw type - compiler throws an unchecked warning
      n.setData("Hello");
      Integer x = (Integer)mn.data; // Causes a ClassCastException to be thrown.

      ACTUAL -
      After type erasure, this code becomes:

      MyNode mn = new MyNode(5);
      Node n = (MyNode)mn; // A raw type - compiler throws an unchecked warning
      n.setData("Hello");
      Integer x = (String)mn.data; // Causes a ClassCastException to be thrown.


      URL OF FAULTY DOCUMENTATION :
      https://docs.oracle.com/javase/tutorial/java/generics/bridgeMethods.html

        1. MyNode.java
          0.2 kB
          Abhijit Roy
        2. Node.java
          0.2 kB
          Abhijit Roy

            bhoran Bernard Horan (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: