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