Skip to content

Instantly share code, notes, and snippets.

@greyaperez
Created February 27, 2014 03:49
Show Gist options
  • Save greyaperez/9244031 to your computer and use it in GitHub Desktop.
Save greyaperez/9244031 to your computer and use it in GitHub Desktop.
Grab generic interfaces of a class by Class#getGenericInterfaces() which you then in turn check if it's a ParameterizedType and then grab the actual type arguments accordingly. .
// Interface
public interface FooBar<T> {
// ...
}
// Impl Class
public class BarFoo implements FooBar<Person> {
// ...
}
// Logic to Grab Generic Interfaces & Args of Class
Type[] genericInterfaces = BarFoo.class.getGenericInterfaces();
for (Type genericInterface : genericInterfaces) {
if (genericInterface instanceof ParameterizedType) {
Type[] genericTypes = ((ParameterizedType) genericInterface).getActualTypeArguments();
for (Type genericType : genericTypes) {
System.out.println("Generic type: " + genericType);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment