开发者

Java reflection anyway to compare Class<?> or Type to Class<T>?

Forgive me for asking such a question but I have been开发者_运维问答 doing C# for the past couple years until very recently so I am a little rusty on type erasure generics in Java compared to the intuitive version of generics in C#.

I have a situation like so:

public void doSomething(Class<T> classType) {
  for (Method method : classType.getMethods()) {
    for (Class<?> parameterClass : method.getParameterTypes()) {
      // How do I compare parameterClass to classType or to String.class?
    }
  }
}

Since parameterClass is of type Class then it is an unknown class type thus it can't be determined at runtime? Does their exist a way to perform this comparison? Again I aplogize I haven't used Java generics in years and I have been reading up on it and am still confused.


Try using method.getGenericParameterTypes() instead of the non-generic getParameterTypes()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