Class GoogleRightCurlyCheck

All Implemented Interfaces:
Configurable, Contextualizable

Checks the placement of right curly braces ('}') for code blocks, following the Google Java Style Guide .

For nonempty blocks the right curly brace must begin its own line, unless it is followed by else, catch, finally, or a comma, in which case no line break follows it.

For empty blocks, either K&R style or the concise {} form is allowed, except within a multi-block statement (if/else, try/catch/finally).

Since:
14.2.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:
    • isCommentNodesRequired

      public boolean isCommentNodesRequired()
      Description copied from class: AbstractCheck
      Whether comment nodes are required or not.
      Overrides:
      isCommentNodesRequired in class AbstractCheck
      Returns:
      false as a default value.
    • 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
    • handleCaseAndDefault

      private void handleCaseAndDefault(DetailAST ast)
      Checks the right curly brace placement for case and default blocks, covering both old style case X: and new style case X -> switch syntax.

      For old-style syntax, a case label may be followed by multiple {} blocks in sequence, and each such block's right curly brace is checked. For new-style syntax, the block following the arrow (e.g. case X -> { ... }) is checked, expression with no block (e.g. case X -> expr;) is skipped.

      Parameters:
      ast - the case or default token
    • logViolations

      private void logViolations(String message, DetailAST brace)
      Logs violation message for given brace token.
      Parameters:
      message - the violation message key.
      brace - the right curly brace.
    • checkRightBrace

      private void checkRightBrace(DetailAST currentBlock, DetailAST brace)
      Checks that a right curly brace is placed correctly.

      If the block is part of a multi-block statement (e.g. if/else, try/catch/finally, or do/while), the closing brace must be on the same line as the next block's starting keyword. Otherwise, the brace must be alone on its own line, unless the block is empty, in which case the concise {} form is allowed.

      Parameters:
      currentBlock - the block whose right curly brace is being checked
      brace - the right curly brace token
    • checkMultiBlockStatement

      private void checkMultiBlockStatement(DetailAST currentBlock, DetailAST brace, DetailAST nextBlock)
      Checks that the right curly brace of a multi-block statement (e.g. if/else, try/catch/finally, do/while) is placed correctly relative to the next block.
      Parameters:
      currentBlock - the current block
      brace - the right curly brace
      nextBlock - the next block in multi-block statement
    • verifyEmptyBlock

      private void verifyEmptyBlock(DetailAST brace, @Nullable DetailAST nextToken)
      Checks empty block which should be concise and alone.
      Parameters:
      brace - the right curly token.
      nextToken - the token after right curly brace.
    • contentAround

      private static boolean contentAround(DetailAST brace, @Nullable DetailAST nextToken)
      Checks if the right curly has content around.
      Parameters:
      brace - the right curly brace
      nextToken - the next token after right curly
      Returns:
      true if right curly has content on its left or right.
    • isPartOfMultiBlock

      private static boolean isPartOfMultiBlock(DetailAST currentBlock, DetailAST nextBlock)
      Checks whether the current block is part of a multi-block statement (if/else, try/catch/finally, or do/while).
      Parameters:
      currentBlock - the current block
      nextBlock - the block following ast
      Returns:
      true if ast and nextBlock belong to the same multi-block statement
    • hasContentOnLeftSide

      private static boolean hasContentOnLeftSide(DetailAST brace)
      Checks if the block is not concise and has content on left side of right brace.
      Parameters:
      brace - the right curly brace token
      Returns:
      true if the brace has content on left.
    • hasContentOnRightSide

      private static boolean hasContentOnRightSide(DetailAST brace, DetailAST nextToken)
      Checks if the right curly brace is part multi-block statement or no content on right side of right brace.
      Parameters:
      brace - the right curly brace token
      nextToken - the next token of right curly.
      Returns:
      true if the brace is on the same line as the previous sibling or parent if no sibling exists
    • isEmpty

      private static boolean isEmpty(DetailAST brace)
      Checks if block is empty.
      Parameters:
      brace - the right curly brace.
      Returns:
      true if the block is empty.
    • isNotConcise

      private static boolean isNotConcise(DetailAST brace)
      Checks if the block is not K&R style or concise {}.
      Parameters:
      brace - right curly token
      Returns:
      true if block is not concise.
    • getNextToken

      @Nullable private static DetailAST getNextToken(DetailAST node)
      Traverses up the AST to find the next sibling token after the right curly brace.
      Parameters:
      node - ast token
      Returns:
      the next sibling token, or null if none exists