Skip to content

Commit

Permalink
Suppress warnings in classes that implement equals() without also imp…
Browse files Browse the repository at this point in the history
…lementing

hashCode().

The contract for Object.hashCode states that if two objects are equal, then
calling the hashCode() method on each of the two objects must produce the same
result. Implementing equals() but not hashCode() causes broken behaviour when
trying to store the object in a collection.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=218187142
  • Loading branch information
sumitbhagwani authored and ronshapiro committed Oct 23, 2018
1 parent cceb259 commit 64276db
Show file tree
Hide file tree
Showing 8 changed files with 10 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ private static class InvalidHashCodeObject {
this.aspect2 = aspect2;
}

@SuppressWarnings("EqualsHashCode")
@Override
public boolean equals(Object o) {
if (!(o instanceof InvalidHashCodeObject)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1384,6 +1384,7 @@ private static String rootLocaleFormat(String format, Object... args) {
}

static class OverridesEquals {
@SuppressWarnings("EqualsHashCode")
@Override
public boolean equals(Object o) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ private static class ClassWhichDoesNotImplementEquals implements Serializable {
private static class ClassWhichIsAlwaysEqualButHasDifferentHashcodes implements Serializable {
private static final long serialVersionUID = 2L;

@SuppressWarnings("EqualsHashCode")
@Override
public boolean equals(Object other) {
return (other instanceof ClassWhichIsAlwaysEqualButHasDifferentHashcodes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ public void testFailsToForwardHashCode() {
@Override
public Runnable apply(final Runnable runnable) {
return new ForwardingRunnable(runnable) {

@SuppressWarnings("EqualsHashCode")
@Override
public boolean equals(Object o) {
if (o instanceof ForwardingRunnable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ private static class InvalidHashCodeObject {
this.aspect2 = aspect2;
}

@SuppressWarnings("EqualsHashCode")
@Override
public boolean equals(Object o) {
if (!(o instanceof InvalidHashCodeObject)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1437,6 +1437,7 @@ private static String rootLocaleFormat(String format, Object... args) {
}

static class OverridesEquals {
@SuppressWarnings("EqualsHashCode")
@Override
public boolean equals(Object o) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ private static class ClassWhichDoesNotImplementEquals implements Serializable {
private static class ClassWhichIsAlwaysEqualButHasDifferentHashcodes implements Serializable {
private static final long serialVersionUID = 2L;

@SuppressWarnings("EqualsHashCode")
@Override
public boolean equals(Object other) {
return (other instanceof ClassWhichIsAlwaysEqualButHasDifferentHashcodes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ public void testFailsToForwardHashCode() {
@Override
public Runnable apply(final Runnable runnable) {
return new ForwardingRunnable(runnable) {

@SuppressWarnings("EqualsHashCode")
@Override
public boolean equals(Object o) {
if (o instanceof ForwardingRunnable) {
Expand Down

0 comments on commit 64276db

Please sign in to comment.