Search code examples
androidgenericsreflectionsuperclass

Android Class.getGenericSuperclass returns java.lang.Object


I am trying to read generic info from a class. Here is what I am doing: first of all I have a class EntityHelper. Somewhere inside it I want to see what is actually that T. I know this can be done with:

(Class<T>) ((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0];

but what method getGenericSuperClass returns is java.lang.Object - which is not correct since I know T is something else, for example DummyEntity.

Android platform I use is 7. Is this some kind of a bug or I am missing something important?


Solution

  • You can not get the generic type of the argument. Generics are implemented using a technique called type erasure. Only way to achieve this effect is to save the Class<T> type( i.e. Use Class Literals as Runtime-Type Tokens ).