CC-XJC

Introduction

CC-XJC is a JAXB 3.0 XJC plugin for adding a copy constructor to schema derived classes. The plugin provides a '-copy-constructor' option which is enabled by adding its jar file to the XJC classpath.

See the examples for further information on how to enable it.

When enabled, the options listed below can be used to control the behavior of the plugin.

CC-XJC 3.x can only be used with JAXB/XJC 3.0 and higher and Java 11 and higher. For use with older version please refer to old versions of the plugin.

Support

Development of CC-XJC is community driven. Please file any issues with the bugtracker.

Options

-cc-visibility

The '-cc-visibility' option can be used to specify the visibility of generated copy methods. It takes one argument from the list [private, package, protected, public]. This option impacts the number of generated methods. Default: private.

-cc-target

The '-cc-target' option can be used to specify a target JDK version for the generated code. It takes one argument from the list [1.5, 1.6, 1.7]. Default: 1.5.

-cc-nullable (since 1.1)

The '-cc-nullable' option got introduced in version 1.1. It can be used to instruct the plugin to allow passing 'null' references to copy constructors as it was the default in versions prior to 1.1. Starting with version 1.1, that default has been changed to throw a 'NullPointerException' for any 'null' references passed to copy constructors.

-cc-hierarchical (since 1.2)

The '-cc-hierarchical' option got introduced in version 1.2. It can be used to instruct the plugin to generate hierarchical copy constructors - copy constructors supporting copying from super types.

Example:

        <xsd:complexType name="Root"/>

        <xsd:complexType name="Parent">
          <xsd:complexContext>
            <xsd:extension base="Root"/>
          </xsd:complexContext>
        </xsd:complexType>

        <xsd:complexType name="Child">
          <xsd:complexContext>
            <xsd:extension base="Parent"/>
          </xsd:complexContext>
        </xsd:complexType>

Without the '-cc-hierarchical' option, the plugin will generate the following copy constructors (compatible with versions 1.0.x):

        public Root(final Root o)
        public Parent(final Parent o)
        public Child(final Child o)

Specifying the '-cc-hierarchical' option, the plugin will generate the following copy constructors allowing to create copies of super types.

        public Root(final Root o)
        public Parent(final Root o)
        public Child(final Root o)

Note: Using the '-cc-hierarchical' option has the drawback of losing type safety. There will be no compile errors and no runtime errors when accidentally passing the wrong instance to such a constructor. If you plan to use the '-cc-hierarchical' option, you may want to verify your code by regenerating your classes without that option and by inspecting any compile errors this produces to verify the instance passed to the copy constructor is the instance you intended to copy.

-cc-cloneable-types (since 1.3)

The '-cc-cloneable-types' option got introduced in version 1.3. It can be used to specify a list of class names of cloneable classes to support separated by ':'. The plugin adds the following types to that list by default.

  • java.util.Date
  • java.util.Calendar
  • java.util.TimeZone
  • java.util.Locale
  • javax.xml.datatype.XMLGregorianCalendar

As of version 1.5, entries starting with an '@' character are interpreted as a name of a file holding one type name per line.

-cc-immutable-types (since 1.3)

The '-cc-immutable-types' option got introduced in version 1.3. It can be used to specify a list of class names of immutable classes to support separated by ':'. The plugin adds the following types to that list by default.

  • java.lang.Boolean
  • java.lang.Byte
  • java.lang.Character
  • java.lang.Double
  • java.lang.Enum
  • java.lang.Float
  • java.lang.Integer
  • java.lang.Long
  • java.lang.Short
  • java.lang.String
  • java.math.BigDecimal
  • java.math.BigInteger
  • java.util.Currency
  • java.util.UUID
  • javax.xml.namespace.QName
  • javax.xml.datatype.Duration

As of version 1.5, entries starting with an '@' character are interpreted as a name of a file holding one type name per line.

-cc-string-types (since 1.5)

The '-cc-string-types' option got introduced in version 1.5. It can be used to specify a list of class names of string based datatype classes to support separated by ':'. Entries starting with an '@' character are interpreted as a name of a file holding one type name per line. A string based datatype class is a class which provides a public constructor taking a single string parameter which can be used to create a new instance of that type using the value returned by that type's toString method. The plugin adds the following types to that list by default.

  • java.io.File
  • java.net.URI
  • java.net.URL
  • jakarta.activation.MimeType

-cc-null-collection-elements (since 3.0.0)

The '-cc-null-collection-elements' option got introduced in version 3.0.0. It can be used to allow the copy of 'null'-elements contained in collections that are copied in the constructor. Before version 3.0.0 'null'-elements would be dropped silently when copying/cloning collections.