Skip to content

Commit

Permalink
Migrate CollectionIncompatibleType from the deprecated `withSignatu…
Browse files Browse the repository at this point in the history
…re` to `named(...).withParameters(...)`.

PiperOrigin-RevId: 667601569
  • Loading branch information
cpovirk authored and Error Prone Team committed Aug 26, 2024
1 parent 78218f2 commit 0a5a5b8
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<BinopMatcher> STATIC_MATCHERS =
ImmutableList.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 0a5a5b8

Please sign in to comment.