Skip to content

JAXB2 Copyable Plugin

Laurent Schoelens edited this page Nov 13, 2023 · 2 revisions

Copyable plugin

Copyable plugin generates methods for content copying:

public class PurchaseOrderType
    implements Cloneable, CopyTo //, ...
 
    // ...   
 
    public Object clone() {
        return copyTo(createNewInstance());
    }
 
    public Object copyTo(Object target) {
        final CopyStrategy strategy = JAXBCopyStrategy.INSTANCE;
        return copyTo(null, target, strategy);
    }
 
    public Object copyTo(ObjectLocator locator, Object target, CopyStrategy strategy) {
        final Object draftCopy = ((target == null)?createNewInstance():target);
        if (draftCopy instanceof PurchaseOrderType) {
            final PurchaseOrderType copy = ((PurchaseOrderType) draftCopy);
            if (this.shipTo!= null) {
                USAddress sourceShipTo;
                sourceShipTo = this.getShipTo();
                USAddress copyShipTo = ((USAddress) strategy.copy(LocatorUtils.property(locator, "shipTo", sourceShipTo), sourceShipTo));
                copy.setShipTo(copyShipTo);
            } else {
                copy.shipTo = null;
            }
            if (this.billTo!= null) {
                USAddress sourceBillTo;
                sourceBillTo = this.getBillTo();
                USAddress copyBillTo = ((USAddress) strategy.copy(LocatorUtils.property(locator, "billTo", sourceBillTo), sourceBillTo));
                copy.setBillTo(copyBillTo);
            } else {
                copy.billTo = null;
            }
            // ...
        }
        return draftCopy;
    }
 
    public Object createNewInstance() {
        return new PurchaseOrderType();
    }
 
    // ...
}

Generated methods allow you to specify a custom copy creation strategy (copy strategy).

Activation

Use the -Xcopyable argument to activate the plugin.

Configuration

By default, generated code uses :

  • org.jvnet.jaxb2_commons.lang.JAXBCopyStrategy as copy strategy for JAXB 2 (javax namespace).
  • org.jvnet.jaxb.lang.JAXBCopyStrategy as copy strategy for JAXB 3+ (jakarta namespace).

You can specify class name of your custom strategy using the -Xcopyable-copyStrategyClass argument:

-Xcopyable-copyStrategyClass=com.acme.foo.CustomCopyStrategy

Clone this wiki locally