Java


JAVA.IDEF.HCNOEQUALS : Defines hashCode but not equals (Java)

要旨

The equals() method seems needed.

Redefinitions of the equals()/hashCode() methods from java.lang.Object must be consistent in the sense that, for instance, if two objects are equals() then hashCode() must yield the same value on both. The validity of such a property is in general undecidable, but most incorrect definitions amount to simple cases, where one of the two methods is missing. However, it is often correct to redefine only one of those methods and the analyzer is aware of many such situations.

Inconsistent definitions of equals()/hashCode() induce unexpected behaviors when objects are put inside most Collection classes of the standard Java library.

プロパティ

クラス名 Defines hashCode but not equals (Java)
日本語クラス名 Defines hashCode but not equals (Java)
クラス分類 信頼性 (reliability)
ニーモニック JAVA.IDEF.HCNOEQUALS
カテゴリー
CWE CWE:581 Object Model Violation: Just One of Equals and Hashcode Defined
CERT-Java CERT-Java:MET09-J Classes that define an equals() method must also define a hashCode() method
対応言語 Java で利用可能です。
有効/無効設定 このワーニングクラスのチェックはデフォルトで有効になっています。チェックを無効にするにはプロジェクト設定ファイル (configuration file)に以下の WARNING_FILTER ルールを追加してください。
WARNING_FILTER += discard class="Defines hashCode but not equals (Java)"

public class MyClass {

  public static void main(String[] args) {
      MyClass p1 = new MyClass("hello");
      MyClass p2 = new MyClass("hello");

      System.out.println("Are hashcode of p1 and hashcode of p2 the same? "+ (p1.hashCode() == p2.hashCode()));
      System.out.println("Are p1 and p2 equal? "+ p1.equals(p2));
  }

  public String c;

  public MyClass(String c) {
      this.c = c;
  }

  @Override
  public int hashCode() { // "Defines hashCode but not equals (Java)" warning issued here 
      return c.hashCode();    
  }
}

The Java documentation for Object.hashCode() specifies that hashCode(A) and hashCode(B) should return the same value if A.equals(B) (and B.equals(A)) returns true. For this reason, if the hashCode method is redefined, the equals method should also be redefined. This will avoid possible inconsistencies.

The code can be adjusted as follows to avoid this problem.

public class MyClass {

  public static void main(String[] args) {
      MyClass p1 = new MyClass("hello");
      MyClass p2 = new MyClass("hello");

      System.out.println("Are hashcode of p1 and hashcode of p2 the same? "+ (p1.hashCode() == p2.hashCode()));
      System.out.println("Are p1 and p2 equal? "+ p1.equals(p2));
  }

  public String c;

  public MyClass(String c) {
      this.c = c;
  }

  @Override
  public int hashCode() {
      return c.hashCode();    
  }

  @Override
  public boolean equals(Object other) {
      if(other instanceof BPlus)
          return c.equals(((MyClass) other).c);
      return false;   
  }
}

解決法

Make equals() consistent with hashCode(). In many cases, this just amounts to provide the missing definition for one of them.

関連のある設定ファイルパラメータ

設定ファイルの以下のパラメータがこのワーニングクラスのチェックに影響します。