Skip to content

Commit

Permalink
Consolidate static #equals matchers.
Browse files Browse the repository at this point in the history
And fix a javadoc.

RELNOTES: N/A

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=207264130
  • Loading branch information
graememorgan authored and ronshapiro committed Aug 6, 2018
1 parent 59a2dd3 commit e74aee3
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@

import static com.google.errorprone.dataflow.nullnesspropagation.Nullness.NONNULL;
import static com.google.errorprone.dataflow.nullnesspropagation.Nullness.NULL;
import static com.google.errorprone.matchers.Matchers.anyOf;
import static com.google.errorprone.matchers.Matchers.instanceMethod;
import static com.google.errorprone.matchers.Matchers.staticMethod;
import static com.google.errorprone.matchers.Matchers.staticEqualsInvocation;

import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker.BinaryTreeMatcher;
Expand Down Expand Up @@ -50,10 +49,8 @@
*/
public abstract class AbstractReferenceEquality extends BugChecker implements BinaryTreeMatcher {

private static final Matcher<ExpressionTree> EQUALS_STATIC_METHODS =
anyOf(
staticMethod().onClass("com.google.common.base.Objects").named("equal"),
staticMethod().onClass("java.util.Objects").named("equals"));
private static final Matcher<MethodInvocationTree> EQUALS_STATIC_METHODS =
staticEqualsInvocation();

private static final Matcher<ExpressionTree> OBJECT_INSTANCE_EQUALS =
instanceMethod()
Expand Down Expand Up @@ -168,7 +165,7 @@ private static Optional<Fix> inOrStatementWithEqualsCheck(VisitorState state, Bi
MethodInvocationTree other = (MethodInvocationTree) otherExpression;

// a == b || Objects.equals(a, b) => Objects.equals(a, b)
if (EQUALS_STATIC_METHODS.matches(otherExpression, state)) {
if (EQUALS_STATIC_METHODS.matches(other, state)) {
List<? extends ExpressionTree> arguments = other.getArguments();
if (treesMatch(arguments.get(0), arguments.get(1), lhs, rhs)) {
return Optional.of(SuggestedFix.replace(parent, state.getSourceForNode(otherExpression)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@
import com.sun.tools.javac.code.Type;
import java.util.List;

/** Bug checker to detect usage of {@code Set<T[]>} or {@code Map<T[], E>}. */
/**
* Warns that users should not have an array as a key to a Set or Map
*
* @author [email protected] (Siyuan Liu)
* @author [email protected] (Eleanor Harris)
*/
@BugPattern(
name = "ArrayAsKeyOfSetOrMap",
summary =
Expand All @@ -43,13 +48,6 @@
+ "consider using a List instead. Otherwise, use IdentityHashMap/Set, "
+ "a Map from a library that handles object arrays, or an Iterable/List of pairs.",
severity = WARNING)

/**
* Warns that users should not have an array as a key to a Set or Map
*
* @author [email protected] (Siyuan Liu)
* @author [email protected] (Eleanor Harris)
*/
public class ArrayAsKeyOfSetOrMap extends BugChecker
implements MethodInvocationTreeMatcher, NewClassTreeMatcher {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
import static com.google.errorprone.BugPattern.SeverityLevel.ERROR;
import static com.google.errorprone.matchers.Description.NO_MATCH;
import static com.google.errorprone.matchers.Matchers.allOf;
import static com.google.errorprone.matchers.Matchers.anyOf;
import static com.google.errorprone.matchers.Matchers.argument;
import static com.google.errorprone.matchers.Matchers.instanceMethod;
import static com.google.errorprone.matchers.Matchers.staticMethod;
import static com.google.errorprone.matchers.Matchers.isArrayType;
import static com.google.errorprone.matchers.Matchers.staticEqualsInvocation;
import static com.google.errorprone.predicates.TypePredicates.isArray;

import com.google.errorprone.BugPattern;
Expand All @@ -34,8 +34,6 @@
import com.google.errorprone.fixes.SuggestedFix;
import com.google.errorprone.matchers.Description;
import com.google.errorprone.matchers.Matcher;
import com.google.errorprone.matchers.Matchers;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.MethodInvocationTree;
import com.sun.tools.javac.tree.JCTree.JCFieldAccess;

Expand All @@ -49,26 +47,11 @@
public class ArrayEquals extends BugChecker implements MethodInvocationTreeMatcher {
/** Matches when the equals instance method is used to compare two arrays. */
private static final Matcher<MethodInvocationTree> instanceEqualsMatcher =
Matchers.allOf(
instanceMethod().onClass(isArray()).named("equals"),
argument(0, Matchers.<ExpressionTree>isArrayType()));
allOf(instanceMethod().onClass(isArray()).named("equals"), argument(0, isArrayType()));

/**
* Matches when the following methods compare two arrays:
*
* <ul>
* <li>Android android.support.v4.util.ObjectsCompat#equals
* <li>Guava com.google.common.base.Objects#equal
* <li>JDK7 java.util.Objects#equals
*/
/** Matches when {@link java.util.Objects#equals}-like methods compare two arrays. */
private static final Matcher<MethodInvocationTree> staticEqualsMatcher =
allOf(
anyOf(
staticMethod().onClass("android.support.v4.util.ObjectsCompat").named("equals"),
staticMethod().onClass("com.google.common.base.Objects").named("equal"),
staticMethod().onClass("java.util.Objects").named("equals")),
argument(0, Matchers.<ExpressionTree>isArrayType()),
argument(1, Matchers.<ExpressionTree>isArrayType()));
allOf(staticEqualsInvocation(), argument(0, isArrayType()), argument(1, isArrayType()));

/**
* Suggests replacing with Arrays.equals(a, b). Also adds the necessary import statement for
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import static com.google.errorprone.matchers.Matchers.methodIsNamed;
import static com.google.errorprone.matchers.Matchers.methodReturns;
import static com.google.errorprone.matchers.Matchers.not;
import static com.google.errorprone.matchers.Matchers.staticEqualsInvocation;
import static com.google.errorprone.matchers.Matchers.staticMethod;
import static com.google.errorprone.matchers.Matchers.variableType;
import static com.google.errorprone.matchers.method.MethodMatchers.instanceMethod;
Expand Down Expand Up @@ -79,11 +80,8 @@ public final class EqualsWrongThing extends BugChecker implements MethodTreeMatc
methodHasParameters(variableType(isSameType(OBJECT_TYPE))),
not(isStatic()));

private static final Matcher<ExpressionTree> COMPARISON_METHOD =
anyOf(
staticMethod().onClass("java.util.Arrays").named("equals"),
staticMethod().onClass("com.google.common.base.Objects").named("equal"),
staticMethod().onClass("java.util.Objects").named("equals"));
private static final Matcher<MethodInvocationTree> COMPARISON_METHOD =
anyOf(staticMethod().onClass("java.util.Arrays").named("equals"), staticEqualsInvocation());

private static final Matcher<ExpressionTree> EQUALS =
instanceMethod().anyClass().named("equals").withParameters("java.lang.Object");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
import static com.google.errorprone.BugPattern.SeverityLevel.ERROR;
import static com.google.errorprone.matchers.Description.NO_MATCH;
import static com.google.errorprone.matchers.Matchers.allOf;
import static com.google.errorprone.matchers.Matchers.anyOf;
import static com.google.errorprone.matchers.Matchers.instanceMethod;
import static com.google.errorprone.matchers.Matchers.receiverSameAsArgument;
import static com.google.errorprone.matchers.Matchers.sameArgument;
import static com.google.errorprone.matchers.Matchers.staticEqualsInvocation;
import static com.google.errorprone.matchers.Matchers.toType;
import static com.google.errorprone.matchers.method.MethodMatchers.staticMethod;

Expand Down Expand Up @@ -74,11 +74,7 @@ public class SelfEquals extends BugChecker implements MethodInvocationTreeMatche
receiverSameAsArgument(0));

private static final Matcher<MethodInvocationTree> STATIC_MATCHER =
allOf(
anyOf(
staticMethod().onClass("com.google.common.base.Objects").named("equal"),
staticMethod().onClass("java.util.Objects").named("equals")),
sameArgument(0, 1));
allOf(staticEqualsInvocation(), sameArgument(0, 1));

@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
Expand Down

0 comments on commit e74aee3

Please sign in to comment.