Class MissingNullCaseInSwitchCheck
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
-
- com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
-
- com.puppycrawl.tools.checkstyle.api.AbstractCheck
-
- com.puppycrawl.tools.checkstyle.checks.coding.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 anull
case label.Rationale: switch statements and expressions in Java throw a
NullPointerException
if the selector expression evaluates tonull
. As of Java 21, it is now possible to integrate a null check within the switch, eliminating the risk ofNullPointerException
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
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions
-
-
Constructor Summary
Constructors Constructor Description MissingNullCaseInSwitchCheck()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description int[]
getAcceptableTokens()
The configurable token set.private static List<DetailAST>
getAllCaseLabels(DetailAST switchAST)
Gets all case labels in the given switch AST node.int[]
getDefaultTokens()
Returns the default token a check is interested in.int[]
getRequiredTokens()
The tokens that this check must be registered for.private static boolean
hasLiteralNull(DetailAST caseAST)
Checks if the given case AST node has a null label.private static boolean
hasPatternCaseLabel(DetailAST caseAST)
Checks if the given case AST node has a pattern variable declaration label or record pattern definition label.private static boolean
hasStringCaseLabel(DetailAST caseAST)
Checks if the given case contains a string in its label.void
visitToken(DetailAST ast)
Called to process a token.-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
beginTree, clearViolations, destroy, finishTree, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokens
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
-
Methods inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
configure, contextualize, getConfiguration, setupChild
-
-
-
-
Field Detail
-
MSG_KEY
public static final String MSG_KEY
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
MissingNullCaseInSwitchCheck
public MissingNullCaseInSwitchCheck()
-
-
Method Detail
-
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 classAbstractCheck
- Returns:
- the default tokens
- See Also:
TokenTypes
-
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 classAbstractCheck
- Returns:
- the token set this check is designed for.
- See Also:
TokenTypes
-
getRequiredTokens
public int[] getRequiredTokens()
Description copied from class:AbstractCheck
The tokens that this check must be registered for.- Specified by:
getRequiredTokens
in classAbstractCheck
- Returns:
- the token set this must be registered for.
- See Also:
TokenTypes
-
visitToken
public void visitToken(DetailAST ast)
Description copied from class:AbstractCheck
Called to process a token.- Overrides:
visitToken
in classAbstractCheck
- 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 representingLITERAL_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 representingLITERAL_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 representingLITERAL_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 representingLITERAL_CASE
- Returns:
- true if switch block contains a string case label
-
-