Error "illegal generic type for instanceof" when using local classes
To me this seems to be an oversight or limitation in the Java language and I do not think it is possible.
The referenced type in an instanceof
expression must be reifiable according to JLS 4.7, meaning that it must be expressed as a reifiable type by its fully qualified name. At the same time, JLS 6.7 states that local classes do not have a fully qualified name, they can therefore not be expressed as reifiable.
If you declare Z as generic, the instanceof
operator treats Z
as a raw type where all generic properties to it - in this case the enclosing class - are considered raw as well. (Similar to a generic methods of a raw type being considered as raw despite any generic signature. This is a measure to retain backwards compatiblity of type generification.) Since any raw type is reifiable, declaring Z to be generic will compile.
A possible workaround is to use reflection:
import java.util.Arrays;
public class X<T> {
void m() {
class Z {}
for (Object o : Arrays.asList(1, 2, 3))
if (Z.class.isInstance(o)) {}
}
}
Apparently, by making Z generic compilation succeeds. I expected that to require <T>
as the type parameter, but you just have to make it generic, so anything will do
import java.util.Arrays;
public class X<T> {
void m() {
class Z<Anything> {}
for (Object o : Arrays.asList(1, 2, 3))
if (Z.class.isInstance(o)) {}
}
}
Proper solution would be qualify the local class, but I don't think you can. Either you refactor it to a private static class or that's probably the best you can get.
This should work either. Using reflection too. But seems a valid solution.
import java.util.Arrays;
public class X<T> {
void m() {
class Z2 {
}
for(Object o: Arrays.asList(1,2,3)) {
if(Z2.class.isAssignableFrom(o.getClass())) {
}
}
}
}