Say I have a parent interface/class like so
interface Parent<T> {}
And a number of implementing interfaces that fix the generic type.
interface Child extends Parent<Type> {}
Can I use reflection to get the instance of Class representing T if I have the Class object for Child. Something like this:
<T, I extends Parent<T>> I create(Class<I> type) {
Class<T> tType = ...
...
}
Currently I’m having tType be passed in as a parameter, but I’d like to simplify things if I can.
Yes, despite what the others have said, this info is available if you have access to the subclass’
Classobject. You need to usegetGenericSuperclassalong withgetActualTypeArguments.In your example, the “actual” type argument should return the
ClassforType.