uber/NullAway

Support for `collect()` in Streams API

jdcaperon opened this issue · 4 comments

How hard would it be to support nullability within stream reducers such as collect()?

https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#collect-java.util.stream.Collector-

class Scratch {
  public class Foo {
    @Nullable
    private final String bar;
    private final String baz;
    public Person(String bar, String baz) {
      this.bar = bar;
      this.baz = baz;
    }
  }

  void streams() {
    List<Person> foos = new ArrayList<>();
    foos.stream()
        .filter(f -> f.bar != null)
        // NullAway complains about nullability of name even though name is enforced
        .collect(Collectors.toMap(f -> f.bar, f -> f.baz));
  }
}

I had a look at how I might support this in the existing model but I think this would require supporting a new kind of method class (reductions) on streaming models. Understandably this might be a bit hard...

Hrm, I can't repro using NullAway's default settings. Here's my version:

msridhar/nullaway-java-sample@d54b686

Can you share what settings you are using?

Hi @msridhar, I am not able to reproduce the issue in your environment, however I did miss a detail about a function call within a collect lambda that might be a different issue. I am still having the original issue in my env.

https://github.com/jdcaperon/nullaway-java-sample/pull/1/files

As for the original issue, which is still failing in my environment.

server/Scratch.java:24: error: [NullAway] passing @Nullable parameter 'f.bar' where @NonNull is required
        .collect(Collectors.toMap(f -> Transformer.transform(f.bar), f -> f.baz));

settings

Let me get back to you on this I might be able to repro in your env with our settings.

Version: 0.10.24

Do you want all ErrorProne settings or just the NullAway ones?

common --javacopt="-Xep:NullAway:ERROR"
common --javacopt="-XepOpt:NullAway:AnnotatedPackages=com.canva"
common --javacopt="-XepOpt:NullAway:ErrorURL=..."
common --javacopt="-XepOpt:NullAway:ExternalInitAnnotations=com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable"
common --javacopt="-XepOpt:NullAway:ExcludedClassAnnotations=javax.annotation.processing.Generated"

Given it's not reproducable on the default I can try changing settings until it works on my end 👍

@msridhar I think this is a confusion on my end. I thought I had an example but apparently not. I think this may be an extension of: #332

I'm going to close this ticket to avoid confusion, but I can re-raise it with the specific example of streams failure if needed.

Ok, thanks, @jdcaperon! Please report back if you have other use cases, and we can look into them.