0

I am using CXF JAXB generated classes in a GWT application. I would like equals() and hashCode() to be added to the generated classes. I have successfully used the JAXB2 Basics Plugins (http://confluence.highsource.org/display/J2B/JAXB2+Basics+Plugins) and the JAXB2 Basics Runtime with CXF in the maven POM to generate them. However, the generated classes are now incompatible with GWT. The classes have dependencies which are not emulated by GWT, e.g., java.util.ResourceBundle, java.net.URL.

I am considering using a CustomEqualsStrategy but that will still leave the org.jvnet.jaxb2_commons.locator.ObjectLocator class. Has anyone got this to work?

lexicore
  • 42,748
  • 17
  • 132
  • 221
ja6a
  • 416
  • 1
  • 6
  • 10
  • Sure. I'm the author. Could you specify what the exact problem with GWT was? – lexicore Nov 26 '14 at 09:47
  • All the jaxb2_common classes are listed as these: (I can't print them all) `[ERROR] Line xxx: No source code is available for type org.jvnet.jaxb2_commons.locator.ObjectLocator; did you forget to inherit a required module?` `[ERROR] Line xxx: No source code is available for type org.jvnet.jaxb2_commons.lang.JAXBEqualsStrategy; did you forget to inherit a required module?` `[ERROR] Line xxx: No source code is available for type org.jvnet.jaxb2_commons.locator.util.LocatorUtils; did you forget to inherit a required module?` – ja6a Nov 26 '14 at 10:08
  • The message `did you forget to inherit a required module?` means GWT can't find the source code. We can include the source for ObjectLocator but this will never work because ObjectLocator depends upon parts of Java which GWT does not emulate. One solution could be to develop a version of ObjectLocator which restricts itself to GWT emulated classes. E.g., GWT is single threaded so that could help simplify things. – ja6a Nov 26 '14 at 10:19
  • Add it to your question. What parts of Java does `ObjectLocator` depend on that GWT does not emulate, exactly? Can your produce a unit test that I could run? – lexicore Nov 26 '14 at 11:44
  • 1
    I got it to work. I downloaded the JAXB2_basics source. I made jaxb2-basics-runtime into a GWT module. I modified ObjectLocator by removing ValidationEventLocator. I modified Reportable by removing the reference to java.util.ResourceBundle. I then included XMLConstants, JAXBElement, QName, and Locator from the JDK - I used super-source in the GWT module for this. I used this modified runtime with CXF. Now the GWT can compile and I can use equals(). I wonder why no-one has done this before? Would you consider publishing these changes for a GWT version? – ja6a Nov 26 '14 at 14:23
  • 1
    Cool. Yes, of course, let's add a runtime-gwt or something like that. [The project is on GitHub](https://github.com/highsource/jaxb2-basics/tree/master/runtime), the easiest would be if you send me a PR or a ZIP file with your files. I'll integrate it. Thank you! – lexicore Nov 26 '14 at 14:26

1 Answers1

2

This question has an answer now.

I have collaborated with James (the OP, @ja6a) and together we have developed the JAXB2 SimpleEquals Plugin and JAXB2 SimpleHashCode Plugin which generate runtime-free reflection-free equals(...) and hashCode() methods. As there's no additional runtime dependencies, this is compatible with GWT.

Examples of the generated code:

Usage:

Below is a snippet from a sample pom.xml:

        <plugin>
            <groupId>org.jvnet.jaxb2.maven2</groupId>
            <artifactId>maven-jaxb2-plugin</artifactId>
            <configuration>
                <extension>true</extension>
                <args>
                    <arg>-XsimpleEquals</arg>
                    <arg>-XsimpleHashCode</arg>
                </args>
                <plugins>
                    <plugin>
                        <groupId>org.jvnet.jaxb2_commons</groupId>
                        <artifactId>jaxb2-basics</artifactId>
                    </plugin>
                </plugins>
            </configuration>
        </plugin>

As mentioned above, no runtime required.

These plugins handle a huge number of cases and corner cases and corners of corners cases. For instance, we had to implement special handling for things like JAXBElements and arrays as they don't implement hashCode() and equals(...) methods. The plugins also handle primitive types.

Many thanks for James Annesley for his help.

lexicore
  • 42,748
  • 17
  • 132
  • 221