Warning equals/hashCode on @Data annotation lombok with inheritance

梦想与她 提交于 2019-11-29 23:28:11

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!