Hello @gayanper,
First, what annotation are you using? Depending on if we are supporting it or not, you might or might not see the expected behavior.
Then, the javax.annotation.Nullable documentation gives hints to answer your question:
In general, this means developers will have to read the documentation to determine when a null value is acceptable and whether it is necessary to check for a null value.
Static analysis tools should generally treat the annotated items as though they had no annotation, […]
Use CheckForNull
to indicate that the element value should always be checked for a null value.
We sometimes refer to them as “Weak Nullable” (javax.annotation.Nullable
), as opposed to “Strong nullable” (javax.annotation.CheckForNull
).
Now, if we look at your example with this in mind, and assume you are using javax.annotation.Nullable
, we have two situations:
@Nullable
parameter
public void someLibraryMethod_ParamCheck(@Nullable List<String> param) {
assert param.size() > 1; // One issue!
}
This code will raise an issue about a possible NPE. And it makes sense, if you added the annotation on the parameter, it means that you expect someone to pass null
to it, strong or weak do not matter. If you dereference it, we are sure that a NPE is possible, otherwise, you might want to simply remove the annotation.
@Nullable
return
@Nullable
public List<String> someLibraryMethod_ReturnCheck() {...}
public void foo() {
List<String> list = someLibraryMethod_ReturnCheck();
assert list.size() > 1; // No issue!
}
This case is not the same. From the citation above, there are cases where it is acceptable to not check the return value for null, because you determined that it was not necessary. We can not be sure statically that a NPE is possible, therefore we do not report an issue. If you want to enforce a null check, you should use CheckForNull
, and an issue will appear!
Hope it clarifies the situation.