NetBeans - More static code analysis

❝Extending static code analysis capabilities in Netbeans.❞
Contents

NetBeans is an IDE that has long been the underdog to Eclipse and IntelliJ. And although many will prefer one of the latter, by now it has caught up quite significantly and is already a pleasant IDE to work in.

To get even more out of Netbeans, there are the abilities of “source inspection” and “inspection and transform”. These may not be the most famous options available. And I know I spent quite some time googling just to find out how to enable/do static analysis on null values. So, here is a quick post detailing on what to expect and how to use these functions.

Source code inspection

The first static analysis feature is “Inspect”. You can find this option in Source -> Inspect. (Make sure that a file window is the focused window when you do this. Otherwise, the scope field containing your projects may not be populated correctly.)

You will be presented with a dialog showing you a number of options:

When you start inspection, a new tool window “Inspector” will appear with the results of the inspection. You can then double-click on a finding in order to look it up in the source code.

Inspect & Transform

Another interesting option is the “Inspect and Transform…” option of the Project context menu. You can find this by right-clicking on the project root and clicking the menu item “Inspect and Transform…”.

Inspect & Transform allows you to automate migrations to newer Java versions by automatically looking up and replacing Java language patterns that are known to be improvable with the added language constructs of a new Java version.

You can start analysis and NetBeans will provide a tool window with a preliminary analysis showing all the candidates for transformation. To start the transformation you can click “Do Refactoring” and NetBeans will start refactoring all discovered candidates (that have been selected).

Advanced analysis capabilities

Apart from the options described above, there are more possibilities of analysis. One that is part of the FindBugs capabilities but was not widely advertised in NetBeans (AFAICT) is the ability to do null analysis using annotations.

The capability originates from JSR-305: Annotations for Software Defect Detection. JSR-305 is an attempt to unify annotations used for such analyses to a common set that is used by all IDEs.

FindBugs (and with it NetBeans) supports analysis of at least:

NetBeans obviously supports JSR-305. It is part of the FindBugs rules set, and is actually enabled by default. Furthermore, IntelliJ supports these annotations by default and has some of their own. Depending on your use case you may want to go with JSR-305 standard annotations or those offered by IntelliJ. Eclipse, at the very least, supports the nullability annotations, as they can be configured in the IDE and at the project level. That is, one can configure which annotation indicates cases where null is not allowed, and the annotation that indicates where null explicitly is allowed.

To include JSR-305 in your project, you can either add the library manually. In case of Maven one can add dependency:

<dependency>
    <groupId>com.google.code.findbugs</groupId>
    <artifactId>jsr305</artifactId>
    <version>3.0.1</version>
</dependency>

… or for Gradle:

compile 'com.google.code.findbugs:jsr305:3.0.1'

jsr305 version 3.0.1 is the latest version at the time of writing.

This kind of static analysis is extremely useful for a project that is starting to mature. One can annotate source code with these annotations and let static analysis find any overlooked error cases.