Skip to content

Instantly share code, notes, and snippets.

@azusa
Created January 13, 2012 02:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save azusa/1604355 to your computer and use it in GitHub Desktop.
Save azusa/1604355 to your computer and use it in GitHub Desktop.
public static Class<?> getGenericClass(Class<?> clazz) throws IllegalArgumentException {
Type[] t = clazz.getGenericInterfaces();
if (t.length == 0) {
Class<?> parent = clazz.getSuperclass();
if (parent == null) {
throw new IllegalArgumentException(clazz.getName());
}
return getGenericClass(parent);
}
for (Type type : t) {
if (type instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) type;
return (Class<?>) pt.getActualTypeArguments()[0];
}
}
throw new IllegalStateException();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment