dev@jaxb.java.net

_at_XmlElement on overridden methods

From: chris cottingham <chris_at_graphicpoet.com>
Date: Fri, 22 Aug 2008 01:22:44 -0400

So, I'm thinking that the element name used to serialize a property
should be determined by the annotation of the most-derived class's getter.

The example I have is related to a javax.swing.TreeNode implementation.
It would be nice to parameterize a tree node T with the assumption that
its children are of <? extends T>. Assuming a pair of classes something
like below.

To summarize the intent of these classes, basically, I have two classes,
one extends the other and the getter of one of the properties overrides
the other. Both getters assign @XmlElement(name="whatever").

@XmlAccessorType(XmlAccessType.PROPERTY)
public class TreeNodeImpl<T extends TreeNode> implements TreeNode {

    protected ArrayList<T> children = new ArrayList<T>();
    protected String name;

    public TreeNodeImpl() {}

    public TreeNodeImpl(String name) {
       this.name = name;
    }

    @XmlElement(name="child")
    public ArrayList<T> getChildren(){
        return children;
    }

    public void addChild(T child) {
       children.add(child);
    }

    public String getName(){
       return name;
    }

//etc

}

public class Derived extends TreeNodeImpl<Derived> {

    public Derived(){}

    public Derived(String name) {
       super(name);
    }

    public Derived(String name, Derived parent) {
       super(name);

       parent.add(this);
    }

    @Override
    @XmlElement(name="derived")
    public ArrayList<Derived> getChildren(){
       return chuldren;
    }

}


If I create a new instance of Derived and feed it to the Marshaller, I
get a xml serialization of both element of name "child" as well as
element of name "derived". Basically, I think it's intuitive to only
include the Derived instance. So, for the code side:

public static void main(...) {
    Derived root = new Derived("root");
    new Derived("child",root);

    //setup JAXBContext

    marshaller.marshal(root, System.out.pruntln);
}

I get something like

<derived>
    <child name="root" xsi:type="derived" xmlns:xsi="..."/>
    <derived name="child/>
</derived>

So, I'm thinking that only the outermost @XmlElement annotated property
should be serialized. Reasonable?


Also, I don't know who all's doing the coding on JAXB. I see
Kohsuke(spelling?) on just about everything. What is involved in
becoming a committer? While the project has a huge amount that it's
already providing, I expect that there is a user community out there
wanting things and think I may be inclined to get involved.

-Chris Cottingham