question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Nullability friendly builder

See original GitHub issue

Most of the static code analysis tools such as NullAway and infer throws error with the current builder setup.

There are 2 issues:

  1. Builder fields are not annotated.
  2. 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:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
benjamin-badercommented, Feb 4, 2018

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.

class ArbitBuilder {
  @Nullable String requiredField = null;
  @Nullable String optionalField = null;

  ArbitBuilder setRequiredField(@NonNull String required) {
    ...
  }

  ArbitBuilder setOptionalField(@Nullable String optionalField) {
    ...
  }
}

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?

0reactions
benjamin-badercommented, Feb 7, 2018

Fixed in #164.

Read more comments on GitHub >

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

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found