Generics runtime check of 'instanceof'
I have an object called Node<Value>
. Objects NodeInternal<Value>
and NodeLeaf<Value>
inherit from Node<Value>
.
I am trying to test what is the type of the object using instanceof
as follows:
if (node instanceof NodeInternal)
. I did not add <Value>
because upon runtime, the type is dropped. Without <Value>
on NodeInternal
however, I get the following warning: NodeInternal is a raw type. References to generic type N开发者_运维问答odeInternal<Value> should be parameterized
.
What should I do in this case?
Use an unbounded wildcard:
if (node instanceof NodeInternal<?>)
node = ((NodeInternal<Value>) node).SE();
http://www.java2s.com/Code/Java/Language-Basics/Usetheinstanceofoperatorwithagenericclasshierarchy.htm
Use <?>
.
You can also play games:
Java: instanceof Generic
精彩评论