Miscellaneous Checks

Checkstyle Logo

GenericIllegalRegexp

Description

A generic check for code problems - the user can search for any pattern. This is similar to a recursive grep, only that it's integrated in checkstyle.

Rationale: This check can be used to prototype checks and to find common bad practice such as calling ex.printStacktrace(), System.out.println(), System.exit(), etc.

Properties

name description type default value
format illegal pattern regular expression ^$ (empty)
ignoreCase Controls whether to ignore case when searching. Boolean false
message message which is used to notify about violations, if empty then default(hard-coded) message is used. String ""(empty)

Examples

To configure the check for calls to System.out.println:

<module name="GenericIllegalRegexp">
    <!-- . matches any character, so we need to
         escape it and use \. to match dots. -->
    <property name="format" value="System\.out\.println"/>
</module>
      

To configure the check to find trailing whitespace at the end of a line:

<module name="GenericIllegalRegexp">
    <!-- \s matches whitespace character, $ matches end of line. -->
    <property name="format" value="\s$"/>
</module>
      

To configure the check to find case-insensitive occurrences of "debug":

<module name="GenericIllegalRegexp">
    <property name="format" value="debug"/>
    <property name="ignoreCase" value="true"/>
</module>
      

Package

com.puppycrawl.tools.checkstyle.checks

Parent Module

TreeWalker

NewlineAtEndOfFile

Description

Checks whether files end with a new line.

Rationale: Any source files and text files in general should end with a newline character, especially when using SCM systems such as CVS. CVS will even print a warning when it encounters a file that doesn't end with a newline.

Properties

name description type default value
lineSeparator type of line separator One of "system" (system default), "crlf" (Windows-style), "cr" (Mac-style) and "lf" (Unix-style) system default

Examples

To configure the check:

<module name="NewlineAtEndOfFile"/>
      

To configure the check to always use Unix-style line separators:

<module name="NewlineAtEndOfFile">
    <property name="lineSeparator" value="lf"/>
</module>
      

Package

com.puppycrawl.tools.checkstyle.checks

Parent Module

Checker

TodoComment

Description

A check for TODO: comments. Actually it is a generic regular expression matcher on Java comments. To check for other patterns in Java comments, set property format.

Properties

name description type default value
format pattern to check regular expression TODO:

Notes

Using TODO: comments is a great way to keep track of tasks that need to be done. Having them reported by Checkstyle makes it very hard to forget about them.

Examples

To configure the check:

<module name="TodoComment"/>
      

To configure the check for comments that contain WARNING:

<module name="TodoComment">
    <property name="format" value="WARNING"/>
</module>
      

Package

com.puppycrawl.tools.checkstyle.checks

Parent Module

TreeWalker

Translation

Description

A FileSetCheck that ensures the correct translation of code by checking property files for consistency regarding their keys. Two property files describing one and the same context are consistent if they contain the same keys.

Consider the following properties file in the same directory:

      #messages.properties
      hello=Hello
      cancel=Cancel

      #messages_de.properties
      hell=Hallo
      ok=OK
      

The Translation check will find the typo in the german hello key, the missing ok key in the default resource file and the missing cancel key in the german resource file:

      messages_de.properties: Key 'hello' missing.
      messages_de.properties: Key 'cancel' missing.
      messages.properties: Key 'hell' missing.
      messages.properties: Key 'ok' missing.
      

Properties

name description type default value
fileExtensions file type extension to identify translation files. Setting this property is typically only required if your translation files are preprocessed and the original files do not have the extension .properties String Set properties

Example

To configure the check:

<module name="Translation"/>
      

Package

com.puppycrawl.tools.checkstyle.checks

Parent Module

Checker

UncommentedMain

Description

Checks for uncommented main() methods (debugging leftovers).

Rationale: main() method can be often used for debug puposes. Thus most of main() method should be removed/commented out of the sources.

Properties

name description type default value
excludedClasses pattern for qualified names of classes which ar allowed to have main method. regular expression ^$

Examples

To configure the check:

<module name="UncommentedMain"/>
      

To configure the check to allow main method for all classes with "Main" name:

<module name="UncommentedMain">
    <property name="excludedClasses" value="\.Main$"/>
</module>
      

Package

com.puppycrawl.tools.checkstyle.checks

Parent Module

TreeWalker

UpperEll

Description

