Make toString(), hashCode(), equals() and friends final in AutoValue classes

Developers usually don't look at the classes that AutoValue generates,
but at the hand-written classes. If the hand-written classes implement
toString(), hashCode(), equals() etc, AutoValue doesn't override them.
To make this more clear to readers of the hand-written classes
ErrorProne suggests to make these methods final.

See https://errorprone.info/bugpattern/AutoValueFinalMethods

Enable this check at ERROR severity to prevent future occurrences.

Signed-off-by: Edwin Kempin <ekempin@google.com>
Change-Id: I972e8e8f9339b811c78157abef771b8c32be079c
This commit is contained in:
Edwin Kempin
2019-07-01 16:10:57 +02:00
committed by David Ostrovsky
parent 14d0a062fa
commit 7360297a66
12 changed files with 17 additions and 16 deletions

View File

@@ -106,7 +106,7 @@ public class StarredChangesUtil {
public abstract String label();
@Override
public String toString() {
public final String toString() {
return accountId() + SEPARATOR + label();
}
}

View File

@@ -263,7 +263,7 @@ public class ProjectWatches {
public abstract ImmutableSet<NotifyType> notifyTypes();
@Override
public String toString() {
public final String toString() {
List<NotifyType> notifyTypes = new ArrayList<>(notifyTypes());
StringBuilder notifyValue = new StringBuilder();
notifyValue.append(firstNonNull(filter(), FILTER_ALL)).append(" [");

View File

@@ -196,7 +196,7 @@ public abstract class ExternalId implements Serializable {
}
@Override
public String toString() {
public final String toString() {
return get();
}
@@ -445,7 +445,7 @@ public abstract class ExternalId implements Serializable {
* that was loaded from Git can be equal with an external ID that was created from code.
*/
@Override
public boolean equals(Object obj) {
public final boolean equals(Object obj) {
if (!(obj instanceof ExternalId)) {
return false;
}
@@ -457,7 +457,7 @@ public abstract class ExternalId implements Serializable {
}
@Override
public int hashCode() {
public final int hashCode() {
return Objects.hash(key(), accountId(), email(), password());
}
@@ -475,7 +475,7 @@ public abstract class ExternalId implements Serializable {
* </pre>
*/
@Override
public String toString() {
public final String toString() {
Config c = new Config();
writeToConfig(c);
return c.toText();

View File

@@ -65,7 +65,7 @@ public abstract class ChangeTriplet {
public abstract Change.Key id();
@Override
public String toString() {
public final String toString() {
return format(branch(), id());
}
}

View File

@@ -34,7 +34,7 @@ public abstract class ConfigKey {
}
@Override
public String toString() {
public final String toString() {
StringBuilder sb = new StringBuilder();
sb.append(section()).append(".");
if (subsection() != null) {

View File

@@ -194,7 +194,7 @@ public abstract class ScheduleConfig {
}
@Override
public String toString() {
public final String toString() {
StringBuilder b = new StringBuilder();
b.append(formatValue(keyInterval()));
b.append(", ");

View File

@@ -143,7 +143,7 @@ public class NoteDbChangeState {
abstract ImmutableMap<Account.Id, ObjectId> draftIds();
@Override
public String toString() {
public final String toString() {
return appendTo(new StringBuilder()).toString();
}

View File

@@ -141,7 +141,7 @@ public class SectionSortCache {
}
@Override
public int hashCode() {
public final int hashCode() {
return cachedHashCode();
}
}

View File

@@ -267,12 +267,12 @@ class RelatedChangesSorter {
}
@Override
public int hashCode() {
public final int hashCode() {
return Objects.hash(patchSet().getId(), commit());
}
@Override
public boolean equals(Object obj) {
public final boolean equals(Object obj) {
if (!(obj instanceof PatchSetData)) {
return false;
}

View File

@@ -694,13 +694,13 @@ abstract class GroupBundle {
}
@Override
public int hashCode() {
public final int hashCode() {
throw new UnsupportedOperationException(
"hashCode is not supported because equals is not supported");
}
@Override
public boolean equals(Object o) {
public final boolean equals(Object o) {
throw new UnsupportedOperationException("Use GroupBundle.compare(a, b) instead of equals");
}

View File

@@ -85,7 +85,7 @@ public abstract class LabelVote {
}
@Override
public String toString() {
public final String toString() {
return format();
}
}

View File

@@ -26,6 +26,7 @@ java_package_configuration(
javacopts = [
"-XepDisableWarningsInGeneratedCode",
"-Xep:AmbiguousMethodReference:WARN",
"-Xep:AutoValueFinalMethods:ERROR",
"-Xep:BadAnnotationImplementation:WARN",
"-Xep:BadComparable:WARN",
"-Xep:BoxedPrimitiveConstructor:ERROR",