我怎样才能做到这一点?
public class GenericClass<T>
{
public Type getMyType()
{
//How do I return the type of T?
}
}
Run Code Online (Sandbox Code Playgroud)
到目前为止我尝试的所有东西总是返回类型Object而不是使用的特定类型.
我希望检查接口中是否存在基于其签名的方法.
该方法应具有的签名是:
Collection<Foo> methodName(Spam arg0, Eggs arg1, ...)
Run Code Online (Sandbox Code Playgroud)
我可以找到方法Class.getMethods()
然后分别找到名称,参数和返回类型method.getName(), method.getParameterTypes()和method.getReturnType().
但是,为了确保只Collection<Foo>选择返回的方法,而不是其他集合,我应该将返回类型与之进行比较?
method.getReturnType().equals(Collection.class)
Run Code Online (Sandbox Code Playgroud)
由于上述内容对于返回集合的所有方法都是如此,而不仅仅是那些返回FooCollection的方法.