The annotation @Data with inheritance produces the next warning: Generating equals/hashCode implementation but without a call to superclass, even though this class does not extend java.
The @Data annotation does the following work: It generates the getter methods for all the fields. It generates the setter methods for all the non-final fields. It generates the toString() method implementation.
@Data is a convenient shortcut annotation that bundles the features of @ToString , @EqualsAndHashCode , @Getter / @Setter and @RequiredArgsConstructor together: In other words, @Data generates all the boilerplate that is normally associated with simple POJOs (Plain Old Java Objects) and beans: getters for all fields, ...
The @EqualsAndHashCode annotation instructs the compiler to execute an AST transformation which adds the necessary equals and hashCode methods to the class. The hashCode() method is calculated using Groovy's HashCodeHelper class which implements an algorithm similar to the one outlined in the book Effective Java.
The default value is false
. That is the one you get if you don't specify it and ignore the warning.
Yes, it is recommended to add an @EqualsAndHashCode
annotation on the @Data
annotated classes that extend something else than Object. I cannot tell you if you need true
or false
, that depends on your class hierarchy, and will need to be examined on a case-by-case basis.
However, for a project or package, you can configure in lombok.config
to call the super methods if it is not a direct subclass of Object.
lombok.equalsAndHashCode.callSuper = call
See the configuration system documentation on how this works, and the @EqualsEndHashCode
documentation for the supported configuration keys.
Disclosure: I am a lombok developer.
@EqualsAndHashCode(callSuper=true)
should resolve the warning.
The main original question is:
Is it advisable to add annotation @EqualsAndHashCode (callSuper = true) or @EqualsAndHashCode (callSuper = false)?
The accepted answer is basically just:
...that depends...
To expand on that, the documentation on @EqualsAndHashCode has some solid guidance on which to choose. Especially this, IMHO:
By setting callSuper to true, you can include the equals and hashCode methods of your superclass in the generated methods. For hashCode, the result of super.hashCode() is included in the hash algorithm, and forequals, the generated method will return false if the super implementation thinks it is not equal to the passed in object. Be aware that not all equals implementations handle this situation properly. However, lombok-generated equals implementations do handle this situation properly, so you can safely call your superclass equals if it, too, has a lombok-generated equals method.
To distill this down a bit: Chose 'callSuper=true' if you are inheriting from a superclass that either has no state information, or itself is using the @Data annotation, or has implementations of equals/hash that "handle the situation properly" - which I interpret to mean returning a proper hash of the state values.
If you want to compare the members of the superclass as well, then use @EqualsAndHashCode(callSuper=true)
. If, however, you only want to compare fields in the current class you can use @EqualsAndHashCode(callSuper=false)
which is the default option.
If you use the Delombok-feature you can see that the difference is that when set to true
this line is added to the generated equals method if (!super.equals(o)) return false;
. If you have members in the superclass that should be taken into account when comparing two objects, then it has to be set to true to compare correctly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With