Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 344
van dench Avatar asked Apr 02 '26 21:04

van dench


1 Answers

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

like image 120
Bas Leijdekkers Avatar answered Apr 04 '26 09:04

Bas Leijdekkers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!