Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for collect() in Streams API #933

Closed
jdcaperon opened this issue Mar 14, 2024 · 4 comments
Closed

Support for collect() in Streams API #933

jdcaperon opened this issue Mar 14, 2024 · 4 comments

Comments

@jdcaperon
Copy link

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...

@msridhar
Copy link
Collaborator

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?

@jdcaperon
Copy link
Author

jdcaperon commented Mar 14, 2024

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 👍

@jdcaperon
Copy link
Author

@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.

@msridhar
Copy link
Collaborator

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

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

No branches or pull requests

2 participants