Skip to content

Add support for filtering null objects from streams - #1597

Merged
msridhar merged 11 commits into
masterfrom
issues-1578-and-1594
Jun 11, 2026
Merged

msridhar merged 11 commits into
masterfrom
issues-1578-and-1594

Conversation

@msridhar

@msridhar msridhar commented Jun 10, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #1578
Fixes #1594

Now, if we have a Stream<@Nullable Foo> s and the code invokes s.filter(Objects::nonNull), the result type is Stream<Foo>. This works for any method that has an appropriate library model or any method annotated @Contract(null -> false).

This PR does not handle lambdas, e.g., s.filter(x -> x != null); we can add such support in a follow-up if it comes up often.

Main support is in StreamNullabilityPropagator.onOverrideMethodType. To identify filtering methods, we add a new Handler method isSingleArgNullImpliesFalseMethod and implement in LibraryModelsHandler and ContractHandler.

Summary by CodeRabbit

  • New Features

    • Stream filter operations now refine element nullability when using null-rejecting predicates (including contract- or library-model-backed predicates).
    • Call-site invocation context is now propagated to nullability handlers, enabling more precise, site-aware inference and special-case detection of single-arg null->false methods.
  • Tests

    • Added tests validating stream filter refinement, contract-based and library-model predicates, custom predicates, and non-refining filters.

@msridhar

Copy link
Copy Markdown
Collaborator Author

This change is part of the following stack:

Change managed by git-spice.

@msridhar msridhar changed the title Add handling of filtering null objects from streams Add support for filtering null objects from streams Jun 10, 2026
@coderabbitai

coderabbitai Bot commented Jun 10, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

Adds an optional MethodInvocationTree parameter to Handler.onOverrideMethodType and a new isSingleArgNullImpliesFalseMethod hook, propagates invocation-site context through GenericsChecks call sites, wires the StreamNullabilityPropagator into handler initialization, implements onOverrideMethodType in that propagator to refine Stream element nullability for null-rejecting filters, and adds tests exercising Objects::nonNull, custom predicates, and @Contract-based predicates.

Possibly related PRs

  • uber/NullAway#1438: Overlaps at GenericsChecks handler invocation call sites and routing of method-derived types through handler overrides.
  • uber/NullAway#1553: Also modifies compareGenericTypeParameterNullabilityForCall and member-reference generic-inference handling.
  • uber/NullAway#1499: Overlaps on getMemberReferenceMethodType and compareGenericTypeParameterNullabilityForCall changes for method references.

Suggested labels

jspecify

Suggested reviewers

  • lazaroclapp
  • yuxincs
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 31.82% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Add support for filtering null objects from streams' accurately summarizes the main change: implementing stream filter refinement for null-rejecting predicates.
Linked Issues check ✅ Passed The PR implements specialized support for Stream.filter with null-rejecting functions [#1578] and refines nullability inference for stream terminal operations [#1594] via the new onOverrideMethodType hook and isSingleArgNullImpliesFalseMethod handler.
Out of Scope Changes check ✅ Passed All changes are directly scoped to implementing stream filter nullability refinement and the handler infrastructure needed to support it, with no unrelated modifications detected.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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 issues-1578-and-1594

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 and usage tips.

@codecov

codecov Bot commented Jun 10, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 70.66667% with 22 lines in your changes missing coverage. Please review.
✅ Project coverage is 87.99%. Comparing base (0f2c373) to head (4a1bb27).

Files with missing lines Patch % Lines
...er/nullaway/handlers/contract/ContractHandler.java 16.66% 3 Missing and 7 partials ⚠️
...nullaway/handlers/StreamNullabilityPropagator.java 78.04% 3 Missing and 6 partials ⚠️
...a/com/uber/nullaway/handlers/CompositeHandler.java 66.66% 1 Missing and 1 partial ⚠️
...m/uber/nullaway/handlers/LibraryModelsHandler.java 50.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #1597      +/-   ##
============================================
- Coverage     88.12%   87.99%   -0.13%     
- Complexity     3001     3022      +21     
============================================
  Files           105      105              
  Lines          9952    10021      +69     
  Branches       2021     2041      +20     
============================================
+ Hits           8770     8818      +48     
- Misses          565      572       +7     
- Partials        617      631      +14     

☔ 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 requested a review from lazaroclapp June 10, 2026 14:55

@lazaroclapp lazaroclapp left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks entirely good to me :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants