Skip to content

Commit

Permalink
NullPointerTester should not require @nullable on the param of an equ…
Browse files Browse the repository at this point in the history
…als() override

This is currently being worked around with things like:

new NullPointerTester()
    .ignore(AutoValue_MyValueType.class.getMethod("equals", Object.class))
    ...

The work-arounds prevent AutoValue from being treated as a pure implementation-generating processor.

Fixes #1819

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=131074329
  • Loading branch information
cushon authored and cpovirk committed Aug 23, 2016
1 parent 484e083 commit af1dabf
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,37 @@ private static boolean isNullable(Parameter param) {
}

private boolean isIgnored(Member member) {
return member.isSynthetic() || ignoredMembers.contains(member);
return member.isSynthetic() || ignoredMembers.contains(member) || isEquals(member);
}

/**
* Returns true if the the given member is a method that overrides {@link Object#equals(Object)}.
*
* <p>The documentation for {@link Object#equals} says it should accept null, so don't require an
* explicit {@link @Nullable} annotation (see <a
* href="https://github.com/google/guava/issues/1819">#1819</a>).
*
* <p>It is not necessary to consider visibility, return type, or type parameter declarations. The
* declaration of a method with the same name and formal parameters as {@link Object#equals} that
* is not public and boolean-returning, or that declares any type parameters, would be rejected at
* compile-time.
*/
private static boolean isEquals(Member member) {
if (!(member instanceof Method)) {
return false;
}
Method method = (Method) member;
if (!method.getName().contentEquals("equals")) {
return false;
}
Class<?>[] parameters = method.getParameterTypes();
if (parameters.length != 1) {
return false;
}
if (!parameters[0].equals(Object.class)) {
return false;
}
return true;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1300,4 +1300,22 @@ public void testNonStaticInnerClass() {
private static String rootLocaleFormat(String format, Object... args) {
return String.format(Locale.ROOT, format, args);
}

static class OverridesEquals {
@Override
public boolean equals(Object o) {
return true;
}
}

static class DoesNotOverrideEquals {
public boolean equals(Object a, Object b) {
return true;
}
}

public void testEqualsMethod() {
shouldPass(new OverridesEquals());
shouldFail(new DoesNotOverrideEquals());
}
}

0 comments on commit af1dabf

Please sign in to comment.