感谢您为此问题输入错误 ( 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() {
}
支持以下类型的工厂类:
带有静态方法的工厂
有了这个 bug 修复,当 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)