Java – use notnull annotation in method parameters
I just started using java 8's @ notnull annotation and got some unexpected results
I have this method:
public List<Found> findStuff(@NotNull List<Searching> searchingList) { ... code here ... }
I wrote a JUnit test that passed the null value of the searchlist parameter I expect some kind of error, but it seems like the comment is not there Is this expected behavior? From my understanding, this allows you to skip writing template empty check code
The explanation of what @ notnull should do will be appreciated
Solution
@Nullable and @ notnull do nothing themselves They should be used as documentation tools
@The nullable annotation reminds you that NPE checks need to be introduced in the following cases:
>Can return null calling method. > Dereference nullable variables (fields, local variables, parameters)
In fact, the @ notnull comment is an explicit contract, stating as follows:
>A method should not return null. > Variables (such as fields, local variables, and parameters) cannot remain null
Example:
Instead of writing:
/** * @param aX should not be null */ public void setX( final Object aX ) { ... }
You can use
public void setX( @NotNull final Object aX ) { ... }