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.COMPARE.CTO.NONCOMP : compareTo in Non-Comparable Class (Java)

要旨

compareTo() is defined in a class that is not an instance of java.lang.Comparable.

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 in Non-Comparable Class (Java)
日本語クラス名 compareTo in Non-Comparable Class (Java)
クラス分類 信頼性 (reliability)
ニーモニック JAVA.COMPARE.CTO.NONCOMP
カテゴリー
CWE CWE:1076 Insufficient Adherence to Expected Conventions
対応言語 Available for Java and Kotlin.
有効/無効設定 このワーニングクラスのチェックはデフォルトで有効になっています。チェックを無効にするにはプロジェクト設定ファイル (configuration file)に以下の WARNING_FILTER ルールを追加してください。
WARNING_FILTER += discard class="compareTo in Non-Comparable Class (Java)"

public class MyClass {

  public int compareTo(MyClass other) { // "compareTo in Non-Comparable Class (Java)" warning issued ehre
      //...
      }
        
  //...
}

To resolve this, ensure that MyClass implements the java.lang.Comparable interface.

public class MyClass implements Comparable<MyClass> {

  public int compareTo(MyClass other) {
      //...
  }
  //...
}        

解決法

Use the non-raw interface java.lang.Comparable<C>; make equals() consistent with compareTo(). In most cases, this just amounts to providing the missing definition of equals().

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

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

 

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