Skip to main content

JAXB and custom toString "strategy"

Please note these forums are being decommissioned and use the new and improved forums at
1 reply [Last post]
Joined: 2011-03-13

About JAXB and its toString strategy:

say, xjc has generated this code for me:

public class PurchaseOrderType
    implements ToString //, ...

    // ...   

    public String toString() {
        final ToStringStrategy strategy = JAXBToStringStrategy.INSTANCE;
        final StringBuilder buffer = new StringBuilder();
        append(null, buffer, strategy);
        return buffer.toString();

    public StringBuilder append(ObjectLocator locator, StringBuilder buffer, ToStringStrategy strategy) {
        strategy.appendStart(locator, this, buffer);
        appendFields(locator, buffer, strategy);
        strategy.appendEnd(locator, this, buffer);
        return buffer;

    public StringBuilder appendFields(ObjectLocator locator, StringBuilder buffer, ToStringStrategy strategy) {
            USAddress theShipTo;
            theShipTo = this.getShipTo();
            strategy.appendField(locator, this, "shipTo", buffer, theShipTo);
            USAddress theBillTo;
            theBillTo = this.getBillTo();
            strategy.appendField(locator, this, "billTo", buffer, theBillTo);
        // ...
        return buffer;

    // ...

Now I need the toString method to do something special for me.
Say, returning a name in all uppercase or so.
They say, I have to write a custom class for that and somehow "plug" that into above code, but without altering above code.
What would that new class look like ? and how do I do the plugging in ?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2003-09-30


The following link provides instructions on writing an XJC plug-in. It may help you out with what you are trying to do: