This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 219261 - Generating equals() and hashCode() should allow to include parents fields
Summary: Generating equals() and hashCode() should allow to include parents fields
Status: NEW
Alias: None
Product: java
Classification: Unclassified
Component: Editor (show other bugs)
Version: 7.2
Hardware: PC Linux
: P3 normal with 2 votes (vote)
Assignee: Dusan Balek
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-10-01 12:33 UTC by hupfdule
Modified: 2015-03-02 18:01 UTC (History)
1 user (show)

See Also:
Issue Type: ENHANCEMENT
Exception Reporter:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description hupfdule 2012-10-01 12:33:42 UTC
The netbeans-generated equals() and hashCode() methods only allow including the fields of the current class. Usually (or at least often) it is necessary to utilize those methods of the parent class to get a proper implementation.

For example, netbeans generates this:

public class Parent {
  private String a;

  @Override
  public int hashCode() {
    int hash = 7;
    hash = 53 * hash + (this.a != null ? this.a.hashCode() : 0);
    return hash;
  }

  @Override
  public boolean equals(Object obj) {
    if (obj == null) {
      return false;
    }
    if (getClass() != obj.getClass()) {
      return false;
    }
    final Parent other = (Parent) obj;
    if ((this.a == null) ? (other.a != null) : !this.a.equals(other.a)) {
      return false;
    }
    return true;
  }
}


public class Child {
  private String b;

  @Override
  public int hashCode() {
    int hash = 7;
    hash = 73 * hash + (this.b != null ? this.b.hashCode() : 0);
    return hash;
  }

  @Override
  public boolean equals(Object obj) {
    if (obj == null) {
      return false;
    }
    if (getClass() != obj.getClass()) {
      return false;
    }
    final Child other = (Child) obj;
    if ((this.b == null) ? (other.b != null) : !this.b.equals(other.b)) {
      return false;
    }
    return true;
  }
}


Here the hashCode() and equals() methods of Child should look more like this:

  @Override
  public int hashCode() {
    int hash = 7 + super.hashCode();   //THIS LINE IS MODIFIED
    hash = 73 * hash + (this.b != null ? this.b.hashCode() : 0);
    return hash;
  }

  @Override
  public boolean equals(Object obj) {
    if (obj == null) {
      return false;
    }
    if (getClass() != obj.getClass()) {
      return false;
    }
    if (!super.equals(obj)) {        //THIS IF CLAUSE IS NEW
      return false;
    }
    final Child other = (Child) obj;
    if ((this.b == null) ? (other.b != null) : !this.b.equals(other.b)) {
      return false;
    }
    return true;
  }