我可以看到@Nullable和@Nonnull注释可以在防止有用NullPointerException秒,但他们并不很远传播.
@Nonnull不为空并因此不执行空检查的危险.下面的代码会导致标有一个参数@Nonnull是null没有提出任何投诉.它会NullPointerException在运行时抛出.
public class Clazz {
public static void main(String[] args){
Clazz clazz = new Clazz();
// this line raises a complaint with the IDE (IntelliJ 11)
clazz.directPathToA(null);
// this line does not
clazz.indirectPathToA(null);
}
public void indirectPathToA(Integer y){
directPathToA(y);
}
public void directPathToA(@Nonnull Integer x){
x.toString(); // do stuff to x
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法使这些注释更严格地执行和/或进一步传播?
java annotations nullable nullpointerexception code-standards