force IntelliJ to fail the compilation on NonNull violations

做~自己de王妃 提交于 2019-12-23 13:16:46

问题


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

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

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.

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

How to see a list of the NonNull violations?

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


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

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);
        }
    }
}

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>

回答1:


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.

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).



来源:https://stackoverflow.com/questions/13291432/force-intellij-to-fail-the-compilation-on-nonnull-violations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!