如何修改这个泛型类,以便主方法可以正常工作?

huangapple go评论102阅读模式
英文:

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&lt;T&gt; 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&lt;T&gt;) Optional.ofNullable(smclass);
    }

and main method:

        XmlService xmlParser = XmlServiceImpl.getInstance();
        Optional&lt;class&gt; smclass= xmlParser.parse(&quot;File.xml&quot;, 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&lt;**class**&gt; **class**= xmlParser.parse(&quot;xmlFile.xml&quot;, **class**.class);
        System.out.println(**class**);

答案1

得分: 1

另一个编辑:从该方法中移除了类型参数
再次编辑:如果您不需要实例化 T(我相信您不需要),您可以执行以下操作。如果该API不在您的控制之下,则必须写成 Class 而不是 Class&lt;T&gt; 并进行强制转换。

    @Override
    public Optional&lt;T&gt; parse(String fileString, Class&lt;T&gt; someClass) {

        File file = new File(fileString);

        T t = null;
        try {
            t = (T) getUnmarshaller().unmarshal(file);
        } catch (JAXBException  e) {
            e.printStackTrace();
        }
        return (Optional&lt;T&gt;) 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&lt;T&gt; and cast.

    @Override
    public Optional&lt;T&gt; parse (String fileString, Class&lt;T&gt; someClass) {

        File file = new File(fileString);

        T t = null;
        try {
            t = (T) getUnmarshaller().unmarshal(file);
        } catch (JAXBException  e) {
            e.printStackTrace();
        }
        return (Optional&lt;T&gt;) 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.

huangapple
  • 本文由 发表于 2020年4月10日 02:12:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/61127563.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定