Bounds on type parameters of implemented interfaces.
Lets say I'm using reflection to walk over a class' methods. I come upon a method and get its return type, lets call it "returnType". Let's say "java.util.List.class.isAssignableFrom(returnType)" is true. What I'd like to know is what is the upper bound for E in List<E> for this "returnType". In other words, if I invoke the method to get an Object of type "returnType" and it has size() > 0, what is the static upper bound on the type of Object returned from "get(0)"? Stated a bit differently, what is the parameterized type associated with E of the List interface generic type for the "returnType"?
After a lot of digging I can see how it is possible to calculate this type by descending through the supertypes, interfaces, genericSupertypes, and genericInterfaces and propagating the static types declared on the top level type downward, but this is a lot of work. Is there a simpler way to do this?
[930 byte] By [
bungemanva] at [2007-11-27 6:51:50]

# 1
> After a lot of digging I can see how it is possible
> to calculate this type by descending through the
> supertypes, interfaces, genericSupertypes, and
> genericInterfaces and propagating the static types
> declared on the top level type downward, but this is
> a lot of work. Is there a simpler way to do this?
I'm afraid there isn't. This is the shortest code I could come up with (and it works only on the assumption that the method returns a generic type with a wildcard-bound type parameter...)
static Class<?> getUpperBoundForReturntype(Method method) {
ParameterizedType genericReturnType = (ParameterizedType)method.getGenericReturnType();
WildcardType type = (WildcardType)genericReturnType.getActualTypeArguments()[0];
return (Class<?>)type.getUpperBounds()[0];
}
# 2
This isn't really what I was looking for. To clarify, let's say I the class structure is something like
class Foo<A extends B, B extends C, C extends D, D extends Foo> implements List<A> {
...
A get(int i) { ... }
...
}
class Bar<X extends Bar, Y, Z super Integer> extends Foo<X, X, X, X> {
...
}
and the method I'm introspecting looks like
class Bazz {
public Bar<Bar, String, Number> myMethod() { ... }
}
Then I would like to be able to discover that the returned value of myMethod is a List<Bar>. It's simple enough to know that it's a List, just use "java.util.List.class.isAssignableFrom(returnType)", but how does one determine the <Bar> part, or even determine if it can be determined? Is it possible to do so without re-writing most of the type inference code in the compiler?