I agree that the annotations "don't propagate very far". However, I see the mistake on the programmer's side.
I understand the Nonnull
annotation as documentation. The following method expresses that is requires (as a precondition) a non-null argument x
.
public void directPathToA(@Nonnull Integer x){
x.toString(); // do stuff to x
}
The following code snippet then contains a bug. The method calls directPathToA()
without enforcing that y
is non-null (that is, it does not guarantee the precondition of the called method). One possibility is to add a Nonnull
annotation as well to indirectPathToA()
(propagating the precondition). Possibility two is to check for the nullity of y
in indirectPathToA()
and avoid the call to directPathToA()
when y
is null.
public void indirectPathToA(Integer y){
directPathToA(y);
}