users@jaxb.java.net

Re: Default marshalling of JRE classes

From: Kohsuke Kawaguchi <Kohsuke.Kawaguchi_at_Sun.COM>
Date: Wed, 25 Oct 2006 08:28:51 -0700

Herve Bitteur wrote:
> Hello,
>
> I made a few attempts to test default marshalling of java.awt.Point and
> java.awt.Dimension.
> They both lead to stack overflow, because of infinite recursion.

I'm bit surprised to see a simple class like Point to cause infinite
recursion, but I guess it's because of "getLocation".

> My understanding is that this is due to the default XmlAccessType being
> PUBLIC_MEMBER. So every public getter/setter pair and every public field
> gets serialized.

Right.

> For class Point, we have fields x & y, plus getter/setter pair
> getLocation/setLocation. The getLocation allocates a new Point, whose
> getLocation allocates a new Point, etc...
> For class Dimension, we have fields width & height, plus getter/setter
> pair getSize/setSize. The getSize method allocates a new Dimension, etc...
>
> For example, marshalling a Dimension gives the following XML endless stream:
>
> <dimension>
> <width>4</width>
> <height>35</height>
> <size>
> <width>4</width>
> <height>35</height>
> <size>
> <width>4</width>
> <height>35</height>
> etc...
>
> Ma question: Is there a way to specify a different policy, for example
> FIELD by default? Or just for the current application (if this makes
> sense, I'm not sure)

Customizing the binding of a class that doesn't belong to you is rather
tricky. I guess this should be really fixed on JRE side, now that JAXB
is a part of the platform. I'll file an RFE to see what they'll say.

One way to work around this is to define an @XmlJavaTypeAdapter and
custom adapter. Adapter can be specified at "the point of reference" so
to speak (I'm sure javadoc has some examples.)

The other more comprehensive approach is to define an "external
customization" for annotations, just like we have it for XJC. We have
hooks to do it, but we don't have code that take advantage of that hook
to provide such functionality.
See
http://forums.java.net/jive/thread.jspa?forumID=46&threadID=18615&messageID=160531#160531
if you are interested in this. I'm looking for someone who can work on this.

-- 
Kohsuke Kawaguchi
Sun Microsystems                   kohsuke.kawaguchi_at_sun.com