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>
        

Example:

class Example1 {

  public void outerMethod1(int i) {}
  public void outerMethod2() {}
  public void outerMethod3(String str) {}

  private void outerMethod4() {
    Runnable r = (new Runnable() {
      public void run() {} // NOT counted towards Example1
    });
  }

  private void outerMethod5(int i) {}
  void outerMethod6(int i, int j) {}

  public static class InnerExample{
    public void innerMethod1() {} // NOT counted towards Example1
    public void innerMethod2() {} // NOT counted towards Example1
  }
}
        

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

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

Example:

class Example2 { // violation, 'Total number of methods is 6 (max allowed is 5)'

  public void outerMethod1(int i) {}
  public void outerMethod2() {}
  public void outerMethod3(String str) {}

  private void outerMethod4() {
    Runnable r = (new Runnable() {
      public void run() {} // NOT counted towards Example2
    });
  }

  private void outerMethod5(int i) {}
  void outerMethod6(int i, int j) {}

  public static class InnerExample{
    public void innerMethod1() {} // NOT counted towards Example2
    public void innerMethod2() {} // NOT counted towards Example2
  }
}
        

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

<module name="Checker">
  <module name="TreeWalker">
    <module name="MethodCount">
      <property name="maxPublic" value="2"/>
      <property name="maxTotal" value="6"/>
    </module>
  </module>
</module>
        

Example:

class Example3 { // violation, 'Number of public methods is 3 (max allowed is 2)'

  public void outerMethod1(int i) {}
  public void outerMethod2() {}
  public void outerMethod3(String str) {}

  private void outerMethod4() {
    Runnable r = (new Runnable() {
      public void run() {} // NOT counted towards Example3
    });
  }

  private void outerMethod5(int i) {}
  void outerMethod6(int i, int j) {}

  public static class InnerExample{
    public void innerMethod1() {} // NOT counted towards Example3
    public void innerMethod2() {} // NOT counted towards Example3
  }
}
        

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