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
maxPackage Specify the maximum number of package methods allowed. int 100 5.3
maxPrivate Specify the maximum number of private 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
maxTotal Specify the maximum number of methods allowed at all scope levels. 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="Checker">
  <module name="TreeWalker">
    <module name="MethodCount"/>
  </module>
</module>
        

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

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

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

<module name="Checker">
  <module name="TreeWalker">
    <module name="MethodCount">
      <property name="maxPublic" value="10"/>
      <property name="maxTotal" value="40"/>
    </module>
  </module>
</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