What is the easiest way to test (using reflection), whether given method (i.e. java.lang.Method instance) has a return type, which can be safely casted to List<String>?
Consider this snippet:
public static class StringList extends ArrayList<String> {} public List<String> method1(); public ArrayList<String> method2(); public StringList method3();
All methods 1, 2, 3 fulfill the requirement. It’s quite easy to test it for the method1 (via getGenericReturnType(), which returns instance of ParameterizedType), but for methods2 and 3, it’s not so obvious. I imagine, that by traversing all getGenericSuperclass() and getGenericInterfaces(), we can get quite close, but I don’t see, how to match the TypeVariable in List<E> (which occurs somewhere in the superclass interfaces) with the actual type parameter (i.e. where this E is matched to String).
Or maybe is there a completely different (easier) way, which I overlook?
EDIT: For those looking into it, here is method4, which also fulfills the requirement and which shows some more cases, which have to be investigated:
public interface Parametrized<T extends StringList> { T method4(); }
Solving this in general is really not easy to do yourself using only the tools provided by Java itself. There are a lot of special cases (nested classes, type parameter bounds,…) to take care of. That’s why I wrote a library to make generic type reflection easier: gentyref. I added sample code (in the form of a JUnit test) to show how to use it to solve this problem: StackoverflowQ182872Test.java. Basically, you just call
GenericTypeReflector.isSuperTypeusing aTypeToken(idea from Neil Gafter) to see ifList<String>is a supertype of the return type.I also added a 5th test case, to show that an extra transformation on the return type (
GenericTypeReflector.getExactReturnType) to replace type parameters with their values is sometimes needed.