What is the difference between these two annotations ?
org.jetbrains.annotations.NonNull
android.support.annotation.NonNull
Is the kotlin compiler IntelliJ going to honor android.support.annotation.NonNull
and add assertions to check if I pass a null parameter to the corresponding java function ?
Is the Android lint going to honor org.jetbrains.annotations.NonNull
and show me a warning if I check a variable that's already NonNull ?
When should I use one over the other ?
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.
Java annotations are pieces of metadata that provide information about the code they are used with, and can instruct the IDE how to process this code. In Java, there is a set of built-in annotations. On top of that, IntelliJ IDEA provides a collection of annotations that are available as a separate project.
@NonNull – The compiler can determine cases where a code path might receive a null value, without ever having to debug a NullPointerException. @ReadOnly – The compiler will flag any attempt to change the object.
The @NonNull/@Nullable annotation can put in front of functions as well to indicate the return value nullability. @NonNull. public A getA() { return null; // warn: 'null' is returned by the method ... }
Using org.jetbrains.annotations.NonNull
IntelliJ IDEA spots that the contract is being violated. Done only if you are using IDE.
Whereas
android.support.annotation.NonNull
is taken care by inspection tools like Lint. This is useful if you are running the inspection without using Android Studio or other IDE (e.g. Running through command line or through Jenkins)
Edit:
Based on the documentation highlighted by @user2340612
Kotlin compiler supports both of them. Since its supported at compiler level, it should work from Command-Line too.
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