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.ASSYM : Asymmetric compareTo (Java)

Summary

compareTo() is not symmetrical.

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.

Properties

Class Name Asymmetric compareTo (Java)
Significance reliability
Mnemonic JAVA.COMPARE.CTO.ASSYM
Categories
CWE CWE:697 Incorrect Comparison
CERT-Java CERT-Java:MET08-J Preserve the equality contract when overriding the equals() method
Availability Available for Java and Kotlin.
Enabling Checks for this warning class are enabled by default. To disable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += discard class="Asymmetric compareTo (Java)"

Example

The following implementation of compareTo() is not symmetrical. In addition, it might return 0 when the inherited equals() method returns false.

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

  @Override
  public int compareTo(CompareToVsEquals3 o) { // Asymmetric compareTo (Java) warning issued here
                                               // compareTo without equals (Java) warning issued here
    if (f > 0)
      return -1;
    else
      return 0;
  }

  @Override
  public String toString() {
    return String.valueOf(f);
  }
}

The following code corrects these problems.

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

  @Override
  public int compareTo(CompareToVsEquals3 o) {
    return f - o.f;
  }

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

  @Override
  public int hashCode() {
    return f;
  }

  @Override
  public String toString() {
    return String.valueOf(f);
  }
}

Resolution

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().

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.

 

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