FindBugs - how to solve EQ_COMPARETO_USE_OBJECT_EQUALS

前端 未结 5 1780
后悔当初
后悔当初 2021-01-11 14:50

I am clueless here...

 1: private static class ForeignKeyConstraint implements Comparable {
 2: String tableName;
 3: String fkFi         


        
5条回答
  •  滥情空心
    2021-01-11 15:11

    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.

提交回复
热议问题