checkstyle, PMD, SpotBugs and Maven

Three tools read Java code without running it, and each looks for a different kind of problem. checkstyle checks the shape: the layout, the comments, the names. PMD checks the source for patterns that are usually wrong. SpotBugs reads the compiled class files and looks for bugs in the bytecode. The image has all three, the Google style file for checkstyle, and Maven, which cannot download anything here.

One file, three tools

The file below is small and has one real bug and several style problems, so each tool finds something and the three reports can be compared:

public class Report { private int[] values; public Report(int[] values) { this.values = values; } public int total() { int sum = 0; for (int i = 0; i < values.length; i++) { sum = sum + values[i]; } return sum; } public static void main(String[] args) { System.out.println(new Report(new int[] {1, 2, 3}).total()); } }

javac -Xlint:all -d out Report.java
java -cp out Report

6

The program is correct and prints 6, which is the point of a linter: the code works and the tools still have something to say.

checkstyle

The Google checks expect two-space indentation, a package declaration and a Javadoc comment for every public type and method, and this file has none of the three. The command names the configuration file and the source:

checkstyle -c /google_checks.xml Report.java

Starting audit...
[WARN] /tmp/report/Report.java:1:1: Missing package declaration. [PackageDeclaration]
[WARN] /tmp/report/Report.java:1:1: Missing a Javadoc comment for 'Report'. [MissingJavadocType]
[WARN] /tmp/report/Report.java:2:5: 'member def modifier' has incorrect indentation level 4, expected level should be 2. [Indentation]
[WARN] /tmp/report/Report.java:4:5: 'ctor def modifier' has incorrect indentation level 4, expected level should be 2. [Indentation]
[WARN] /tmp/report/Report.java:4:5: Missing a Javadoc comment for 'Report'. [MissingJavadocMethod]
[WARN] /tmp/report/Report.java:5:9: 'ctor def' child has incorrect indentation level 8, expected level should be 4. [Indentation]
[WARN] /tmp/report/Report.java:6:5: 'ctor def rcurly' has incorrect indentation level 4, expected level should be 2. [Indentation]
[WARN] /tmp/report/Report.java:8:5: 'method def modifier' has incorrect indentation level 4, expected level should be 2. [Indentation]
[WARN] /tmp/report/Report.java:8:5: Missing a Javadoc comment for 'total'. [MissingJavadocMethod]
[WARN] /tmp/report/Report.java:9:9: 'method def' child has incorrect indentation level 8, expected level should be 4. [Indentation]
[WARN] /tmp/report/Report.java:10:9: 'for' has incorrect indentation level 8, expected level should be 4. [Indentation]
[WARN] /tmp/report/Report.java:11:13: 'for' child has incorrect indentation level 12, expected level should be 6. [Indentation]
[WARN] /tmp/report/Report.java:12:9: 'for rcurly' has incorrect indentation level 8, expected level should be 4. [Indentation]
[WARN] /tmp/report/Report.java:13:9: 'method def' child has incorrect indentation level 8, expected level should be 4. [Indentation]
[WARN] /tmp/report/Report.java:14:5: 'method def rcurly' has incorrect indentation level 4, expected level should be 2. [Indentation]
[WARN] /tmp/report/Report.java:16:5: 'method def modifier' has incorrect indentation level 4, expected level should be 2. [Indentation]
[WARN] /tmp/report/Report.java:16:5: Missing a Javadoc comment for 'main'. [MissingJavadocMethod]
[WARN] /tmp/report/Report.java:17:9: 'method def' child has incorrect indentation level 8, expected level should be 4. [Indentation]
[WARN] /tmp/report/Report.java:18:5: 'method def rcurly' has incorrect indentation level 4, expected level should be 2. [Indentation]
Audit done.

Nineteen warnings of two kinds: eleven indentation findings, which one reformat fixes, and seven missing-comment findings with the package declaration, which need a person. The path prefix is the folder the command ran in and differs on every machine; the line and the column after it are what a reader uses.

PMD

PMD reads the source for patterns, and its quickstart set gives two findings on this file: the class is in the default package, and the loop that walks an array by index can be a for-each. The command sends the two streams into one, because the tool writes the findings to the standard output and the two notices and the count to the error output:

pmd check -d Report.java -R rulesets/java/quickstart.xml -f text 2>&1

[WARN] Progressbar rendering conflicts with reporting to STDOUT. No progressbar will be shown. Try running with argument -r <file> to output the report to a file instead.
[WARN] This analysis could be faster, please consider using Incremental Analysis: https://docs.pmd-code.org/pmd-doc-7.29.0/pmd_userdocs_incremental_analysis.html
Report.java:1: NoPackage: All classes, interfaces, enums and annotations must belong to a named package
Report.java:10: ForLoopCanBeForeach: This for loop can be replaced by a foreach loop
[INFO] Found 2 violations.

