我有下面的简单代码用于测试 IntelliJ 中的 NonNull 注释。
然后我去:
IntelliJ -> 文件 -> 设置 -> 检查 -> 可能的错误 -> 恒定条件和异常
我设置了严重性:作为错误。
这样做,它标记了“print(null);”行正如预期的那样是一个错误。
但是,执行 IntelliJ -> Build -> Rebuild 项目,
它有效,并且不显示任何错误,也不显示任何警告。
为什么会这样呢?为什么IntelliJ在构建项目时没有抱怨?
如何查看 NonNull 违规列表?
如果发现 NonNull 违规,是否有办法强制 IntelliJ 使编译失败?
注意:IntelliJ 设置为考虑 firebug 注释(默认情况下);
此外,使用 org.jetbrains.annotations.NotNull 会产生完全相同的结果。
src/main/java/test/Hello.java
package test;
import edu.umd.cs.findbugs.annotations.NonNull;
public class Hello {
static public void print(@NonNull Object value) {
System.out.println("value: " + value.toString());
}
static public void main(String[] args) {
if (args.length > 0) {
print(args[0]);
} else {
print(null);
}
}
}
和 pom.xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>hello</groupId>
<artifactId>hello</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>net.sourceforge.findbugs</groupId>
<artifactId>annotations</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>net.sourceforge.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>1.3.7</version>
</dependency>
</dependencies>
</project>