是否可以为使用jaxb生成的类生成equals和compareTo方法

时间:2015-01-13 10:54:22

标签: java jaxb equals

是否可以为使用jaxb生成的类生成equals和compareTo方法,我使用jaxb从模式生成类。这些类实际上有guid,允许它们被唯一标识,但是我如何实现equals / compare方法,以便Set等集合类可以识别同一实体的重复实例?

2 个答案:

答案 0 :(得分:4)

好的,这是另一种方法。

您可以使用-XcodeInjector插件添加hashCodeequals方法。

看到这个问题:

  

Inserting code with XJC+xsd+jxb using the options " -Xinject-code -extension "

类似的东西:

<jxb:bindings schemaLocation="schema.xsd">
    <jxb:bindings node="/xs:schema/xs:complexType[@name='MyItemType']">
        <ci:code>
            @Override
            public int hashCode() { return guid == null? 0 : guid.hashCode();}
        </ci:code>
    </jxb:bindings>
</jxb:bindings>

如果这还不够好,请考虑filing an issue in JAXB2-Basics(&#34;允许选择hashCode / equals&#34的属性;)或实现自己的插件。

答案 1 :(得分:2)

免责声明:我是jaxb2-basics的作者,它提供了能够生成hashCodeequals方法的JAXB2插件。

Maven的使用示例:

        <plugin>
            <groupId>org.jvnet.jaxb2.maven2</groupId>
            <artifactId>maven-jaxb2-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <extension>true</extension>
                <args>
                    <arg>-Xequals</arg>
                    <arg>-XhashCode</arg>
                </args>
                <plugins>
                    <plugin>
                        <groupId>org.jvnet.jaxb2_commons</groupId>
                        <artifactId>jaxb2-basics</artifactId>
                        <version>...</version>
                    </plugin>
                </plugins>
            </configuration>
        </plugin>

(请参阅documentation了解Ant。)

您可以使用生成无运行时hashCodeequals方法的-XsimpleHashCode-XsimpleEquals(内联哈希码或等于计算)或-XhashCode / -Xequals生成&#34;战略&#34; hashCodeequals方法(哈希代码/等于计算被委托给传递的策略方法)。

这是-XsimpleHashCode生成的内容:

public class Customer {

    ...

    public int hashCode() {
        int currentHashCode = 1;
        {
            currentHashCode = (currentHashCode* 31);
            String theAddress;
            theAddress = this.getAddress();
            if (theAddress!= null) {
                currentHashCode += theAddress.hashCode();
            }
        }
        {
            currentHashCode = (currentHashCode* 31);
            Boolean theBlueEyes;
            theBlueEyes = this.isBlueEyes();
            if (theBlueEyes!= null) {
                currentHashCode += theBlueEyes.hashCode();
            }
        }
        {
            currentHashCode = (currentHashCode* 31);
            String theFamilyName;
            theFamilyName = this.getFamilyName();
            if (theFamilyName!= null) {
                currentHashCode += theFamilyName.hashCode();
            }
        }
        {
            currentHashCode = (currentHashCode* 31);
            String theGivenName;
            theGivenName = this.getGivenName();
            if (theGivenName!= null) {
                currentHashCode += theGivenName.hashCode();
            }
        }
        {
            currentHashCode = (currentHashCode* 31);
            List<String> theMiddleInitials;
            theMiddleInitials = (this.isSetMiddleInitials()?this.getMiddleInitials():null);
            if (theMiddleInitials!= null) {
                currentHashCode += theMiddleInitials.hashCode();
            }
        }
        {
            currentHashCode = (currentHashCode* 31);
            String thePostCode;
            thePostCode = this.getPostCode();
            if (thePostCode!= null) {
                currentHashCode += thePostCode.hashCode();
            }
        }
        {
            currentHashCode = (currentHashCode* 31);
            boolean theSingle;
            theSingle = this.isSingle();
            currentHashCode += (theSingle? 1231 : 1237);
        }
        return currentHashCode;
    }

}

这是-XhashCode生成的内容:

public class Customer implements HashCode
{

    ...

    public int hashCode(ObjectLocator locator, HashCodeStrategy strategy) {
        int currentHashCode = 1;
        {
            String theAddress;
            theAddress = this.getAddress();
            currentHashCode = strategy.hashCode(LocatorUtils.property(locator, "address", theAddress), currentHashCode, theAddress);
        }
        {
            Boolean theBlueEyes;
            theBlueEyes = this.isBlueEyes();
            currentHashCode = strategy.hashCode(LocatorUtils.property(locator, "blueEyes", theBlueEyes), currentHashCode, theBlueEyes);
        }
        {
            String theFamilyName;
            theFamilyName = this.getFamilyName();
            currentHashCode = strategy.hashCode(LocatorUtils.property(locator, "familyName", theFamilyName), currentHashCode, theFamilyName);
        }
        {
            String theGivenName;
            theGivenName = this.getGivenName();
            currentHashCode = strategy.hashCode(LocatorUtils.property(locator, "givenName", theGivenName), currentHashCode, theGivenName);
        }
        {
            List<String> theMiddleInitials;
            theMiddleInitials = (this.isSetMiddleInitials()?this.getMiddleInitials():null);
            currentHashCode = strategy.hashCode(LocatorUtils.property(locator, "middleInitials", theMiddleInitials), currentHashCode, theMiddleInitials);
        }
        {
            String thePostCode;
            thePostCode = this.getPostCode();
            currentHashCode = strategy.hashCode(LocatorUtils.property(locator, "postCode", thePostCode), currentHashCode, thePostCode);
        }
        {
            boolean theSingle;
            theSingle = this.isSingle();
            currentHashCode = strategy.hashCode(LocatorUtils.property(locator, "single", theSingle), currentHashCode, theSingle);
        }
        return currentHashCode;
    }

    public int hashCode() {
        final HashCodeStrategy strategy = JAXBHashCodeStrategy.INSTANCE;
        return this.hashCode(null, strategy);
    }

}

从我的PoV,&#34;战略&#34;版本更强大。类实现接受定位符和哈希代码/等于策略的HashCodeEquals接口。这允许您从外部控制哈希码计算或比较。我经常在单元测试中使用它,不仅要检查两个对象是否等于,还要确切地找出它们的不同之处。

两个插件都生成无反射方法(这对性能至关重要)。他们还考虑JAXB特殊情况,如JAXBElement s,原始数组等。