Class FinalLocalVariableCheck
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.FinalLocalVariableCheck
- All Implemented Interfaces:
- Configurable,- Contextualizable
 Checks that local variables that never have their values changed are declared final.
 The check can be configured to also check that unchanged parameters are declared final.
 
 Notes: When configured to check parameters, the check ignores parameters of interface methods and abstract methods.
- Since:
- 3.2
- 
Nested Class SummaryNested ClassesModifier and TypeClassDescriptionprivate static final classRepresents information about final local variable candidate.private static final classHolder for the scope data.Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBeanAbstractAutomaticBean.OutputStreamOptions
- 
Field SummaryFieldsModifier and TypeFieldDescriptionprivate static final BitSetAssign operator types.Assigned variables of current scope.private static final BitSetLoop types.static final StringA key is pointing to the warning message text in "messages.properties" file.private final Deque<FinalLocalVariableCheck.ScopeData>Scope Deque.private booleanControl whether to check enhanced for-loop variable.private booleanControl whether to check unnamed variables.
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionprivate static voiddetermineAssignmentConditions(DetailAST ident, FinalLocalVariableCheck.FinalVariableCandidate candidate) Determines identifier assignment conditions (assigned or already assigned).private static DetailASTFind the Class, Constructor, Enum, Method, or Field in which it is defined.private static DetailASTfindLastCaseGroupWhichContainsSlist(DetailAST literalSwitchAst) Returns the last token of typeTokenTypes.CASE_GROUPwhich containsTokenTypes.SLIST.int[]The configurable token set.int[]Returns the default token a check is interested in.Gets final variable candidate for ast.private static DetailASTgetParentLoop(DetailAST ast) Get the ast node of typeLOOP_TYPESthat is the ancestor of the current ast node, if there is no such node, null is returned.int[]The tokens that this check must be registered for.private voidinsertParameter(DetailAST ast) Insert a parameter at the topmost scope stack.private voidinsertVariable(DetailAST ast) Insert a variable at the topmost scope stack.private static booleanisAssignOperator(int parentType) Is Arithmetic operator.private static booleanIf token is CASE_GROUP or SWITCH_RULE and there is anothercasefollowing.private static booleanisFirstChild(DetailAST ast) Whether the ast is the first child of its parent.private static booleanisInitialized(DetailAST ast) Check if VARIABLE_DEF is initialized or not.private static booleanisInLambda(DetailAST paramDef) Check if current param is lambda's param.private static booleanisInMethodWithoutBody(DetailAST parameterDefAst) Checks if a parameter is within a method that has no implementation body.private static booleanisInSpecificCodeBlocks(DetailAST node, int... blockTypes) Checks whether the scope of a node is restricted to a specific code blocks.private static booleanisLoopAst(int ast) Checks whether the ast is a loop.private static booleanisMultipleTypeCatch(DetailAST parameterDefAst) Check if given parameter definition is a multiple type catch.private static booleanisSameVariables(DetailAST ast1, DetailAST ast2) Check if both the Variables are same.private static booleanisVariableInForInit(DetailAST variableDef) Checks if current variable is defined infor-loop init, e.g.:voidleaveToken(DetailAST ast) Called after all the child nodes have been process.private voidRemoves the final variable candidate from the Stack.final voidsetValidateEnhancedForLoopVariable(boolean validateEnhancedForLoopVariable) Setter to control whether to check enhanced for-loop variable.final voidsetValidateUnnamedVariables(boolean validateUnnamedVariables) Setter to control whether to check unnamed variables.private booleanDetermines whether enhanced for-loop variable should be checked or not.private booleanDetermines whether unnamed variable should be checked or not.private static booleanWhether the final variable candidate should be removed from the list of final local variable candidates.private static booleanIf there is anelsefollowing or token is CASE_GROUP or SWITCH_RULE and there is anothercasefollowing, then update the uninitialized variables.private voidStore un-initialized variables in a temporary stack for future use.private voidUpdate current scope data uninitialized variable according to the whole scope data.private voidUpdate assigned variables in a temporary stack.private voidupdateUninitializedVariables(Deque<DetailAST> scopeUninitializedVariableData) Update current scope data uninitialized variable according to the specific scope data.voidvisitToken(DetailAST ast) Called to process a token.Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheckbeginTree, clearViolations, destroy, finishTree, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, log, log, log, setFileContents, setTabWidth, setTokensMethods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporterfinishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityMethods inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBeanconfigure, contextualize, getConfiguration, setupChild
- 
Field Details- 
MSG_KEYA key is pointing to the warning message text in "messages.properties" file.- See Also:
 
