AndreiBogdan
AndreiBogdan

Reputation: 11164

Check type of generic

I have this method:

public <T> void onMultipleSelectionTextFinished(
          @NonNull ArrayList<FMultipleSelectionText.HolderItem<T>> holderItems, 
          int payload) {
    // check here
}

How can I check inside of it that holderItem is of type ArrayList<FMultipleSelectionText.HolderItem<EFunction>> and if it is then cast it to that and handle if in a special way ?

EDIT:

I tried using instanceof but it says this:

enter image description here

Also, I went by the rout of castig it to Object first and then castig it again to what I needed, but it's not an elegant solution :(

Upvotes: 1

Views: 665

Answers (1)

Rohit Gulati
Rohit Gulati

Reputation: 542

You cannot do that at the compile time because of TypeErasure at runtime. Alternatively you can do this using reflection, Guava API also provide methods to accomplish this task. Please refer this link.

Upvotes: 1

Related Questions