Add toString, hashCode, equal when generating JAXB classes in Java

I am trying to generate JAXB classes from an XSD file programmatically using Java. For this, I used the following code snippet:

.... 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); } } 

Generated classes contain only getter methods for fields. But I want to include the hashCode , equals and setter methods. How to do this when creating code?

+10
java jaxb xjc jaxb2-basics
Sep 01 '15 at 14:33
source share
2 answers

On the Java.net website, you will find the JAXB 2.x Commons project , which contains a common set of JAXB plugins, including 4 to decide what you are trying to achieve:

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

Configuration

In terms of XML Schema configuration, you will add links as shown below:

 <?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> 

Additional options are available, such as defining object properties that should be ignored when creating an equals( that ) implementation, toString() implementation, etc.

Java code generation

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

The constructive approach used by plugins usually generates 2 implementation options:

  • A simple / standard implementation, such as the equals( that ) method (when using Equals Plugin ).
  • A more complex implementation, which includes the locator and strategy parameters, which allows you to implement custom processing (if you want). To do this, you will see the signature of the method, for example: equals( thisLocator, thatLocator, that, strategy) .

Build / Play Time

In terms of runtime, you should enable ja JAXB2 Basics Runtime and provide parameter options such as: -Xequals , -XhashCode or -XtoString . Examples of using JAXB2 Basics from Ant and Maven are given in the examples, if you use any of them to perform assemblies, and more detailed information about the assembly is given in the JAXB2 Basics User Guide .

+10
Sep 01 '15 at 16:43
source share

Refresh The following is an incorrect answer. I was misled by the interface, generateCode really does nothing with the plugins at the moment. As @Sidola noted, SchemaCompiler should be used SchemaCompiler .

In addition to @SeanMickey's answer, I will refer to code generation.

  • Add the JA JAXB2-Basics JAR to your class path.
  • Instantiate
    • org.jvnet.jaxb2_commons.plugin.tostring.ToStringPlugin
    • org.jvnet.jaxb2_commons.plugin.equals.EqualsPlugin
    • org.jvnet.jaxb2_commons.plugin.hashcode.HashCodePlugin
    • org.jvnet.jaxb2_commons.plugin.setters.SettersPlugin
  • ... or what you need.
  • Transfer plugins to model.generateCode(plugins errorListener) as the first parameter.

By the way, why are you programmatically generating code?

+4
Sep 01 '15 at
source share



All Articles