Class MissingNullCaseInSwitchCheck

All Implemented Interfaces:
Configurable, Contextualizable

Checks that a given switch statement or expression that use a reference type in its selector expression has a null case label.

Rationale: switch statements and expressions in Java throw a NullPointerException if the selector expression evaluates to null. As of Java 21, it is now possible to integrate a null check within the switch, eliminating the risk of NullPointerException and simplifies the code as there is no need for an external null check before entering the switch.

See the Java Language Specification for more information about switch statements and expressions.

Specifically, this check validates switch statement or expression that use patterns or strings in their case labels.

Due to Checkstyle not being type-aware, this check cannot validate other reference types, such as enums; syntactically, these are no different from other constants.

Attention: this Check should be activated only on source code that is compiled by jdk21 or above.

Parent is com.puppycrawl.tools.checkstyle.TreeWalker

Violation Message Keys:

  • missing.switch.nullcase
Since:
10.18.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
    • getAllCaseLabels

      private static List<DetailAST> getAllCaseLabels(DetailAST switchAST)
      Gets all case labels in the given switch AST node.
      Parameters:
      switchAST - the AST node representing LITERAL_SWITCH
      Returns:
      a list of all case labels in the switch
    • hasLiteralNull

      private static boolean hasLiteralNull(DetailAST caseAST)
      Checks if the given case AST node has a null label.
      Parameters:
      caseAST - the AST node representing LITERAL_CASE
      Returns:
      true if the case has null label, false otherwise
    • hasPatternCaseLabel

      private static boolean hasPatternCaseLabel(DetailAST caseAST)
      Checks if the given case AST node has a pattern variable declaration label or record pattern definition label.
      Parameters:
      caseAST - the AST node representing LITERAL_CASE
      Returns:
      true if case has a pattern in its label
    • hasStringCaseLabel

      private static boolean hasStringCaseLabel(DetailAST caseAST)
      Checks if the given case contains a string in its label. It may contain a single string literal or a string literal in a concatenated expression.
      Parameters:
      caseAST - the AST node representing LITERAL_CASE
      Returns:
      true if switch block contains a string case label