Content

AnonInnerLength

Since Checkstyle 3.2

Description

Checks for long anonymous inner classes.

Rationale: If an anonymous inner class becomes very long it is hard to understand and to see the flow of the method where the class is defined. Therefore long anonymous inner classes should usually be refactored into a named inner class. See also Bloch, Effective Java, p. 93.

Properties

name description type default value since
max Specify the maximum number of lines allowed. int 20 3.2

Examples

To configure the check to accept anonymous classes with up to 20 lines:

<module name="AnonInnerLength"/>
        

To configure the check to accept anonymous classes with up to 60 lines:

<module name="AnonInnerLength">
  <property name="max" value="60"/>
</module>
        

Example of Usage

Violation Messages

All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.sizes

Parent Module

TreeWalker

ExecutableStatementCount

Since Checkstyle 3.2

Description

Restricts the number of executable statements to a specified limit.

Properties

name description type default value since
max Specify the maximum threshold allowed. int 30 3.2
tokens tokens to check subset of tokens CTOR_DEF , METHOD_DEF , INSTANCE_INIT , STATIC_INIT , COMPACT_CTOR_DEF . CTOR_DEF , METHOD_DEF , INSTANCE_INIT , STATIC_INIT , COMPACT_CTOR_DEF . 3.2

Examples

To configure the check:

<module name="ExecutableStatementCount"/>
        

To configure the check with a threshold of 20 for constructor and method definitions:

<module name="ExecutableStatementCount">
  <property name="max" value="20"/>
  <property name="tokens" value="CTOR_DEF,METHOD_DEF"/>
</module>
        

Example of Usage

Violation Messages

All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.sizes

Parent Module

TreeWalker

FileLength

Since Checkstyle 5.0

Description

Checks for long source files.

Rationale: If a source file becomes very long it is hard to understand. Therefore long classes should usually be refactored into several individual classes that focus on a specific task.

Properties

name description type default value since
max Specify the maximum number of lines allowed. int 2000 3.2
fileExtensions Specify the file type extension of files to process. String[] all files 5.0

Examples

To configure the check:

<module name="FileLength"/>
        

To configure the check to accept files with up to 1500 lines:

<module name="FileLength">
  <property name="max" value="1500"/>
</module>
        

Example of Usage

Violation Messages

All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.sizes

Parent Module

Checker

LambdaBodyLength

Since Checkstyle 8.37

Description

Checks lambda body length.

Rationale: Similar to anonymous inner classes, if lambda body becomes very long it is hard to understand and to see the flow of the method where the lambda is defined. Therefore long lambda body should usually be extracted to method.

Properties

name description type default value since
max Specify the maximum number of lines allowed. int 10 8.37

Examples

To configure the check to accept lambda bodies with up to 10 lines:

<module name="LambdaBodyLength"/>
        

Example:

class Test {
  Runnable r = () -> { // violation, 11 lines
      System.out.println(2); // line 2 of lambda
      System.out.println(3);
      System.out.println(4);
      System.out.println(5);
      System.out.println(6);
      System.out.println(7);
      System.out.println(8);
      System.out.println(9);
      System.out.println(10);
  }; // line 11

  Runnable r2 = () -> // violation, 11 lines
      "someString".concat("1") // line 1 of lambda
                  .concat("2")
                  .concat("3")
                  .concat("4")
                  .concat("5")
                  .concat("6")
                  .concat("7")
                  .concat("8")
                  .concat("9")
                  .concat("10")
                  .concat("11"); // line 11

  Runnable r3 = () -> { // ok, 10 lines
      System.out.println(2); // line 2 of lambda
      System.out.println(3);
      System.out.println(4);
      System.out.println(5);
      System.out.println(6);
      System.out.println(7);
      System.out.println(8);
      System.out.println(9);
  }; // line 10
}
        

To configure the check to accept lambda bodies with max 5 lines:

<module name="LambdaBodyLength">
  <property name="max" value="5"/>
</module>
        

Example:

class Test {
  Runnable r = () -> { // violation, 6 lines
      System.out.println(2); // line 2 of lambda
      System.out.println(3);
      System.out.println(4);
      System.out.println(5);
  };

  Runnable r2 = () -> // violation, 6 lines
      "someString".concat("1")
                  .concat("2")
                  .concat("3")
                  .concat("4")
                  .concat("5")
                  .concat("6");

  Runnable r3 = () -> { // ok, 5 lines
      System.out.println(2);
      System.out.println(3);
      System.out.println(4);
  };
}
        

Example of Usage

