Miscellaneous Checks

Checkstyle Logo

EqualsHashCode

Description

Checks that classes that override equals() also override hashCode().

Rationale: The contract of equals() and hashCode() requires that equal objects have the same hashCode. Hence, whenever you override equals() you must override hashCode() to ensure that your class can be used in collections that are hash based.

Example

To configure the check:

<module name="EqualsHashCode"/>
      

Package

com.puppycrawl.tools.checkstyle.checks

Parent Module

TreeWalker

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)

Example

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

<module name="GenericIllegalRegexp">
    <property name="format" value="System\.out\.println"/>
</module>
      

Package

com.puppycrawl.tools.checkstyle.checks

Parent Module

TreeWalker

HiddenField

Description

Checks that a local variable or a parameter does not shadow a field that is defined in the same class.

Properties

name description type default value
tokens tokens to check subset of tokens PARAMETER_DEF, VARIABLE_DEF PARAMETER_DEF, VARIABLE_DEF

Examples

To configure the check:

<module name="HiddenField"/>
      

To configure the check so that it checks local variables but not parameters:

<module name="HiddenField">
    <property name="tokens" value="VARIABLE_DEF"/>
</module>
      

Package

com.puppycrawl.tools.checkstyle.checks

Parent Module

TreeWalker

IllegalInstantiation

Description

Checks for illegal instantiations where a factory method is preferred.

Rationale: Depending on the project, for some classes it might be preferable to create instances through factory methods rather than calling the constructor.

A simple example is the java.lang.Boolean class. In order to save memory and CPU cycles, it is preferable to use the predefined constants TRUE and FALSE. Constructor invocations should be replaced by calls to Boolean.valueOf().

Some extremely performance sensitive projects may require the use of factory methods for other classes as well, to enforce the usage of number caches or object pools.

Notes

There is a limitation that it is currently not possible to specify array classes.

name description type default value
classes classes that should not be instantiated String Set {}

Example

To configure the check:

<module name="IllegalInstantiation"/>
      

Package

com.puppycrawl.tools.checkstyle.checks

Parent Module

TreeWalker

InnerAssignment

Description

Checks for assignments in subexpressions, such as in String s = Integer.toString(i = 2);.

Rationale: With the exception of for iterators, all assignments should occur in their own toplevel statement to increase readability. With inner assignments like the above it is difficult to see all places where a variable is set.

Properties

name description type default value
tokens assignments to check subset of tokens ASSIGN, BAND_ASSIGN, BOR_ASSIGN, BSR_ASSIGN, BXOR_ASSIGN, DIV_ASSIGN, MINUS_ASSIGN, MOD_ASSIGN, PLUS_ASSIGN, SL_ASSIGN, SR_ASSIGN, STAR_ASSIGN all tokens

Examples

To configure the check:

<module name="InnerAssignment"/>
      

To configure the check for only =, +=, and -= operators:

<module name="InnerAssignment">
    <property name="tokens" value="ASSIGN,PLUS_ASSIGN,MINUS_ASSIGN"/>
</module>
      

Package

com.puppycrawl.tools.checkstyle.checks

Parent Module

TreeWalker

SimplifyBooleanExpression

Description

Checks for overly complicated boolean expressions. Currently finds code like if (b == true), b || true, !false, etc.

Rationale: Complex boolean logic makes code hard to understand and maintain.

Example

To configure the check:

<module name="SimplifyBooleanExpression"/>
      

Package

com.puppycrawl.tools.checkstyle.checks

Parent Module

TreeWalker

SimplifyBooleanReturn

Description

Checks for overly complicated boolean return statements. For example the following code

if (valid())
    return false;
else
    return true;
      

could be written as

return !valid();
      

The Idea for this Check has been shamelessly stolen from the equivalent PMD rule.

Example

To configure the check:

<module name="SimplifyBooleanReturn"/>
      

Package

com.puppycrawl.tools.checkstyle.checks

Parent Module

TreeWalker

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

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

AvoidInlineConditionals

Description

Detects inline conditionals. An example inline conditional is this:

String a = getParameter("a");
String b = (a==null || a.length<1) ? null : a.substring(1);
      

Rationale: Some developers find inline conditionals hard to read, so their company's coding standards forbids them.

Examples

To configure the check:

<module name="AvoidInlineConditionals"/>
      

Package

com.puppycrawl.tools.checkstyle.checks

Parent Module

TreeWalker

InterfaceIsType

Description

Implements Bloch, Effective Java, Item 17 - Use Interfaces only to define types.

According to Bloch, an interface should describe a type. It is therefore inappropriate to define an interface that does not contain any methods but only constants. The Standard class javax.swing.SwingConstants is an example of a class that would be flagged by this check.

The check can be configured to also disallow marker interfaces like java.io.Serializable, that do not contain methods or constants at all.

Properties

name description type default value
allowMarkerInterfaces Controls whether marker interfaces like Serializable are allowed. Boolean true

Examples

To configure the check:

<module name="InterfaceIsType"/>
      

Package

com.puppycrawl.tools.checkstyle.checks

Parent Module

TreeWalker

HideUtilityClassConstructor

Description

Make sure that utility classes (classes that contain only static methods) do not have a public constructor.

Rationale: Instantiating utility classes does not make sense. Hence the constructors should either be private or (if you want to allow subclassing) protected. A common mistake is forgetting to hide the default constructor.

If you make the constructor protected you may want to consider the following constructor implementation technique to disallow instantiating subclasses:

public class StringUtils // not final to allow subclassing
{
    protected StringUtils() {
        throw new UnsupportedOperationException(); // prevents calls from subclass
    }

    public int count(char c, String s) {
        // ...
    }
}
      

Examples

To configure the check:

<module name="HideUtilityClassConstructor"/>
      

Package

com.puppycrawl.tools.checkstyle.checks

Parent Module

TreeWalker

DesignForInheritance

Description

Checks that classes are designed for inheritance. More specifically, it enforces a programming style where superclasses provide empty "hooks" that can be implemented by subclasses.

The exact rule is that nonprivate methods of classes that can be subclassed must either be

  • abstract or
  • final or
  • have an empty implementation

Rationale: This API design style protects superclasses against beeing broken by subclasses. The downside is that subclasses are limited in their flexibility, in particular they cannot prevent execution of code in the superclass, but that also means that subclasses cannot corrupt the state of the superclass by forgetting to call the super method.

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

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 mathod 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


Copyright © 2002 Oliver Burn. All rights Reserved.