Skip to content

JAXB2 Basics Plugins List

Laurent Schoelens edited this page Jan 22, 2024 · 3 revisions

Introduction

Schema compiler (XJC) produces schema-derived classes which can be used to turn XML into object structures and back. However, generated classes lack by default lack convenience and utility methods like equals(...), hashCode(...), toString() and so on.

JAXB2 Basics provides a package of plugins which can generate such utility code

Plugins

Custom strategues

Most utility methods generated by JAXB2 Basics plugins come in two flavours : default (like equals(that)) and with additional locator and strategy parameters (like equals(thisLocator, thatLocator, that, strategy)).

This gives you a possibility to provide a custom strategy which will be used for object comparison, hash code calculation, content copying and so on.

Implemented interfaces

When generating methods, plugins also make schema-derived classes implement corresponding interfaces from the runtime package. For instance, if you use the Equals Plugin, your classes will implement the org/jvnet/jaxb2_commons/lang/Equals interface.

No Reflection

In order to access data from objects, generated code uses getters, setters and fields. For performance reasons, no reflection is used.

Excluding properties

In some cases you may need to exclude certain properties from calculations in utility methods. You may use binding customizations to do this :

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
  jaxb:extensionBindingPrefixes="basic copyable equals hashCode mergeable toString"
  xmlns:basic="http://jaxb2-commons.dev.java.net/basic"
  xmlns:copyable="http://jaxb2-commons.dev.java.net/basic/copyable"
  xmlns:equals="http://jaxb2-commons.dev.java.net/basic/equals"
  xmlns:hashCode="http://jaxb2-commons.dev.java.net/basic/hashCode"
  xmlns:mergeable="http://jaxb2-commons.dev.java.net/basic/mergeable"
  xmlns:toString="http://jaxb2-commons.dev.java.net/basic/toString">
  <xs:complexType name="copyable">
    <xs:sequence>
      <xs:element name="notIgnored" type="xs:string"/>
      <xs:element name="ignored" type="xs:string">
        <xs:annotation>
          <xs:appinfo>
            <basic:ignored/>
          </xs:appinfo>
        </xs:annotation>
      </xs:element>
      <xs:element name="alsoIgnored" type="xs:string">
        <xs:annotation>
          <xs:appinfo>
            <copyable:ignored/>
          </xs:appinfo>
        </xs:annotation>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
  <!-- ... -->
</xs:schema>

As you may guess, basic:ignored marks the field to be ignored for all the plugins, copyable:ignored - only for the copyable plugin.

Clone this wiki locally