Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Android @NonNull and Jetbrains @NonNull

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 ?

like image 914
mbonnin Avatar asked May 23 '18 10:05

mbonnin


People also ask

What is org Jetbrains annotations 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.

What are Jetbrains annotations?

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.

What is @NonNull Java?

@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.

What does @nullable mean in Java?

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 ... }


1 Answers

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.

like image 177
Sagar Avatar answered Sep 29 '22 17:09

Sagar