Given a Type instance (which may be a Class or ParameterizedType), I need to get the specific Type of an interface implemented by the class. Without generics this is easy, call getInterfaces() on a Class instance and you're done. However, I need this to work even when the implemented interface has its own type parameters that may or may not depend on the type parameters of the original class itself.
Some examples, the function should return Iterable<Integer> when given a class
class Foo implements Iterable<Integer> {}
but must also return Iterable<String> given the class
class Bar<T> implements Iterable<T> {}
and a ParmeterizedType representing Bar<String>
Is there an easy way to do this with built in reflection, third party tools etc?
To clarify, this needs to work not only with type instances retrieved via literals (Foo.class etc), but also those returned via reflection that can contain applied type parameters, for example the return type returned via reflection from the method
static Bar<String> magic() { ... }
This would be a ParameterizedType referencing the raw Bar class and the String type argument.