FindBugs - 如何解决EQ_COMPARETO_USE_OBJECT_EQUALS [英] FindBugs - how to solve EQ_COMPARETO_USE_OBJECT_EQUALS

查看:1412
本文介绍了FindBugs - 如何解决EQ_COMPARETO_USE_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: }

在第6行我从FindBugs获得:错误:net.blabla.SqlFixer $ ForeignKeyConstraint定义的compareTo(SqlFixer $ ForeignKeyConstraint),并使用的Object.Equals()

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.

推荐答案

这个错误意味着你没有覆盖 ForeignKeyConstraint 中等于(从而从继承等于对象)所以以下不是真的(来自 compareTo ):

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


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

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

要修复FindBugs检查,请覆盖等于 - 和 hashCode - 如果通常情况有意义(或排除对此类和文档的检查)你的班级使用建议的说明违反了这个条件。)

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 - 如何解决EQ_COMPARETO_USE_OBJECT_EQUALS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