JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.

If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.

If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.

CodeSonar® 9.2p0 CONFIDENTIAL CodeSecure Inc
Java


JAVA.IDEF.CTOEQ : compareTo/equals mismatch (Java)

要旨

compareTo() is defined is defined inconsistently from equals().

By convention the compareTo should return 0 if two instances are equal, and it should return 1 or -1 only if the instances are different in order to preserve the total ordering.

A class C that implements the raw interface java.lang.Comparable must implement the compareTo(Object) method. If C is made to implement the non-raw interface java.lang.Comparable<C>, then it must implement the compareTo(C) method instead.

Moreover, it is good practice to make compareTo() consistent with equals(): if the comparison of two objects yields 0, then they should be equal. The validity of this implication is in general undecidable. There are, however, frequent situations when, typically, this implication does not hold. An example is when equals() is inherited from java.lang.Object.

This checker verifies that compareTo(Object) is defined for classes implementing the raw java.lang.Comparable interface, instead of the (possibly more logical) method compareTo(C). Moreover, it verifies the consistency of compareTo() wrt equals().

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

プロパティ

クラス名 compareTo/equals mismatch (Java)
日本語クラス名 compareTo/equals mismatch (Java)
クラス分類 信頼性 (reliability)
ニーモニック JAVA.IDEF.CTOEQ
カテゴリー
CWE CWE:697 Incorrect Comparison
CERT-Java CERT-Java:MET08-J Preserve the equality contract when overriding the equals() method
対応言語 Available for Java and Kotlin.
有効/無効設定 このワーニングクラスのチェックはデフォルトで有効になっています。チェックを無効にするにはプロジェクト設定ファイル (configuration file)に以下の WARNING_FILTER ルールを追加してください。
WARNING_FILTER += discard class="compareTo/equals mismatch (Java)"

// CompareToVsEquals9.java
public final class CompareToVsEquals9 implements Comparable<CompareToVsEquals9> {
  private int f;
        
  public CompareToVsEquals9(int f) {
    this.f = f;
  }

  @Override
  public int compareTo(CompareToVsEquals9 o) {
    if (this == o || this.equals(o)) // "compareTo/equals mismatch (Java)" warning issued here
      return 1;
    else
      return -1;
  }

  @Override
  public boolean equals(Object other) {
    return other instanceof CompareToVsEquals9 && ((CompareToVsEquals9) other).f == f;
  }

  @Override
  public String toString() {
    return String.valueOf(f);
  }
}    
// Mango.java
public class Mango implements Comparable<Mango> {
  public int compareTo(Mango o) { // "compareTo/equals mismatch (Java)" warning issued here
      if (this == o || this.equals(o))
          return 1;
      else
          return -1;
  }
        
  //...
}

解決法

RespectJava convention, the compareTo should return 0 if two instances are equal, and it should return 1 or -1 only if the instances are different in order to preserve the total ordering.

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

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

 

To report problems with this documentation, please visit https://support.codesecure.com/.