I have:
interface I<T> {}
and:
interface I1 extends I<Clazz1> {}
I have also I2, I3 and so on which extend I<Clazz2>, I<Clazz3> etc.
I don’t know about Clazz1 in runtime. How could I get this information?
I tried:
subInterface1.getInterfaces()[0].getTypeParameters()[0]
but in only knows about T, not about Clazz1.
When you do
you are getting the first formal type parameter of the
Classobject representing the interfaceI, which is indeed the variableT. Instead you need to useto get the
Typeobject representing the particular instantiation ofIthat has been extended byI1. This will be an instance ofParameterizedType, so you can cast to that and then usegetRawType()andgetActualTypeArguments()to extract the raw typeIand the actual type parameter valueClazz1respectively. Note that the return type ofgetActualTypeArguments()isType[]rather thanClass[]because the actual arguments might not be concrete classes, they could be variables or wildcards (or indeed other parameterized types, in a case such asList<List<String>>).