JAXB MOXy 中的静态工厂方法

发布于 2024-12-13 14:57:08 字数 1213 浏览 1 评论 0原文

我定义一个静态工厂方法:

@XmlType(factoryClass=DummyFactory.class, factoryMethod="createNew")
public abstract MyClass() {
}

我使用工厂方法的原因是 MyClass 是抽象的,如何获取它的实例取决于类的某些注释。此逻辑嵌入在工厂方法 createNew 中。

另外,工厂类DummyFactory是抽象的。据我了解,如果工厂方法是静态的,工厂类不需要提供默认构造函数(http://download.oracle.com/javaee/6/api/javax/xml/bind/annotation/XmlType.html)。

这是工厂类的原始简化:

public abstract class DummyFactory {
    public static MyClass createNew() {
        // code for returning a new instance of MyClass
    }   
}

但是,当我尝试解组 XML 文档时,我收到以下异常:

Exception [EclipseLink-171] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: The factory class does not define a public default constructor, or the constructor raised an exception.
Internal Exception: java.lang.InstantiationException

首先,我认为我在工厂类和方法的解释中没有得到正确的信息,但后来我尝试使用 JAXB RI,效果很好。 所以我的问题是:

有没有办法让 MOXy 与抽象工厂类一起工作?

(JAXB RI 给我带来了其他类型的问题,这就是我不想使用它的原因)。

I am defining a static factory method with:

@XmlType(factoryClass=DummyFactory.class, factoryMethod="createNew")
public abstract MyClass() {
}

The reason I am using factory methods is that MyClass is abstract, and how to obtain an instance of it depends on certain annotations of the class. This logic is embedded in the factory method createNew.

In addition, the factory class DummyFactory is abstract. As far as I understand factory classes do not need to provide a default constructor if their factory method is static (http://download.oracle.com/javaee/6/api/javax/xml/bind/annotation/XmlType.html).

This is a raw simplification of how the factory class looks like:

public abstract class DummyFactory {
    public static MyClass createNew() {
        // code for returning a new instance of MyClass
    }   
}

However, when I try to unmarshal a XML document, I am getting the following exception:

Exception [EclipseLink-171] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: The factory class does not define a public default constructor, or the constructor raised an exception.
Internal Exception: java.lang.InstantiationException

First I thought that I did not get something correctly in the explanation of factory classes and methods, but then I tried with JAXB RI and this is working fine there.
So my question is:

Is there a way to make MOXy work with abstract factory classes ?

(JAXB RI is giving me other kind of problems, that is the reason I would prefer not using it).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对你的占有欲 2024-12-20 14:57:08

感谢您为此问题输入错误 (https://bugs.eclipse.org/362984)。该问题已在 EclipseLink 2.4 流中得到修复,并将于今天(2011 年 11 月 9 日)向后移植到 EclipseLink 2.3.2 流中。您可以尝试从以下位置获取每晚下载的修复:

现在,如果您使用 @XmlType 注释指定工厂类,例如:

@XmlType(factoryClass=DummyFactory.class, factoryMethod="createNew")
public abstract MyClass() {
}

支持以下类型的工厂类:

具有静态方法的工厂< /strong>

通过此错误修复,当 MOXy使用工厂方法创建 MyClass 实例,但未创建 DummyFactory 实例。

public abstract class DummyFactory {
    public static MyClass createNew() {
        // code for returning a new instance of MyClass
    }   
}

具有实例方法的工厂

除了静态方法之外,MOXy 还允许您指定实例级创建方法。对于这些方法,MOXy 将创建工厂类的实例。

public class DummyFactory {
    public MyClass createNew() {
        // code for returning a new instance of MyClass
    }   
}

JAXB RI 中不允许此配置,您将收到以下异常:

Exception in thread "main" com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
Factory class "forum8022136.DummyFactory" does not have static zero args factory method "createNew".
    this problem is related to the following location:
        at forum8022136.MyClass

    at com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException$Builder.check(IllegalAnnotationsException.java:91)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:436)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:277)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1100)
    at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:143)
    at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:110)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:202)
    at javax.xml.bind.ContextFinder.find(ContextFinder.java:376)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:574)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:522)
    at forum8022136.Demo.main(Demo.java:14)

Thank you for entering a bug for this issue (https://bugs.eclipse.org/362984). The issue has been fixed in the EclipseLink 2.4 stream, and will be backported today (Nov 9, 2011) to the EclipseLink 2.3.2 stream. You can try out the fix obtaining a nightly download from:

Now if you specify a factory class using the @XmlType annotation like:

@XmlType(factoryClass=DummyFactory.class, factoryMethod="createNew")
public abstract MyClass() {
}

The following types of factory classes are supported:

Factory with Static Methods

With this bug fix, when MOXy uses the factory method to create an instance of MyClass an instance of DummyFactory is not created.

public abstract class DummyFactory {
    public static MyClass createNew() {
        // code for returning a new instance of MyClass
    }   
}

Factory with Instance Methods

In addition to static methods, MOXy allows you to specify instance level creation methods. For these methods MOXy will create an instance of the factory class.

public class DummyFactory {
    public MyClass createNew() {
        // code for returning a new instance of MyClass
    }   
}

This configuration is not allowed in the JAXB RI, and you will get the following exception:

Exception in thread "main" com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
Factory class "forum8022136.DummyFactory" does not have static zero args factory method "createNew".
    this problem is related to the following location:
        at forum8022136.MyClass

    at com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException$Builder.check(IllegalAnnotationsException.java:91)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:436)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:277)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1100)
    at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:143)
    at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:110)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:202)
    at javax.xml.bind.ContextFinder.find(ContextFinder.java:376)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:574)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:522)
    at forum8022136.Demo.main(Demo.java:14)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文