Created
July 5, 2011 03:00
-
-
Save talios/1064186 to your computer and use it in GitHub Desktop.
Extracting generic class information from generified classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Something extends SomeOther<Thing> { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment