ParameterNumber

Since Checkstyle 3.0

Description

Checks the number of parameters of a method or constructor.

Properties

name description type default value since
ignoreAnnotatedBy Ignore methods and constructors annotated with the specified annotation(s). String[] {} 10.15.0
ignoreOverriddenMethods Ignore number of parameters for methods with @Override annotation. boolean false 6.2
max Specify the maximum number of parameters allowed. int 7 3.0
tokens tokens to check subset of tokens METHOD_DEF , CTOR_DEF . METHOD_DEF , CTOR_DEF . 3.0

Examples

To configure the check:

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

Example:

class Example1 extends ExternalService1 {

  @JsonCreator
  // violation below, 'More than 7 parameters (found 8)'
  Example1(int a, int b, int c, int d,
           int e, int f, int g, int h) {}

  // violation below, 'More than 7 parameters (found 8)'
  Example1(String a, String b, String c, String d,
           String e, String f, String g, String h) {}

  @Override
  // violation below, 'More than 7 parameters (found 8)'
  public void processData(String a, String b, String c, String d,
                          String e, String f, String g, String h) {}

}

class ExternalService1 {

  // violation below, 'More than 7 parameters (found 8)'
  public void processData(String a, String b, String c, String d,
                          String e, String f, String g, String h) {}

}
        

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

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

Example:

class Example2 extends ExternalService2 {

  @JsonCreator
  // ok below, constructor is not in tokens to check
  Example2(int a, int b, int c, int d,
           int e, int f, int g, int h) {}

  // ok below, constructor is not in tokens to check
  Example2(String a, String b, String c, String d,
           String e, String f, String g, String h) {}

  @Override
  // ok below, less than 10 parameters (found 8)
  public void processData(String a, String b, String c, String d,
                          String e, String f, String g, String h) {}

}

class ExternalService2 {

  // ok below, less than 10 parameters (found 8)
  public void processData(String a, String b, String c, String d,
                          String e, String f, String g, String h) {}

}
        

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="Checker">
  <module name="TreeWalker">
    <module name="ParameterNumber">
      <property name="ignoreOverriddenMethods" value="true"/>
    </module>
  </module>
</module>
        

Example:

class Example3 extends ExternalService3 {

  @JsonCreator
  // violation below, 'More than 7 parameters (found 8)'
  Example3(int a, int b, int c, int d,
           int e, int f, int g, int h) {}

  // violation below, 'More than 7 parameters (found 8)'
  Example3(String a, String b, String c, String d,
           String e, String f, String g, String h) {}

  @Override
  // ok below, overridden method is ignored
  public void processData(String a, String b, String c, String d,
                          String e, String f, String g, String h) {}

}

class ExternalService3 {

  // violation below, 'More than 7 parameters (found 8)'
  public void processData(String a, String b, String c, String d,
                          String e, String f, String g, String h) {}

}
        

To configure the check to ignore methods and constructors annotated with JsonCreator annotation:

<module name="Checker">
  <module name="TreeWalker">
    <module name="ParameterNumber">
      <property name="ignoreAnnotatedBy" value="JsonCreator"/>
    </module>
  </module>
</module>
        

Example:

class Example4 extends ExternalService4 {

  @JsonCreator
  // ok below, constructor annotated with JsonCreator annotation is ignored
  Example4(int a, int b, int c, int d,
           int e, int f, int g, int h) {}

  // violation below, 'More than 7 parameters (found 8)'
  Example4(String a, String b, String c, String d,
           String e, String f, String g, String h) {}

  @Override
  // violation below, 'More than 7 parameters (found 8)'
  public void processData(String a, String b, String c, String d,
                          String e, String f, String g, String h) {}

}

class ExternalService4 {

  // violation below, 'More than 7 parameters (found 8)'
  public void processData(String a, String b, String c, String d,
                          String e, String f, String g, String h) {}

}
        

Note: Annotation names specified in the ignoreAnnotatedBy property must be an exact match for the annotation name on the method or constructor.

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