force IntelliJ to fail the compilation on NonNull violations

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 comment
Comment actions Permalink

Hi,

There's currently no way of forcing IDEA to fail compilation if it finds @NotNull violation. It's because the compiler knows nothing about those annotations, only IDE does. On incremental compilation it would be a problem to detect which files should be re-checked for those violations; even a tiny code change in one file could lead to the whole project re-check. You can write a plugin though that checks the modified files on compilation for such violations.

To see the list of all violations you can run "Constant conditions & exceptions" and "@NotNull/@Nullable problems" inspections on a given scope. The easiest way to do this is to invoke Analyze | Run inspection by name (Ctrl+Alt+Shift+I) and find these inspection names there. Analyze | Inspect code allows for more flexibility in this.

You could also use TeamCity (http://www.jetbrains.com/teamcity/) to run this inspection remotely on every commit.

0

Please sign in to leave a comment.