Is it possible to get the type of a generic parameter?
An example:
public final class Voodoo {
public static void chill(List> aListWithTy
I noticed that many people lean towards the getGenericSuperclass()
solution:
class RootGeneric {
public Class persistentClass = (Class)
((ParameterizedType)getClass().getGenericSuperclass())
.getActualTypeArguments()[0];
}
However, this solution is error prone. It will not work properly if there are generics in the descendants. Consider this:
class Foo extends RootGeneric {}
class Bar extends Foo {}
Which type will Bar.persistentClass
have? Class
? Nope, it will be Class
. This will happen due to getClass()
always returns the top most class, which is Bar
in this case, and its generic super class is Foo
. Hence, the argument type will be Double
.
If you need a reliable solution which doesn't fail I can suggest two.
Guava
. It has a class that was made exactly for this purpose: com.google.common.reflect.TypeToken
. It handles all the corner cases just fine and offers some more nice functionality. The downside is an extra dependency. Given you've used this class, your code would look simple and clear, like this:class RootGeneric {
@SuppressWarnings("unchecked")
public final Class persistentClass = (Class) (new TypeToken(getClass()) {}.getType());
}
abstract class RootGeneric {
@SuppressWarnings("unchecked")
private Class getTypeOfT() {
Class type = null;
Class> iter = getClass();
while (iter.getSuperclass() != null) {
Class> next = iter.getSuperclass();
if (next != null && next.isAssignableFrom(RootGeneric.class)) {
type =
(Class)
((ParameterizedType) iter.getGenericSuperclass()).getActualTypeArguments()[0];
break;
}
iter = next;
}
if (type == null) {
throw new ClassCastException("Cannot determine type of T");
}
return type;
}
}