Skip to content

Instantly share code, notes, and snippets.

@talios
Created July 5, 2011 03:00
Show Gist options
  • Save talios/1064186 to your computer and use it in GitHub Desktop.
Save talios/1064186 to your computer and use it in GitHub Desktop.
Extracting generic class information from generified classes
private ParameterizedType findParameterizedClass(Class clazz) {
if (clazz.getGenericSuperclass() != null && clazz.getGenericSuperclass() instanceof ParameterizedType) {
return (ParameterizedType) clazz.getGenericSuperclass();
} else {
if (clazz.getSuperclass() != null) {
return findParameterizedClass(clazz.getSuperclass());
} else {
throw new ClassCastException(clazz.getName() + " is not generic.");
}
}
}
...
Class<T> targetClass = (Class<T>) findParameterizedClass(this).getActualTypeArguments()[0];
public class Something extends SomeOther<Thing> {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment