The following code does not compile:
class OuterClass<T> {
class BaseClass { }
class SubClass extends BaseClass { }
public void test(BaseClass myObject) {
boolean b = (myObject instanceof SubClass);
}
}
The error message on method test() is: “Cannot perform instanceof check against parameterized type MyOuterClass.MySubClass. Use the form MySubClass instead since further generic type information will be erased at runtime“
So how do I determine whether the variable is of type BaseClass? I have tried all of the following and they don’t work:
boolean b1 = (myObject instanceof SubClass);
boolean b2 = (myObject instanceof SubClass<T>);
boolean b3 = (myObject instanceof SubClass<?>);
boolean b4 = (myObject instanceof OuterClass.SubClass);
boolean b5 = (myObject instanceof OuterClass<T>.SubClass);
boolean b6 = (myObject instanceof OuterClass<?>.SubClass);
boolean b7 = (myObject instanceof OuterClass.SubClass<T>);
boolean b8 = (myObject instanceof OuterClass.SubClass<?>);
I think this problem is independent of the JVM version, but just in case, I am using Sun’s version 1.6.0_22-b04.
Thanks to highlycaffeinated and Nulldevice for pointing out that b4 and b6 are correct.
I have discovered that this is a bug in the version of Eclipse that I was using. (I was using Ganymede, and it was fixed when I upgraded to Helios.)
Just for the record, I have filed it as Eclipse bug 353354.