users@jaxb.java.net

Re: Re: JAXB and List interface

From: <aldrop8_at_gmail.com>
Date: Mon, 13 Jun 2011 09:11:17 +0000 (GMT)

Hi, i'm doing a Java-first binding, and i noticed a behavior that could
be related to the same issue.
I wanted to use a custom list implementation on many properties across
my model classes. I knew i could use an XmlAdapter, but, just out of
curiosity, i've tried a quick-and-dirty solution first. I didn't expect
it to work.
Here's what i've done:
- first, i created my custom List impl by extending AbstractList
- then, in my model class, i simply declared a field of my custom type.

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
class Foo {
  @XmlElementWrapper(name="bars", required=false)
  MyList<Bar> bar;
}

class MyList<T> extends AbstractList<T> {
  ...custom List impl...
}

When i run schemagen, this is what it generates for the "bar" property:

...
<xs:element name="bars" minOccurs="0">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="bar" type="bar" nillable="true" minOccurs="0"
maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
...

Looks like the normal output for a plain java.util.List.
The XSD also contains the definition of the "myList" type, but it is
never used.
So, i tried to exclude the MyList class from schemagen: now the
"myList" type is not generated in the XSD, and the generation still
works.
Finally, i've tried to unmarshall/marshall, and it worked perfectly.
It seems to me that the jaxb implementation sees the field type as
"instanceof List" and treats it like a plain List for schema generation
purposes.
Then, in the unmarshall phase, it looks at the field declaration and
does a "newInstance()" on the declared class.
In my specific case this is actually a nice feature, so i was wondering
whether it is intended behavior, and will remain consistent in future
versions, or just a coincidence.
Maybe i've missed something, but the spec doesn't seem to mention
anything about using specific List implementations, neither do the
various tutorials i've found on the net.

Thanks for your patience,

Alberto