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()
精彩评论