Class MissingNullCaseInSwitchCheck

  • All Implemented Interfaces:
    Configurable, Contextualizable

    public class MissingNullCaseInSwitchCheck
    extends AbstractCheck
    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
    • Method Detail

      • 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:
        TokenTypes
      • getAllCaseLabels

        private static List<DetailASTgetAllCaseLabels​(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