Repository navigation
Description
Since #1758, an explicit @Nullable return annotation on an override of a method that carries a nullImpliesNull library model is ignored in annotated code, and the dereference error NullAway used to report disappears. The regression is on 0.14.1-SNAPSHOT only; 0.14.0 does not have it.
Reproducer
Added to a class extending NullAwayTestsBase, so com.uber is an annotated package:
@Test
public void annotatedOverrideWithExplicitNullableReturn() {
defaultCompilationHelper
.addSourceLines(
"Probe.java",
"package com.uber;",
"import java.util.HashMap;",
"import javax.annotation.Nullable;",
"class MyMap extends HashMap<String, String> {",
" @Override",
" public @Nullable String getOrDefault(Object key, String defaultValue) { return null; }",
"}",
"class Probe {",
" void f(MyMap m) {",
" // BUG: Diagnostic contains: dereferenced",
" m.getOrDefault(\"a\", \"b\").length();",
" }",
"}")
.doTest();
}At f455521, the parent of the squash merge, the test passes: NullAway reports the dereference. At 94003d2 it fails with Did not see an error on line 11 matching dereferenced. There were no errors.
Mechanism
OptimizedLibraryModels.nullImpliesNullParameters hard-codes the inheritance flag (LibraryModelsHandler.java:1687):
ImmutableSet<Integer> nullImpliesNullParameters(Symbol.MethodSymbol symbol, Types types) {
Symbol.MethodSymbol modelSymbol =
lookupHandlingOverrides(
symbol, types, nullImpliesNullParams, /* allowInheritedModelLookup= */ true);hasNonNullReturn and hasNullableReturn beside it receive !isMethodAnnotated, so MyMap.getOrDefault inherits Map.getOrDefault's model although MyMap is annotated code. onDataflowVisitMethodInvocation tests nullImpliesNull before it consults isMethodAnnotated at all, and returns FORCE_NONNULL because the default argument is non-null. NullAway.mayBeNullExpr ends with return exprMayBeNull && nullnessFromDataflow(state, expr); (NullAway.java:2957), so that FORCE_NONNULL wins over the annotation the user wrote.
Two places state the older rule and still stand: the comment at LibraryModelsHandler.java:229-233, a dozen lines above the changed call site, and the javadoc on LibraryModels#nullImpliesNullParameters, which describes the model as equivalent to @Contract("!null -> !null") @Nullable on the listed method and says nothing about overriders.
Scope
java.util.Map.getOrDefault is the only entry in NULL_IMPLIES_NULL_PARAMETERS (LibraryModelsHandler.java:1071) that user code can override: Class.cast and Optional.orElse sit on final classes, Closer is final, and Objects.toString is static. An entry supplied through the LibraryModels SPI has no such limit, and one placed on an interface now covers every implementation in the user's own annotated code.
The question
Should an explicit @Nullable return in annotated code win over a nullImpliesNull model inherited from an overridden library method?
If it should, lookupHandlingOverrides already returns the model symbol, so the caller can distinguish an inherited model from an exact one and drop the inherited one when the callee is annotated and carries a nullable return annotation. That leaves #1758's fix in place, since HashMap.getOrDefault's nullability comes from the astubx nullableRet model rather than a source annotation. I can send a PR with that plus the two documentation updates.
If the current behavior is what you want, the comment and the javadoc still need to change, and a test should pin it.
Activity
msridhar commentedon Aug 28, 2026
Hi @vlsi the right fix here is to check contract overriding, which is #803. What we should be enforcing is:
MyMap.getOrDefaultshould be declared with a_, !null -> !nullcontract; the missing contract itself should be an error.MyMap.getOrDefault.For this case, we also need to reason about library models, of course, since there is no explicit contract on
Map.getOrDefault.We should fix this, but I don't think it's the highest priority. Do you have specific changes to documentation you'd like to see in the meantime?
vlsi commentedon Aug 28, 2026
That framing makes sense: the model is the contract, and the missing piece is verification of the override rather than precedence between the model and the annotation.
Two comments state the older rule, and both sit where I looked while working out what happened. Concretely:
On the first one: the comment cites #445, where the whole point was that an annotated subclass narrowing
Throwable.getMessage()must win over the inherited model. Since #1758 that rule holds for return nullness and not fornullImpliesNull, so the comment reads as a claim about both. The rewrite records the asymmetry as intentional, which is what your answer says it is.On the second:
LibraryModelsis a published SPI, so the reader implementing it cannot look at the lookup code to find out that a model on an interface method reaches every implementation, or that nothing checks those implementations yet.Happy to send this as a PR.
msridhar commentedon Aug 28, 2026
Yes, please send as a PR, thanks!
Document that a nullImpliesNull model applies to overriding methods (#…
msridhar commentedon Aug 29, 2026
I'm going to close this one in favor of #803