Use MoreObjects in toString Method (requires guava 18.0)

selected

package net.sf.guavaeclipse.test;

import com.google.common.base.MoreObjects;

public class SampleSimple {

  private int intValue;

  private String strValue;

  @Override
  public String toString() {
    return MoreObjects.toStringHelper(this)
        .add("intValue", intValue)
        .add("strValue", strValue)
        .toString();
  }

}
        

deselected

package net.sf.guavaeclipse.test;

import com.google.common.base.Objects;

public class SampleSimple {

  private int intValue;

  private String strValue;

  @Override
  public String toString() {
    return Objects.toStringHelper(this)
        .add("intValue", intValue)
        .add("strValue", strValue)
        .toString();
  }

}
        

super method behavior

Use super class Methods (toString(), equals() and hashCode())

package net.sf.guavaeclipse.test;

import com.google.common.base.Objects;
import com.google.common.base.MoreObjects;

public class SampleSimple {

  private int intValue;

  private String strValue;

  @Override
  public String toString() {
    return MoreObjects.toStringHelper(this)
        .add("super", super.toString())
        .add("intValue", intValue)
        .add("strValue", strValue)
        .toString();
  }

  @Override
  public int hashCode() {
    return Objects.hashCode(super.hashCode(), intValue, strValue);
  }

  @Override
  public boolean equals(Object object) {
    if (object instanceof SampleSimple) {
      if (!super.equals(object))
        return false;
      SampleSimple that = (SampleSimple) object;
      return Objects.equal(this.intValue, that.intValue)
          && Objects.equal(this.strValue, that.strValue);
    }
    return false;
  }
  
}
        

Don't use super class Methods (toString(), equals() and hashCode())

package net.sf.guavaeclipse.test;

import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;

public class ClassExtendsSomeComparable extends ClassWithComparable {

  private int intValue2;

  public int getIntValue2() {
    return intValue2;
  }

  public void setIntValue2(int intValue2) {
    this.intValue2 = intValue2;
  }

  @Override
  public String toString() {
    return MoreObjects.toStringHelper(this).add("intValue2", intValue2).toString();
  }

  @Override
  public int hashCode() {
    return Objects.hashCode(intValue2);
  }

  @Override
  public boolean equals(Object object) {
    if (object instanceof ClassExtendsSomeComparable) {
      ClassExtendsSomeComparable that = (ClassExtendsSomeComparable) object;
      return Objects.equal(this.intValue2, that.intValue2);
    }
    return false;
  }

}
        

Use super class Methods (Only if superclass is not "java.lang.Object")

example one
package net.sf.guavaeclipse.test;

import com.google.common.base.Objects;
import com.google.common.base.MoreObjects;

public class SampleSimple {

  private int intValue;

  private String strValue;

  @Override
  public String toString() {
    return MoreObjects.toStringHelper(this).add("intValue", intValue).add("strValue", strValue)
        .toString();
  }

  @Override
  public int hashCode() {
    return Objects.hashCode(intValue, strValue);
  }

  @Override
  public boolean equals(Object object) {
    if (object instanceof SampleSimple) {
      SampleSimple that = (SampleSimple) object;
      return Objects.equal(this.intValue, that.intValue)
          && Objects.equal(this.strValue, that.strValue);
    }
    return false;
  }
  
}
        
example two
package net.sf.guavaeclipse.test;

import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;

public class ClassExtendsSomeComparable extends ClassWithComparable {

  private int intValue2;

  public int getIntValue2() {
    return intValue2;
  }

  public void setIntValue2(int intValue2) {
    this.intValue2 = intValue2;
  }

  @Override
  public String toString() {
    return MoreObjects.toStringHelper(this).add("super", super.toString())
        .add("intValue2", intValue2).toString();
  }

  @Override
  public int hashCode() {
    return Objects.hashCode(super.hashCode(), intValue2);
  }

  @Override
  public boolean equals(Object object) {
    if (object instanceof ClassExtendsSomeComparable) {
      if (!super.equals(object))
        return false;
      ClassExtendsSomeComparable that = (ClassExtendsSomeComparable) object;
      return Objects.equal(this.intValue2, that.intValue2);
    }
    return false;
  }

}