2015-11-07

Generating Java classes with the Java 8 date and time API types by the JAXB

The JAXB's XJC is very popular tool for generating Java classes from a specification files (XSD or WSDL). Java 8 introduced new Date and Time API. Unfortunately this API does not come with (un)marshaller so XJC still generates code using XMLGregorianCalendar type by default.

Because I want to work with the LocalDateTime I had to find and link (un)marshaller for the new API to my project. I decided to use Mikhail Sokolov's adapters.

Then I had to tell XJC that it should generate code with the LocalDateTime for the specification's date-time types. This can be achieved by a binding file. So create a new binding.xjb file and put mapping of xsd:dateTime to LocalDateTime type by using LocalDateTimeXmlAdapter adapter/(un)marshaller.

This binding file uses nonstandard adapter parameter so you need to add -extension switch to be able to execute XJC. Whole command should look like this: xjc -d <target_directory> -b binding.xjb -extension <specification_file.xsd>

The generated code is pretty ok except for one small thing. For initialisation of generic types the generated code uses verbose form instead of diamond operator. IntelliJ IDEA's static code analysis complains about it. To fix this I decided to post-process the code by regular expression and replace all verbose initialisations by the shorter diamond.

Whole process of the generating and the post-processing can be put into a shell script generate.sh for easy use.