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.1.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
    • precededByWhitespace

      private boolean precededByWhitespace(DetailAST token)
      Checks if a token is preceded by whitespace.
      Parameters:
      token - the token to check
      Returns:
      true if preceded by whitespace, false otherwise
    • followedByWhitespace

      private boolean followedByWhitespace(DetailAST token)
      Checks if a token is followed by whitespace.
      Parameters:
      token - the token to check
      Returns:
      true if followed by whitespace, false otherwise
    • findNextToken

      private static DetailAST findNextToken(DetailAST arrayToken, DetailAST rightBracket)
      Finds the next token after a right bracket by searching in a limited scope.
      Parameters:
      arrayToken - the array token (ARRAY_DECLARATOR or INDEX_OP)
      rightBracket - the right bracket token for position checking
      Returns:
      the next token that appears after the bracket, or null if none found
    • findClosestTokenAfter

      private static DetailAST findClosestTokenAfter(DetailAST node, int line, int column)
      Recursively scans the AST to find the token closest to and after the given position.
      Parameters:
      node - the current node to scan
      line - the line number of the bracket
      column - the column number of the bracket
      Returns:
      the closest token after the position, or null if none found
    • isValidTokenToCheck

      private static boolean isValidTokenToCheck(DetailAST node)
      Checks if a token is valid to consider as the "next" token. Filters out structural/container tokens that don't represent actual code.
      Parameters:
      node - the token to check
      Returns:
      true if this is a valid token to check
    • isStructuralToken

      private static boolean isStructuralToken(int type)
      Checks if a token type is a structural/container token.
      Parameters:
      type - the token type to check
      Returns:
      true if this is a structural token
    • isExpressionContainer

      private static boolean isExpressionContainer(int type)
      Checks if a token type is an expression container.
      Parameters:
      type - the token type to check
      Returns:
      true if this is an expression container
    • isTypeContainer

      private static boolean isTypeContainer(int type)
      Checks if a token type is a type-related container.
      Parameters:
      type - the token type to check
      Returns:
      true if this is a type container
    • isDeclarationContainer

      private static boolean isDeclarationContainer(int type)
      Checks if a token type is a declaration/definition container.
      Parameters:
      type - the token type to check
      Returns:
      true if this is a declaration container
    • isTokenAfter

      private static boolean isTokenAfter(DetailAST token, int line, int column)
      Checks if a token appears after the given position in the source code.
      Parameters:
      token - the token to check
      line - the line number to compare against
      column - the column number to compare against
      Returns:
      true if the token is on a later line or same line but later column
    • 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
    • isBracketOrAccessor

      private static boolean isBracketOrAccessor(int type)
      Checks if a token type is a bracket or accessor.
      Parameters:
      type - the token type to check
      Returns:
      true if this is a bracket or accessor
    • isPunctuation

      private static boolean isPunctuation(int type)
      Checks if a token type is punctuation that can follow without whitespace.
      Parameters:
      type - the token type to check
      Returns:
      true if this is valid punctuation
    • isPostfixOrShift

      private static boolean isPostfixOrShift(int type)
      Checks if a token type is a postfix operator or shift operator.
      Parameters:
      type - the token type to check
      Returns:
      true if this is a postfix or shift operator