Sergey Nivens - Fotolia
It's always a good idea to employ a few static code analysis tools as part of your software development routine. There are a number of great tools that perform static analysis of Java code, such as SonarQube, FindBugs, JaCoCo and PMD source code analyzer, but SourceForge's Checkstyle might be the simplest static code analysis tool to use when getting started.
Checkstyle produces easily understood results. There is a simple Maven Checkstyle plugin, and the Checkstyle plugins for Eclipse, NetBeans and IntelliJ enable it to easily integrate into all of the most popular Java IDEs. The Checkstyle plugin for Eclipse will be the focus of this static analysis tutorial, but the concepts apply equally to other IDEs and build tools, such as Jenkins or Maven.
What does Checkstyle do?
Checkstyle's goal is to ensure that the Java code developer's write complies with accepted conventions and governance standards. Incorrect indentation levels, a lack of Javadoc comments, line lengths exceeding a predefined number of characters and instances where shadow variables occur are all flagged by Checkstyle's static analysis for Java routines.
View Checkstyle reports
Checkstyle's static analysis of Java code stops short of bug identification and instead focuses on composition and form. Most of its warnings are simply structural, such as "Line contains a tab character" or "Unused Imports," although some warnings are a bit more insightful, such as suggestions that code could be optimized by making certain variables final or that a given method has not been designed for future extension.
Checkstyle can also be configured to use supplemental modules to perform tasks such as McCabe cyclomatic complexity calculations, but those aren't default features and need to be added by a developer.
How does the Checkstyle plugin for Eclipse work?
So, how do Eclipse users take advantage of the insights from Checkstyle's static analysis of Java code? The easiest way is to simply access the Marketplace link inside the IDE and search for the Checkstyle download. After you install the Checkstyle plugin for Eclipse, the IDE will restart, and two new views will be accessible from within your Java perspectives: the Checkstyle for Eclipse violations view and the Checkstyle for Eclipse violations chart. The plugin will also highlight lines of code that contain Checkstyle warnings in yellow to let the developer know exactly where the code problems lie.
Configure Checkstyle rules
The Checkstyle for Eclipse plugin comes with two default configuration files: google_checks.xml and sun_checks.xml. A developer can switch between files and also manage various Checkstyle rules via the tool's configuration page in the IDE's preferences window. For example, is the Javadoc rule creating too many false positives? If so, an easy rule configuration change to make is to have Checkstyle ignore Javadoc.
For developers interested in getting started with static analysis of Java code, the Checkstyle plugin for Eclipse is a good place to start. The installation is seamless, the configuration is easy and the Checkstyle reports are easy to comprehend.