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

@Builder should respect @Nullable annotation #1570

Closed
lanusau opened this issue Feb 2, 2018 · 5 comments
Closed

@Builder should respect @Nullable annotation #1570

lanusau opened this issue Feb 2, 2018 · 5 comments

Comments

@lanusau
Copy link

lanusau commented Feb 2, 2018

Just like @Setter, @builder should copy @nullable annotation to the method that sets the value for the field.

For example, having this class:

@Builder
public class LombokNullable {
    @Nullable  private String stringValue;
}

Lombok generates:

public class LombokNullable {
    @Nullable  private String stringValue;

    @java.beans.ConstructorProperties({"stringValue"})
    LombokNullable(String stringValue) {
        this.stringValue = stringValue;
    }

    public static LombokNullableBuilder builder() {
        return new LombokNullableBuilder();
    }

    public static class LombokNullableBuilder {
        private String stringValue;

        LombokNullableBuilder() {
        }

        public LombokNullableBuilder stringValue(String stringValue) {
            this.stringValue = stringValue;
            return this;
        }

        public LombokNullable build() {
            return new LombokNullable(stringValue);
        }

        public String toString() {
            return "LombokNullable.LombokNullableBuilder(stringValue=" + this.stringValue + ")";
        }
    }

The method "public LombokNullableBuilder stringValue(String stringValue) {" should copy @nullable annotation to the parameter:
public LombokNullableBuilder stringValue(@Nullable String stringValue) {

Without this, IntelliJ's null inspection will flag this as a warning:
LombokNullable.builder().stringValue(methodAnnotatedWithNullable()

@voidengineer
Copy link

The same should be done for @nonnull annotations.
Further .builder() and .build() should be @nonnull too.

@rzwitserloot
Copy link
Collaborator

Fixed in latest edge, thanks to @wmdietl :)

https://projectlombok.org/download-edge

@Olard
Copy link

Olard commented Nov 20, 2018

There still is the problem that the annotations are not copied to the fields in the builder. This triggers the eclipse null analysis :-(

@nithatech
Copy link

error: [NullAway] assigning @nullable expression to @nonnull field
@builder

@ondrej-simon
Copy link

This unfortunately still is not fixed and basically makes builder unusable along Kotlin, which introduces strict nullability detection.

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

6 participants