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)?
@NotNull The @NotNull annotation is, actually, an explicit contract declaring that: A method should not return null. Variables (fields, local variables, and parameters) cannot hold a null value.
The nonNull method is a static method of the Objects class in Java that checks whether the input object reference supplied to it is non-null or not. If the passed object is non-null, then the method returns true. If the passed object is null , then the method returns false.
On the other hand, if the parameter is mark as @Nullable and we don't add null check inside the function, Android Studio will warn you with lint error and some visual hint. The @NonNull/@Nullable annotation can put in front of functions as well to indicate the return value nullability.
notNull() is a static method of the Validate class that is used to check whether the passed object is null or not. If the passed object is null , then the method raises an exception with a formatted message. If the passed object is not null , then the method returns the input as it is.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With