1

Here's the java-doc of NonNull annotation of Lombok:

If put on a parameter, lombok will insert a null-check at the start of the method / constructor's body, throwing a {@code NullPointerException} with the parameter's name as message. If put on a field, any generated method assigning a value to this field will also produce these nullchecks. Note that any annotation named {@code NonNull} with any casing and any package will result in nullchecks produced for generated methods (and the annotation will be copied to the getter return type and any parameters of generated methods), but only this annotation, if present on a parameter, will result in a null check inserted into your otherwise handwritten method.

WARNING: If the java community ever does decide on supporting a single {@code @NonNull} annotation (for example via JSR305), then this annotation will be deleted from the lombok package. If the need to update an import statement scares you, you should use your own annotation named {@code @NonNull} instead of this one.

What is the simplest way to have my own annotation, let's say NonNullNonnull, and Lombok to inject null-check based on my annotation?

Update: my question is hot to have an annotation to use for method arguments.

Rad
  • 4,292
  • 8
  • 33
  • 71
  • 1
    jsr305 seems to be dead, so I would not worry about this. https://stackoverflow.com/a/22215289/7465516 – julaine Dec 01 '22 at 13:58

1 Answers1

1

First, you need to name it nonNull (casing is irrelevant). NotNull will not be recognized by Lombok. Additionally you need to set another Lombok annotation (e.g. @Data, @Setter, ...), so that your type gets processed by Lombok.

Summarizing your custom annotation isn't probably as valuable as the @lombok.NonNull-annotation itself. An example where you benefit from the @lombok.NonNull-annotation, where your custom annotation wouldn't even be processed, is, when your type doesn't contain any other Lombok annotation, e.g.:

class NoLombokAnnotationsAnywhere {
  void testMe(@lombok.NonNull String nonNull) { /* .. */ }
}

will produce a NullPointerException as soon as you call new NoLombokAnnotationsAnywhere().testMe(null). Whereas this wouldn't throw anything with your custom annotation. Of course this only applies as long as you don't have any other Lombok annotations there. As soon as the type gets processed by Lombok, your annotation gets processed too.

If you have your own NonNull-annotation, then you can add just another Lombok-annotation that seems appropriate and Lombok adds a null-check for you, e.g.:

@Data
class NonNullData {
  @mycustom.Nonnull
  String value;
}

// Calling the following throws a NullPointerException as expected
new NonNullData(null);

You may also find the following issue relevant: Support annotations named @NotNull as well as @NonNull

Roland
  • 22,259
  • 4
  • 57
  • 84
  • Thanks for the answer. About `NotNull`, you're right. It was just a typo. Fixed. But about the rest of the answer: Using non-Lombok annotation only injects assertions when the method is being generated by Lombok (like your example `NonNullData`), otherwise it has no effect, even if you make the class process-able for Lombok (e.g. by adding `@Data`). I'ts stated clearly in the documentation. I updated my question to be clearer. Thanks. – Rad Apr 16 '17 at 12:48
  • I am not sure, whether I understand you correctly... your custom `Nonnull`-annotation only leads to a `NullPointerException` if the code is processed _and generated_ by Lombok. That's the case if something like `@Data` (but could also be `@AllArgsContructor`) is in place and if you place your custom `@nonnull`-annotation on a field. If you however supply any of the constructors / methods yourself and place the custom annotation on it, it will have no effect. For that however the `@lombok.NonNull` may work... Hopefully it's clearer now? – Roland Apr 18 '17 at 12:32
  • Yes and that's exactly my question, how to have Lombok to treat my annotation the same way it treats its own. If the you use Lombok non-null annotation, then it injects null-assertion for both auto-generated and hand-written code. Thanks. – Rad Apr 19 '17 at 17:47