Checks that long constants are defined with an upper ell. That is ' L' and not 'l'. This is in accordance to the Java Language Specification, Section 3.10.1.

Rationale: The letter l looks a lot like 1.

Examples

To configure the check:

<module name="UpperEll"/>
      

Package

com.puppycrawl.tools.checkstyle.checks

Parent Module

TreeWalker

ArrayTypeStyle

Description

Checks the style of array type definitions. Some like Java-style: public static void main(String[] args) and some like C-style: public static void main(String args[])

Properties

name description type default value
javaStyle Controls whether to enforce Java style (true) or C style (false). Boolean true

Examples

To configure the check to enforce Java style:

<module name="ArrayTypeStyle"/>
      

To configure the check to enforce C style:

<module name="ArrayTypeStyle">
    <property name="javaStyle" value="false"/>
</module>
      

Package

com.puppycrawl.tools.checkstyle.checks

Parent Module

TreeWalker

FinalParameters

Description

Check that method/constructor parameters are final. Interface methods are not checked - the final keyword does not make sense for interface method parameters as there is no code that could modify the parameter.

Rationale: Changing the value of parameters during the execution of the method's algorithm can be confusing and should be avoided. A great way to let the Java compiler prevent this coding style is to declare parameters final.

Properties

name description type default value
tokens blocks to check subset of tokens METHOD_DEF, CTOR_DEF METHOD_DEF, CTOR_DEF

Examples

To configure the check to enforce final parameters for methods and constructors:

<module name="FinalParameters"/>
      

To configure the check to enforce final parameters only for constructors:

<module name="FinalParameters">
    <property name="tokens" value="CTOR_DEF"/>
</module>
      

Package

com.puppycrawl.tools.checkstyle.checks

Parent Module

TreeWalker

DescendantToken

Description

Checks for restricted tokens beneath other tokens.

WARNING: This is a very powerful and flexible check, but, at the same time, it is low level and very implementation dependent because its results depend on the grammar we use to build abstract syntax trees. Thus we recomend using other checks when they provide the desired funcionality. All in all, this check just works on the level of an abstract tree and knows nothing about language structures.

Properties

name description type default value
tokens token types to check subset of tokens declared in TokenTypes empty set
limitedTokens set of tokens with limited occurances as descendants subset of tokens declared in TokenTypes empty set
minimumDepth the mimimum depth for descendant counts Integer 0
maximumDepth the maximum depth for descendant counts Integer java.lang.Integer.MAX_VALUE
minimumNumber a minimum count for descendants Integer 0
maximumNumber a maximum count for descendants Integer java.lang.Integer.MAX_VALUE
minimumMessage error message when minimum count not reached String "descendant.token.min"
maximumMessage error message when maximum count exceeded String "descendant.token.max"

