Alex Rivera | Logout

How to test whether method return type matches List<String>

Asked 2008-10-08T13:53:20.703
16

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();
}
Edit
Report

2 Answers

4

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.isSuperType using a TypeToken (idea from Neil Gafter) to see if List<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.

answered 2009-01-01T22:41:48.880
0

This thread on the java.net forums might be helpful (although I have to admit I didn't understand everything they said).

answered 2008-10-08T15:44:16.033

Your Answer