Class FinalPatternVariableCheck
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.FinalPatternVariableCheck
- All Implemented Interfaces:
Configurable,Contextualizable
Checks that pattern variables that never have their values changed are declared final.
Pattern variables are not implicitly final in Java, and they can be reassigned. This check verifies that pattern variables are explicitly declared as final if they are not reassigned within their flow scope.
- Since:
- 14.4.0
-
Nested Class Summary
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions -
Field Summary
FieldsModifier and TypeFieldDescriptionThe set of all valid types of ASSIGN token for this check.static final StringA key is pointing to the warning message in "messages.properties" file. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a newFinalPatternVariableCheckinstance. -
Method Summary
Modifier and TypeMethodDescriptionprivate static voidaddBodyBranch(Collection<DetailAST> branches, DetailAST scopeRoot) Adds the body branch of a conditional (if/else) to the list.private static voidcollectRecordPatternIdents(DetailAST recordPatternDef, Collection<DetailAST> patternVariableIdentsArray) Collects pattern variable identifiers from a record pattern AST definition.expandReassignmentScopes(DetailAST scopeRoot) Expands the reassignment scope root into concrete AST branches that may contain reassigned pattern variables.private static DetailASTFinds the nearest AST node that defines the scope in which reassignment of a pattern variable may occur.int[]The configurable token set.int[]Returns the default token a check is interested in.private static DetailASTgetMatchedAssignToken(DetailAST preAssignBranch) Gets the type of ASSIGN tokens that matches with what follows the preceding branch.Gets the list of all pattern variable idents in instanceof expression.Gets the list of AST branches of reassigned variable identifiers.int[]The tokens that this check must be registered for.getStatementsInExtendedScope(DetailAST conditionalStatement) Gets statements that follow conditional where pattern variable scope extends.private static DetailASTshiftToNextTraversedBranch(DetailAST ast, DetailAST boundAst) Shifts once to the next possible branch within traverse trajectory.private static booleanshouldBeFinal(DetailAST patternVariableIdent, Iterable<DetailAST> reassignedVariableIdents) Checks whether a pattern variable should be declared final.voidvisitToken(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, setTokensMethods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityMethods inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
configure, contextualize, getConfiguration, setupChild
-
Field Details
-
MSG_KEY
A key is pointing to the warning message in "messages.properties" file.- See Also:
-
ASSIGN_TOKEN_TYPES
The set of all valid types of ASSIGN token for this check.
-
-
Constructor Details
-
FinalPatternVariableCheck
public FinalPatternVariableCheck()Creates a newFinalPatternVariableCheckinstance.
-
-
Method Details
-
getRequiredTokens
Description copied from class:AbstractCheckThe tokens that this check must be registered for.- Specified by:
getRequiredTokensin classAbstractCheck- Returns:
- the token set this must be registered for.
- See Also:
-
getDefaultTokens
Description copied from class:AbstractCheckReturns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.- Specified by:
getDefaultTokensin classAbstractCheck- Returns:
- the default tokens
- See Also:
-
getAcceptableTokens
Description copied from class:AbstractCheckThe 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:
getAcceptableTokensin classAbstractCheck- Returns:
- the token set this check is designed for.
- See Also:
-
visitToken
Description copied from class:AbstractCheckCalled to process a token.- Overrides:
visitTokenin classAbstractCheck- Parameters:
ast- the token to process
-
getPatternVariableIdents
Gets the list of all pattern variable idents in instanceof expression.- Parameters:
ast- ast tree of instanceof to get the list from.- Returns:
- list of pattern variables.
-
collectRecordPatternIdents
private static void collectRecordPatternIdents(DetailAST recordPatternDef, Collection<DetailAST> patternVariableIdentsArray) Collects pattern variable identifiers from a record pattern AST definition.- Parameters:
recordPatternDef- AST of record pattern definitionpatternVariableIdentsArray- collection to add pattern variable identifiers to
-
getReassignedVariableIdents
Gets the list of AST branches of reassigned variable identifiers.- Parameters:
ast- ast tree of checked instanceof statement- Returns:
- list of AST identifiers that represent reassigned variables
-
getStatementsInExtendedScope
Gets statements that follow conditional where pattern variable scope extends. Only returns top-level statements that are siblings of the conditional, excluding statements nested in control structures like while loops.- Parameters:
conditionalStatement- The if statement.- Returns:
- List of statement nodes in the extended scope.
-
shiftToNextTraversedBranch
Shifts once to the next possible branch within traverse trajectory.- Parameters:
ast- AST branch to shift from.boundAst- AST Branch that traverse cannot extend to.- Returns:
- next possible branch within traverse trajectory.
-
getMatchedAssignToken
Gets the type of ASSIGN tokens that matches with what follows the preceding branch.- Parameters:
preAssignBranch- branch preceding ASSIGN token types.- Returns:
- type of ASSIGN token.
-
shouldBeFinal
private static boolean shouldBeFinal(DetailAST patternVariableIdent, Iterable<DetailAST> reassignedVariableIdents) Checks whether a pattern variable should be declared final.- Parameters:
patternVariableIdent- AST ident of the pattern variablereassignedVariableIdents- list of AST idents of reassigned vars- Returns:
- true if pattern variable should be final, false otherwise
-
findReassignmentScopeRoot
Finds the nearest AST node that defines the scope in which reassignment of a pattern variable may occur.The scope is determined by locating the closest enclosing conditional structure such as
if,else, or ternary operator.- Parameters:
ast- the AST node to start searching from- Returns:
- the AST node representing the reassignment scope root,
or
nullif none is found
-
expandReassignmentScopes
Expands the reassignment scope root into concrete AST branches that may contain reassigned pattern variables.For ternary expressions, the conditional expression itself is treated as the reassignment scope. For
if/elsestatements, the method includes the statement list and any subsequent statements where the pattern variable remains in scope.- Parameters:
scopeRoot- the root AST node of the reassignment scope- Returns:
- list of AST branches that may contain reassignments
-
addBodyBranch
Adds the body branch of a conditional (if/else) to the list. For braced blocks, adds the SLIST. For unbraced statements, adds the single statement directly.- Parameters:
branches- collection to add the body branch toscopeRoot- the if/else AST node
-