Getting a generic parameter from a supertype class
Let's say I have a parent interface / class similar to
interface Parent<T> {}
And a series of implementation interfaces that capture the generic type.
interface Child extends Parent<Type> {}
Is it possible to use reflection to get an instance Class
representing T
if I have an object Class
for Child
. Something like that:
<T, I extends Parent<T>> I create(Class<I> type) {
Class<T> tType = ...
...
}
I am currently passing tType
as a parameter, but I would like to simplify everything if I can.
source to share
Yes, despite what others have said, this information is available if you have access to the subclass object Class
. You must use getGenericSuperclass
together with getActualTypeArguments
.
ParameterizedType superClass = (ParameterizedType)childClass.getGenericSuperclass();
System.out.println(superClass.getActualTypeArguments()[0]);
In your example, an argument of type "actual" should return Class
for Type
.
source to share
If you need to do something non-trivial with generic types at runtime, consider Guava TypeToken
. He can answer your question (and more!) By addressing some of the nuanced issues raised by commentators:
private interface Parent<T> {}
private interface Intermediate<U, V> extends Parent<V> {}
private interface Child<Z> extends Comparable<Double>, Intermediate<Z, Iterable<String>> {}
public void exploreGuavaTypeTokens() {
final TypeToken<? super Child> token = TypeToken.of(Child.class).getSupertype(Parent.class);
final TypeToken<?> resolved = token.resolveType(Parent.class.getTypeParameters()[0]);
System.out.println(resolved); // "java.lang.Iterable<java.lang.String>"
final Class<?> raw = resolved.getRawType();
System.out.println(raw); // "interface java.lang.Iterable"
}
source to share
I do not think so. Read about erasure dash : generic types are only used for compile-time checking and then discarded. They are not stored in compiled class files, so they are not available at runtime.
source to share