Cannot perform instanceof check against parameterized type ArrayList<Foo>
The following code:
((tempVar instanceof ArrayList<Foo>) ? tempVar : null);
causes:
Cannot perform
instanceof
check against parameterized typeArrayList<Foo>
. Use the formArrayList<?>开发者_StackOverflow社区;
instead since further generic type information will be erased at runtime
Can someone explain me what is meant by "further generic type information will be erased at runtime" and how to fix this?
It means that if you have anything that is parameterized, e.g. List<Foo> fooList = new ArrayList<Foo>();
, the Generics information will be erased at runtime. Instead, this is what the JVM will see List fooList = new ArrayList();
.
This is called type erasure. The JVM has no parameterized type information of the List
(in the example) during runtime.
A fix? Since the JVM has no information of the Parameterized type on runtime, there's no way you can do an instanceof
of ArrayList<Foo>
. You can "store" the parameterized type explicitly and do a comparison there.
You could always do this instead
try
{
if(obj instanceof ArrayList<?>)
{
if(((ArrayList<?>)obj).get(0) instanceof MyObject)
{
// do stuff
}
}
}
catch(NullPointerException e)
{
e.printStackTrace();
}
Due to type erasure, the parameterized type of the ArrayList
won't be known at runtime. The best you can do with instanceof
is to check whether tempVar
is an ArrayList
(of anything). To do this in a generics-friendly way, use:
((tempVar instanceof ArrayList<?>) ? tempVar : null);
This is sufficient:
if(obj instanceof ArrayList<?>)
{
if(((ArrayList<?>)obj).get(0) instanceof MyObject)
{
// do stuff
}
}
In fact, instanceof
checks whether the left operand is null
or not and returns false
if it is actually null
.
So: no need to catch NullPointerException
.
You can't fix that. The type information for Generics is not available at runtime and you won't have access to it. You can only check the content of the array.
instanceof operator works at runtime. But java does not carry the parametrized type info at runtime. They are erased at compile time. Hence the error.
You could always do this
Create a class
public class ListFoo {
private List<Foo> theList;
public ListFoo(List<Foo> theList {
this.theList = theLista;
}
public List<Foo> getList() {
return theList;
}
}
Is not the same but ...
myList = new ArrayList<Foo>;
.....
Object tempVar = new ListFoo(myList);
....
((tempVar instanceof ListFoo) ? tempVar.getList() : null);
To perform an exact check type instead of this:
protected <Foo> ArrayList<Foo> myMethod(Object tempVar ) {
return tempVar instanceof ArrayList<Foo>) ? (ArrayList<Foo>) tempVar : null;
}
You can do this:
protected <Foo> ArrayList<Foo> myMethod(Object tempVar, Class<Foo> clz) {
if (tempVar instanceof ArrayList && (tempVar.size() == 0 || clz.isInstance(tempVar.get(0))))
return (ArrayList<Foo>) tempVar;
return null;
}
In this way your code is full secured.
You can use
boolean isInstanceArrayList = tempVar.getClass() == ArrayList.class
精彩评论