users@jaxb.java.net

Re: Subclassing JAXB 2.0-generated classes

From: Kohsuke Kawaguchi <Kohsuke.Kawaguchi_at_Sun.COM>
Date: Tue, 11 Jul 2006 12:10:14 -0700

I hope section 1.5 "Adding behaviors" of this document answers your
question:

   https://jaxb.dev.java.net/guide/Adding_behaviors.html

Pelenur, Matias wrote:
> 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
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>
>


-- 
Kohsuke Kawaguchi
Sun Microsystems                   kohsuke.kawaguchi_at_sun.com