Cannot perform instanceof check against parameterized type ArrayList<Foo>

JavaAndroidGenerics

Java Problem Overview


The following code:

((tempVar instanceof ArrayList<Foo>) ? tempVar : null);

causes:

> Cannot perform instanceof check against parameterized type ArrayList<Foo>. Use the form ArrayList<?> 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?

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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();
}

Solution 3 - Java

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);

Solution 4 - Java

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.

Solution 5 - Java

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.

Solution 6 - Java

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.

Solution 7 - Java

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);

Solution 8 - Java

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.

Solution 9 - Java

You can use

boolean isInstanceArrayList = tempVar.getClass() == ArrayList.class

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCanerView Question on Stackoverflow
Solution 1 - JavaBuhake SindiView Answer on Stackoverflow
Solution 2 - JavaGregView Answer on Stackoverflow
Solution 3 - JavaPaul BelloraView Answer on Stackoverflow
Solution 4 - JavaStøcciDView Answer on Stackoverflow
Solution 5 - JavadeeKayView Answer on Stackoverflow
Solution 6 - JavaflobView Answer on Stackoverflow
Solution 7 - JavajmfmView Answer on Stackoverflow
Solution 8 - JavaLucio MenciView Answer on Stackoverflow
Solution 9 - JavaDevishankarView Answer on Stackoverflow