Lombok: Custom @NotNull annotation makes Lombok do things

Created on 18 Jan 2018  Â·  6Comments  Â·  Source: projectlombok/lombok

Consider the following code:

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Thing {
  @interface NonNull {}

  @NonNull
  private String a;

  public static void main(String[] args) {
    Thing.builder()
        .build();
  }
}

This ends up with a NPE:

Exception in thread "main" java.lang.NullPointerException: a
    at com.remondis.renew.validation.test.Thing.<init>(Thing.java:9)
    at com.remondis.renew.validation.test.Thing$ThingBuilder.build(Thing.java:11)
    at com.remondis.renew.validation.test.Thing.main(Thing.java:22)

I would not expect that, because the custom NonNull annotation is not part of lombok and should do very different things!

Most helpful comment

According to me, this is probably a design/language constraint.

Related code links:
https://github.com/rzwitserloot/lombok/blob/master/src/core/lombok/core/handlers/HandlerUtil.java#L221
https://github.com/rzwitserloot/lombok/blob/master/src/core/lombok/javac/handlers/HandleConstructor.java#L287-L297

The same has been called out in the documentation as well.
https://projectlombok.org/features/NonNull reads :
Lombok has always treated any annotation named @NonNull on a field as a signal to generate a null-check if lombok generates an entire method or constructor

All 6 comments

@NonNull is implemented locally in the class to demonstrate that this
annonation is not part of lombok.

Am 19.01.2018 21:26 schrieb "Mithun" notifications@github.com:

@NonNull is part of lombok @NonNull
https://projectlombok.org/features/NonNull
Are you saying, this NonNull , you are using is something else ? Can you
post the full import statement to show which NonNull is this ?

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/rzwitserloot/lombok/issues/1564#issuecomment-359079795,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ANpawiKMXm3SKMSylx6h6JXHE2J5q_Biks5tMPqBgaJpZM4Rilg6
.

Right, sorry I think I misread somewhere and then realized, my question was unnecessary.

No problem ;-)

Am 19.01.2018 22:59 schrieb "Mithun" notifications@github.com:

Right, sorry I think I misread somewhere and then realized, my question
was unnecessary.

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/rzwitserloot/lombok/issues/1564#issuecomment-359100994,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ANpawt95NNzJiQb32EsDU_UuEjxgImFzks5tMRApgaJpZM4Rilg6
.

According to me, this is probably a design/language constraint.

Related code links:
https://github.com/rzwitserloot/lombok/blob/master/src/core/lombok/core/handlers/HandlerUtil.java#L221
https://github.com/rzwitserloot/lombok/blob/master/src/core/lombok/javac/handlers/HandleConstructor.java#L287-L297

The same has been called out in the documentation as well.
https://projectlombok.org/features/NonNull reads :
Lombok has always treated any annotation named @NonNull on a field as a signal to generate a null-check if lombok generates an entire method or constructor

Yes I read that a few months ago, but I could not believe that this is by design. Okay if lombok reacts on javax.validation.constraints.NotNull maybe that would not hurt. But you cannot simply treat all @NotNull the same way because lombok then makes it impossible to design other APIs independently. I think this is not a good idea.

This should be opt-in or opt-out for non-Lombok annotations.

Was this page helpful?
0 / 5 - 0 ratings