We extend JAXBEqualsStrategy via pom:
<xjcArg>-Xequals-equalsStrategyClass=com.acme.foo.CustomEqualsStrategy</xjcArg>
The CustomEqualsStrategy extends JAXBEqualsStrategy. After running MAVEN clean install generate-source in Eclipse (Keplar) our model classes have equals method like this:
public boolean equals(Object object) {
final EqualsStrategy strategy = new CustomEqualsStrategy();
return equals(null, null, object, strategy);
}
Whereas if we do not extend JAXBEqualsStrategy, our model classes have equals method like this:
public boolean equals(Object object) {
final EqualsStrategy strategy = JAXBEqualsStrategy.INSTANCE;
return equals(null, null, object, strategy);
}
JAXBEqualsStrategy has
public static EqualsStrategy INSTANCE = new JAXBEqualsStrategy();
We expected to get
final EqualsStrategy strategy = CustomEqualsStrategy.INSTANCE;
in the generated equals method and are struggling to accomplish it.
You do not want to use
CustomEqualsStrategy.INSTANCE. Usingnew CustomEqualsStrategy()is correct and should be preferred unless you have very good reasons for doing otherwise.Since
CustomEqualsStrategyextendsJAXBEqualsStrategy, that means that unless you define your ownINSTANCEfield insideCustomEqualsStrategy,CustomEqualsStrategy.INSTANCEis the same asJAXBEqualsStrategy.INSTANCE, which means that you would be using an instance ofJAXBEqualsStrategyafter all.Plus, using an
INSTANCEfield like that effectively signals that your class is meant to be used as a singleton, and thus has to be stateless. Most classes are not stateless, and even for classes that are, many such classes don't need to be used in a singleton style.In short, really just stick with
new CustomEqualsStrategy(). The code will have fewer surprises and you'll be happier for it. (Also, from reading the code forJAXBEqualsStrategy, perhaps you should be extendingDefaultEqualsStrategyinstead.)