强制IntelliJ无法对NonNull违规进行编译 [英] force IntelliJ to fail the compilation on NonNull violations

查看:94
本文介绍了强制IntelliJ无法对NonNull违规进行编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下简单的代码来测试IntelliJ中的NonNull注释。

I have the simple code below for testing the NonNull annotations in IntelliJ.

然后我转到:
IntelliJ - >文件 - >设置 - >检查 - >可能的错误 - >恒定条件&例外
和我设置严重程度:作为错误。

Then I go to: IntelliJ -> File -> Settings -> Inspections -> Probable bugs -> Constant conditions & exceptions and I set-up Severity: As error.

这样做,它标记了行print(null);如预期的错误。
但是,执行IntelliJ - > Build - > Rebuild项目,
它可以工作,它没有显示任何错误,也没有显示任何警告。

Doing so, it marks the line "print(null);" as an error as expected. However, executing IntelliJ -> Build -> Rebuild project, it works and it does not show any error and it does not show any warning.

为什么会这样?为什么IntelliJ在构建项目时没有抱怨?

Why is so? Why IntelliJ does not complain when building the project?

如何查看NonNull违规列表?

How to see a list of the NonNull violations?

如果发现NonNull违规,是否有办法强制IntelliJ使编译失败?

Is there a way to enforce IntelliJ to fail the compilation if it finds a NonNull violation?

注意:IntelliJ设置为 - 最多考虑萤火虫注释(默认情况下);
此外,使用org.jetbrains.annotations.NotNull会产生完全相同的结果。

Note: IntelliJ is set-up to take into account the firebug annotations (as by default); moreover, using the org.jetbrains.annotations.NotNull produces exactly the same result.

src / main /java/test/Hello.java

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文件:

and the pom.xml file:

<?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>


推荐答案

代码检查不是编译器错误,目前还有如果检查报告了某些问题,即使严重性级别设置为错误,也无法使编译失败。

Code inspections are not compiler errors, at the moment there is no way to fail compilation if there is some problem reported by inspection, even if the severity level is set to error.

要获得项目范围的结果,请使用分析 | 使用适当的检验资料检查代码

To get the project wide results use Analyze | Inspect Code with the appropriate inspection profile.

类似功能请求,但似乎并不是非常受欢迎(差不多2年零票)。

There is similar feature request in IDEA project issue tracker, but it doesn't seem to be very popular (almost 2 years old and zero votes).

这篇关于强制IntelliJ无法对NonNull违规进行编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