FindBugs - 如何解决 EQ_COMPARETO_USE_OBJECT_EQUALS
我在这里一无所知...
1: private static class ForeignKeyConstraint implements Comparable<ForeignKeyConstraint> {
2: String tableName;
3: String fkFieldName;
4:
5: public int compareTo(ForeignKeyConstraint o) {
6: if (this.tableName.compareTo(o.tableName) == 0) {
7: return this.fkFieldName.compareTo(o.fkFieldName);
8: }
9: return this.tableName.compareTo(o.tableName);
10: }
11: }
在第 6 行中,我从 FindBugs 得到: Bug: net.blabla.SqlFixer$ForeignKeyConstraint 定义了compareTo(SqlFixer$ForeignKeyConstraint) 并使用 Object.equals()
我不知道如何纠正这个问题。
I am clueless here...
1: private static class ForeignKeyConstraint implements Comparable<ForeignKeyConstraint> {
2: String tableName;
3: String fkFieldName;
4:
5: public int compareTo(ForeignKeyConstraint o) {
6: if (this.tableName.compareTo(o.tableName) == 0) {
7: return this.fkFieldName.compareTo(o.fkFieldName);
8: }
9: return this.tableName.compareTo(o.tableName);
10: }
11: }
In line 6 I get from FindBugs: Bug: net.blabla.SqlFixer$ForeignKeyConstraint defines compareTo(SqlFixer$ForeignKeyConstraint) and uses Object.equals()
I don't know how to correct this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
此错误意味着您没有覆盖
ForeignKeyConstraint
中的equals
(从而从Object
继承equals
),因此以下内容不正确(来自比较
):要修复 FindBugs 检查,请覆盖
equals
- 和hashCode
- 如果有意义(通常是这种情况)(或者排除对该类的检查并记录您的类违反了此规定)使用建议注释的条件)。This errors means that you're not overriding
equals
inForeignKeyConstraint
(and thus inheriting theequals
fromObject
) so the following is not true (from the javadoc ofcompareTo
):To fix the FindBugs check, override
equals
- andhashCode
- if it makes sense which is generally the case (or exclude the check for this class and document that your class violates this condition using the suggested note).它告诉您,compareTo() 和 equals() 可能会出现不一致。他们真的应该永远不同意。
equals() 方法继承自 java.lang.Object,默认情况下它会检查两个对象是否是同一个实例。您的compareTo方法正在比较基于tableName和fkFieldName的对象。因此,您可能会发现自己处于这样的情况:compareTo 声明两个对象相同(因为 tableName 和 fkFieldName 匹配),但 equals 声明它们不同(因为它们是不同的实例)。
有一些 Java API 依赖于 CompareTo 和 equals 的一致性;这是 java 语言的一部分,被认为是核心语言契约。理想情况下,实现 equals(和 hashcode)方法来检查基于 tableName 和 fkFieldName 的相等性。
It's telling you that there's the potential for compareTo() and equals() to disagree. And they should, really, never disagree.
The equals() method is being inherited from java.lang.Object, which by default checks to see if two objects are the same instance. Your compareTo method is comparing objects are based on tableName and fkFieldName. So you'll potentially find yourself in a situation where compareTo states that two objects are the same (because tableName and fkFieldName match), but equals states they are different (because they're different instances).
There are a few java APIs that depend on compareTo and equals being consistant; this is part of the java language and is considered a core language contract. Ideally implement an equals (and hashcode) method to check for equality based on tableName and fkFieldName.
您可以通过实现 equals() 方法来解决它。参考 FindBugs 的定义:
“一般来说,当且仅当 equals 返回 true 时,compareTo 的值才应返回零。如果违反这一点,PriorityQueue 等类中将会出现奇怪且不可预测的失败。”
“强烈建议,但不严格要求 (x.compareTo(y)==0) == (x.equals(y))。”
另一个例子是 TreeSet。它通过调用compareTo来实现相等性检查,与equals不一致的compareTo实现会使TreeSet违反Set接口的约定,这可能会导致程序故障。
You can solve it by implementing an equals() method. Refer to the FindBugs definition:
"Generally, the value of compareTo should return zero if and only if equals returns true. If this is violated, weird and unpredictable failures will occur in classes such as PriorityQueue."
"It is strongly recommended, but not strictly required that (x.compareTo(y)==0) == (x.equals(y))."
Another example is the TreeSet. It implements equality checks by invoking compareTo, and a compareTo implementation that is inconsistent with equals makes the TreeSet violate the contract of the Set interface, which might lead to program malfunction.
您是否也尝试过在 SqlFixer.ForeignKeyConstraint 中重写 equals 方法?
我相信警告的基础是,正如定义中所述,如果您重写compareTo而不是等于,可能会发生奇怪的事情。
有关详细信息,请查看 Joshua Bloch 的《Effective Java》,第二版。第 12 条更深入地介绍了实现 Comparable 的细节以及一些需要注意的事项。
Have you tried overriding the equals method as well in SqlFixer.ForeignKeyConstraint?
I believe the basis of the warning is that, as stated in the definition, strange things can happen if you override compareTo and not equals.
For more information check out Joshua Bloch's Effective Java, 2nd Edition. Item 12 goes more in depth about the ins and outs of implementing Comparable and some of the things to look out for.
Findbugs 很满意:
Findbugs is happy with: