ale*_*2k8 7 java static-analysis nullpointerexception
以下是一些可以抛出NullPointerException的代码片段.
01:
public void m1(@Nullable String text) {
System.out.print(text.toLowerCase()); // <-- expect to be reported.
}
Run Code Online (Sandbox Code Playgroud)
02:
private boolean _closed = false;
public void m1(@Nullable String text) {
if(_closed)
return;
System.out.print(text.toLowerCase()); // <-- expect to be reported.
}
Run Code Online (Sandbox Code Playgroud)
03:
public void m1(@NotNull String text) {
System.out.print(text.toLowerCase());
}
public @Nullable String getText() {
return "Some text";
}
public void m2() {
m1(getText()); // <-- expect to be reported.
}
Run Code Online (Sandbox Code Playgroud)
不同的人可以访问不同的静态分析工具.收集信息,能够检测和报告问题的工具以及失败的信息会很好.另外,如果你有自己的风景,请发布它们.
在这里我的结果
FindBugs(1.3.9):
IntelliJ IDE 9.0.2(社区版):
注释包:
javax.annotation.* // JSR 305
edu.umd.cs.findbugs.annotations.* // FindBugs
org.jetbrains.annotations.* // IntelliJ
checkers.nullness.quals.* // Checker Framework
Run Code Online (Sandbox Code Playgroud)
请注意,FindBugs 对待 @Nullable 和 @CheckForNull 的方式截然不同——前者基本上是说“我不知道这是否应该允许 null”,而后者则说“这个方法明确允许 null”。当我们在代码中将所有 @Nullable 全局更改为 @CheckForNull 时,我的团队获得了更有用的 FindBugs 结果,因为后者才是我们真正的意思。
| 归档时间: |
|
| 查看次数: |
1309 次 |
| 最近记录: |