Class WhitespaceBeforeEmptyBodyCheck

All Implemented Interfaces:
Configurable, Contextualizable

Checks that a whitespace is present before the opening brace of an empty body.

This check applies to the following tokens: Methods and constructors, Classes, interfaces, enums, and records, Annotation definitions, Loops: while, for, do-while, Lambdas and anonymous classes, Static initializer blocks, Try-catch-finally statements, Synchronized blocks, Switch statements.

A body containing only comments is considered empty by this check.

Since:
13.9.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
    • findOpeningBrace

      @Nullable private static DetailAST findOpeningBrace(DetailAST ast)
      Returns left curly token for an ast.
      Parameters:
      ast - the AST to get brace token for
      Returns:
      the left curly token, or null if the construct has no-body
    • precededByWhiteSpace

      private static boolean precededByWhiteSpace(DetailAST lcurly)
      Checks if the opening brace is preceded by whitespace.
      Parameters:
      lcurly - token representing the opening brace
      Returns:
      true if preceded by whitespace
    • descendToLeafNode

      private static DetailAST descendToLeafNode(DetailAST token)
      Return the last nested child of the token.
      Parameters:
      token - the ast token
      Returns:
      nested child
    • isBodyEmpty

      private static boolean isBodyEmpty(DetailAST lcurly)
      Checks if the body is empty.
      Parameters:
      lcurly - the token representing the opening brace
      Returns:
      true if the body is empty
    • isSpaceBetweenTokens

      private static boolean isSpaceBetweenTokens(DetailAST firstToken, DetailAST secondToken)
      Checks if space exists between first and second token.
      Parameters:
      firstToken - token in ast which is behind second token
      secondToken - token next to firstToken
      Returns:
      true if space exists between first and second tokens