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

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P4
    • Resolution: Fixed
    • Affects Version/s: 8u74
    • Fix Version/s: None
    • Component/s: docs
    • Labels:
    • Subcomponent:
    • CPU:
      x86
    • OS:
      windows_8

      Description

      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

        Attachments

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

          Activity

            People

            Assignee:
            bhoran Bernard Horan
            Reporter:
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: