O'Reilly logo

Java Power Tools by John Ferguson Smart

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 21. Detecting and Enforcing Coding Standards with Checkstyle[18]

Using Checkstyle to Enforce Coding Standards

Checkstyle is an open source tool that enforces coding conventions and best practice rules for Java code. Although it was originally designed to enforce coding standards, it now lets you verify coding best practices as well, in much the same way as PMD (Chapter 22) and FindBugs (Chapter 23). It works by analyzing Java source code and reporting any breach of standards. It can be integrated into your favorite IDE via a plug-in so that developers can immediately see and correct any breaches of the official standards. It can also be used to generate project-wide reports that summarize the breaches found.

Checkstyle comes “out-of-the-box” with the standard Sun conventions, including more than 120 rules and standards, dealing with issues that range from code formatting and naming conventions to Enterprise JavaBean (EJB) best practices and code complexity metrics. Checkstyle supports standards related to the following:

  • Javadoc comments

  • Naming conventions

  • File headers

  • Import statements

  • Whitespace

  • Modifiers

  • Blocks

  • Coding problems

  • Class design

  • J2EE (Java 2 Platform, Enterprise Edition)

  • And other miscellaneous issues

You can run Checkstyle from the command line, if you are so inclined. Download the Checkstyle distribution from the web site[*] and extract it in a convenient place. Then run the checkstyle-all-4.3.jar file as shown here:

$ java -jar checkstyle-all-4.3.jar -c sun_checks.xml -r ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required