In case it helps anyone:
I managed to do this by setting the Adapter in the List field that contains the Span element.
Setting the adapter at package level or the Span class level would not work.
Also the adapter is not converting from List to List but from Span to Span, and modifying the list internally.
The final configuration is:
The place where I define the adapter:
@XmlElementRefs({
@XmlElementRef(type = Span.class),
@XmlElementRef(type = Other.class),
@XmlElementRef(type = Another.class)
})
@XmlJavaTypeAdapter(value = SpanAdapter.class)
public List<Component> getComponents() {
if (components == null) components = new ArrayList();
return components;
}
The adapter (can't do it from span to span because I use it in a List field that may contain other classes):
public class SpanAdapter extends XmlAdapter<Object, Object> {
@Override
public Object unmarshal(Object o) throws Exception {
if (o instanceof Span) {
Span span = (Span) o;
List<Span.Component> list = new ArrayList<>();
for (Object child : span.getValue()) {
if (child instanceof String) {
String s = (String) child;
for (String w : s.split(" ")) {
Word word = new Word();
word.setValue(w.trim());
list.add(word);
}
} else if (child instanceof Group) {
list.add((Group) child);
}
}
span.setValue(list);
return span;
}
return o;
}
@Override
public Object marshal(Object o) throws Exception {
if(o instanceof Span) {
Span span = (Span) o;
List list = new ArrayList<>();
Iterator<Span.Component> iterator = span.getValue().iterator();
while (iterator.hasNext()) {
Span.Component w = iterator.next();
if (w instanceof Group) {
list.add(w);
} else if (w instanceof Word) {
String value = ((Word) w).getValue();
list.add(iterator.hasNext() ? value + " " : value);
}
}
span.setValue(list);
return span;
}else return o;
}
}