JAXB 2.1 Generating Primitive Type Getters

We’ve been using JAXB generated classes at work for quite some time without problem. Recently it became necessary to make them serializable so we could stow certain objects safely in the HttpSession for later retrieval. So a quick look around on the web revealed the following example in the Sun docs showing how to use xjc’s extension mechanism to force generated classes to implement Serializable. All good. Except as it turns out the Sun docs included the generateIsSetMethod attribute on the <jaxb:globalBindings> element: <jaxb:globalBindings generateIsSetMethod="true">. Originally I included this attribute in my external bindings file that I passed to XJC, thinking I should follow the Sun example, but it soon became clear that this wasn’t the way to go: when I swapped the newly generated classes with our previous version a colleague spotted some of our code complaining about primitive types being returned from the Getters in the generated classes.

By including the generateIsSetMethod attribute in the bindings file, XJC produced the following generated code:

    protected Short number;
 
    public short getNumber() {
        return number;
    }
 
    public boolean isSetNumber() {
        return (this.number!= null);
    }
 
    public void unsetNumber() {
        this.number = null;
    }

You’ll notice the getter is returning the primitive type short rather than object type Short. What you also get is the two additional methods isSetNumber() and unsetNumber(). If this behaviour isn’t desirable then the generateIsSetMethod attribute needs removing from the bindings file. This will result in generated class with object type getters and none of the other unneeded methods.

    protected Short number;
 
    public Short getNumber() {
        return number;
    }

It seems the default settings for generated classes will produce object getters rather than primitive, but a byproduct of including the isSet method is that the getter will return a primitive type. I totally appreciate this might suit some requirements, but some of you out there might be battling against this, I hope this post helps!

I couldn’t have solved this without the suffering of others in this Nabble forum thread. And thanks to this post on java.net which helped me get the XJC binding file to behave.

Share

3 thoughts on “JAXB 2.1 Generating Primitive Type Getters”

  1. Very nice post!
    Anyway there is no problem if xjc was able to generate code like that

    protected short number;

    public short getNumber() {
    return number;
    }

    public void unsetNumber() {
    this.number = null;
    }

    Is it possible?

  2. sorry without
    public void unsetNumber() {
    this.number = null;
    }

    but with

    public void setNumber(short number) {
    this.number = number;
    }

    my cut & past fault

    Regards

  3. Alberto,

    xjc will create the setter in both cases, I just excluded it from my examples.
    Object Type:
    public void setNumber(Short number) {
    this.number = number;
    }

    primitive:
    public void setNumber(short number) {
    this.number = number;
    }

    obviously with the former, you could call setNumber(null)

Leave a Reply

Your email address will not be published. Required fields are marked *