简体   繁体   中英

What does using @NonNull on a void method do?

What's the point in Lombok annotation @NonNull for a method?

class MyClass {
    @NonNull
    void run() {
        // code here
    }
}

Are we check the instance of class MyClass (MyClass obj == null)?

For a void method, there's no point.

For a method with a return type, it shows that it won't return null:

@NonNull String foo() {
    // I promise I won't return null herre!
}

Judging by the tutorial , it looks like it won't generate a check within the method to ensure that it isn't null, but it tells any caller that they can realistically expect a non-null value to be returned.

You can use @NonNull on the parameter of a method or constructor to have lombok generate a null-check statement for you.

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 for you, via for example @Data . Now, however, using lombok's own @lombok.NonNull on a parameter results in the insertion of just the null-check statement inside your own method or constructor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM