Why doesn’t JAXB generate setters for Lists

Here is the justification from the JAXB specification – page 60.

Design Note – There is no setter method for a List property. The
getter returns the List by reference. An item can be added to the
List returned by the getter method using an appropriate method
defined on java.util.List. Rationale for this design in JAXB 1.0 was
to enable the implementation to wrapper the list and be able to
perform checks as content was added or removed from the List.

So if the implementation of the List was overriding add/remove to perform validation, replacing that ‘special’ List with (for instance) an ArrayList would defeat these checks.

Leave a Comment