According to the documentation JAXB factory methods do not have arguments. Is there a JAXB implementation that allow me to create a factory method that receives as a parameter the class of the object I need to create ?
It happens that all my JAXB objects follow the same creation pattern (a particular byte code instrumentation), therefore I would like to encapsulate this in one single factory method having as a parameter the class of the JAXB object to create, avoiding in this way the creation of different factory methods for each JAXB class that basically do exactly the same thing.
I found someone asking the same question in an OTN forum: https://forums.oracle.com/forums/thread.jspa?messageID=9969927#9969927, but not a real answer has been proposed yet.
Thanks for any help
This is currently not possible using the standard JAXB APIs. I have entered the following enhancement request to have this behaviour added to EclipseLink JAXB (MOXy):
MOXy Specific Solution
You could leverage the
@XmlCustomizerextension in EclipseLink JAXB (MOXy) to customize how the objects are instantiated. This mechanism is leveraged to tweak MOXy’s underlying metadata.CommonFactory
Foo.class
The
Fooclass is annotated normally except that we will use the@XmlCustomizerannotation to specify aDescriptorCustomizerthat we are going to use to tweak MOXy’s metadata.Bar
Again we will use the
@XmlCustomizerannotation to reference the sameDescriptorCustomizerthat we did in theFooclass.FactoryCustomizer
MOXy has the concept of an
InstantiationPolicyto build new objects. In this example we will swap in our own instanceInstantiationPolicythat can use parameterized factory methods:Demo
Output
For More Information