免责声明:我是jaxb2-basics 的作者,它提供能够生成hashCode 和equals 方法的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>
(有关 Ant,请参阅 documentation。)
您可以使用-XsimpleHashCode 和-XsimpleEquals 生成无运行时的hashCode 和equals 方法(内联哈希码或等于计算)或-XhashCode/-Xequals 生成“战略”@987654335 @ 和 equals 方法(哈希码/等于计算委托给传递的策略方法)。
这是-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 来看,“战略”版本更强大。类实现接受定位器和哈希码/等于策略的HashCode 或Equals 接口。这允许您从外部控制哈希码计算或比较。我经常在单元测试中使用它,不仅可以检查两个对象是否相等,还可以准确地找出它们的不同之处。
两个插件都生成无反射方法(这对性能至关重要)。他们还考虑了 JAXB 特殊情况,例如 JAXBElements、原始数组等。