Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public boolean onOverrideMayBeNullExpr(
if (optLibraryModels.hasNullableReturn(methodSymbol, state.getTypes(), isMethodUnannotated)) {
return true;
}
if (!optLibraryModels.nullImpliesNullParameters(methodSymbol).isEmpty()) {
if (!optLibraryModels.nullImpliesNullParameters(methodSymbol, state.getTypes()).isEmpty()) {
return true;
}
return false;
Expand Down Expand Up @@ -294,8 +294,9 @@ public NullnessHint onDataflowVisitMethodInvocation(
setUnconditionalArgumentNullness(bothUpdates, node.getArguments(), callee, state, apContext);
setConditionalArgumentNullness(thenUpdates, elseUpdates, node, callee, state, apContext);
OptimizedLibraryModels optLibraryModels = getOptLibraryModels(state.context);
Types types = state.getTypes();
ImmutableSet<Integer> nullImpliesNullIndexes =
optLibraryModels.nullImpliesNullParameters(callee);
optLibraryModels.nullImpliesNullParameters(callee, types);
if (!nullImpliesNullIndexes.isEmpty()) {
// If the method is marked as having argument dependent nullability and any of the
// corresponding arguments is null, then the return is nullable. If the method is
Expand All @@ -310,7 +311,6 @@ public NullnessHint onDataflowVisitMethodInvocation(
}
return anyNull ? NullnessHint.HINT_NULLABLE : NullnessHint.FORCE_NONNULL;
}
Types types = state.getTypes();
if (optLibraryModels.hasNonNullReturn(callee, types, !isMethodAnnotated)) {
return NullnessHint.FORCE_NONNULL;
} else if (optLibraryModels.hasNullableReturn(callee, types, !isMethodAnnotated)) {
Expand Down Expand Up @@ -1074,13 +1074,6 @@ private static class DefaultLibraryModels implements LibraryModels {
.put(methodRef("java.util.Optional", "orElse(T)"), 0)
.put(methodRef("com.google.common.io.Closer", "<C>register(C)"), 0)
.put(methodRef("java.util.Map", "getOrDefault(java.lang.Object,V)"), 1)
// We add ImmutableMap.getOrDefault explicitly, since when
// AcknowledgeRestrictiveAnnotations is enabled, the explicit annotations in the code
// override the inherited library model
.put(
methodRef(
"com.google.common.collect.ImmutableMap", "getOrDefault(java.lang.Object,V)"),
1)
.put(methodRef("java.util.Objects", "toString(java.lang.Object,java.lang.String)"), 1)
.build();

Expand Down Expand Up @@ -1659,12 +1652,12 @@ boolean nameNotPresent(Symbol.MethodSymbol symbol) {
makeOptimizedNestedAnnotationLookup(names, models.nestedAnnotationsForMethods());
}

boolean hasNonNullReturn(Symbol.MethodSymbol symbol, Types types, boolean checkSuper) {
return lookupHandlingOverrides(symbol, types, nonNullRet, checkSuper) != null;
boolean hasNonNullReturn(Symbol.MethodSymbol symbol, Types types, boolean allowInherited) {
return lookupHandlingOverrides(symbol, types, nonNullRet, allowInherited) != null;
}

boolean hasNullableReturn(Symbol.MethodSymbol symbol, Types types, boolean checkSuper) {
return lookupHandlingOverrides(symbol, types, nullableRet, checkSuper) != null;
boolean hasNullableReturn(Symbol.MethodSymbol symbol, Types types, boolean allowInherited) {
return lookupHandlingOverrides(symbol, types, nullableRet, allowInherited) != null;
}

ImmutableSet<Integer> failIfNullParameters(Symbol.MethodSymbol symbol) {
Expand All @@ -1691,8 +1684,13 @@ ImmutableSet<MethodRef> ensuresNonNullIfTrueMethodCalls(Symbol.MethodSymbol symb
return lookupImmutableSet(symbol, ensuresNonNullIfTrueMethodCalls);
}

ImmutableSet<Integer> nullImpliesNullParameters(Symbol.MethodSymbol symbol) {
return lookupImmutableSet(symbol, nullImpliesNullParams);
ImmutableSet<Integer> nullImpliesNullParameters(Symbol.MethodSymbol symbol, Types types) {
Symbol.MethodSymbol modelSymbol =
lookupHandlingOverrides(
symbol, types, nullImpliesNullParams, /* allowInheritedModelLookup= */ true);
return modelSymbol == null
? ImmutableSet.of()
: lookupImmutableSet(modelSymbol, nullImpliesNullParams);
Comment thread
msridhar marked this conversation as resolved.
Resolved
}

ImmutableSet<Integer> castToNonNullMethod(Symbol.MethodSymbol symbol) {
Expand Down Expand Up @@ -1746,30 +1744,38 @@ private <T> NameIndexedMap<T> makeOptimizedLookup(
}

/**
* checks if symbol is present in the NameIndexedMap or if it overrides some method in the
* NameIndexedMap
* Checks if {@code symbol} is present in {@code optLookup}, optionally falling back to an
* overridden method in the lookup.
*
* <p>The method-name index avoids traversing supertypes when no model with the relevant name
* exists. A model on the method itself always takes precedence over an inherited model.
*
* @param symbol symbol to look up
* @param types for type operations
* @param optLookup map to check
* @param allowInheritedModelLookup true if we should look for a model in overridden methods
* @return the symbol for the method present in {@code optLookup}, possibly a method overriden
* by {@code symbol}, or {@code null} if no such method exists
*/
private static Symbol.@Nullable MethodSymbol lookupHandlingOverrides(
Symbol.MethodSymbol symbol,
Types types,
NameIndexedMap<Boolean> optLookup,
boolean checkSuperTypes) {
NameIndexedMap<?> optLookup,
boolean allowInheritedModelLookup) {
if (optLookup.nameNotPresent(symbol)) {
// no model matching the method name, so we don't need to check for overridden methods
return null;
}
if (optLookup.get(symbol) != null) {
return symbol;
}
if (checkSuperTypes == false) {
// Consider only a model on the exact class and method, used when checking annotated code
return null;
}
// For unannotated code, we allow a single model to cover all overriding implementations /
// subtypes
for (Symbol.MethodSymbol superSymbol : ASTHelpers.findSuperMethods(symbol, types)) {
if (optLookup.get(superSymbol) != null) {
return superSymbol;
// No exact model exists. If the model can be inherited, look for a model on an
// overridden method.
if (allowInheritedModelLookup) {
for (Symbol.MethodSymbol superSymbol : ASTHelpers.findSuperMethods(symbol, types)) {
if (optLookup.get(superSymbol) != null) {
return superSymbol;
}
}
}
return null;
Expand Down
9 changes: 9 additions & 0 deletions nullaway/src/test/java/com/uber/nullaway/FrameworkTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -2344,6 +2344,7 @@ public void mapGetOrDefault() {
String[] sourceLines =
new String[] {
"package com.uber;",
"import java.util.HashMap;",
"import java.util.Map;",
"import com.google.common.collect.ImmutableMap;",
"import org.jspecify.annotations.Nullable;",
Expand All @@ -2356,6 +2357,14 @@ public void mapGetOrDefault() {
" // BUG: Diagnostic contains: dereferenced",
" m.getOrDefault(\"key\", nullableString).toString();",
" }",
" void testGetOrDefaultHashMap(HashMap<String, String> m, String nonNullString, @Nullable String nullableString) {",
" m.getOrDefault(\"key\", \"value\").toString();",
" m.getOrDefault(\"key\", nonNullString).toString();",
" // BUG: Diagnostic contains: dereferenced",
" m.getOrDefault(\"key\", null).toString();",
" // BUG: Diagnostic contains: dereferenced",
" m.getOrDefault(\"key\", nullableString).toString();",
" }",
" void testGetOrDefaultImmutableMap(ImmutableMap<String, String> im, String nonNullString, @Nullable String nullableString) {",
" im.getOrDefault(\"key\", \"value\").toString();",
" im.getOrDefault(\"key\", nonNullString).toString();",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,67 @@ abstract class InvalidTest implements Map<String, String> {
.doTest();
}

@Test
public void mapGetOrDefaultModelInheritedByOverrides() {
makeHelper()
.addSourceLines(
"Test.java",
"""
import java.util.HashMap;
import java.util.Hashtable;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
@NullMarked
class Test {
static String viaInterface(Map<Long, String> m, long k) {
return m.getOrDefault(k, "");
}
static String viaTreeMap(TreeMap<Long, String> m, long k) {
return m.getOrDefault(k, "");
}
static String viaHashMap(HashMap<Long, String> m, long k) {
return m.getOrDefault(k, "");
}
static String viaLinkedHashMap(LinkedHashMap<Long, String> m, long k) {
return m.getOrDefault(k, "");
}
static String viaHashtable(Hashtable<Long, String> m, long k) {
return m.getOrDefault(k, "");
}
static String viaConcurrentHashMap(ConcurrentHashMap<Long, String> m, long k) {
return m.getOrDefault(k, "");
}
static void nullableDefaults(
Map<Long, String> map,
TreeMap<Long, String> treeMap,
HashMap<Long, String> hashMap,
LinkedHashMap<Long, String> linkedHashMap,
Hashtable<Long, String> hashtable,
ConcurrentHashMap<Long, String> concurrentHashMap,
long k,
@Nullable String defaultValue) {
// BUG: Diagnostic contains: dereferenced expression 'map.getOrDefault(k, defaultValue)' is @Nullable
map.getOrDefault(k, defaultValue).length();
// BUG: Diagnostic contains: dereferenced expression 'treeMap.getOrDefault(k, defaultValue)' is @Nullable
treeMap.getOrDefault(k, defaultValue).length();
// BUG: Diagnostic contains: dereferenced expression 'hashMap.getOrDefault(k, defaultValue)' is @Nullable
hashMap.getOrDefault(k, defaultValue).length();
// BUG: Diagnostic contains: dereferenced expression 'linkedHashMap.getOrDefault(k, defaultValue)' is @Nullable
linkedHashMap.getOrDefault(k, defaultValue).length();
// BUG: Diagnostic contains: dereferenced expression 'hashtable.getOrDefault(k, defaultValue)' is @Nullable
hashtable.getOrDefault(k, defaultValue).length();
// BUG: Diagnostic contains: dereferenced expression 'concurrentHashMap.getOrDefault(k, defaultValue)' is @Nullable
concurrentHashMap.getOrDefault(k, defaultValue).length();
}
}
""")
.doTest();
}

@Test
public void collectionToArrayOverrideUsesJSpecifyModel() {
makeHelper()
Expand Down
Loading