The two findings are of different weights, and that is what a rule set is for: the package is a design decision, and the loop is a small rewrite with no change of behaviour. A project turns a rule off or down with its own rule set, and a report of two lines is easy to keep at zero.

The two warnings are the tool talking about itself: it cannot draw its progress bar beside a report on the standard output, and it would analyse faster with its incremental cache. Neither is about the code, and a build log keeps them out of the way with -r, which writes the report to a file and leaves stdout for the progress. The status of the command is 4, because PMD returns the number of its findings.

SpotBugs

SpotBugs reads the class files, so the command takes the compiled folder and not the source. It finds the bug the other two cannot see: the constructor stores the array it was given, so a caller that changes the array changes the report:

spotbugs -textui out

M V EI2: new Report(int[]) may expose internal representation by storing an externally mutable object into values At Report.java:[line 5]

EI2 is the code of the rule, M its priority, and the rest is the sentence. The fix is the defensive copy of lesson 042: the constructor copies the array, so the object owns its data. SpotBugs finds it because the bytecode shows the assignment of a reference, which the source shows too but only a tool with a rule notices.

Maven and the pom

Maven is the build tool that runs all of this from a description instead of a list of commands. The description is pom.xml, and this is the smallest one that compiles for Java 21:

<project xmlns="http://maven.apache.org/POM/4.0.0"> <modelVersion>4.0.0</modelVersion> <groupId>demo</groupId> <artifactId>report</artifactId> <version>1.0.0</version> <properties> <maven.compiler.release>21</maven.compiler.release> </properties> </project>

A real pom adds the plugins of the three tools and the JUnit dependency, and then mvn verify runs the lot; the modern project group shows the same pom and the same offline failure for a service of its own. That build does not run in this container: Maven downloads its plugins on the first run, and the container has no network. The offline mode says exactly that:

mvn -o -q package

[ERROR] Plugin org.apache.maven.plugins:maven-resources-plugin:3.4.0 or one of its dependencies could not be resolved:
[ERROR] Cannot access central (https://repo.maven.apache.org/maven2) in offline mode and the artifact org.apache.maven.plugins:maven-resources-plugin:jar:3.4.0 has not been downloaded from it before.
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginResolutionException

The message names the plugin, the version and the repository it could not reach, so the page shows the real limit of the environment and not a build that was never tried. The lessons of this course use javac, java, jar and the JUnit launcher for that reason: they are in the image and they need no download.

Exercises

  1. Reformat the file with two-space indentation and a package and run checkstyle again.
  2. Change the loop of total to a for-each and run PMD again.
  3. Copy the array in the constructor and run SpotBugs again.
  4. Run SpotBugs on a class with a public static array field and read the rule code.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does checkstyle check?

    Answer

    The shape of the code: layout, comments and names

    It reads the source against a style file and reports the rules it broke.

  2. What does SpotBugs read?

    Answer

    The compiled class files

    The tool analyses bytecode, so the classes have to be built first.

  3. What is the bug SpotBugs reports on the file of this lesson?

    Answer

    The constructor stores an array the caller can still change

    EI2 is the exposed internal representation, and the fix is a defensive copy.

  4. What does this program print? public class Q1 { private final int[] values; Q1(int[] values) { this.values = values.clone(); } int total() { int sum = 0; for (int value : values) { sum += value; } return sum; } public static void main(String[] args) { int[] data = {1, 2, 3}; Q1 report = new Q1(data); data[0] = 100; System.out.println(report.total()); } }

    Answer

    6

    The clone keeps the original values, so the change to data does not reach the report.

  5. What is the difference between PMD and SpotBugs?

    Answer

    PMD reads the source and SpotBugs the class files

    The source and the bytecode show different things, which is why both are run.

  6. Why does mvn package fail in this container?

    Answer

    It must download its plugins, and there is no network

    The offline mode message names the plugin and the repository it could not reach.

  7. What does the code of a SpotBugs finding name?

    Answer

    The rule that fired, here EI2

    The rule code is what a project turns off or documents, and the message explains it.

  8. What does this program print? public class Q2 { private final int[] values; Q2(int[] values) { this.values = values; } int first() { return values[0]; } public static void main(String[] args) { int[] data = {1, 2}; Q2 report = new Q2(data); data[0] = 9; System.out.println(report.first()); } }

    Answer

    9

    Without the copy the report reads the array the caller changed, which is the EI2 bug.

  9. Why does a linter complain about code that works?

    Answer

    It checks the shape and the patterns, which a correct program can still get wrong

    The three tools of the lesson find style, patterns and bugs in a program that prints 6.

  10. Which command runs checkstyle with the Google rules?

    Answer

    checkstyle -c /google_checks.xml File.java

    The configuration file is named with -c, and the Google one ships inside the jar.

The code on this page was built with OpenJDK 21.0.13 (javac and java) and run on Alpine Linux in a Docker container.

Article author: Arthur Isaev