Class OpenjdkMethodParameterAlignmentCheck

All Implemented Interfaces:
Configurable, Contextualizable

Checks that the parameters of a wrapped method declaration follow one of the two forms allowed by the OpenJDK style guidelines.

The OpenJDK Java Style Guidelines allow a wrapped method declaration to be formatted either by listing the parameters vertically, one per line, or by breaking the line and indenting the continuation by eight extra spaces. This check validates a wrapped declaration against those two forms:

  • when every parameter that starts a new line is indented eight spaces past the declaration, the declaration uses the eight extra spaces form and any number of parameters may share a line;
  • otherwise the parameters are expected to be listed vertically, that is, every parameter that starts a new line is aligned with the first parameter and each line holds exactly one parameter.

A declaration whose parameter list fits on a single line is ignored. A wrapped declaration in which no parameter starts a new line, because it is wrapped inside a parameter type rather than between two parameters, is expected to be listed vertically, so a line that holds more than one parameter is reported.

Since:
14.4.0
  • Field Details

  • Constructor Details

  • Method Details

    • getDefaultTokens

      public int[] getDefaultTokens()
      Description copied from class: AbstractCheck
      Returns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.
      Specified by:
      getDefaultTokens in class AbstractCheck
      Returns:
      the default tokens
      See Also:
    • getAcceptableTokens

      public int[] getAcceptableTokens()
      Description copied from class: AbstractCheck
      The configurable token set. Used to protect Checks against malicious users who specify an unacceptable token set in the configuration file. The default implementation returns the check's default tokens.
      Specified by:
      getAcceptableTokens in class AbstractCheck
      Returns:
      the token set this check is designed for.
      See Also:
    • getRequiredTokens

      public int[] getRequiredTokens()
      Description copied from class: AbstractCheck
      The tokens that this check must be registered for.
      Specified by:
      getRequiredTokens in class AbstractCheck
      Returns:
      the token set this must be registered for.
      See Also:
    • visitToken

      public void visitToken(DetailAST ast)
      Description copied from class: AbstractCheck
      Called to process a token.
      Overrides:
      visitToken in class AbstractCheck
      Parameters:
      ast - the token to process
    • getParameters

      private static List<DetailAST> getParameters(DetailAST parameters)
      Collects the parameters of the given parameter list.
      Parameters:
      parameters - the PARAMETERS node of a method declaration
      Returns:
      every parameter, in declaration order
    • spansMoreThanOneLine

      private static boolean spansMoreThanOneLine(SequencedCollection<DetailAST> parameters)
      Checks whether the parameters of a declaration are spread over more than one line. A declaration can be wrapped without any parameter starting a new line, when it is wrapped inside a parameter type rather than between two parameters. Parameters are listed in source order, so a parameter that starts a new line always leaves the last parameter on a later line than the first one.
      Parameters:
      parameters - every parameter of a method declaration
      Returns:
      true if the parameters do not all start on the same line
    • getLineStartColumns

      private List<Integer> getLineStartColumns(List<DetailAST> parameters)
      Collects the column the line of every parameter that starts a new line begins in. The first parameter of a declaration is the column the other parameters are compared against, so it is never collected.
      Parameters:
      parameters - every parameter of a method declaration
      Returns:
      the column every parameter that starts a new line is expected to be aligned in
    • hasMultipleParametersOnLine

      private static boolean hasMultipleParametersOnLine(List<DetailAST> parameters)
      Checks whether any parameter is declared on the line the parameter before it ends on. The comma that separates two parameters is not taken into account, as a parameter list can be wrapped before that comma instead of after it.
      Parameters:
      parameters - every parameter of a method declaration
      Returns:
      true if a line holds more than one parameter
    • sharesLineWithPreviousParameter

      private static boolean sharesLineWithPreviousParameter(List<DetailAST> parameters, int index)
      Checks whether the parameter at the given position begins on the line the parameter before it ends on. A parameter can end on a later line than it begins on, when it is wrapped inside its own type or annotations.
      Parameters:
      parameters - every parameter of a method declaration
      index - the position of the parameter to locate, never the first one
      Returns:
      true if the parameter shares a line with the parameter before it
    • getLastLineNo

      private static int getLastLineNo(DetailAST ast)
      Returns the number of the last line the given parameter is declared on.
      Parameters:
      ast - the parameter to measure
      Returns:
      the greatest line number of the tokens of the parameter
    • getLineStart

      private static DetailAST getLineStart(DetailAST parameter)
      Returns the token the line of a parameter that starts a new line begins with. A parameter list can be wrapped before the comma that separates two parameters instead of after it, which leaves that comma as the first token of the line, so such a line begins at the comma rather than at the parameter. The comma that separates a parameter from the parameter before it is the sibling preceding it, so a parameter that does not start a new line is never passed here.
      Parameters:
      parameter - the parameter that starts a new line
      Returns:
      the comma preceding the parameter when both are on one line, the first token of the parameter otherwise
    • isAlignedOn

      private static boolean isAlignedOn(Collection<Integer> lineStartColumns, int column)
      Checks whether every parameter that starts a new line begins in the given column. A declaration whose parameters all start on one line has no parameter that starts a new line, so it is aligned on every column and is never reported.
      Parameters:
      lineStartColumns - the column of every parameter that starts a new line
      column - the column the parameters are expected to begin in
      Returns:
      true if every parameter that starts a new line begins in that column
    • expandedColumnNo

      private int expandedColumnNo(DetailAST ast)
      Returns the column the given token starts in, counting the tabs that precede it on its line as the configured tab width. Alignment is what the reader sees, so a declaration indented with tabs has to be measured the same way an editor renders it.
      Parameters:
      ast - the token to locate
      Returns:
      the column of the token, with preceding tabs expanded