Hi there,
I followed the instructions for Adding Behaviors, but I have a question regarding list properties.
In my generated root JAXB object, I have a couple of List properties, that are declared in the generated class as something like:
@XmlElement(name = "someSubElement")
protected List<SomeSubElement> someSubElements;
and there's a generated accessor method, getSomeSubElements(), that returns a new ArrayList<SomeSubElement>() if the list is null.
In my subclass of this root class, I'm overriding the accessor method, storing my own copy of the subElement list, and doing additional work when an element is added to the list (in this case keeping a Map of an element key to the element itself, which I use to then access elements by key instead of having to iterate through the list).
However, it doesn't seem like the unmarshaller ever calls the getSomeSubElements() method that was overriden; instead, it seems to directly access the someSubElements member variable (of the generated class) and populate it. This happens even though the unmarshaller does get an instance of my subclass correctly (from my ObjectFactory subclass).
So, I guess the question is, should the unmarshaller be getting the list through the accessor and populating whatever it gets from the accessor, instead of directly via reflection on the class field?
Hope this is clear enough.
Thanks,
matias
> -----Original Message-----
> From: Kohsuke Kawaguchi [mailto:Kohsuke.Kawaguchi_at_Sun.COM]
> Sent: Tuesday, July 11, 2006 12:10 PM
> To: users_at_jaxb.dev.java.net
> Subject: Re: Subclassing JAXB 2.0-generated classes
>
>
> 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_does
> nt_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
>