Nullability friendly builder
See original GitHub issueMost of the static code analysis tools such as NullAway and infer throws error with the current builder setup.
There are 2 issues:
- Builder fields are not annotated.
- Builder fields can’t be non-null with the current setup since the constructor is usually empty.
public Builder() {}
public Builder nonNullField(@NonNull String nonNullField) {}
public Builder nullableField(@Nullable String nullableField) {}
An alternate way will be to set all the required values in Builder’s constructor and only have separate methods for Nullable fields.
public Builder(@NonNull String nonNullField) {}
public Builder nullableField(@Nullable String nullableField) {}
Since this breaks backward compatibility it needed to be flagged.
Complete class - https://gist.github.com/bangarharshit/c4ec0bee365cc4ef30270f10d4d1aa2f
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Builder set methods null friendly · Issue #1451 - GitHub
I'm using protobuf 3 beta version(Java) and I'm wondering if it wouldn't be better to make Builder's setter methods null friendly.
Read more >Part 2: Avoiding Null-Pointer Exceptions in a Modern Java ...
Don't use null to implicitly indicate errors — be explicit and throw an exception. Use method overloading and the Builder pattern to avoid ......
Read more >Changing NULL values with model builder - Esri Community
I'm using model builder to build a tool, where at a certain point, I need to take the Z elevation values from the...
Read more >How do I...
How do I... This page answers common how-to questions that may come up when using AutoValue. You should read and understand the Introduction...
Read more >AI builder extracting all the information from Invoice but not ...
So I have made a flow that uses AI builder and extracts data from ... amount" column is = null, for both invoice...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks for the demo project! I see that NullAway indeed doesn’t like the builder fields being without annotations.
I think it’s interesting that there’s no warning in the
Arbit
constructor - looks like the tool is smart enough to see that the ctor is manually checking for null and throwing? In that case, how about just annotating every builder field as@Nullable
, and annotating the setter parameters according to the field’s requiredness? e.g.This way, the tool would be satisfied, and the annotations themselves would still be honest. Does that sound like it would work? What do you think?
Fixed in #164.