Resolving type parameter values passed to ancester type using reflection

Posted by Tom Tucker on Stack Overflow See other posts from Stack Overflow or by Tom Tucker
Published on 2010-12-31T01:35:39Z Indexed on 2010/12/31 1:54 UTC
Read the original article Hit count: 283

Filed under:
|

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!

© Stack Overflow or respective owner

Related posts about java

Related posts about reflection