Class PatternVariableAssignmentCheck
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.PatternVariableAssignmentCheck
- All Implemented Interfaces:
Configurable
,Contextualizable
Checks for assignment of pattern variables.
Pattern variable assignment is considered bad programming practice. The pattern variable is meant to be a direct reference to the object being matched. Reassigning it can break this connection and mislead readers.
Parent is com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
-
pattern.variable.assignment
- Since:
- 10.26.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 String
A key is pointing to the warning message in "messages.properties" file. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionint[]
The configurable token set.private static DetailAST
Gets the closest consistent AST branch that leads to reassigned variable's ident.int[]
Returns the default token a check is interested in.private static DetailAST
getMatchedAssignToken
(DetailAST preAssignBranch) Gets the type of ASSIGN tokens that particularly matches with what follows the preceding branch.private static DetailAST
getNeededAssignIdent
(DetailAST assignToken) Gets the needed AST Ident of reassigned variable for check to compare.Gets the list of all pattern variable idents in instanceof expression.Gets the array list made out of AST branches of reassigned variable idents.int[]
The tokens that this check must be registered for.private static DetailAST
shiftToNextTraversedBranch
(DetailAST ast, DetailAST boundAst) Shifts once to the next possible branch within traverse trajectory.private static DetailAST
traverseUntilNeededBranchType
(DetailAST startingBranch, DetailAST bound, int neededTokenType) Traverses along the AST tree to locate the first branch of certain token type.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 in "messages.properties" file.- See Also:
-
ASSIGN_TOKEN_TYPES
The set of all valid types of ASSIGN token for this check.
-
-
Constructor Details
-
PatternVariableAssignmentCheck
public PatternVariableAssignmentCheck()
-
-
Method Details
-
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:
-
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:
-
visitToken
Description copied from class:AbstractCheck
Called to process a token.- Overrides:
visitToken
in 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.
-
getReassignedVariableIdents
Gets the array list made out of AST branches of reassigned variable idents.- Parameters:
ast
- ast tree of checked instanceof statement.- Returns:
- the list of AST branches of reassigned variable idents.
-
getBranchLeadingToReassignedVars
Gets the closest consistent AST branch that leads to reassigned variable's ident.- Parameters:
ast
- ast tree of checked instanceof statement.- Returns:
- the closest consistent AST branch that leads to reassigned variable's ident.
-
traverseUntilNeededBranchType
@Nullable private static DetailAST traverseUntilNeededBranchType(DetailAST startingBranch, DetailAST bound, int neededTokenType) Traverses along the AST tree to locate the first branch of certain token type.- Parameters:
startingBranch
- AST branch to start the traverse from, but not check.bound
- AST Branch that the traverse cannot further extend to.neededTokenType
- Token type whose first encountered branch is to look for.- Returns:
- the AST tree of first encountered branch of needed token type.
-
shiftToNextTraversedBranch
Shifts once to the next possible branch within traverse trajectory.- Parameters:
ast
- AST branch to shift from.boundAst
- AST Branch that the traverse cannot further extend to.- Returns:
- the AST tree of next possible branch within traverse trajectory.
-
getMatchedAssignToken
Gets the type of ASSIGN tokens that particularly matches with what follows the preceding branch.- Parameters:
preAssignBranch
- branch that precedes the branch of ASSIGN token types.- Returns:
- type of ASSIGN token.
-
getNeededAssignIdent
Gets the needed AST Ident of reassigned variable for check to compare.- Parameters:
assignToken
- The AST branch of reassigned variable's ASSIGN token.- Returns:
- needed AST Ident.
-