将java.lang.reflect.Type转换为Class< T>爵士乐 [英] Convert java.lang.reflect.Type to Class<T> clazz
本文介绍了将java.lang.reflect.Type转换为Class< T>爵士乐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将java.lang.reflect.Type
转换为Class<T> clazz
?
如果下一个方法的参数为Class<T>
:
If I have one method as next which has an argument of Class<T>
:
public void oneMethod(Class<T> clazz) {
//Impl
}
然后是另一个方法,该方法的参数为java.lang.reflect.Type
,它调用oneMethod(Class<T> clazz)
,为此,我需要将java.lang.reflect.Type type
转换为Class<T>
:
Then another method which has an argument of java.lang.reflect.Type
and it calls oneMethod(Class<T> clazz)
and for it I need to convert java.lang.reflect.Type type
to Class<T>
:
public void someMehtod(java.lang.reflect.Type type) {
// I want to pass type arg to other method converted in Class<T>
otherMethod(¿How to convert java.lang.reflect.Type to Class<T>?);
}
有可能吗?
推荐答案
您必须确保type
是Class
的实例,然后将其强制转换.
You have to ensure that type
is an instance of Class
, and then cast it.
if (type instanceof Class) {
Class<?> clazz = (Class<?>) type;
otherMethod(clazz);
}
当然,您还必须处理它不是Class
的情况.
Of course, you also have to handle the case of it not being a Class
.
这篇关于将java.lang.reflect.Type转换为Class< T>爵士乐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文