Java 警告 equals/hashCode 在带有继承的 @Data 注释 lombok 上
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38572566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Warning equals/hashCode on @Data annotation lombok with inheritance
提问by Pau
I have a entity which inherits from other. On other hand, I'm using lombok project to reduce boilerplate code, so I put @Data
annotation. The annotation @Data
with inheritance produces the next warning:
我有一个继承自其他实体的实体。另一方面,我使用 lombok 项目来减少样板代码,所以我放了@Data
注释。@Data
带有继承的注释会产生下一个警告:
Generating equals/hashCode implementation but without a call to superclass, even though this class does not extend java.lang.Object. If this is intentional, add
@EqualsAndHashCode(callSuper=false)
to your type.
生成 equals/hashCode 实现但不调用超类,即使此类不扩展 java.lang.Object。如果这是故意的,请添加
@EqualsAndHashCode(callSuper=false)
到您的类型中。
Is it advisable to add annotation @EqualsAndHashCode (callSuper = true)
or @EqualsAndHashCode (callSuper = false)
? If it is not added, Which one is it callSuper=false
or callSuper=true
?
是否建议添加注释@EqualsAndHashCode (callSuper = true)
或@EqualsAndHashCode (callSuper = false)
?如果不加它,哪一个是它callSuper=false
还是callSuper=true
?
采纳答案by Roel Spilker
The default valueis false
. That is the one you get if you don't specify it and ignore the warning.
该默认值是false
。如果您不指定它并忽略警告,这就是您得到的。
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.
是的,建议@EqualsAndHashCode
在@Data
扩展对象以外的其他内容的带注释的类上添加注释。我无法告诉您是否需要true
or false
,这取决于您的类层次结构,并且需要根据具体情况进行检查。
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.config
如果它不是 Object 的直接子类,则可以配置 in调用超级方法。
lombok.equalsAndHashCode.callSuper = call
See the configuration system documentationon how this works, and the @EqualsEndHashCode
documentationfor the supported configuration keys.
请参阅有关其工作原理的配置系统文档以及支持的配置密钥的@EqualsEndHashCode
文档。
Disclosure: I am a lombok developer.
披露:我是龙目岛的开发人员。
回答by noscreenname
@EqualsAndHashCode(callSuper=true)
should resolve the warning.
@EqualsAndHashCode(callSuper=true)
应该解决警告。
回答by Adam Wise
The main original question is:
主要的原始问题是:
Is it advisable to add annotation @EqualsAndHashCode (callSuper = true) or @EqualsAndHashCode (callSuper = false)?
是否建议添加注解@EqualsAndHashCode (callSuper = true) 或@EqualsAndHashCode (callSuper = false)?
The accepted answer is basically just:
接受的答案基本上只是:
...that depends...
...那要看...
To expand on that, the documentation on @EqualsAndHashCodehas some solid guidance on which to choose. Especially this, IMHO:
为了扩展这一点,@EqualsAndHashCode上的文档有一些关于选择的可靠指导。尤其是这一点,恕我直言:
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.
通过将 callSuper 设置为 true,您可以在生成的方法中包含超类的 equals 和 hashCode 方法。对于hashCode,super.hashCode()的结果包含在hash算法中,对于forequals,如果super实现认为它不等于传入的对象,则生成的方法将返回false。请注意,并非所有 equals 实现都能正确处理这种情况。但是,lombok 生成的 equals 实现确实可以正确处理这种情况,因此,如果超类也具有 lombok 生成的 equals 方法,则可以安全地调用它。
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.
稍微提炼一下:如果您从没有状态信息的超类继承,或者本身正在使用@Data 注释,或者具有“正确处理情况”的 equals/hash 实现,请选择“callSuper=true” - 我认为这意味着返回状态值的正确散列。
回答by EvR2f
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 defaultoption.
如果您还想比较超类的成员,请使用@EqualsAndHashCode(callSuper=true)
. 但是,如果你只是想在当前类比较字段,你可以使用@EqualsAndHashCode(callSuper=false)
它是默认选项。
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 equalsmethod 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.
如果您使用Delombok-feature,您可以看到不同之处在于,当设置为true
这一行时,它会添加到生成的equals方法中if (!super.equals(o)) return false;
。如果在比较两个对象时应考虑超类中的成员,则必须将其设置为 true 才能正确比较。