是否有可能为使用jaxb生成的类生成equals和compareTo方法,我使用jaxb从模式生成类。这些类实际上具有GUID,它们可以被唯一标识,但我如何实现一个equals/compare方法,以便集合类(如Set)能够识别同一个实体的重复实例?是否有可能为使用jaxb生成的类生成equals和compareTo方法
回答
好的,这是另一种方法。
您可以使用-XcodeInjector
插件添加hashCode
和equals
方法。
看到这个问题:
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( “允许的hashCode /等于选择属性” )或者实现你自己的插件。
谢谢,看起来像一个解决方案 –
免责声明:我的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>
(见documentation为Ant)
您可以使用-XsimpleHashCode
和-XsimpleEquals
其产生运行时少hashCode
和equals
方法(散列码或等于计算内联)或-XhashCode
/-Xequals
,它们生成“战略”hashCode
和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特殊情况,如JAXBElement
s,原始数组等。
谢谢,但有点困惑,你使用jaxbbasics而不是jaxb或以及? –
JAXB2 Basics是JAXB/XJC的一组插件,您不能使用它们,而必须使用* JAXB/XJC。 – lexicore
好吧,我的类已经有一个uniqueId字段,所以有办法使用该字段来生成equals和hashcode。 –
- 1. 是否有可能使用JAXB生成共享对象?
- 2. 使用JAXB可以生成超类型?
- 3. 使用equals()和hashCode()方法从DTD生成Java类
- 4. 是否有可能为REST服务生成Web引用类?
- 5. netbeans能否为映射自动生成正确的hashCode()和equals()方法?
- 6. 我们是否需要HyperJAXB生成的hashCode&equals方法?
- 7. 是否有可能无法使用npm 5生成package-lock.json?
- 8. 是否有可能生成ASMX
- 9. 是否有可能为xsd简单类型生成xml?
- 10. 是否有可能为Java类自动生成Hibernate映射?
- 11. JAXB没有为包含生成的Java类的类生成所需的模式?
- 12. 如何在jaxws中使用wsimport生成equals()和hashcode()方法
- 13. JAXB生成可笑的奇怪和长的方法名称
- 14. 由jaxb生成的ObjectFactory方法
- 15. 使用JAXB从XMLSchema.xsd中生成Java类
- 16. JAXB:使用本地schemaLocation生成Java类
- 17. 如何使用java.util.Optional生成JaxB类?
- 18. JAXB生成没有注释的java类
- 19. Intellij为方法和类生成javadoc
- 20. 从模式生成jaxb类
- 21. 从xsd生成jaxb groovy类
- 22. JAXB不生成Java类
- 23. 变化包生成JAXB类
- 24. JAXB Bean生成
- 25. 是否可以更改为equals()和hashCode()的IntelliJ的代码生成模板
- 26. 是否有可能使eclipse生成hashCode并等于HashCodeBuilder和EqualsBuilder
- 27. 是否有可能从部分类型生成严格类型?
- 28. 是否有一个JAXB插件可以生成构建器?
- 29. 是否有可能设置类型的输入生成的TextBoxFor
- 30. 是否有可能更改Silverlight RiaServices生成的DomainContexts的方式?
可能的重复http://stackoverflow.com/questions/6523497/how-to-generate-equals-and-hashcode-methods-using-wsimport-in-jaxws –
没有什么不同,因为想创建基于这是班级中的一个特殊元素,而不是通过查看所有元素。目前我使用的解决方案是始终使用基于所述id的自定义比较器的TreeSet。 –