OpenjdkMethodThrowsAlignment

Since Checkstyle 14.2.0

Description

Checks that the throws clause of a wrapped method or constructor declaration is properly aligned according to the OpenJDK Java Style Guide.

This check only applies when the method or constructor declaration is wrapped, that is, the parameter list spans more than one line. Single-line declarations are not in scope.

The two rules enforced are:

  1. The throws keyword must start on a new line, it must not share the line with the closing ) of the parameter list.
  2. The throws keyword must stand out from the parameter list by being indented 8 columns relative to either:
    • the column of the method/constructor declaration (i.e. the indentation of the line containing the declaration keyword)
    • the indentation (first non-whitespace column) of the source line immediately above the line where throws appears

Examples

To configure the check to follow openjdk guidelines for throws clause alignment:


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

Example:


public class Example1 {
  public void valid(int a) throws Exception {
  }

  public void sameLine(int first,
    int second) throws Exception {
    // violation above """The 'throws' clause should be on a new line when
    // the method declaration is wrapped."""
  }

  public void newLine(int first,
    int second)
          throws Exception {
  }

  public void incorrectIndentation(int first,
                                  int second)
      throws Exception {
    // violation above """The 'throws' clause should be indented 8 spaces relative
    // to the method declaration or the previous line and should not align with the
    // previous line."""
  }

  public void correctIndentation(int first,
                                int second)
          throws Exception {
  }

  public void alignedWithPreviousLine(int first,
          int second)
          throws Exception {
    // violation above """The 'throws' clause should be indented 8 spaces relative
    // to the method declaration or the previous line and should not align with the
    // previous line."""
  }

  public void notAlignedWithPreviousLine(int first,
          int second)
                  throws Exception {
  }

  public void previousLineRelative(int first,
                                  int second)
                                          throws Exception {
  }
}

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.indentation.OpenjdkMethodThrowsAlignmentCheck

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

Parent Module

TreeWalker