Created
February 27, 2014 03:49
-
-
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.
.
This file contains hidden or 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
// 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