Resolving type parameter values passed to ancester type using reflection
- by Tom Tucker
I've asked a similar question before, but this one is much more challenging.
How do I find a value of a specific type parameter that is passed to an ancestor class or an interface implemented by one of its ancestor classes using reflection? I basically need to write a method that looks like this.
// Return the value of the type parameter at the index passed to the parameterizedClass from the clazz.
Object getParameterValue(Class<?> clazz, Class<?> parameterizedClass, int index)
For the example below, getParameterValue(MyClass.class, Map.class, 1) would return String.class
public class Foo<K, V> implements Map<K, V>{
}
public class Bar<V> extends Foo<Integer, V> {
}
public class MyClass extends Bar<String> {
}
Thanks!