Class EmptyLineWrappingInBlockCheck

All Implemented Interfaces:
Configurable, Contextualizable

Checks the empty line right after the opening brace and right before the closing brace of a block.

topSeparator controls the line after the opening brace and bottomSeparator controls the line before the closing brace. Both accept the following values:

  • empty_line - exactly one empty line is required;
  • empty_line_allowed - not validated, an empty line may be present or absent;
  • no_empty_line - an empty line is forbidden.
Since:
14.4.0
  • Field Details

  • Constructor Details

  • Method Details

    • setTopSeparator

      public void setTopSeparator(String value)
      Specify the policy for the empty line after the opening brace.
      Parameters:
      value - one of empty_line, empty_line_allowed, no_empty_line
      Throws:
      IllegalArgumentException - if the value is not recognized
      Since:
      14.4.0
    • setBottomSeparator

      public void setBottomSeparator(String value)
      Specify the policy for the empty line before the closing brace.
      Parameters:
      value - one of empty_line, empty_line_allowed, no_empty_line
      Throws:
      IllegalArgumentException - if the value is not recognized
      Since:
      14.4.0
    • getDefaultTokens

      public int[] getDefaultTokens()
      Returns the default token set that this check validates.
      Specified by:
      getDefaultTokens in class AbstractCheck
      Returns:
      the default tokens
      See Also:
    • getAcceptableTokens

      public int[] getAcceptableTokens()
      Returns the set of tokens that can be configured for this check.
      Specified by:
      getAcceptableTokens in class AbstractCheck
      Returns:
      the acceptable tokens
      See Also:
    • getRequiredTokens

      public int[] getRequiredTokens()
      Returns the tokens that this check must be registered for.
      Specified by:
      getRequiredTokens in class AbstractCheck
      Returns:
      the empty array as this check has no required tokens
      See Also:
    • visitToken

      public void visitToken(DetailAST ast)
      Called to process a token. Validates empty line wrapping after the opening brace and before the closing brace of the block according to the configured policies.
      Overrides:
      visitToken in class AbstractCheck
      Parameters:
      ast - the token to process
    • findLeftBrace

      private static DetailAST findLeftBrace(DetailAST ast)
      Finds the left (opening) brace of the block for the given AST node. The result depends on the token type (e.g. SLIST for statements, OBJBLOCK for type bodies, or the node itself for array inits).
      Parameters:
      ast - the AST node representing a block or block-containing construct
      Returns:
      the AST of the opening brace, or null if not found
    • findRightBrace

      private static DetailAST findRightBrace(DetailAST leftBrace)
      Finds the right (closing) brace of the block for the given opening brace. The result depends on the token type: for SLIST/array-init the closing brace is the last child; for OBJBLOCK etc. it is the last child of the parent.
      Parameters:
      leftBrace - the opening brace AST, non-null
      Returns:
      the closing brace AST
    • hasBlankLineAfter

      private boolean hasBlankLineAfter(DetailAST leftCurly, DetailAST rightCurly)
      Checks whether the line immediately after the opening brace is blank.
      Parameters:
      leftCurly - the opening brace token
      rightCurly - the closing brace token
      Returns:
      true if the braces are on different lines and the next line is blank
    • hasOneEmptyLineAfter

      private boolean hasOneEmptyLineAfter(DetailAST leftCurly, DetailAST rightCurly)
      Checks whether there is exactly one empty line after the opening brace. Used when topSeparator is EMPTY_LINE. Two or more consecutive empty lines are rejected.
      Parameters:
      leftCurly - the opening brace token
      rightCurly - the closing brace token
      Returns:
      true if the next line is blank and the line after that is not blank
    • hasBlankLineBefore

      private boolean hasBlankLineBefore(DetailAST leftCurly, DetailAST rightCurly)
      Checks whether the line immediately before the closing brace is blank. Used when bottomSeparator is NO_EMPTY_LINE. Always false for same-line braces.
      Parameters:
      leftCurly - the opening brace token
      rightCurly - the closing brace token
      Returns:
      true if the braces are on different lines and the previous line is blank
    • hasOneEmptyLineBefore

      private boolean hasOneEmptyLineBefore(DetailAST leftCurly, DetailAST rightCurly)
      Checks whether there is exactly one empty line before the closing brace. Used when bottomSeparator is EMPTY_LINE. Two or more consecutive empty lines are rejected.
      Parameters:
      leftCurly - the opening brace token
      rightCurly - the closing brace token
      Returns:
      true if the previous line is blank and the line before that is not blank