我有下面的简单代码,用于测试IntelliJ中的NonNull批注。
然后我去:
IntelliJ->文件->设置->检查->可能的错误->恒定条件和异常
我设置了严重性:错误。
这样做会将行标记为“print(null);”。作为预期的错误。
但是,执行IntelliJ->构建->重建项目,
它有效,并且不显示任何错误,也不显示任何警告。
为什么会这样呢?为什么在构建项目时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>
最佳答案
代码检查不是编译器错误,即使严重性级别设置为error
,目前如果检查报告存在某些问题,也无法使编译失败。
要获得整个项目的结果,请使用Analyze
|带有适当检查配置文件的Inspect Code
。
IDEA项目问题跟踪器中有similar feature request,但它似乎不太受欢迎(将近2岁,票数为零)。