英文:
How do I change this generic class, so the main method can work?
问题
以下是您要翻译的内容:
我在一个类中有这个方法
@Override
public Optional<T> parse(String fileString, Class someClass) {
File file = new File(fileString);
Class smclass = new Class();
try {
smclass = (Class) getUnmarshaller().unmarshal(file);
} catch (JAXBException e) {
e.printStackTrace();
}
return (Optional<T>) Optional.ofNullable(smclass);
}
以及主方法:
XmlService xmlParser = XmlServiceImpl.getInstance();
Optional<class> smclass = xmlParser.parse("File.xml", smclass.class);
System.out.println(smclass);
我想更改这个类的方法,以便我可以在主方法中调用不同的类,不仅仅是一个类,但我不知道如何做。例如:
XmlService xmlParser = XmlServiceImpl.getInstance();
Optional<**class**> **class** = xmlParser.parse("xmlFile.xml", **class**.class);
System.out.println(**class**);
英文:
I have this method in a class
@Override
public Optional<T> parse (String fileString, Class someClass) {
File file = new File(fileString);
Class smclass= new Class();
try {
smclass= (Class) getUnmarshaller().unmarshal(file);
} catch (JAXBException e) {
e.printStackTrace();
}
return (Optional<T>) Optional.ofNullable(smclass);
}
and main method:
XmlService xmlParser = XmlServiceImpl.getInstance();
Optional<class> smclass= xmlParser.parse("File.xml", smclass.class);
System.out.println(smclass);
I want to change the class method so I can call different classes in the main method, not only one, but I don't know how. For example:
XmlService xmlParser = XmlServiceImpl.getInstance();
Optional<**class**> **class**= xmlParser.parse("xmlFile.xml", **class**.class);
System.out.println(**class**);
答案1
得分: 1
另一个编辑:从该方法中移除了类型参数
再次编辑:如果您不需要实例化 T
(我相信您不需要),您可以执行以下操作。如果该API不在您的控制之下,则必须写成 Class
而不是 Class<T>
并进行强制转换。
@Override
public Optional<T> parse(String fileString, Class<T> someClass) {
File file = new File(fileString);
T t = null;
try {
t = (T) getUnmarshaller().unmarshal(file);
} catch (JAXBException e) {
e.printStackTrace();
}
return (Optional<T>) Optional.ofNullable(t);
}
如果您确实需要一个 Class
对象,以便解组件知道要创建什么类型的对象,那么您唯一能做的就是使用反射(这是 Class
类的特性)。此外,我非常确定 JAXB 在内部使用反射,而且一切都不安全。
另外,似乎您并未使用 someClass
,所以您可能只需要带有第一个参数的方法。但由于您正在覆盖超类中的某个方法,您可能需要尝试上述内容。
英文:
Another edit: Removed the type parameter from the method
EDIT(again): If you don't need to instantiate T
(which I believe you don't), you can do what's below. If the API isn't under your control, then you'll have to write Class
instead of Class<T>
and cast.
@Override
public Optional<T> parse (String fileString, Class<T> someClass) {
File file = new File(fileString);
T t = null;
try {
t = (T) getUnmarshaller().unmarshal(file);
} catch (JAXBException e) {
e.printStackTrace();
}
return (Optional<T>) Optional.ofNullable(t);
}
If you do need a Class
object for the unmarshaller to know what kind of object to make, then the only way you can do it is with reflection (that's the nature of the Class
class. Also, I'm pretty sure JAXB uses reflection internally anyways and it's all unsafe.
Also, it doesn't seem as if you're using someClass
, so you could probably have a method with only the first parameter. But since you are overriding some method in a superclass, you might just have to try what's above.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论