Violation Messages

All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.sizes

Parent Module

TreeWalker

LineLength

Since Checkstyle 3.0

Description

Checks for long lines.

Rationale: Long lines are hard to read in printouts or if developers have limited screen space for the source code, e.g. if the IDE displays additional information like project tree, class hierarchy, etc.

Properties

name description type default value since
fileExtensions Specify file extensions that are accepted. String[] all files 8.24
ignorePattern Specify pattern for lines to ignore. Pattern "^$" 3.0
max Specify the maximum line length allowed. int 80 3.0

Examples

To configure the check to accept lines up to 80 characters long:

<module name="LineLength"/>
        

To configure the check to accept lines up to 120 characters long:

<module name="LineLength">
  <property name="max" value="120"/>
</module>
        

To configure the check to ignore lines that begin with " * "code, followed by just one word, such as within a Javadoc comment:

<module name="LineLength">
  <property name="ignorePattern" value="^ *\* *[^ ]+$"/>
</module>
        

To configure the check to only validate java files and ignore other extensions:

<module name="LineLength">
  <property name="fileExtensions" value="java"/>
</module>
        

To configure the check to only validate xml and property files and ignore other extensions:

<module name="LineLength">
  <property name="fileExtensions" value="xml, properties"/>
</module>
        

Notes

  • The calculation of the length of a line takes into account the number of expanded spaces for a tab character ('\t'). The default number of spaces is 8. To specify a different number of spaces, the user can set TreeWalker property tabWidth which applies to all Checks, including LineLength; or can set property tabWidth for LineLength alone.
  • Package and import statements (lines matching pattern ^(package|import) .*) are not verified by this check.

Example of Usage

Violation Messages

All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.sizes

Parent Module

Checker

MethodCount

Since Checkstyle 5.3

Description

Checks the number of methods declared in each type declaration by access modifier or total count.

This check can be configured to flag classes that define too many methods to prevent the class from getting too complex. Counting can be customized to prevent too many total methods in a type definition (maxTotal), or to prevent too many methods of a specific access modifier (private, package, protected or public). Each count is completely separated to customize how many methods of each you want to allow. For example, specifying a maxTotal of 10, still means you can prevent more than 0 maxPackage methods. A violation won't appear for 8 public methods, but one will appear if there is also 3 private methods or any package-private methods.

Methods defined in anonymous classes are not counted towards any totals. Counts only go towards the main type declaration parent, and are kept separate from it's children's inner types.

public class ExampleClass {
  public enum Colors {
    RED, GREEN, YELLOW;

    public String getRGB() { ... } // NOT counted towards ExampleClass
  }

  public void example() { // counted towards ExampleClass
    Runnable r = (new Runnable() {
      public void run() { ... } // NOT counted towards ExampleClass, won't produce any violations
    });
  }

  public static class InnerExampleClass {
    protected void example2() { ... } // NOT counted towards ExampleClass,
                                   // but counted towards InnerExampleClass
  }
}
        

Properties

name description type default value since
maxTotal Specify the maximum number of methods allowed at all scope levels. int 100 5.3
maxPrivate Specify the maximum number of private methods allowed. int 100 5.3
maxPackage Specify the maximum number of package methods allowed. int 100 5.3
maxProtected Specify the maximum number of protected methods allowed. int 100 5.3
maxPublic Specify the maximum number of public methods allowed. int 100 5.3
tokens tokens to check subset of tokens CLASS_DEF , ENUM_CONSTANT_DEF , ENUM_DEF , INTERFACE_DEF , ANNOTATION_DEF , RECORD_DEF . CLASS_DEF , ENUM_CONSTANT_DEF , ENUM_DEF , INTERFACE_DEF , ANNOTATION_DEF , RECORD_DEF . 5.3

Examples

To configure the default check:

<module name="MethodCount"/>
        

To configure the check to allow no more than 30 methods per type declaration:

<module name="MethodCount">
  <property name="maxTotal" value="30"/>
</module>
        

To configure the check to allow no more than 10 public methods per type declaration, and 40 methods in total:

<module name="MethodCount">
  <property name="maxPublic" value="10"/>
  <property name="maxTotal" value="40"/>
</module>
        

Example of Usage

Violation Messages

All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.sizes

Parent Module

TreeWalker

MethodLength

Since Checkstyle 3.0

Description

Checks for long methods and constructors.

Rationale: If a method becomes very long it is hard to understand. Therefore long methods should usually be refactored into several individual methods that focus on a specific task.

Properties

