Skip to content

Fix handling of reference to method with @Nullable parameter from library model - #1642

Merged
msridhar merged 4 commits into
masterfrom
nonnull-param-stream-issue
Jul 24, 2026
Merged

msridhar merged 4 commits into
masterfrom
nonnull-param-stream-issue

Conversation

@msridhar

@msridhar msridhar commented Jul 21, 2026 •

Copy link
Copy Markdown
Contributor

Consider the following case:

class Test {
  static boolean hasNonNullString(List<@Nullable String> strings) {
    return strings.stream()
        .filter(UnannotatedWithModels::isNonNull)
        .anyMatch(string -> true);
  }
}

Say that the parameter of UnannotatedWithModels::isNonNull is not declared with a @Nullable annotation, but should be treated as @Nullable due to a library model. In such a case, before we would still report a warning for the above code that UnannotatedWithModels::isNonNull cannot take a @Nullable parameter, since we weren't checking the library models in the right place. This PR corrects this issue. This fix will be relevant to uses of Objects::isNonNull in stream pipelines when we enable JDK library models.

Summary by CodeRabbit

Summary by CodeRabbit

  • Bug Fixes

    • Improved nullness analysis for method references used as functional-interface predicates by applying referenced-method parameter nullness overrides with the correct precedence.
  • Tests

    • Updated test library models to add an additional explicitly modeled parameter nullness case.
    • Added a regression test to verify modeled nullable parameters flow correctly through Stream.filter(...) when using method references (validated via anyMatch(...)).

@codecov

codecov Bot commented Jul 21, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.20%. Comparing base (bee56c9) to head (0f1d1b0).

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #1642      +/-   ##
============================================
+ Coverage     88.17%   88.20%   +0.03%     
- Complexity     3058     3060       +2     
============================================
  Files           105      105              
  Lines         10271    10282      +11     
  Branches       2073     2074       +1     
============================================
+ Hits           9056     9069      +13     
+ Misses          573      572       -1     
+ Partials        642      641       -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@msridhar
msridhar marked this pull request as ready for review July 21, 2026 01:23
@msridhar
msridhar requested a review from lazaroclapp July 21, 2026 01:23
@coderabbitai

coderabbitai Bot commented Jul 21, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: d4fff5ad-160e-4b3c-ae78-e26978d92595

📥 Commits

Reviewing files that changed from the base of the PR and between a1a802f and 0f1d1b0.

📒 Files selected for processing (3)
  • nullaway/src/main/java/com/uber/nullaway/NullAway.java
  • test-library-models/src/main/java/com/uber/nullaway/testlibrarymodels/TestLibraryModels.java
  • test-library-models/src/test/java/com/uber/nullaway/CustomLibraryModelsTests.java

Walkthrough

Method-reference override checking now propagates handler-provided nullness for the referenced method and gives it precedence when determining overriding parameter nullness. The library model marks UnannotatedWithModels.isNonNull parameter zero as explicitly nullable, and a JSpecify regression test covers its use as a Stream.filter method reference.

Possibly related PRs

  • uber/NullAway#1436: Modifies related member-reference override parameter nullness handling.
  • uber/NullAway#1438: Changes member-reference parameter nullness logic in the same override-checking function.

Suggested labels: jspecify

Suggested reviewers: lazaroclapp

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main fix: method references to library-modeled methods with nullable parameters.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch nonnull-param-stream-issue

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
nullaway/src/main/java/com/uber/nullaway/NullAway.java (1)

994-1014: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Map index to varargs parameter index before querying handler overrides.

The newly added logic queries referencedMethodParameterNullnessOverrides using methodParamInd. However, if the method reference targets a varargs method and the varargs are passed individually, methodParamInd can exceed the formal parameters' indices. Handlers like LibraryModelsHandler store the varargs element nullness at the index of the varargs array parameter (i.e. varargsIndex).

By passing the unmapped methodParamInd, getParameterNullness will return null for indices beyond the varargs array parameter, causing the handler-provided nullness override for varargs elements to be incorrectly ignored.

