Review of Java Static Analysis Tools

Tags: codequality, performance, java, productivity
Published: 2021-03-09 16:04:42

If you code in Java and code reviews are part of your workflow we recommend you to go through the list below. Here are some of the Java Static Analysis tools you should know about:

##1. PMD Java

[PMD](https://pmd.github.io/) scans Java source code and looks for [potential problems](https://pmd.github.io/pmd-5.4.1/pmd-java/rules/index.html).

Problems range from breaking naming conventions and unused code or variables to performance and complexity of code, not forgetting lots of possible bugs.

The PMD project also supports JavaScript, PLSQL, Apache Velocity, XML and XSL. It also ships with a CPD, a tool to detect duplicated code in several languages.

PMD integrates with several tools and editors, including Eclipse, NetBeans, IntelliJ IDEA, TextPad, Maven, Ant and Emacs.

Here’s a sample of what running PMD through some code looks like:

```java
$ pmd pmd -R java-basic,java-unusedcode -d Deck.java
/Users/pmd/my/project/Deck.java:35: Avoid unused private fields such as 'classVar2'.
/Users/pmd/my/project/Deck.java:47: Avoid unused private fields such as 'instanceVar3'.
```
You can suppress warnings (in a variety of ways) and you can also write your own rules in either Java or XPath..

##2. Checkstyle
As the name implies, [Checkstyle](http://checkstyle.sourceforge.net/) checks that your code adheres to a coding standard.

The tool is configurable, which makes it able to support different code style conventions. Two examples are the [Sun Code Conventions](http://www.oracle.com/technetwork/java/javase/documentation/codeconvtoc-136057.html) and [Google Java Style](http://checkstyle.sourceforge.net/reports/google-java-style.html) (although the one from Sun hasn’t been maintained since 1999).

You can find a [configuration file](https://github.com/checkstyle/checkstyle/blob/3e4367941c3e9680703e8ea8400abbd5dc78e1d9/src/main/resources/google_checks.xml) for Google’s Java Style on the checkstyle repository.

Speaking of configuration, this is done in an XML file where you set which modules are to be used. Here’s a (tiny) example of a configuration file:

```java
$ checkstyle -c checkstyle.xml Deck.java
Starting audit...
/Users/checkstyle/my/project/Blah.java:0: File does not end with a newline.
/Users/checkstyle/my/project/Deck.java:23: Line has trailing spaces.
/Users/checkstyle/my/project/Deck.java:70: Line has trailing spaces.
Audit done.
Checkstyle ends with 3 errors.
```
##3. FindBugs
[FindBugs](http://findbugs.sourceforge.net/) looks for bugs in Java Code, and this means over [400 different bugs](http://findbugs.sourceforge.net/bugDescriptions.html).

Patterns are separated into several categories: bad practice, correctness, malicious code vulnerability, multithreaded correctness, performance, security and dodgy code (two additional categories exist, with just a couple of patterns each: experimental and internationalization).

There are several ways of running FindBugs, but here’s what the [command line interface](http://findbugs.sourceforge.net/manual/running.html) can feel like:

```java
$ findbugs -textui .
M P UuF: Unused field: java.deck.Deck.classVar2  In Deck.java
M P UuF: Unused field: java.deck.Deck.instanceVar3  In Deck.java
M D UuF: Unused public or protected field: java.deck.Deck.instanceVar2  In Deck.java
M D UuF: Unused public or protected field: java.deck.Deck.classVar1  In Deck.java
M D UuF: Unused public or protected field: java.deck.Deck.instanceVar1  In Deck.java
Warnings generated: 5
```

The first letter in the output refers to the severity of the (potential) bug (low, medium, high) and the second is the category (in this case P for Performance and D for Dodgy Code).

It integrates with Eclipse, Maven, Netbeans, Jenkins, Hudson and IntelliJ.

FindBugs supports a plugin architecture that allows anyone to add new bug detectors; which brings us to…

##4. Find Security Bugs

[Find Security Bugs](http://find-sec-bugs.github.io/) is a plugin for FindBugs which adds checks for [80 additional different vulnerability types](http://find-sec-bugs.github.io/bugs.htm).

You’ll find a range of patterns that relate to [OWASP 10](https://www.owasp.org/index.php/OWASP_Top_10) vulnerabilities, from different types of injection and XSS protection to sensitive data exposure and unvalidated redirects.

There are also several patterns that are specific for Android.

There’s also other common things such as hashing methods and DOS vulnerabilities, not forgetting simpler things such as hard coded passwords.

##Conclusion

As with similar tools in different languages, these Java Static Analysis tools complement each other, and we do recommend that you check them out if you care about Code Quality and avoiding technical debt.

Both PMD and CheckStyle are already integrated with automated code reviews such as Codacy, which means you can start using them right now.

Using an automated code review tool means you’ll get all of these analyses done for you automatically every time you do a commit, plus a list of issues that are expansible to reveal additional detail on the particular problem and how to solve it.