uber / NullAway

A tool to help eliminate NullPointerExceptions (NPEs) in your Java code with low build-time overhead
MIT License
3.63k stars 293 forks source link

`Collectors.toMap` handling for streams #938

Closed msridhar closed 6 months ago

msridhar commented 6 months ago

Fixes #934

The key new thing with the support here is we have further nesting. Rather than a map method, where the relevant lambda is passed directly:

stream.filter(foo -> foo.bar != null).map(foo -> foo.bar.baz)

In this case we have a collect call, which gets as its argument the result of Collectors.toMap, and the relevant lambdas are passed to toMap:

stream
  .filter(foo -> foo.bar != null)
  .collect(Collectors.toMap(foo -> foo.bar.baz, foo -> foo.bar.other))

Supporting this requires some new types of logic in our streams handler (particularly because there are multiple relevant lambdas for a single collect call). We do also handle anonymous inner classes. I only added support for collecting into toMap for now; I'm not sure if there are other important cases.

I did a couple drive-by refactorings in this PR as I couldn't help it. I can undo them if they really make review more difficult, but I hope it's ok.

codecov[bot] commented 6 months ago

Codecov Report

Attention: Patch coverage is 96.51163% with 3 lines in your changes are missing coverage. Please review.

Project coverage is 87.13%. Comparing base (ce892d7) to head (3d985b7).

Files Patch % Lines
...nullaway/handlers/StreamNullabilityPropagator.java 95.16% 0 Missing and 3 partials :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #938 +/- ## ============================================ + Coverage 87.04% 87.13% +0.08% - Complexity 1995 2011 +16 ============================================ Files 77 78 +1 Lines 6447 6513 +66 Branches 1252 1264 +12 ============================================ + Hits 5612 5675 +63 Misses 422 422 - Partials 413 416 +3 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

msridhar commented 6 months ago

@lazaroclapp this is ready for another look. I added support for a couple more collector factory methods based on feedback in https://github.com/uber/NullAway/issues/934#issuecomment-2017282256. This required some generalization, as we couldn't support multiple factory methods for a single collect method before; see d317d0e and c6ac215. But the overall logical structure is the same.