dimanche 13 mars 2016

Checking if Class parameter is of certain type

I have the following class descriptor:

public class MyArray<T extends MyValue> extends ArrayList<T> {

In that case, MyValue is an interface, which is implemented by MyIntValue and MyStringValue.

I now need to check what kind of value a MyArray instance holds, whether or not it is empty (I cannot just use get(0) to check the type of value).

I have tried this:

MyArray array = (MyArray) object;
if (array instanceof MyArray<MyIntValue>) { // Error: Illegal generic type for instanceof
    // Do stuff
}

Is there a simple way to achieve this, or should I go with reflection (and how)?





Aucun commentaire:

Enregistrer un commentaire