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.HCNOEQUALS : Defines hashCode but not equals (Java)

Summary

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.

Properties

Class Name Defines hashCode but not equals (Java)
Significance reliability
Mnemonic JAVA.IDEF.HCNOEQUALS
Categories
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
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="Defines hashCode but not equals (Java)"

Example

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;   
  }
}

Resolution

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

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/.