The most common convention is you describe returns by putting the @Nullable/@NotNull before everything in the method declaration, e.g. before the public. You describe parameters by putting the @Nullable/@NotNull before everything in the parameter e.g. before the final.
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; ... // placementof @Nullable and @NotNull @NotNull private String expand( @NotNull File targetFile, @Nullable String image) { ... }
If your code violates the annotations, you will be warned to add specific code for null checking, possibly an assert or if ( var != null ) perhaps overly conservatively. If you don’t fix it, the code will still run, but could possibly throw a NullPointerException.
It is not as though you have to write more code than you would normally (other than the annotations themselves). The annotations just remind you about code you forgot to write.If you am passing in a value to one method that requires � @NotNull parameters, and that value was obtained by another method whose return value is marked @NotNull, you need not write an explicit null check. That saves you coding and reduces the need for overly conservative defensive programming.
Of course you are not obligated to mark every return, parameter and variable with an annotation. The unmarked ones you deal with at run time with a NullPointerException and/or with explicit null-checking code.
This sounds simple enough but there are quite a number of steps and complications to getting it to work:
At this point all will work fine, except that if you reorganize your imports the following lines will disappear:
// Disappearing imports for @NotNull and @Nullable import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable
What you have to do now is configure the auto-import function to ignore these imports:
|
|
You can get the freshest copy of this page from: | or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror) |
| http://mindprod.com/jgloss/atnullable.html | J:\mindprod\jgloss\atnullable.html | |
![]() | Please email your feedback for publication,
letters to the editor, errors, omissions, typos, formatting errors, ambiguities, unclear wording,
broken/redirected link reports, suggestions to improve this page or comments to
Roedy Green :
| |
| Canadian Mind Products | ||
| mindprod.com IP:[65.110.21.43] | ||
| view Blog | Your face IP:[38.107.179.212] | |
| Feedback | You are visitor number 11. | |