Add toString, hashCode, equals while generating JAXB classes in Java

Arka Ghosh picture Arka Ghosh · Sep 1, 2015 · Viewed 16.1k times · Source

I'm trying to generate JAXB classes from an XSD file programmatically, using Java. I've used the following code snippet to achieve that:

....
import java.io.File;
import java.io.IOException;
import org.xml.sax.InputSource;
import com.sun.codemodel.JCodeModel;
import com.sun.tools.xjc.api.S2JJAXBModel;
import com.sun.tools.xjc.api.SchemaCompiler;
import com.sun.tools.xjc.api.XJC;
....
....
public static void generateJaxb(String schemaPath,
                                    String outputDirectory,
                                        String packageName) throws DataLoadingException
{
    try {
        // Setup schema compiler
        SchemaCompiler sc = XJC.createSchemaCompiler();
        sc.forcePackageName(packageName);

        // Setup SAX InputSource
        File schemaFile = new File(schemaPath);
        InputSource is = new InputSource(schemaFile.toURI().toString());

        // Parse & build
        sc.parseSchema(is);
        S2JJAXBModel model = sc.bind();

        JCodeModel jCodeModel = model.generateCode(null, null);
        jCodeModel.build(new File(outputDirectory));
    } catch (IOException exec) {
        LOGGER.error("Error while generating JAXB classes: " + exec);
    }
}

The generated classes contain only the getter methods for the fields. But, I want to include the hashCode, equals and setter methods as well. How to do that while generating the code?

Answer

Sean Mickey picture Sean Mickey · Sep 1, 2015

On the GitHub website, you will find the JAXB2 Basics project, which provides a common set of JAXB utility plugins, including 4 that should address what you are trying to achieve:

  1. Equals Plugin
  2. HashCode Plugin
  3. Setters Plugin
  4. ToString Plugin

There are other plugins available that cover similar common aspects of Java domain objects.

Configuration

From an XML Schema configuration perspective, you will add references as shown here:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:basic="http://jaxb2-commons.dev.java.net/basic"
    xmlns:equals="http://jaxb2-commons.dev.java.net/basic/equals"
    xmlns:hashCode="http://jaxb2-commons.dev.java.net/basic/hashCode"
    xmlns:toString="http://jaxb2-commons.dev.java.net/basic/toString"
    jaxb:extensionBindingPrefixes="basic equals hashCode toString">
    <!-- ... -->
</xs:schema>

There are additional options available, such as defining object properties that should be ignored when generating an equals( that ) implementation, a toString() implementation, etc.

Java Code Generation

From a Java perspective, the plugins generally have the generated classes implement an interface; as an example, generated classes that include an equals( that ) implementation will implement the [Equals][6] interface.

The design approach used by the plugins usually generates 2 flavors of implementation:

  1. A simple/standard implementation, such as an equals( that ) method (when using the Equals Plugin).
  2. A more complex implementation that includes locator and strategy parameters, which allows you to implement custom handling (if you wish). For these, you will see a method signature such as: equals( thisLocator, thatLocator, that, strategy).

Build/Runtime

From a runtime perspective, you must include the JAXB2 Basics Runtime jar and provide option parameters such as: -Xequals, -XhashCode, or -XtoString. There are examples provided for using the JAXB2 Basics from Ant and Maven, if you are using either of those to perform builds and more build-related details are provided in the JAXB2 Basics User Guide.