public class FinalLocalVariableCheck extends AbstractCheck
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.
When configured to check parameters, the check ignores parameters of interface methods and abstract methods.
validateEnhancedForLoopVariable - Control whether to check
enhanced for-loop variable.
Type is boolean.
Default value is false.
tokens - tokens to check
Type is java.lang.String[].
Validation type is tokenSet.
Default value is:
VARIABLE_DEF.
To configure the check:
<module name="FinalLocalVariable"/>
To configure the check so that it checks local variables and parameters:
<module name="FinalLocalVariable"> <property name="tokens" value="VARIABLE_DEF,PARAMETER_DEF"/> </module>
By default, this Check skip final validation on Enhanced For-Loop.
Option 'validateEnhancedForLoopVariable' could be used to make Check to validate even variable from Enhanced For Loop.
An example of how to configure the check so that it also validates enhanced For Loop Variable is:
<module name="FinalLocalVariable"> <property name="tokens" value="VARIABLE_DEF"/> <property name="validateEnhancedForLoopVariable" value="true"/> </module>
Example:
for (int number : myNumbers) { // violation
System.out.println(number);
}
An example of how to configure check on local variables and parameters but do not validate loop variables:
<module name="FinalLocalVariable">
<property name="tokens" value="VARIABLE_DEF,PARAMETER_DEF"/>
<property name="validateEnhancedForLoopVariable" value="false"/>
</module>
Example:
public class MyClass {
static int foo(int x, int y) { //violations, parameters should be final
return x+y;
}
public static void main (String []args) { //violation, parameters should be final
for (String i : args) {
System.out.println(i);
}
int result=foo(1,2); // violation
}
}
Parent is com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
final.variable
| Modifier and Type | Class and Description |
|---|---|
private static class |
FinalLocalVariableCheck.FinalVariableCandidate
Represents information about final local variable candidate.
|
private static class |
FinalLocalVariableCheck.ScopeData
Holder for the scope data.
|
AutomaticBean.OutputStreamOptions| Modifier and Type | Field and Description |
|---|---|
private static int[] |
ASSIGN_OPERATOR_TYPES
Assign operator types.
|
private java.util.Deque<java.util.Deque<DetailAST>> |
currentScopeAssignedVariables
Assigned variables of current scope.
|
private static int[] |
LOOP_TYPES
Loop types.
|
static java.lang.String |
MSG_KEY
A key is pointing to the warning message text in "messages.properties"
file.
|
private java.util.Deque<java.util.Deque<DetailAST>> |
prevScopeUninitializedVariables
Uninitialized variables of previous scope.
|
private java.util.Deque<FinalLocalVariableCheck.ScopeData> |
scopeStack
Scope Deque.
|
private boolean |
validateEnhancedForLoopVariable
Control whether to check
enhanced for-loop variable.
|
| Constructor and Description |
|---|
FinalLocalVariableCheck() |
| Modifier and Type | Method and Description |
|---|---|
private static void |
determineAssignmentConditions(DetailAST ident,
FinalLocalVariableCheck.FinalVariableCandidate candidate)
Determines identifier assignment conditions (assigned or already assigned).
|
private static DetailAST |
findFirstUpperNamedBlock(DetailAST ast)
Find the Class, Constructor, Enum, Method, or Field in which it is defined.
|
private static DetailAST |
findLastChildWhichContainsSpecifiedToken(DetailAST ast,
int childType,
int containType)
Returns the last child token that makes a specified type and contains containType in
its branch.
|
int[] |
getAcceptableTokens()
The configurable token set.
|
int[] |
getDefaultTokens()
Returns the default token a check is interested in.
|
private java.util.Optional<FinalLocalVariableCheck.FinalVariableCandidate> |
getFinalCandidate(DetailAST ast)
Gets final variable candidate for ast.
|
int[] |
getRequiredTokens()
The tokens that this check must be registered for.
|
private void |
insertParameter(DetailAST ast)
Insert a parameter at the topmost scope stack.
|
private void |
insertVariable(DetailAST ast)
Insert a variable at the topmost scope stack.
|
private static boolean |
isAssignOperator(int parentType)
Is Arithmetic operator.
|
private static boolean |
isCaseTokenWithAnotherCaseFollowing(DetailAST ast)
If token is CASE_GROUP or SWITCH_RULE and there is another
case following. |
private static boolean |
isFirstChild(DetailAST ast)
Whether the ast is the first child of its parent.
|
private static boolean |
isIfTokenWithAnElseFollowing(DetailAST ast)
If token is LITERAL_IF and there is an
else following. |
private static boolean |
isInAbstractOrNativeMethod(DetailAST ast)
Determines whether an AST is a descendant of an abstract or native method.
|
private static boolean |
isInitialized(DetailAST ast)
Check if VARIABLE_DEF is initialized or not.
|
private static boolean |
isInLambda(DetailAST paramDef)
Check if current param is lambda's param.
|
private static boolean |
isInSpecificCodeBlocks(DetailAST node,
int... blockTypes)
Checks whether the scope of a node is restricted to a specific code blocks.
|
private static boolean |
isInTheSameLoop(DetailAST ast1,
DetailAST ast2)
Check if both the variables are in the same loop.
|
private static boolean |
isLoopAst(int ast)
Checks whether the ast is a loop.
|
private static boolean |
isMultipleTypeCatch(DetailAST parameterDefAst)
Check if given parameter definition is a multiple type catch.
|
private static boolean |
isSameVariables(DetailAST ast1,
DetailAST ast2)
Check if both the Variables are same.
|
private static boolean |
isUseOfExternalVariableInsideLoop(DetailAST variable)
Checks whether a variable which is declared outside loop is used inside loop.
|
private static boolean |
isVariableInForInit(DetailAST variableDef)
Checks if current variable is defined in
for-loop init, e.g.: |
void |
leaveToken(DetailAST ast)
Called after all the child nodes have been process.
|
private void |
removeFinalVariableCandidateFromStack(DetailAST ast)
Removes the final variable candidate from the Stack.
|
void |
setValidateEnhancedForLoopVariable(boolean validateEnhancedForLoopVariable)
Setter to control whether to check
enhanced for-loop variable.
|
private boolean |
shouldCheckEnhancedForLoopVariable(DetailAST ast)
Determines whether enhanced for-loop variable should be checked or not.
|
private static boolean |
shouldRemoveFinalVariableCandidate(FinalLocalVariableCheck.ScopeData scopeData,
DetailAST ast)
Whether the final variable candidate should be removed from the list of final local variable
candidates.
|
private static boolean |
shouldUpdateUninitializedVariables(DetailAST ast)
If token is LITERAL_IF and there is an
else following or token is CASE_GROUP or
SWITCH_RULE and there is another case following, then update the
uninitialized variables. |
private void |
storePrevScopeUninitializedVariableData()
Store un-initialized variables in a temporary stack for future use.
|
private void |
updateAllUninitializedVariables(java.util.Deque<DetailAST> prevScopeUninitializedVariableData)
Update current scope data uninitialized variable according to the whole scope data.
|
private void |
updateCurrentScopeAssignedVariables()
Update assigned variables in a temporary stack.
|
private void |
updateUninitializedVariables(java.util.Deque<DetailAST> scopeUninitializedVariableData)
Update current scope data uninitialized variable according to the specific scope data.
|
void |
visitToken(DetailAST ast)
Called to process a token.
|
beginTree, clearViolations, destroy, finishTree, getFileContents, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, log, log, log, setFileContents, setTabWidth, setTokensfinishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityconfigure, contextualize, getConfiguration, setupChildpublic static final java.lang.String MSG_KEY
private static final int[] ASSIGN_OPERATOR_TYPES
private static final int[] LOOP_TYPES
private final java.util.Deque<FinalLocalVariableCheck.ScopeData> scopeStack
private final java.util.Deque<java.util.Deque<DetailAST>> prevScopeUninitializedVariables
private final java.util.Deque<java.util.Deque<DetailAST>> currentScopeAssignedVariables
private boolean validateEnhancedForLoopVariable
public FinalLocalVariableCheck()
public final void setValidateEnhancedForLoopVariable(boolean validateEnhancedForLoopVariable)
validateEnhancedForLoopVariable - whether to check for-loop variablepublic int[] getRequiredTokens()
AbstractCheckgetRequiredTokens in class AbstractCheckTokenTypespublic int[] getDefaultTokens()
AbstractCheckgetDefaultTokens in class AbstractCheckTokenTypespublic int[] getAcceptableTokens()
AbstractCheckgetAcceptableTokens in class AbstractCheckTokenTypespublic void visitToken(DetailAST ast)
AbstractCheckvisitToken in class AbstractCheckast - the token to processpublic void leaveToken(DetailAST ast)
AbstractCheckleaveToken in class AbstractCheckast - the token leavingprivate void updateCurrentScopeAssignedVariables()
private static void determineAssignmentConditions(DetailAST ident, FinalLocalVariableCheck.FinalVariableCandidate candidate)
ident - identifier.candidate - final local variable candidate.private static boolean isInSpecificCodeBlocks(DetailAST node, int... blockTypes)
node - node.blockTypes - int array of all block types to check.private java.util.Optional<FinalLocalVariableCheck.FinalVariableCandidate> getFinalCandidate(DetailAST ast)
ast - ast.FinalLocalVariableCheck.FinalVariableCandidate for ast from scopeStack.private void storePrevScopeUninitializedVariableData()
private void updateAllUninitializedVariables(java.util.Deque<DetailAST> prevScopeUninitializedVariableData)
prevScopeUninitializedVariableData - variable for previous stack of uninitialized
variablesprivate void updateUninitializedVariables(java.util.Deque<DetailAST> scopeUninitializedVariableData)
scopeUninitializedVariableData - variable for specific stack of uninitialized variablesprivate static boolean shouldUpdateUninitializedVariables(DetailAST ast)
else following or token is CASE_GROUP or
SWITCH_RULE and there is another case following, then update the
uninitialized variables.ast - token to be checkedprivate static boolean isIfTokenWithAnElseFollowing(DetailAST ast)
else following.ast - token to be checkedelse following, else falseprivate static boolean isCaseTokenWithAnotherCaseFollowing(DetailAST ast)
case following.ast - token to be checkedcase
following, else falseprivate static DetailAST findLastChildWhichContainsSpecifiedToken(DetailAST ast, int childType, int containType)
ast - token to be testedchildType - the token type to matchcontainType - the token type which has to be present in the branchprivate boolean shouldCheckEnhancedForLoopVariable(DetailAST ast)
ast - The ast to compare.private void insertParameter(DetailAST ast)
ast - the variable to insert.private void insertVariable(DetailAST ast)
ast - the variable to insert.private static boolean isInitialized(DetailAST ast)
ast - VARIABLE_DEF to be checkedprivate static boolean isFirstChild(DetailAST ast)
ast - the ast to check.private void removeFinalVariableCandidateFromStack(DetailAST ast)
ast - variable to remove.private static boolean isMultipleTypeCatch(DetailAST parameterDefAst)
parameterDefAst - parameter definitionprivate static boolean shouldRemoveFinalVariableCandidate(FinalLocalVariableCheck.ScopeData scopeData, DetailAST ast)
scopeData - the scope data of the variable.ast - the variable ast.private static boolean isUseOfExternalVariableInsideLoop(DetailAST variable)
int x;
for (int i = 0, j = 0; i < j; i++) {
x = 5;
}
variable - variable.private static boolean isAssignOperator(int parentType)
parentType - token ASTprivate static boolean isVariableInForInit(DetailAST variableDef)
for-loop init, e.g.:
for (int i = 0, j = 0; i < j; i++) { . . . }
i, j are defined in for-loop initvariableDef - variable definition node.for-loop initprivate static boolean isInAbstractOrNativeMethod(DetailAST ast)
ast - the AST to check.private static boolean isInLambda(DetailAST paramDef)
paramDef - parameter def.private static DetailAST findFirstUpperNamedBlock(DetailAST ast)
ast - Variable for which we want to find the scope in which it is definedprivate static boolean isSameVariables(DetailAST ast1, DetailAST ast2)
ast1 - Variable to compareast2 - Variable to compareprivate static boolean isInTheSameLoop(DetailAST ast1, DetailAST ast2)
ast1 - variable to compare.ast2 - variable to compare.private static boolean isLoopAst(int ast)
ast - the ast to check.Copyright © 2001-2022. All Rights Reserved.