JAXB nie budzi wątpliwości co do listy interfejsów

Wygląda na to, że JAXB nie może przeczytać tego, co pisze. Rozważ następujący kod:

interface IFoo {
    void jump();
}

@XmlRootElement
class Bar implements IFoo {
    @XmlElement
    public String y;

    public Bar() {
        y = "";
    }

    public Bar(String y) {
        this.y = y;
    }

    @Override
    public void jump() {
        System.out.println(y);
    }
}

@XmlRootElement
class Baz implements IFoo {
    @XmlElement
    public int x;

    public Baz() {
        x = 0;
    }

    public Baz(int x) {
        this.x = x;
    }

    @Override
    public void jump() {
        System.out.println(x);
    }
}

@XmlRootElement
public class Holder {
    private List<IFoo> things;

    public Holder() {
        things = new ArrayList<>();
    }

    @XmlElementWrapper
    @XmlAnyElement
    public List<IFoo> getThings() {
        return things;
    }

    public void addThing(IFoo thing) {
        things.add(thing);
    }
}

// ...

try {
    JAXBContext context = JAXBContext.newInstance(Holder.class, Bar.class, Baz.class);

    Holder holder = new Holder();
    holder.addThing(new Bar("1"));
    holder.addThing(new Baz(2));
    holder.addThing(new Baz(3));

    for (IFoo thing : holder.getThings()) {
        thing.jump();
    }

    StringWriter s = new StringWriter();
    context.createMarshaller().marshal(holder, s);

    String data = s.toString();

    System.out.println(data);

    StringReader t = new StringReader(data);
    Holder holder2 = (Holder)context.createUnmarshaller().unmarshal(t);

    for (IFoo thing : holder2.getThings()) {
        thing.jump();
    }
}
catch (Exception e) {
    System.err.println(e.getMessage());
}

To oczywiście uproszczony przykład. Chodzi o to, że muszę przechowywać dwie bardzo różne klasy, Bar i Baz, w jednej kolekcji. Cóż, zauważyłem, że mają całkiem podobny interfejs publiczny, więc stworzyłem interfejs IFoo i zmusiłem ich do implementacji. Teraz chcę mieć narzędzia do zapisywania i ładowania tej kolekcji do / z XML. Niestety ten kod nie działa: kolekcja jest zapisana, ale nie można jej załadować! Zamierzone wyjście to

1
2
3
some xml
1
2
3

Ale niestety rzeczywiste wyjście jest

1
2
3
some xml
com.sun.org.apache.xerces.internal.dom.ElementNSImpl cannot be cast to testapplication1.IFoo

Najwyraźniej muszę użyć adnotacji w inny sposób? Albo zrezygnować z JAXB i szukać czegoś innego? Ja, no cóż, mogę napisać metodę "XMLNode toXML ()" dla wszystkich klas, które chcę (de) marshal, ale ...

questionAnswers(1)

yourAnswerToTheQuestion