Mateusz Wierciński
Mateusz Wierciński

Reputation: 91

Lombok generating constructor without @Nonnull annotation

I'm using Lombok for generating my POJOs. I've got a bunch of fields annotated with javax.annotation.Nonnull and I expect this annotation to be transferred to the constructor automatically.
However, this is not happening, Lombok seems to omit this annotation at all and no warning is generated when using @Nullable value for this field.

When the constructor is generated by IDE - everything works as expected. This also works for getters/setters.
I've tried adding javax.annotation.Nonnull to lombok.copyableAnnotations and adding javax.annotation.ParametersAreNonnullByDefault annotation, all to no avail.

@Data
class TestObject {
  @Nonnull private String nonnull;
}

TestObject testObject = new TestObject(null); <-- no warning
testObject.setNonnull(null); <-- warning generated

I expect to receive the same warning on a constructor as I'm getting on a setter. Is that a bug in Lombok, something impossible to achieve, or am I simply missing something?

Upvotes: 0

Views: 684

Answers (1)

pirho
pirho

Reputation: 12215

It is because Lombok "scans" only @lombok.NonNull. See below example:

@lombok.RequiredArgsConstructor
public class LombokNoNull {
    @lombok.NonNull
    private String lombokRequires;
    @javax.validation.constraints.NotNull
    private String lombokDoesNotRequire;
    @javax.annotation.Nonnull
    private String lombokDoesNotRequireEither;

    public static LombokNoNull newInstance() {
        // for this class the required args constructor contains only field
        // "lombokRequires"
        return new LombokNoNull("stringRequiredByLombok");
    }
}

Upvotes: 2

Related Questions