FindBugs - 如何解决 EQ_COMPARETO_USE_OBJECT_EQUALS

发布于 2024-08-28 19:33:35 字数 758 浏览 2 评论 0原文

我在这里一无所知...

 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()

Link to definition

I don't know how to correct this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

豆芽 2024-09-04 19:33:35

此错误意味着您没有覆盖 ForeignKeyConstraint 中的 equals (从而从 Object 继承 equals),因此以下内容不正确(来自 比较):

强烈建议(但不严格要求)(x.compareTo(y)==0) == (x.equals(y))。一般来说,任何实现 Comparable 接口并违反此条件的类都应该明确指出这一事实。推荐的语言是“注意:这个类有一个与 equals 不一致的自然顺序。”

要修复 FindBugs 检查,请覆盖 equals - 和 hashCode - 如果有意义(通常是这种情况)(或者排除对该类的检查并记录您的类违反了此规定)使用建议注释的条件)。

This errors means that you're not overriding equals in ForeignKeyConstraint (and thus inheriting the equals from Object) so the following is not true (from the javadoc of 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."

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).

§普罗旺斯的薰衣草 2024-09-04 19:33:35

它告诉您,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.

2024-09-04 19:33:35

您可以通过实现 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.

你没皮卡萌 2024-09-04 19:33:35

您是否也尝试过在 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.

很酷不放纵 2024-09-04 19:33:35

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() {
  ...
}

Findbugs is happy with:

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() {
  ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文