- 
ASSIGN_OPERATOR_TYPESAssign operator types.
- 
LOOP_TYPESLoop types.
- 
scopeStackScope Deque.
- 
currentScopeAssignedVariablesAssigned variables of current scope.
- 
validateEnhancedForLoopVariableControl whether to check enhanced for-loop variable.
- 
validateUnnamedVariablesControl whether to check unnamed variables.
 
- 
- 
Constructor Details- 
FinalLocalVariableCheckpublic FinalLocalVariableCheck()
 
- 
- 
Method Details- 
setValidateEnhancedForLoopVariableSetter to control whether to check enhanced for-loop variable.- Parameters:
- validateEnhancedForLoopVariable- whether to check for-loop variable
- Since:
- 6.5
 
- 
setValidateUnnamedVariablesSetter to control whether to check unnamed variables.- Parameters:
- validateUnnamedVariables- whether to check unnamed variables
- Since:
- 10.18.0
 
- 
getRequiredTokensDescription copied from class:AbstractCheckThe tokens that this check must be registered for.- Specified by:
- getRequiredTokensin class- AbstractCheck
- Returns:
- the token set this must be registered for.
- See Also:
 
- 
getDefaultTokensDescription 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 class- AbstractCheck
- Returns:
- the default tokens
- See Also:
 
- 
getAcceptableTokensDescription 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 class- AbstractCheck
- Returns:
- the token set this check is designed for.
- See Also:
 
- 
visitTokenDescription copied from class:AbstractCheckCalled to process a token.- Overrides:
- visitTokenin class- AbstractCheck
- Parameters:
- ast- the token to process
 
- 
leaveTokenDescription copied from class:AbstractCheckCalled after all the child nodes have been process.- Overrides:
- leaveTokenin class- AbstractCheck
- Parameters:
- ast- the token leaving
 
- 
updateCurrentScopeAssignedVariablesUpdate assigned variables in a temporary stack.
- 
determineAssignmentConditionsprivate static void determineAssignmentConditions(DetailAST ident, FinalLocalVariableCheck.FinalVariableCandidate candidate) Determines identifier assignment conditions (assigned or already assigned).- Parameters:
- ident- identifier.
- candidate- final local variable candidate.
 
- 
isInSpecificCodeBlocksChecks whether the scope of a node is restricted to a specific code blocks.- Parameters:
- node- node.
- blockTypes- int array of all block types to check.
- Returns:
- true if the scope of a node is restricted to specific code block types.
 
- 
getFinalCandidateGets final variable candidate for ast.- Parameters:
- ast- ast.
- Returns:
- Optional of FinalLocalVariableCheck.FinalVariableCandidatefor ast from scopeStack.
 
- 
storePrevScopeUninitializedVariableDataStore un-initialized variables in a temporary stack for future use.
- 
updateAllUninitializedVariablesUpdate current scope data uninitialized variable according to the whole scope data.
- 
updateUninitializedVariablesUpdate current scope data uninitialized variable according to the specific scope data.- Parameters:
- scopeUninitializedVariableData- variable for specific stack of uninitialized variables
 
- 
shouldUpdateUninitializedVariablesIf there is anelsefollowing or token is CASE_GROUP or SWITCH_RULE and there is anothercasefollowing, then update the uninitialized variables.- Parameters:
- ast- token to be checked
- Returns:
- true if should be updated, else false
 
