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 , LAMBDA . CTOR_DEF , METHOD_DEF , INSTANCE_INIT , STATIC_INIT , COMPACT_CTOR_DEF , LAMBDA . 3.2

Examples

To configure the check:

<module name="Checker">
  <module name="TreeWalker">
    <module name="ExecutableStatementCount"/>
  </module>
</module>
        

Example1:

class Example1 {

  Example1() {
    int a=10;
    int b=20;
    int sub=b-a;
  }
  void testMethod1() {
    int a = 10;
    int b = 20;
    int sum = a + b;
  }
}
        

To configure the check with a threshold of 2 for constructor:

<module name="Checker">
  <module name="TreeWalker">
    <module name="ExecutableStatementCount">
      <property name="max" value="2"/>
      <property name="tokens" value="CTOR_DEF"/>
    </module>
  </module>
</module>
        

Example2:

class Example2 {

  Example2() { // violation, 'Executable statement count is 3 (max allowed is 2)'
    int a=10;
    int b=20;
    int sub=b-a;
  }
  void testMethod1() {
    int a = 10;
    int b = 20;
    int sum = a + b;
  }
}
        

To configure the check with a threshold of 2 for method definitions:

<module name="Checker">
  <module name="TreeWalker">
    <module name="ExecutableStatementCount">
      <property name="max" value="2"/>
      <property name="tokens" value="METHOD_DEF"/>
    </module>
  </module>
</module>
        

Example3:

class Example3 {

  Example3() {
    int a=10;
    int b=20;
    int sub=b-a;
  }
  void testMethod1() { // violation, 'Executable statement count is 3'
    int a = 10;
    int b = 20;
    int sum = a + b;
  }
}
        

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