Skip to content

JAXB2 ToString Plugin

maffe edited this page Sep 3, 2019 · 2 revisions

Adds reflection-free strategy-based toString(...) methods to the schema-derived classes.

Generated classes will implement the org.jvnet.jaxb2_commons.lang.ToString2 interface which, in addition to toString declares append and appendFields methods. Below is an example:

public class SequenceType implements ToString2
{
    // ...
    public String toString() {
        final ToStringStrategy2 strategy = JAXBToStringStrategy.INSTANCE;
        final StringBuilder buffer = new StringBuilder();
        append(null, buffer, strategy);
        return buffer.toString();
    }

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

    public StringBuilder appendFields(ObjectLocator locator, StringBuilder buffer, ToStringStrategy2 strategy) {
        {
            String theA;
            theA = this.getA();
            strategy.appendField(locator, this, "a", buffer, theA, this.isSetA());
        }
        {
            Long theB;
            theB = this.getB();
            strategy.appendField(locator, this, "b", buffer, theB, this.isSetB());
        }
        return buffer;
    }
}

Usage

  • Activate the plugin using the -XtoString switch.
  • Optionally provide custom strategy class using -XtoString-toStringStrategyClass=com.acme.foo.MyToStringStrategy (org.jvnet.jaxb2_commons.lang.JAXBToStringStrategy is used by default).

See Using JAXB2 Basics Plugins.