users@jaxb.java.net

Subclassing JAXB 2.0-generated classes

From: Pelenur, Matias <pelenur_at_amazon.com>
Date: Thu, 29 Jun 2006 12:54:52 -0700

Hi,
I know this has come up before in the forums, and I've read Kohsuke's blog entry on "Why doesn't JAXB find my subclass?" (http://weblogs.java.net/blog/kohsuke/archive/2006/04/why_doesnt_jaxb.html).

I have my own subclasses of the JAXB-generated classes. When I unmarshall an XML document using the JAXB unmarshaller, I want to get instances of my subclasses instead of instances of the JAXB-generated classes. The purpose of this is to add additional methods or override certain methods that are generated by JAXB.

I tried creating an ObjectFactory similar to the one created by JAXB but that instantiates my subclasses instead of the JAXB classes (by implementing the public SomeClass createSomeClass() methods), but that didn't help. I tried tweaking the parameters to JAXBContext.newInstance() to just list my ObjectFactory instance as the class, or the package where it lives, but the Unmarshaller keeps creating instances of the original JAXB classes; I presume this is because those classes are still statically reacheable from my version of ObjectFactory.

Is there a way to force the unmarshaller to instantiate my own subclasses instead of the JAXB classes? Is that what the "xsi:type" attribute in the schema is used for? I couldn't figure out from the documentation how xsi:type is used, exactly.

Sorry if this has already been answered, I searched through the newsgroups, mailing lists and documentation and couldn't find anything clear on this.

Thanks,
Matias Pelenur