java FindBugs - 如何解决 EQ_COMPARETO_USE_OBJECT_EQUALS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2609037/
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
FindBugs - how to solve EQ_COMPARETO_USE_OBJECT_EQUALS
提问by Trick
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()
在第 6 行,我从 FindBugs 得到: Bug: net.blabla.SqlFixer$ForeignKeyConstraint defines compareTo(SqlFixer$ForeignKeyConstraint) and uses Object.equals()
I don't know how to correct this.
我不知道如何纠正这个。
回答by Pascal Thivent
This errors means that you're not overriding equalsin ForeignKeyConstraint(and thus inheriting the equalsfrom Object) so the following is not true (from the javadoc of compareTo):
这种错误的手段,你没有覆盖equals的ForeignKeyConstraint(因而继承了equals从Object(从Javadoc中),所以下面是不正确的compareTo):
It is strongly recommended, but not strictly required that
(x.compareTo(y)==0) == (x.equals(y)). Generally speaking, any class that implements the Comparable interface and violates this condition should clearly indicate this fact. The recommended language is "Note: this class has a natural ordering that is inconsistent with equals."
强烈推荐,但不严格要求
(x.compareTo(y)==0) == (x.equals(y))。一般而言,任何实现 Comparable 接口并违反此条件的类都应清楚表明这一事实。推荐的语言是“注意:这个类有一个与equals不一致的自然顺序。”
To fix the FindBugs check, override equals- and hashCode- 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).
要修复 FindBugs 检查,覆盖equals- 并且hashCode- 如果通常情况下有意义(或排除此类检查并使用建议的注释记录您的类违反此条件)。
回答by dannywartnaby
It's telling you that there's the potential for compareTo() and equals() to disagree. And they should, really, never disagree.
它告诉您 compareTo() 和 equals() 有可能不一致。他们应该,真的,永远不要不同意。
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).
equals() 方法是从 java.lang.Object 继承的,它默认检查两个对象是否是同一个实例。您的 compareTo 方法是基于 tableName 和 fkFieldName 比较对象。因此,您可能会发现自己处于一种情况,其中 compareTo 声明两个对象相同(因为 tableName 和 fkFieldName 匹配),但 equals 声明它们不同(因为它们是不同的实例)。
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.
有一些 Java API 依赖于 compareTo 和 equals 是一致的;这是 java 语言的一部分,被认为是核心语言契约。理想情况下,实现一个 equals(和 hashcode)方法来检查基于 tableName 和 fkFieldName 的相等性。
回答by Christian Semrau
You can solve it by implementing an equals() method. Refer to the FindBugs definition:
您可以通过实现 equals() 方法来解决它。参考 FindBugs 定义:
"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."
“通常,当且仅当等于返回 true 时,compareTo 的值应返回零。如果违反了这一点,则在 PriorityQueue 等类中将发生奇怪且不可预测的故障。”
"It is strongly recommended, but not strictly required that (x.compareTo(y)==0) == (x.equals(y))."
“强烈推荐,但不严格要求 (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.
另一个例子是 TreeSet。它通过调用compareTo来实现相等检查,与equals不一致的compareTo实现使得TreeSet违反Set接口的约定,可能导致程序故障。
回答by jnt30
Have you tried overriding the equals method as well in SqlFixer.ForeignKeyConstraint?
您是否尝试过在 SqlFixer.ForeignKeyConstraint 中也覆盖 equals 方法?
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.
我相信警告的基础是,如定义中所述,如果您覆盖 compareTo 和 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.
有关更多信息,请查看Joshua Bloch 的 Effective Java,第 2 版。第 12 条更深入地介绍了实现 Comparable 的来龙去脉以及需要注意的一些事项。
回答by r2d2
Findbugs is happy with:
Findbugs 很满意:
public int compareTo(ForeignKeyConstraint o) {
if (this.equals(o)) {
return 0;
} else if (this.tableName.equals(o.tableName)) {
// fkFieldName must be different
return this.fkFieldName.compareTo(o.fkFieldName);
} else {
// tableName must be different
return this.tableName.compareTo(o.tableName);
}
}
@Override
public equals() {
...
}
@Override
public int hashCode() {
...
}

