Given the reason in @Blaise's answer, it doesn't seem possible to have MOXy (or any JAXB implementation in general?) populate a lazily-initialized collection via a setter method on the collection. However, a combination of xml-accessor-type="FIELD"
(or @XmlAccessorType
if using annotations) and defining a JAXB unmarshal event callback will get the job done. In my afterUnmarshal()
implementation I do the special work on Something instances that is done in addSomething()
.
private void afterUnmarshal(Unmarshaller, Object parent) {
for (Something aThing : getSomethings()) {
// Do special stuff on aThing
}
}
Using FIELD
access type gets JAXB/MOXy to directly inject the collection into the field, bypassing the getter. Then the call back cleans things up properly.