Caner
Caner

Reputation: 59258

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 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?

Upvotes: 62

Views: 53100

Answers (9)

Lucio Menci
Lucio Menci

Reputation: 143

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.

Upvotes: 0

Devishankar
Devishankar

Reputation: 229

You can use

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

Upvotes: -1

St&#248;cciD
St&#248;cciD

Reputation: 123

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.

Upvotes: 4

jmfm
jmfm

Reputation: 1

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

Upvotes: 0

Greg
Greg

Reputation: 514

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

Upvotes: 32

deeKay
deeKay

Reputation: 287

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.

Upvotes: 2

Paul Bellora
Paul Bellora

Reputation: 55223

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

Upvotes: 11

flob
flob

Reputation: 3908

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.

Upvotes: 2

Buhake Sindi
Buhake Sindi

Reputation: 89189

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.

Upvotes: 58

Related Questions