users@jaxb.java.net

Writing an XJC plugin to stabilize generated Java code

From: David Easley <easleydp_at_gmail.com>
Date: Sun, 22 Mar 2009 21:33:29 +0000

Hi,

Can anyone give me any pointers on how to write an XJC plugin to do the
following:
XJC outputs certain code lines in a random order from one build to the next
(see examples below). I want the code to be generated the same each time (as
long as the source schema hasn't changed) because the randomness leads to a
lot of noise in my version control history. (I'm storing the generated Java
files under SCM because (i) it's a useful way to see what impact schema
changes have had on the generated Java code, and (ii) some of our generated
classes contain 'simple-preserve' blocks).

All the example plugins I've looked at generate additional code to the
output file. What I want to do is alter the way the standard code is output
(by sorting the collections).

Here are some examples showing where the randomness crops up:

(1) @XmlElement lines are output in a random order:

        @XmlElements({
            @XmlElement(name = "timeCriterion", type =
TimeCriterionDto.class),
            @XmlElement(name = "dateCriterion", type =
DateCriterionDto.class),
            @XmlElement(name = "stringCriterion", type =
StringCriterionDto.class),
            @XmlElement(name = "decimalCriterion", type =
DecimalCriterionDto.class),
            @XmlElement(name = "integerCriterion", type =
IntegerCriterionDto.class)
        })

(2) @link lines in comments are output in a random order:

        /**
         ....
         * Objects of the following type(s) are allowed in the list
         * {_at_link TimeCriterionDto }
         * {_at_link DateCriterionDto }
         * {_at_link StringCriterionDto }
         * {_at_link DecimalCriterionDto }
         * {_at_link IntegerCriterionDto }
         ....
         */

Note: I've raised an issue on this randomness (which I'm guessing is due to
the use of Sets, in which case the fix may be as simple as switching from
(e.g.) HashSet to LinkedHashSet):
  https://jaxb.dev.java.net/issues/show_bug.cgi?id=618
But I'm guessing this won't be seen as a priority, hence my need of a
plugin.
Or, if I was to fix this in core XJC and submit a patch, how soon do you
think the patch might be accepted and merged into trunk (I wouldn't want to
stay on a custom fork for too long)?

David