A Student's Guide to Software Engineering Tools & Techniques »

CheckStyle

Author: Xiao Pu

Overview

CheckStyle is a static analyser for Java. It can be used to assist developers in static analysis process.

Features

CheckStyle will examine code based on different rules(or checks).

According to the checks list provided by CheckStyle, the checks(rules) can be divided into 14 sections.

  • Annotations
  • Block Checks
  • Class Design
  • Coding
  • Headers
  • Imports
  • Javadoc Comments
  • Metrics
  • Miscellaneous
  • Modifiers
  • Naming Conventions
  • Regexp
  • Size Violations
  • Whitespace

Limitations

As described here, there are several limitations in CheckStyle.

  • The code must be written in ASCII characters only.
  • The examined code has to be compilable. The reason is described in How does it work section.
  • Files will be examined one by one, which means you cannot check multiple files at the same time.
    • For example, you cannot determine the full inheritance hierarchy of a class as you need to examine the parent class while checking the child class.

How to Use It?

Configuration

CheckStyle uses a configuration file to know all the rules that it is supposed to check.

Suppress Warnings

CheckStyle supports suppressing warnings in four ways:

Running

There are several ways to run CheckStyle.

Command Line:

Build Automation Tools:

IDE Integration:

Available Configurations

There are two widely used configurations: Sun Code Conversions and Google Java Style. Some common rules are already included in these configurations.

How Does It Work?

CheckStyle will use ANTLR to parse your code into a AST(Abstract Syntax Tree) and visit it in a DFS(Depth First Search) pattern to check violations. Thus, it is necessary to make the code compilable in order for the ANTLR to work. You can view the syntax tree using CheckStyle Grammar Tree Viewer

Customisation

Resources

  • CheckStyle: CheckStyle official website. You can download the latest version, view online documentation there.
  • CheckStyle Github: CheckStyle GitHub page. You can contribute to the project or report bugs there.
  • StackOverflow CheckStyle: Question/Answer forum in StackOverflow for CheckStyle. You can ask question related to the using of CheckStyle.