users@jaxb.java.net

Re: Comparing two jaxb-generated objects by value

From: Aleksei Valikov <valikov_at_gmx.net>
Date: Fri, 14 Jan 2005 13:59:57 +0100

Hi.

> this question has been posted 2 years ago under
> the subject "JAXB and equals" with no solution
> at that time
> is it possible now with the last JAXB version to
> compare two jaxb-generated objects by value ?

Since I needed to compare JAXB objects by value in my current project,
I've implemented a small add-on to generate equals(...) and hashCode()
methods.
For the time being, I've included this add-on into the jaxb-verification
(https://jaxb-verification.dev.java.net). I'm not sure, may be I'll move
it to HyperJAXB since Hibernate also requires well-implemented
equals(...) and hashCode() methods.

No documentation yet, sorry. No thorough test also. The add-on is far
from the release state (as I see release states), but is (hopefully)
functional and usable.

Usage:

     <xjc target="${generated.sources}">
       <arg line="-nv"/>
       <arg line="-extension"/>
       <arg line="-Xverification"/>
       <arg line="-Xequality"/>
       <binding dir="${basedir}">
         <include name="binding/*.xml"/>
       </binding>
       <schema dir="${basedir}">
         <include name="schema/*.xsd"/>
       </schema>
     </xjc>

You'll need hyperjaxb.jar and jaxb-verification.jar on the classpath.

The generated code looks as follows:

     public boolean equals(java.lang.Object obj) {
         if (this == obj) {
             return true;
         }
         if ((null == obj)||(obj.getClass()!= this.getClass())) {
             return false;
         }
         de.fzi.dbs.verification.tests.one.impl.RootImpl target =
((de.fzi.dbs.verification.tests.one.impl.RootImpl) obj);
         {
             de.fzi.dbs.verification.tests.one.SimpleTypesType value =
this.getSimpleTypes();
             de.fzi.dbs.verification.tests.one.SimpleTypesType
targetValue = target.getSimpleTypes();
             if (!((value == targetValue)||((value!=
null)&&value.equals(targetValue)))) {
                 return false;
             }
         }
         {
             de.fzi.dbs.verification.tests.one.SequenceType value =
this.getSequence();
             de.fzi.dbs.verification.tests.one.SequenceType targetValue
= target.getSequence();
             if (!((value == targetValue)||((value!=
null)&&value.equals(targetValue)))) {
                 return false;
             }
         }
         {
             de.fzi.dbs.verification.tests.one.ElementWithListAttribute1
value = this.getElementWithListAttribute1();
             de.fzi.dbs.verification.tests.one.ElementWithListAttribute1
targetValue = target.getElementWithListAttribute1();
             if (!((value == targetValue)||((value!=
null)&&value.equals(targetValue)))) {
                 return false;
             }
         }
         {
             de.fzi.dbs.verification.tests.one.FacetType value =
this.getFacet();
             de.fzi.dbs.verification.tests.one.FacetType targetValue =
target.getFacet();
             if (!((value == targetValue)||((value!=
null)&&value.equals(targetValue)))) {
                 return false;
             }
         }
         return true;
     }

     public int hashCode() {
         int hash = 7;
         {
             de.fzi.dbs.verification.tests.one.SimpleTypesType value =
this.getSimpleTypes();
             hash = ((31 *hash)+((null == value)? 0 :value.hashCode()));
         }
         {
             de.fzi.dbs.verification.tests.one.SequenceType value =
this.getSequence();
             hash = ((31 *hash)+((null == value)? 0 :value.hashCode()));
         }
         {
             de.fzi.dbs.verification.tests.one.ElementWithListAttribute1
value = this.getElementWithListAttribute1();
             hash = ((31 *hash)+((null == value)? 0 :value.hashCode()));
         }
         {
             de.fzi.dbs.verification.tests.one.FacetType value =
this.getFacet();
             hash = ((31 *hash)+((null == value)? 0 :value.hashCode()));
         }
         return hash;
     }

Bye.
/lexi

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
For additional commands, e-mail: users-help_at_jaxb.dev.java.net