java 从泛型类型 T 获取“Class”对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10800463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get "Class" object from generic type T
提问by MyTitle
I want to make generic function that return Object representation of XML document (using JAXB). I need to pass "class" object to JAXBContext constructor, but how I can get it from T?
我想制作返回 XML 文档的对象表示的通用函数(使用 JAXB)。我需要将“类”对象传递给 JAXBContext 构造函数,但是如何从 T 获取它?
public <T> readXmlToObject(String xmlFileName, T jaxbClass) {
JAXBContext context = JAXBContext.newInstance(T.class); // T.class - here error, how to get it?
.......
}
回答by biziclop
Pass the class object instead and it's easy.
改为传递类对象,这很容易。
public <T> T readXmlToObject(String xmlFileName, Class<T> jaxbClass) {
JAXBContext context = JAXBContext.newInstance( jaxbClass ); // T.class - here error, how to get it?
Object o = context.createUnmarshaller().unmarshal( new File( xmlFileName ) );
return jaxbClass.cast( o );
}
The idea here is that since you can't extract the type parameter from the object, you have to do it the other way around: start with the class and then manipulate the object to match the type parameter.
这里的想法是,由于您无法从对象中提取类型参数,因此您必须以相反的方式进行:从类开始,然后操作对象以匹配类型参数。
回答by Bohemian
Don't listen to the others... you CAN get it.
不要听其他人的...你可以得到它。
Simply change the type of the jaxbClass
parameter to Class<T>
:
只需将jaxbClass
参数的类型更改为Class<T>
:
public <T> T readXmlToObject(String xmlFileName, Class<T> jaxbClass) {
JAXBContext context = JAXBContext.newInstance(jaxbClass);
.......
}
回答by Rich Hill
You cannot get the class of at run time. Java implements Generics using Type Safe Erasure which means that the understanding of the Generic type is only applied at up through compilation. You must interogate the actual object at run time if you want to get its class.
您无法在运行时获得类。Java 使用类型安全擦除来实现泛型,这意味着对泛型类型的理解只能通过编译来应用。如果你想获得它的类,你必须在运行时询问实际的对象。
回答by Dan Vinton
Take a look at this SO answer.
看看这个 SO 答案。
Basically, the type T isn't available at runtime - Java generics are subject to erasure by the compiler.
基本上,类型 T 在运行时不可用 - Java 泛型会被编译器擦除。
Fortunately you already have an instance of your class, so you can get the type information from there:
幸运的是,您已经有一个类的实例,因此您可以从那里获取类型信息:
public <T> readXmlToObject(String xmlFileName, T jaxbClass) {
// if jaxbClass is an instance of the data object, you can do this:
JAXBContext context = JAXBContext.newInstance(jaxbClass.getClass());
// alternatively if jaxbClass is an instance of the Class object:
JAXBContext context = JAXBContext.newInstance(jaxbClass);
// ......
}
回答by bpgergo
try to pass the class itself, something like this
尝试通过类本身,像这样
public <T> readXmlToObject(String xmlFileName, Class<T> class) {
回答by Anderson Junqueira
public class XYZ<T> {
...
private Class<T> tClass;
...
public <T> readXmlToObject(String xmlFileName) {
JAXBContext context = JAXBContext.newInstance(tClass);
...
}
...
}