OverloadMethodsDeclarationOrder

Since Checkstyle 5.8

Description

Checks that overloaded methods are grouped together. Overloaded methods have the same name but different signatures where the signature can differ by the number of input parameters or type of input parameters or both.

Properties

name description type default value since
orderByIncreasingParameterCount Control whether to enforce order by increasing parameter count (arity) or not. boolean false 13.7.0

Examples

To configure the check:


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

Example:


class Example1 {
  void same(int i) {}
  void same(String s, int i, int k) {}
  // comments between overloaded methods are allowed.
  void same(String s) {}

  void same(int i, String s) {}

  void notSame() {}
  interface notSame{}

  void otherSame(String s) {}
  void foo() {}
  // violation 2 lines below """All overloaded methods should be placed next
  //  to each other. Previous overloaded method located at line '24'."""
  void otherSame(String s, int i) {}

  public enum ExampleEnum {
    VALUE_ONE,
    VALUE_TWO;

    public void example() {}
    public void example(String s, int i) {}

    void foo() {}

    // violation 2 lines below """All overloaded methods should be placed next
    //  to each other. Previous overloaded method located at line '35'."""
    public void example(String s) {}

  }
}

To configure the check according to OpenJDK guidelines, where overloaded methods should be grouped together and ordered by increasing parameter count:


<module name="Checker">
  <module name="TreeWalker">
    <module name="OverloadMethodsDeclarationOrder">
        <property name="orderByIncreasingParameterCount" value="true"/>
    </module>
  </module>
</module>

Example:


class Example2 {
  void same(int i) {}
  void same(String s, int i, int k) {}
  // comments between overloaded methods are allowed.
  void same(String s) {}
  // violation above, 'Overloaded methods should be ordered by increasing parameter count.'
  void same(int i, String s) {}
  // violation above, 'Overloaded methods should be ordered by increasing parameter count.'
  void notSame() {}
  interface notSame{}

  void otherSame(String s) {}
  void foo() {}
  // violation 2 lines below """All overloaded methods should be placed next
  //  to each other. Previous overloaded method located at line '24'."""
  void otherSame(String s, int i) {}

  public enum ExampleEnum {
    VALUE_ONE,
    VALUE_TWO;

    public void example() {}
    public void example(String s, int i) {}

    void foo() {}

    // violation 2 lines below """All overloaded methods should be placed next
    //  to each other. Previous overloaded method located at line '35'."""
    public void example(String s) {}
    // violation above, 'Overloaded methods should be ordered by increasing parameter count.'
  }
}

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.

Fully Qualified Name

com.puppycrawl.tools.checkstyle.checks.coding.OverloadMethodsDeclarationOrderCheck

Use this fully qualified class name in configuration when an exact class reference is required.

Parent Module

TreeWalker