- 
isCaseTokenWithAnotherCaseFollowingIf token is CASE_GROUP or SWITCH_RULE and there is anothercasefollowing.- Parameters:
- ast- token to be checked
- Returns:
- true if token is CASE_GROUP or SWITCH_RULE and there is another casefollowing, else false
 
- 
findLastCaseGroupWhichContainsSlistReturns the last token of typeTokenTypes.CASE_GROUPwhich containsTokenTypes.SLIST.- Parameters:
- literalSwitchAst- ast node of type- TokenTypes.LITERAL_SWITCH
- Returns:
- the matching token, or null if no match
 
- 
shouldCheckEnhancedForLoopVariableDetermines whether enhanced for-loop variable should be checked or not.- Parameters:
- ast- The ast to compare.
- Returns:
- true if enhanced for-loop variable should be checked.
 
- 
shouldCheckUnnamedVariableDetermines whether unnamed variable should be checked or not.- Parameters:
- ast- The ast to compare.
- Returns:
- true if unnamed variable should be checked.
 
- 
insertParameterInsert a parameter at the topmost scope stack.- Parameters:
- ast- the variable to insert.
 
- 
insertVariableInsert a variable at the topmost scope stack.- Parameters:
- ast- the variable to insert.
 
- 
isInitializedCheck if VARIABLE_DEF is initialized or not.- Parameters:
- ast- VARIABLE_DEF to be checked
- Returns:
- true if initialized
 
- 
isFirstChildWhether the ast is the first child of its parent.- Parameters:
- ast- the ast to check.
- Returns:
- true if the ast is the first child of its parent.
 
- 
removeFinalVariableCandidateFromStackRemoves the final variable candidate from the Stack.- Parameters:
- ast- variable to remove.
 
- 
isMultipleTypeCatchCheck if given parameter definition is a multiple type catch.- Parameters:
- parameterDefAst- parameter definition
- Returns:
- true if it is a multiple type catch, false otherwise
 
- 
shouldRemoveFinalVariableCandidateprivate static boolean shouldRemoveFinalVariableCandidate(FinalLocalVariableCheck.ScopeData scopeData, DetailAST ast) Whether the final variable candidate should be removed from the list of final local variable candidates.- Parameters:
- scopeData- the scope data of the variable.
- ast- the variable ast.
- Returns:
- true, if the variable should be removed.
 
- 
getParentLoopGet the ast node of typeLOOP_TYPESthat is the ancestor of the current ast node, if there is no such node, null is returned.- Parameters:
- ast- ast node
- Returns:
- ast node of type LOOP_TYPESthat is the ancestor of the current ast node, null if no such node exists
 
- 
isAssignOperatorIs Arithmetic operator.- Parameters:
- parentType- token AST
- Returns:
- true is token type is in arithmetic operator
 
- 
isVariableInForInitChecks if current variable is defined infor-loop init, e.g.:for (int i = 0, j = 0; i < j; i++) { . . . }i, jare defined infor-loop init- Parameters:
- variableDef- variable definition node.
- Returns:
- true if variable is defined in for-loop init
 
- 
isInMethodWithoutBodyChecks if a parameter is within a method that has no implementation body.- Parameters:
- parameterDefAst- the AST node representing the parameter definition
- Returns:
- true if the parameter is in a method without a body
 
- 
isInLambdaCheck if current param is lambda's param.- Parameters:
- paramDef-- parameter def.
- Returns:
- true if current param is lambda's param.
 
- 
findFirstUpperNamedBlockFind the Class, Constructor, Enum, Method, or Field in which it is defined.- Parameters:
- ast- Variable for which we want to find the scope in which it is defined
- Returns:
- ast The Class or Constructor or Method in which it is defined.
 
- 
isSameVariablesCheck if both the Variables are same.- Parameters:
- ast1- Variable to compare
- ast2- Variable to compare
- Returns:
- true if both the variables are same, otherwise false
 
- 
isLoopAstChecks whether the ast is a loop.- Parameters:
- ast- the ast to check.
- Returns:
- true if the ast is a loop.
 
 
-