Move the getMemberRefParamIndex computation to the top of the method and use it to bound the index before querying the override.

🐛 Proposed fix
   private boolean paramOfOverridingMethodIsNonNull(
       VarSymbol paramSymbol,
       int methodParamInd,
       Symbol.@Nullable MethodSymbol overridingMethod,
       boolean isMethodAnnotated,
       `@Nullable` MemberReferenceTree memberReferenceTree,
       Type.@Nullable MethodType memberReferenceMethodType,
       `@Nullable` MethodParameterNullness referencedMethodParameterNullnessOverrides) {
+    int memberRefParamIndex =
+        getMemberRefParamIndex(methodParamInd, overridingMethod, memberReferenceTree);
     if (referencedMethodParameterNullnessOverrides != null) {
       Nullness parameterNullnessOverride =
-          referencedMethodParameterNullnessOverrides.getParameterNullness(methodParamInd);
+          referencedMethodParameterNullnessOverrides.getParameterNullness(memberRefParamIndex);
       if (parameterNullnessOverride != null) {
         // An explicit handler value takes precedence over annotations and generic substitution.
         return parameterNullnessOverride.equals(Nullness.NONNULL);
       }
     }
     boolean result = false;
     if (isMethodAnnotated) {
       result = !Nullness.hasNullableAnnotation(paramSymbol, config);
     } else if (config.acknowledgeRestrictiveAnnotations()) {
       // can still be `@NonNull` if there is a restrictive annotation
       result = Nullness.hasNonNullAnnotation(paramSymbol, config);
     }
     if (result && memberReferenceMethodType != null) {
       // when the overriding method is a member reference, also check that the parameter is not
       // effectively `@Nullable` due to generics.  memberReferenceMethodType should be the method type
       // of the member reference after handling generics
       com.sun.tools.javac.util.List<Type> parameterTypes =
           memberReferenceMethodType.getParameterTypes();
-      int memberRefParamIndex =
-          getMemberRefParamIndex(methodParamInd, overridingMethod, memberReferenceTree);
       Type paramType = parameterTypes.get(memberRefParamIndex);
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@nullaway/src/main/java/com/uber/nullaway/NullAway.java` around lines 994 -
1014, Move the getMemberRefParamIndex computation to the beginning of
paramOfOverridingMethodIsNonNull, then query
referencedMethodParameterNullnessOverrides.getParameterNullness using the mapped
index. Ensure varargs method-reference arguments beyond the formal varargs
parameter use the varargs array index, while preserving the existing annotation
and generic-substitution logic.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@nullaway/src/main/java/com/uber/nullaway/NullAway.java`:
- Around line 994-1014: Move the getMemberRefParamIndex computation to the
beginning of paramOfOverridingMethodIsNonNull, then query
referencedMethodParameterNullnessOverrides.getParameterNullness using the mapped
index. Ensure varargs method-reference arguments beyond the formal varargs
parameter use the varargs array index, while preserving the existing annotation
and generic-substitution logic.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 54256134-7590-466b-8314-beb79dc3e145

📥 Commits

Reviewing files that changed from the base of the PR and between b67e9d3 and 439fe21.

📒 Files selected for processing (3)
  • nullaway/src/main/java/com/uber/nullaway/NullAway.java
  • test-library-models/src/main/java/com/uber/nullaway/testlibrarymodels/TestLibraryModels.java
  • test-library-models/src/test/java/com/uber/nullaway/CustomLibraryModelsTests.java

@msridhar

msridhar commented Jul 21, 2026 •

Copy link
Copy Markdown
Contributor Author

The integration test failure is due to the new checking here. I put up a fix: spring-projects/spring-boot#51091

@msridhar
msridhar force-pushed the nonnull-param-stream-issue branch from a1a802f to 0f1d1b0 Compare July 24, 2026 01:44

@lazaroclapp lazaroclapp left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@msridhar
msridhar merged commit 72aab40 into master Jul 24, 2026
10 checks passed
@msridhar
msridhar deleted the nonnull-param-stream-issue branch July 24, 2026 02:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants