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).
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
@XmlTypeannotation like: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
MyClassan instance ofDummyFactoryis not created.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.
This configuration is not allowed in the JAXB RI, and you will get the following exception: