Author: Oliver Burn
This document describes the core engine of Checkstyle, including what checks it performs and what can be configured. The engine can be executed via different interfaces. The standard distribution includes support for:
To run checkstyle you will need:
Checkstyle has been tested using:
Checks for import statements that are not used. It will warn about wild-card imports like import java.io.*;. It will also warn about duplicate import statements.
Removing unused import statements reduces unnecessary dependencies in a code base. This feature can be turned off.
Checks that the following constructs have a Javadoc comment:
Javadoc comments for class and interface declarations are checked to ensure that the @author tag exists. This can be turned off.
You can control the visibility scope where Javadoc comments are checked. For example, you can check Javadoc code only for public and protected variables, methods, interfaces and class definitions. Scoping rules apply, in the above example a public method in a package visible class is not checked. You can also completely turn off all checking for Javadoc comments.
Javadoc comments for methods are checked to ensure that the following tags exist (if required):
For example the following is valid:
/**
* Checks for a return tag.
* @return the index of the next unchecked tag
* @param aTagIndex the index to start in the tags
* @param aTags the tags to check
* @param aLineNo the line number of the expected tag
**/
public int checkReturnTag(final int aTagIndex,
JavadocTag[] aTags,
int aLineNo)
It can be extremely painful writing or duplicating Javadoc for a method required for an interface. Hence checkstyle supports using the convention of using a single @see tag instead of all the other tags. For example, if the previous method was implementing a method required by the com.puppycrawl.tools.checkstyle.Verifier interface, then the Javadoc could be done as:
/** @see com.puppycrawl.tools.checkstyle.Verifier **/
public int checkReturnTag(final int aTagIndex,
JavadocTag[] aTags,
int aLineNo)
Checks for lines that are longer than a specified length. The default is "80". This can be turned off for import statements.
Checks for method bodies that are longer than a specified number of lines. The default is "150".
Checks for constructor bodies that are longer than a specified number of lines. The default is "150".
Checks for files that are longer than a specified number of lines. The default is "2000".
Checks for lines that contain tab ('\t') characters. This can be turned off.
Verifies that the format of the variable names conform to a specified regular expression. The formats can be overridden. The following table outlines the defaults:
| Scope | Default Format | Example |
| Constants (static and final) | ^[A-Z]([A-Z0-9_]*[A-Z0-9])?$ The exception to the rule is for serialVersionUID. |
public static final int MAX_ROWS = 2; |
| Static variables (static only) | ^[a-z][a-zA-Z0-9]*$ | private static int numCreated = 0; |
| Members (non static) | ^[a-z][a-zA-Z0-9]*$ | private int mySize = 0; |
| Public members (non static public) | ^f[A-Z][a-zA-Z0-9]*$ | public int fCMPField; |
The default values prior to release 1.4 were:
| Scope | Old Format |
| static only | ^s[A-Z][a-zA-Z0-9]*$ |
| non static | ^m[A-Z][a-zA-Z0-9]*$ |
Verifies that the format of parameter names conform to a specified regular expression. The default is ^[a-z][a-zA-Z0-9]*$. The default value prior to release 1.4 was ^a[A-Z][a-zA-Z0-9]*$.
Verifies that the format of class and interface names conform to a specified regular expression. The default is ^[A-Z][a-zA-Z0-9]*$.
Verifies that the format of public member names conform to a specified regular expression. The default is ^f[A-Z][a-zA-Z0-9]*$. This is useful for the fields required for Container Managed Persistence (CMP) Enterprise JavaBeans 1.1.
Checks for data members that are not declared private. Also finds static non-final data members that are not declared as private.
Note: you can turn on allowing protected data members.
Container Managed Persistence EJBs require (in the EJB 1.1 specification) that managed fields are declared public. This will cause checkstyle to complain that the fields should be declared private. Hence checkstyle supports ignoring public that match a specified regular expression. The default is ^f[A-Z][a-zA-Z0-9]*$.
Checks for the following use of white space:
This feature can be turned off.
Checks for missing braces {}'s for the following constructs:
This feature can be turned off.
Ensure that the file starts with a specified header. The header contents are specified in a file. If no file is specified, checkstyle does not check for a header.
Most copyright headers will contain date information (for example the year) which will change over time. To support this, checkstyle supports specifying a line in the header to ignore when comparing the start of the file with the header. For example, consider the following header:
line 1: /////////////////////////////////////////////////////////////////////// line 2: // checkstyle: Checks Java source code for adherence to a set of rules. line 3: // Copyright (C) 2001 Oliver Burn line 4: ///////////////////////////////////////////////////////////////////////
Since the year information will change over time, you can tell checkstyle to ignore line 3.
The Checkstyle engine has a flexible output mechanism inspired by ANT. AuditEvent objects are generated (for found errors) and are consumed by listeners that implement the AuditListener interface. The standard distribution comes with the following listeners:
Copyright © 2001 Oliver Burn. All rights Reserved.