diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/collectionincompatibletype/ContainmentMatchers.java b/core/src/main/java/com/google/errorprone/bugpatterns/collectionincompatibletype/ContainmentMatchers.java index 53b837c59ff..f2f5182d7cf 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/collectionincompatibletype/ContainmentMatchers.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/collectionincompatibletype/ContainmentMatchers.java @@ -46,23 +46,25 @@ public final class ContainmentMatchers { // "Normal" cases, e.g. Collection#remove(Object) // Make sure to keep that the type or one of its supertype should be present in // FIRST_ORDER_MATCHER - new MethodArgMatcher("java.util.Collection", "contains(java.lang.Object)", 0, 0), - new MethodArgMatcher("java.util.Collection", "remove(java.lang.Object)", 0, 0), - new MethodArgMatcher("java.util.Deque", "removeFirstOccurrence(java.lang.Object)", 0, 0), - new MethodArgMatcher("java.util.Deque", "removeLastOccurrence(java.lang.Object)", 0, 0), - new MethodArgMatcher("java.util.Dictionary", "get(java.lang.Object)", 0, 0), - new MethodArgMatcher("java.util.Dictionary", "remove(java.lang.Object)", 0, 0), - new MethodArgMatcher("java.util.List", "indexOf(java.lang.Object)", 0, 0), - new MethodArgMatcher("java.util.List", "lastIndexOf(java.lang.Object)", 0, 0), - new MethodArgMatcher("java.util.Map", "containsKey(java.lang.Object)", 0, 0), - new MethodArgMatcher("java.util.Map", "containsValue(java.lang.Object)", 1, 0), - new MethodArgMatcher("java.util.Map", "get(java.lang.Object)", 0, 0), - new MethodArgMatcher("java.util.Map", "getOrDefault(java.lang.Object,V)", 0, 0), - new MethodArgMatcher("java.util.Map", "remove(java.lang.Object)", 0, 0), - new MethodArgMatcher("java.util.Stack", "search(java.lang.Object)", 0, 0), - new MethodArgMatcher("java.util.Vector", "indexOf(java.lang.Object,int)", 0, 0), - new MethodArgMatcher("java.util.Vector", "lastIndexOf(java.lang.Object,int)", 0, 0), - new MethodArgMatcher("java.util.Vector", "removeElement(java.lang.Object)", 0, 0)); + new MethodArgMatcher("java.util.Collection", 0, 0, "contains", "java.lang.Object"), + new MethodArgMatcher("java.util.Collection", 0, 0, "remove", "java.lang.Object"), + new MethodArgMatcher( + "java.util.Deque", 0, 0, "removeFirstOccurrence", "java.lang.Object"), + new MethodArgMatcher("java.util.Deque", 0, 0, "removeLastOccurrence", "java.lang.Object"), + new MethodArgMatcher("java.util.Dictionary", 0, 0, "get", "java.lang.Object"), + new MethodArgMatcher("java.util.Dictionary", 0, 0, "remove", "java.lang.Object"), + new MethodArgMatcher("java.util.List", 0, 0, "indexOf", "java.lang.Object"), + new MethodArgMatcher("java.util.List", 0, 0, "lastIndexOf", "java.lang.Object"), + new MethodArgMatcher("java.util.Map", 0, 0, "containsKey", "java.lang.Object"), + new MethodArgMatcher("java.util.Map", 1, 0, "containsValue", "java.lang.Object"), + new MethodArgMatcher("java.util.Map", 0, 0, "get", "java.lang.Object"), + new MethodArgMatcher( + "java.util.Map", 0, 0, "getOrDefault", "java.lang.Object", "java.lang.Object"), + new MethodArgMatcher("java.util.Map", 0, 0, "remove", "java.lang.Object"), + new MethodArgMatcher("java.util.Stack", 0, 0, "search", "java.lang.Object"), + new MethodArgMatcher("java.util.Vector", 0, 0, "indexOf", "java.lang.Object", "int"), + new MethodArgMatcher("java.util.Vector", 0, 0, "lastIndexOf", "java.lang.Object", "int"), + new MethodArgMatcher("java.util.Vector", 0, 0, "removeElement", "java.lang.Object")); /** * Cases where we need to extract the type argument from a method argument, e.g. @@ -74,25 +76,28 @@ public final class ContainmentMatchers { // FIRST_ORDER_MATCHER new TypeArgOfMethodArgMatcher( "java.util.Collection", // class that defines the method - "containsAll(java.util.Collection)", // method signature 0, // index of the owning class's type argument to extract 0, // index of the method argument whose type argument to extract "java.util.Collection", // type of the method argument - 0), // index of the method argument's type argument to extract + 0, // index of the method argument's type argument to extract + "containsAll", // method name + "java.util.Collection"), // method parameter new TypeArgOfMethodArgMatcher( "java.util.Collection", // class that defines the method - "removeAll(java.util.Collection)", // method signature 0, // index of the owning class's type argument to extract 0, // index of the method argument whose type argument to extract "java.util.Collection", // type of the method argument - 0), // index of the method argument's type argument to extract + 0, // index of the method argument's type argument to extract + "removeAll", // method name + "java.util.Collection"), // method parameter new TypeArgOfMethodArgMatcher( "java.util.Collection", // class that defines the method - "retainAll(java.util.Collection)", // method signature 0, // index of the owning class's type argument to extract 0, // index of the method argument whose type argument to extract "java.util.Collection", // type of the method argument - 0)); // index of the method argument's type argument to extract + 0, // index of the method argument's type argument to extract + "retainAll", // method name + "java.util.Collection")); // method parameter private static final ImmutableList STATIC_MATCHERS = ImmutableList.of( diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/collectionincompatibletype/MethodArgMatcher.java b/core/src/main/java/com/google/errorprone/bugpatterns/collectionincompatibletype/MethodArgMatcher.java index 5f5df71b7ca..83c47e8c81e 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/collectionincompatibletype/MethodArgMatcher.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/collectionincompatibletype/MethodArgMatcher.java @@ -46,12 +46,24 @@ final class MethodArgMatcher extends AbstractCollectionIncompatibleTypeMatcher { /** * @param typeName The fully-qualified name of the type whose descendants to match on - * @param signature The signature of the method to match on * @param typeArgIndex The index of the type argument that should match the method argument * @param methodArgIndex The index of the method argument that should match the type argument + * @param name The name of the method to match on + * @param firstParam The type of the first parameter of the method + * @param otherParams The types of any additional parameters of the method */ - MethodArgMatcher(String typeName, String signature, int typeArgIndex, int methodArgIndex) { - this.methodMatcher = instanceMethod().onDescendantOf(typeName).withSignature(signature); + MethodArgMatcher( + String typeName, + int typeArgIndex, + int methodArgIndex, + String name, + String firstParam, + String... otherParams) { + this.methodMatcher = + instanceMethod() + .onDescendantOf(typeName) + .named(name) + .withParameters(firstParam, otherParams); this.typeName = typeName; this.typeArgIndex = typeArgIndex; this.methodArgIndex = methodArgIndex; diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/collectionincompatibletype/TypeArgOfMethodArgMatcher.java b/core/src/main/java/com/google/errorprone/bugpatterns/collectionincompatibletype/TypeArgOfMethodArgMatcher.java index e78e11dbaf8..9c8e0e7ae56 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/collectionincompatibletype/TypeArgOfMethodArgMatcher.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/collectionincompatibletype/TypeArgOfMethodArgMatcher.java @@ -51,22 +51,30 @@ class TypeArgOfMethodArgMatcher extends AbstractCollectionIncompatibleTypeMatche /** * @param receiverTypeName The fully-qualified name of the type of the method receiver whose * descendants to match on - * @param signature The signature of the method to match on * @param receiverTypeArgIndex The index of the type argument that should match the method * argument * @param methodArgIndex The index of the method argument whose type argument we should extract * @param methodArgTypeName The fully-qualified name of the type of the method argument whose type * argument we should extract * @param methodArgTypeArgIndex The index of the type argument to extract from the method argument + * @param name The name of the method to match on + * @param firstParam The type of the first parameter of the method + * @param otherParams The types of any additional parameters of the method */ public TypeArgOfMethodArgMatcher( String receiverTypeName, - String signature, int receiverTypeArgIndex, int methodArgIndex, String methodArgTypeName, - int methodArgTypeArgIndex) { - this.methodMatcher = instanceMethod().onDescendantOf(receiverTypeName).withSignature(signature); + int methodArgTypeArgIndex, + String name, + String firstParam, + String... otherParams) { + this.methodMatcher = + instanceMethod() + .onDescendantOf(receiverTypeName) + .named(name) + .withParameters(firstParam, otherParams); this.receiverTypeName = receiverTypeName; this.receiverTypeArgIndex = receiverTypeArgIndex; this.methodArgIndex = methodArgIndex;