Skip to content

Commit

Permalink
removeIf
Browse files Browse the repository at this point in the history
  • Loading branch information
msridhar committed Apr 24, 2024
1 parent 282c55a commit f139ef7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
Expand Up @@ -12,22 +12,27 @@
import com.sun.source.tree.Tree;
import com.sun.source.util.TreePath;
import com.sun.tools.javac.code.Symbol;
import com.uber.nullaway.LibraryModels;
import com.uber.nullaway.LibraryModels.MethodRef;
import com.uber.nullaway.dataflow.AccessPath;
import java.util.function.Predicate;

public class SynchronousCallbackHandler extends BaseNoOpHandler {

// TODO this should work on subtypes of the methods as well, like java.util.HashMap. Use a
// Matcher?
private static final ImmutableMap<String, ImmutableMap<LibraryModels.MethodRef, Integer>>
private static final ImmutableMap<String, ImmutableMap<MethodRef, Integer>>
METHOD_NAME_TO_SIG_AND_PARAM_INDEX =
ImmutableMap.of(
"forEach",
ImmutableMap.of(
LibraryModels.MethodRef.methodRef(
MethodRef.methodRef(
"java.util.Map",
"forEach(java.util.function.BiConsumer<? super K,? super V>)"),
0),
"removeIf",
ImmutableMap.of(
MethodRef.methodRef(
"java.util.Collection", "removeIf(java.util.function.Predicate<? super E>)"),
0));

@Override
Expand All @@ -46,9 +51,9 @@ public Predicate<AccessPath> getAccessPathPredForSavedContext(TreePath path, Vis
}
String invokedMethodName = symbol.getSimpleName().toString();
if (METHOD_NAME_TO_SIG_AND_PARAM_INDEX.containsKey(invokedMethodName)) {
ImmutableMap<LibraryModels.MethodRef, Integer> entriesForMethodName =
ImmutableMap<MethodRef, Integer> entriesForMethodName =
METHOD_NAME_TO_SIG_AND_PARAM_INDEX.get(invokedMethodName);
for (LibraryModels.MethodRef methodRef : entriesForMethodName.keySet()) {
for (MethodRef methodRef : entriesForMethodName.keySet()) {
if (symbol.toString().equals(methodRef.fullMethodSig)
&& ASTHelpers.isSubtype(
symbol.owner.type, state.getTypeFromString(methodRef.enclosingClass), state)) {
Expand Down
22 changes: 22 additions & 0 deletions nullaway/src/test/java/com/uber/nullaway/SyncLambdasTests.java
Expand Up @@ -86,4 +86,26 @@ public void otherForEach() {
"}")
.doTest();
}

@Test
public void removeIf() {
defaultCompilationHelper
.addSourceLines(
"Test.java",
"package com.uber;",
"import java.util.List;",
"import java.util.ArrayList;",
"import org.jspecify.annotations.Nullable;",
"public class Test {",
" private @Nullable Object f;",
" public void test1() {",
" if (this.f == null) {",
" throw new IllegalArgumentException();",
" }",
" List<Object> l = new ArrayList<>();",
" l.removeIf(v -> this.f.toString().equals(v.toString()));",
" }",
"}")
.doTest();
}
}

0 comments on commit f139ef7

Please sign in to comment.