vandench
vandench

Reputation: 2257

Does @NotNull work at runtime

I have recently decided to start using null checksum annotations (@NotNull), I use Intellij Idea's annotations library to get access these. I am trying to figure out whether or not these annotations will work at runtime to check if values are null and if so how does it handle these errors (e.g. does it throw a NullPointerException, does it just return a default value (e.g. null, 0, false)). Also if these annotations do not work is there a different set that may be more standardized annotations that will work at runtime (e.g. javax)? If these do not work should I stop using them and return to a standard null checksum (if(x == null)) or should use both the annotations as well as standard null checksums? And while I am here are annotations like @Nullable a good idea?

Upvotes: 3

Views: 3273

Answers (2)

John Bollinger
John Bollinger

Reputation: 181714

Also if these annotations do not work is there a different set that may be more standardized annotations that will work at runtime (e.g. javax)? If these do not work should I stop using them and return to a standard null checksum (if(x == null)) or should use both the annotations as well as standard null checksums?

The problem with relying on annotations for runtime behavior is that the correctness of your program then depends on the expected form of annotation processing being performed. For null checking, for example, you need an annotation processor (possibly integrated into your compiler) to recognize the particular annotation at or before compile time, and to take the action you intend: inserting null-checking code.

Although some annotations are retained at runtime, you do not get runtime behavior directly from that. Rather, such annotations and their properties can be examined and acted upon by code that happens to reflectively analyze your classes. Basically, you get nothing from runtime retention of annotations except from code that actively looks for them.

If you require runtime null checking then you should insert explicit checks. If you are willing to rely on annotations and associated static analysis to ensure that null checks are unnecessary, then that's your call. Among the considerations you should keep in mind are:

  • Annotations on public methods and their parameters, or on public fields, cannot reliably protect statically against third parties feeding nulls to your code, because they don't need to build their code using tools that perform the appropriate static analysis.

  • Annotations also do not protect against nulls being fed to your methods reflectively or via native code, as both of these approaches bypass the compiler and any associated static analysis altogether.

  • People tend to make too big a deal out of NullPointerExceptions. There is little, if any, objective advantage to being notified of misuse by an IllegalArgumentException (say) instead of a NullPointerException. NPE describes the nature of the problem more specifically, whereas IAE describes the context of the problem more specifically. The important thing is to ensure that the problem is detected early.

And while I am here are annotations like @Nullable a good idea?

They add an external dependency to your code, which is a cost that should be taken into consideration. They express your expectations specifically and machine-actionably, which is a corresponding benefit. If the annotation library you're using is readily available to everyone to whom you expect to distribute source, and if those annotations having runtime retention are readily available to everyone whom you expect to run your code, then the annotations are probably a net benefit. Otherwise, they are a non-starter.

Upvotes: 0

Bas Leijdekkers
Bas Leijdekkers

Reputation: 26577

Yes, it checks at runtime and throws an IllegalArgumentException when null.

You can disable/enable this in the settings Preferences | Build, Execution, Deployment | Compiler. Add runtime assertions for notnull-annotated methods and parameters checkbox, second from the top

Upvotes: 5

Related Questions