Examples

        String literal equality check:
        <module name="DescendantToken">
            <property name="tokens" value="EQUAL,NOT_EQUAL"/>
            <property name="limitedTokens" value="STRING_LITERAL"/>
            <property name="maximumNumber" value="0"/>
            <property name="maximumDepth" value="1"/>
        </module>

        Switch with no default:
        <module name="DescendantToken">
            <property name="tokens" value="LITERAL_SWITCH"/>
            <property name="maximumDepth" value="2"/>
            <property name="limitedTokens" value="LITERAL_DEFAULT"/>
            <property name="minimumNumber" value="1"/>
        </module>

        Assert statement may have side effects (formatted for browser display):
        <module name="DescendantToken">
            <property name="tokens" value="LITERAL_ASSERT"/>
            <property name="limitedTokens" value="ASSIGN,DEC,INC,POST_DEC,
                POST_INC,PLUS_ASSIGN,MINUS_ASSIGN,STAR_ASSIGN,DIV_ASSIGN,MOD_ASSIGN,
                BSR_ASSIGN,SR_ASSIGN,SL_ASSIGN,BAND_ASSIGN,BXOR_ASSIGN,BOR_ASSIGN,
                METHOD_CALL"/>
            <property name="maximumNumber" value="0"/>
        </module>

        Initialiser in for performs no setup (use while instead?):
        <module name="DescendantToken">
            <property name="tokens" value="FOR_INIT"/>
            <property name="limitedTokens" value="EXPR"/>
            <property name="minimumNumber" value="1"/>
        </module>

        Condition in for performs no check:
        <module name="DescendantToken">
            <property name="tokens" value="FOR_CONDITION"/>
            <property name="limitedTokens" value="EXPR"/>
            <property name="minimumNumber" value="1"/>
        </module>

        Switch within switch:
        <module name="DescendantToken">
            <property name="tokens" value="LITERAL_SWITCH"/>
            <property name="limitedTokens" value="LITERAL_SWITCH"/>
            <property name="maximumNumber" value="0"/>
            <property name="minimumDepth" value="1"/>
        </module>

        Return from within a catch or finally block:
        <module name="DescendantToken">
            <property name="tokens" value="LITERAL_FINALLY,LITERAL_CATCH"/>
            <property name="limitedTokens" value="LITERAL_RETURN"/>
            <property name="maximumNumber" value="0"/>
        </module>

        Try within catch or finally block:
        <module name="DescendantToken">
            <property name="tokens" value="LITERAL_CATCH,LITERAL_FINALLY"/>
            <property name="limitedTokens" value="LITERAL_TRY"/>
            <property name="maximumNumber" value="0"/>
        </module>

        Too many cases within a switch:
        <module name="DescendantToken">
            <property name="tokens" value="LITERAL_SWITCH"/>
            <property name="limitedTokens" value="LITERAL_CASE"/>
            <property name="maximumDepth" value="2"/>
            <property name="maximumNumber" value="10"/>
        </module>

        Too many local variables within a method:
        <module name="DescendantToken">
            <property name="tokens" value="METHOD_DEF"/>
            <property name="limitedTokens" value="VARIABLE_DEF"/>
            <property name="maximumDepth" value="2"/>
            <property name="maximumNumber" value="10"/>
        </module>

        Too many returns from within a method:
        <module name="DescendantToken">
            <property name="tokens" value="METHOD_DEF"/>
            <property name="limitedTokens" value="LITERAL_RETURN"/>
            <property name="maximumNumber" value="3"/>
        </module>

        Too many fields within an interface:
        <module name="DescendantToken">
            <property name="tokens" value="INTERFACE_DEF"/>
            <property name="limitedTokens" value="VARIABLE_DEF"/>
            <property name="maximumDepth" value="2"/>
            <property name="maximumNumber" value="0"/>
        </module>

        Limit the number of exceptions a method can throw:
        <module name="DescendantToken">
            <property name="tokens" value="LITERAL_THROWS"/>
            <property name="limitedTokens" value="IDENT"/>
            <property name="maximumNumber" value="1"/>
        </module>

        Limit the number of expressions in a method:
        <module name="DescendantToken">
            <property name="tokens" value="METHOD_DEF"/>
            <property name="limitedTokens" value="EXPR"/>
            <property name="maximumNumber" value="200"/>
        </module>

        Disallow empty statements:
        <module name="DescendantToken">
            <property name="tokens" value="EMPTY_STAT"/>
            <property name="limitedTokens" value="EMPTY_STAT"/>
            <property name="maximumNumber" value="0"/>
            <property name="maximumDepth" value="0"/>
            <property name="maximumMessage"
                value="Empty statement is not allowed."/>
        </module>

        Too many fields within a class:
        <module name="DescendantToken">
            <property name="tokens" value="CLASS_DEF"/>
            <property name="limitedTokens" value="VARIABLE_DEF"/>
            <property name="maximumDepth" value="2"/>
            <property name="maximumNumber" value="10"/>
        </module>
        

Package

com.puppycrawl.tools.checkstyle.checks

Parent Module

TreeWalker

Indentation

Indentation

Description

Checks correct indentation of Java Code.

The basic idea behind this is that while pretty printers are sometimes convienent for bulk reformats of legacy code, they often either aren't configurable enough or just can't anticipate how format should be done. Sometimes this is personal preference, other times it is practical experience. In any case, this check should just ensure that a minimal set of indentation rules are followed.

Properties

name description type default value
basicOffset how many spaces to use for new indentation level Integer 4
braceAdjustment how far brace should be indented when on next line Integer 0
caseIndent how much to indent a case label Integer 4

Examples

To configure the check:

<module name="Indentation"/>
      

To configure the check to enforce indentation style recommended by Sun:

<module name="Indentation">
    <property name="caseIndent" value="0"/>
</module>
      

Package

com.puppycrawl.tools.checkstyle.checks.indentation

Parent Module

TreeWalker


Back to the Checkstyle Home Page