jeudi 12 avril 2018

How to do use java reflection to determine generic type of base class field

I know that if I have a field with generic type arguments such as this

List<String> mList

if I can get a reference to the field, I can use this code to determine that the generic type is actually a String:

((ParameterizedType field.getGenericType()).getActualTypeArguments()

However, my problem is a bit more complicated. We have a superclass that looks something like this:

abstract class CustomList extends List<T> {
  List<T> mList;    
}

Then we have another class that looks something like this:

class FooList extends CustomList<Foo> {
  //...
}

When I try and reflect on the field mList, it tells me that that generic type parameter is T. Is there any way to determine that it's actually of type Foo? If I could determine that the FooList class has a generic parameter Foo, that would probably be good enough, but I'm not sure how to do that either?





Aucun commentaire:

Enregistrer un commentaire