Skip to content

JAXB2 ToString Plugin

Laurent Schoelens edited this page Nov 13, 2023 · 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 (for JAXB2) or org.jvnet.jaxb.lang.ToString (for JAXB3+) 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 for JAXB2, org.jvnet.jaxb.lang.JAXBToStringStrategy is used by default for JAXB3+).

See Using JAXB2 Basics Plugins.

Clone this wiki locally