Given 2 interfaces:
public interface BaseInterface<T> { }
public interface ExtendedInterface<T0, T1> extends BaseInterface<T0> {}
and a concrete class:
public class MyClass implements ExtendedInterface<String, Object> { }
How do I find out the type parameter passed to the BaseInterface interface?
(I can retrieve the ExtendedInterface type parameters by calling something like
MyClass.class.getGenericInterfaces()[0].getActualTypeArguments()
but I can't spot an easy way to recurse into any base generic interfaces and get anything meaningful back).
I think about the only option I can think of is to inspect a generic method which is declared by
BaseInterface
, and not overridden.Apache Commons has a utility for doing just this... http://commons.apache.org/lang/api/org/apache/commons/lang3/reflect/TypeUtils.html
See how it is used on their github page: https://github.com/apache/commons-lang/blob/72be39f4facb4a5758b9f646309328b764216da3/src/test/java/org/apache/commons/lang3/reflect/TypeUtilsTest.java
I don't think you can as these are really instance specific not class specific. Consider the following:
The fact that a is generic list of Strings is specific to the instance a and not to the class List. Thus, none of the methods of the List.class object could tell you that the genericized type would be of type String for a. Although MyClass in your example happens to have set values for the genricized types of the interface, i do not think this would be available at the interface Class object instance.