Duplicate :
|
FULL PRODUCT VERSION : java version "1.5.0" Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-b64) Java HotSpot(TM) Client VM (build 1.5.0-b64, mixed mode, sharing) p ADDITIONAL OS VERSION INFORMATION : Linux genhome 2.6.9-gentoo-r12 #1 SMP Sat Dec 25 09:36:48 WET 2004 i686 Intel(R) Pentium(R) 4 CPU 1.80GHz GenuineIntel GNU/Linux EXTRA RELEVANT SYSTEM CONFIGURATION : Eclipse 3.1 M4 A DESCRIPTION OF THE PROBLEM : I created a class called TransformEnum<S,D>: S - Source Type D - Destination Type this class basically accepts an java.util.Enumeration<S>() and enumerates on those elements that are of Type D: I had the Following Problem: When I created an TransformEnum<Node,NodeCategory> where Node (is an interface - base class) and NodeCategory is a derived class (further down the tree) part of the code is as follows: protected D getNextElement() { D oReturn = null; S oTemp; while (m_oWrapped.hasMoreElements()) { oTemp = m_oWrapped.nextElement(); if (m_oFilter != null) { if (!m_oFilter.pass(oTemp)) { // Failed Filter continue; } } if (m_oTransform != null) { oReturn = m_oTransform.transform(oTemp); if (oReturn != null) { // Object Transformed break; } } else { try { (*) oReturn = (D) oTemp; break; } catch (ClassCastException e) { continue; } } } return oReturn; } // end method getNextElement On the line marked (*) I'm casting oTemp to Type (D). What is happening: the wrapped enumerator is returning an Object of Type StandardValueNode (a class that implements the Node Class) : Tree Something Like Node | AbstractNode | AbstractValueNode | StandardValueNode and casts it to D Whose Tree is Something Like: Node | AbstractNode | AbstractBrowserNode | ..... | NodeCategory without throwing the expected ClassCastException: EXPECTED VERSUS ACTUAL BEHAVIOR : EXPECTED - I expect that the cast should throw the exception (as Expected) Tried: oTemp instanceof D - Compile Error : cannot perform instanceof check against type parameter D Tried: oReturn.getClass().cast(oTemp); - Compile Error : cannot convert from ? extends Object to D ACTUAL - No exception is thrown and my code crashes somewhere else REPRODUCIBILITY : This bug can be reproduced always. ###@###.### 2004-12-30 06:19:18 GMT