Class MissingNullCaseInSwitchCheck
- All Implemented Interfaces:
Configurable
,Contextualizable
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
-
Nested Class Summary
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionint[]
The configurable token set.getAllCaseLabels
(DetailAST switchAST) Gets all case labels in the given switch AST node.int[]
Returns the default token a check is interested in.int[]
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 Details
-
MSG_KEY
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
-
Constructor Details
-
MissingNullCaseInSwitchCheck
public MissingNullCaseInSwitchCheck()
-
-
Method Details
-
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:
-
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:
-
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:
-
visitToken
Description copied from class:AbstractCheck
Called to process a token.- Overrides:
visitToken
in classAbstractCheck
- Parameters:
ast
- the token to process
-
getAllCaseLabels
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
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
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
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
-