name description type default value since
max Specify the maximum number of lines allowed. int 150 3.0
countEmpty Control whether to count empty lines and single line comments of the form //. boolean true 3.2
tokens tokens to check subset of tokens METHOD_DEF , CTOR_DEF , COMPACT_CTOR_DEF . METHOD_DEF , CTOR_DEF , COMPACT_CTOR_DEF . 3.0

Examples

To configure the check:

<module name="MethodLength"/>
        

To configure the check so that it accepts methods with at most 60 lines:

<module name="MethodLength">
  <property name="tokens" value="METHOD_DEF"/>
  <property name="max" value="60"/>
</module>
        

To configure the check so that it accepts methods with at most 60 lines, not counting empty lines and single line comments:

<module name="MethodLength">
  <property name="tokens" value="METHOD_DEF"/>
  <property name="max" value="60"/>
  <property name="countEmpty" value="false"/>
</module>
        

Example of Usage

Violation Messages

All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.sizes

Parent Module

TreeWalker

OuterTypeNumber

Since Checkstyle 5.0

Description

Checks for the number of types declared at the outer (or root) level in a file.

Rationale: It is considered good practice to only define one outer type per file.

Properties

name description type default value since
max Specify the maximum number of outer types allowed. int 1 5.0

Examples

To configure the check to accept 1 outer type per file:

<module name="OuterTypeNumber"/>
        

To configure the check to accept 2 outer types per file:

<module name="OuterTypeNumber">
  <property name="max" value="2"/>
</module>
        

Example of Usage

Violation Messages

All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.sizes

Parent Module

TreeWalker

ParameterNumber

Since Checkstyle 3.0

Description

Checks the number of parameters of a method or constructor.

Properties

name description type default value since
max Specify the maximum number of parameters allowed. int 7 3.0
ignoreOverriddenMethods Ignore number of parameters for methods with @Override annotation. boolean false 6.2
tokens tokens to check subset of tokens METHOD_DEF , CTOR_DEF . METHOD_DEF , CTOR_DEF . 3.0

Examples

To configure the check:

<module name="ParameterNumber"/>
        

To configure the check to allow 10 parameters for a method:

<module name="ParameterNumber">
  <property name="max" value="10"/>
  <property name="tokens" value="METHOD_DEF"/>
</module>
        

To configure the check to ignore number of parameters for methods with @Override or @java.lang.Override annotation.

Rationale: developer may need to override method with many parameters from 3-rd party library. In this case developer has no control over number of parameters.

<module name="ParameterNumber">
  <property name="ignoreOverriddenMethods" value="true"/>
</module>
        

Java code example:

@Override
public void needsLotsOfParameters(int a,
    int b, int c, int d, int e, int f, int g, int h) {
    ...
}
        

Example of Usage

Violation Messages

All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.sizes

Parent Module

TreeWalker

RecordComponentNumber

Since Checkstyle 8.36

Description

Checks the number of record components in the header of a record definition.

Properties

name description type default value since
max Specify the maximum number of components allowed in the header of a record definition. int 8 8.36
accessModifiers Access modifiers of record definitions where the number of record components should be checked. AccessModifierOption[] public, protected, package, private 8.36

Examples

To configure the check:

<module name="RecordComponentNumber"/>
        

Java code example:

public record MyRecord1(int x, int y) { // ok, 2 components
   ...
}

record MyRecord2(int x, int y, String str,
                         Node node, Order order, Data data
                         String location, Date date, Image image) { // violation, 9 components
   ...
}

        

To configure the check to allow 5 record components at all access modifier levels for record definitions:

<module name="RecordComponentNumber">
  <property name="max" value="5"/>
</module>
        

Java code example:

public record MyRecord1(int x, int y, String str) { // ok, 3 components
   ...
}

public record MyRecord2(int x, int y, String str,
                         Node node, Order order, Data data) { // violation, 6 components
   ...
}

        

To configure the check to allow 10 record components for a public record definition, but 3 for private record definitions:

<module name="RecordComponentNumber">
  <property name="max" value="3"/>
  <property name="accessModifiers" value="private"/>
</module>
<module name="RecordComponentNumber">
  <property name="max" value="10"/>
  <property name="accessModifiers" value="public"/>
</module>
        

Java code example:

public record MyRecord1(int x, int y, String str) { // ok, public record definition allowed 10
   ...
}

private record MyRecord2(int x, int y, String str, Node node) { // violation
   ...                                // private record definition allowed 3 components
}

        

Example of Usage

Violation Messages

All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.sizes

Parent Module

TreeWalker