Skip to content

Commit

Permalink
Add support for Map.putIfAbsent. (#568)
Browse files Browse the repository at this point in the history
From the perspective of nullability and Map access paths, this method is equivalent to `Map.put(...)`.
  • Loading branch information
lazaroclapp authored Feb 9, 2022
1 parent e1cae62 commit 36586e4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,8 @@ public static boolean isContainsKey(Symbol.MethodSymbol symbol, VisitorState sta
}

public static boolean isMapPut(Symbol.MethodSymbol symbol, VisitorState state) {
return NullabilityUtil.isMapMethod(symbol, state, "put", 2);
return NullabilityUtil.isMapMethod(symbol, state, "put", 2)
|| NullabilityUtil.isMapMethod(symbol, state, "putIfAbsent", 2);
}

/**
Expand Down
20 changes: 20 additions & 0 deletions nullaway/src/test/java/com/uber/nullaway/NullAwayCoreTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,26 @@ public void testMapGetChainWithCast() {
.doTest();
}

@Test
public void testMapPutAndPutIfAbsent() {
defaultCompilationHelper
.addSourceLines(
"Test.java",
"package com.uber;",
"import java.util.Map;",
"class Test {",
" Object testPut(String key, Object o, Map<String, Object> m){",
" m.put(key, o);",
" return m.get(key);",
" }",
" Object testPutIfAbsent(String key, Object o, Map<String, Object> m){",
" m.putIfAbsent(key, o);",
" return m.get(key);",
" }",
"}")
.doTest();
}

@Test
public void tryFinallySupport() {
defaultCompilationHelper.addSourceFile("NullAwayTryFinallyCases.java").doTest();
Expand Down

0 comments on commit 36586e4

Please sign in to comment.