Class ArrayBracketNoWhitespaceCheck

All Implemented Interfaces:
Configurable, Contextualizable

Checks that the whitespace around square-bracket tokens [ and ] follows the Google Java Style Guide requirements for array declarations, array creation, and array indexing.

Left square bracket ("["):

  • must not be preceded with whitespace when preceded by a TYPE or IDENT in array declarations or array access
  • must not be followed with whitespace

Right square bracket ("]"):

  • must not be preceded with whitespace
  • must be followed with whitespace in all cases, except when followed by:
    • another bracket: [][]
    • a dot for member access: arr[i].length
    • a comma or semicolon: arr[i], or arr[i];
    • postfix operators: arr[i]++ or arr[i]--
    • a right parenthesis or another closing construct: (arr[i])
Since:
13.6.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
    • processRightBracket

      private void processRightBracket(DetailAST ast)
      Processes a right bracket token and logs violations if it is preceded or followed by whitespace inappropriately.
      Parameters:
      ast - the right bracket token to process
    • isPrecededByAnnotation

      private static boolean isPrecededByAnnotation(DetailAST ast)
      Checks whether an ARRAY_DECLARATOR is immediately preceded by an ANNOTATIONS sibling, which happens in constructs like int @Ann [] x.
      Parameters:
      ast - the ARRAY_DECLARATOR or INDEX_OP token
      Returns:
      true if the token's previous sibling is an ANNOTATIONS node
    • isWhitespaceAt

      private boolean isWhitespaceAt(DetailAST token, int columnNo)
      Checks if a whitespace character is present at the given column on the same line as the provided token.
      Parameters:
      token - the token whose line should be checked
      columnNo - the column number to inspect for whitespace
      Returns:
      true if the character at columnNo is a whitespace character
    • findNextToken

      @Nullable private static DetailAST findNextToken(DetailAST rightBracket)
      Finds the next token after a right bracket by climbing the AST and scanning next-sibling chains at each level. At every level all siblings are visited: siblings on a later line are skipped and set an outOfLine flag; siblings on the same line are passed to findBestCandidate(com.puppycrawl.tools.checkstyle.api.DetailAST, com.puppycrawl.tools.checkstyle.api.DetailAST, com.puppycrawl.tools.checkstyle.api.DetailAST). Once a later-line sibling is found at any level the climb stops immediately, since no ancestor sibling can be on the target line either. The candidate with the smallest qualifying column is returned.
      Parameters:
      rightBracket - the right bracket token whose successor is needed
      Returns:
      the closest same-line token that follows the bracket, or null if no such token exists on that line
    • findBestCandidate

      @Nullable private static DetailAST findBestCandidate(@Nullable DetailAST candidate, DetailAST rightBracket, DetailAST current)
      Evaluates whether current is a better next-token candidate than the existing candidate relative to rightBracket. A token qualifies as a better candidate when it sits on the same line as the right bracket, has a greater column number than the bracket, and either no candidate exists yet or its column number is closer to the bracket than the current best. When the criteria are met the new token is returned; otherwise the existing candidate is returned unchanged.
      Parameters:
      candidate - the current best candidate
      rightBracket - the right bracket token
      current - the current AST node being evaluated
      Returns:
      the new best candidate
    • isValidWithoutWhitespace

      private static boolean isValidWithoutWhitespace(DetailAST nextToken)
      Checks if the given token can follow a right bracket without whitespace. Uses TokenTypes to determine valid tokens.
      Parameters:
      nextToken - the token that follows the right bracket
      Returns:
      true if the token can follow without whitespace