I wish to a check if a method exists in an interface based on its signatures.
The signature that the method should have is:
Collection<Foo> methodName(Spam arg0, Eggs arg1, ...)
I can find the methods via Class.getMethods()
then find the name, parameters and return type respectively with method.getName(), method.getParameterTypes() and method.getReturnType().
But what do I compare the return type to in order to ensure that only methods that return Collection<Foo> are chosen, and not other collections?
method.getReturnType().equals(Collection.class)
Since the above will be true for all methods that return a collection, not just for those that return a Foo Collection.
There is a method named
public Type getGenericReturnType()which can return (if it’s the case) aParameterizedType.A
ParameterizedTypecan give you more informations on a generic type such asCollection<Foo>.In particular with the
getActualTypeArguments()method you can get the actual type for each parameter.Here,
ParameterizedTyperepresents Collection andgetActualTypeArguments()represents an array containing FooYou can try this to list the parameters of your generic type :
Sources :
http://tutorials.jenkov.com/