public class VariableDeclarationUsageDistanceCheck extends AbstractCheck
Checks the distance between declaration of variable and its first usage.
Example #1:
int count;
a = a + b;
b = a + a;
count = b; // DECLARATION OF VARIABLE 'count'
// SHOULD BE HERE (distance = 3)
Example #2:
int count;
{
a = a + b;
count = b; // DECLARATION OF VARIABLE 'count'
// SHOULD BE HERE (distance = 2)
}
Check can detect a block of initialization methods. If a variable is used in such a block and there is no other statements after this variable then distance=1.
Case #1:
int minutes = 5; Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(timeNow); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); cal.set(Calendar.HOUR_OF_DAY, hh); cal.set(Calendar.MINUTE, minutes); The distance for the variable minutes is 1 even though this variable is used in the fifth method's call.
Case #2:
int minutes = 5; Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(timeNow); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); System.out.println(cal); cal.set(Calendar.HOUR_OF_DAY, hh); cal.set(Calendar.MINUTE, minutes); The distance for the variable minutes is 6 because there is one more expression (except the initialization block) between the declaration of this variable and its usage.
There are several additional options to configure the check:
1. allowedDistance - allows to set a distance between declaration of variable and its first usage. 2. ignoreVariablePattern - allows to set a RegEx pattern for ignoring the distance calculation for variables listed in this pattern. 3. validateBetweenScopes - allows to calculate the distance between declaration of variable and its first usage in the different scopes. 4. ignoreFinal - allows to ignore variables with a 'final' modifier.ATTENTION!! (Not supported cases)
Case #1:
{
int c;
int a = 3;
int b = 2;
{
a = a + b;
c = b;
}
}
Distance for variable 'a' = 1;
Distance for variable 'b' = 1;
Distance for variable 'c' = 2.
As distance by default is 1 the Check doesn't raise warning for variables 'a'
and 'b' to move them into the block.
Case #2:
int sum = 0;
for (int i = 0; i < 20; i++) {
a++;
b--;
sum++;
if (sum > 10) {
res = true;
}
}
Distance for variable 'sum' = 3.
As the distance is more then the default one, the Check raises warning for variable 'sum' to move it into the 'for(...)' block. But there is situation when variable 'sum' hasn't to be 0 within each iteration. So, to avoid such warnings you can use Suppression Filter, provided by Checkstyle, for the whole class.
An example how to configure this Check:
<module name="VariableDeclarationUsageDistance"/>
An example of how to configure this Check: - to set the allowed distance to 4; - to ignore variables with prefix '^temp'; - to force the validation between scopes; - to check the final variables;
<module name="VariableDeclarationUsageDistance"> <property name="allowedDistance" value="4"/> <property name="ignoreVariablePattern" value="^temp.*"/> <property name="validateBetweenScopes" value="true"/> <property name="ignoreFinal" value="false"/> </module>
AutomaticBean.OutputStreamOptions
Modifier and Type | Field and Description |
---|---|
static String |
MSG_KEY
Warning message key.
|
static String |
MSG_KEY_EXT
Warning message key.
|
Constructor and Description |
---|
VariableDeclarationUsageDistanceCheck() |
Modifier and Type | Method and Description |
---|---|
int[] |
getAcceptableTokens()
The configurable token set.
|
int[] |
getDefaultTokens()
Returns the default token a check is interested in.
|
int[] |
getRequiredTokens()
The tokens that this check must be registered for.
|
void |
setAllowedDistance(int allowedDistance)
Sets an allowed distance between declaration of variable and its first
usage.
|
void |
setIgnoreFinal(boolean ignoreFinal)
Sets ignore option for variables with 'final' modifier.
|
void |
setIgnoreVariablePattern(Pattern pattern)
Sets RegExp pattern to ignore distance calculation for variables listed in this pattern.
|
void |
setValidateBetweenScopes(boolean validateBetweenScopes)
Sets option which allows to calculate distance between declaration of
variable and its first usage in different scopes.
|
void |
visitToken(DetailAST ast)
Called to process a token.
|
beginTree, clearMessages, destroy, finishTree, getClassLoader, getFileContents, getLine, getLines, getMessages, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, log, setClassLoader, setFileContents, setTabWidth, setTokens
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
configure, contextualize, getConfiguration, setupChild
public static final String MSG_KEY
public static final String MSG_KEY_EXT
public VariableDeclarationUsageDistanceCheck()
public void setAllowedDistance(int allowedDistance)
allowedDistance
- Allowed distance between declaration of variable and its first
usage.public void setIgnoreVariablePattern(Pattern pattern)
pattern
- a pattern.public void setValidateBetweenScopes(boolean validateBetweenScopes)
validateBetweenScopes
- Defines if allow to calculate distance between declaration of
variable and its first usage in different scopes or not.public void setIgnoreFinal(boolean ignoreFinal)
ignoreFinal
- Defines if ignore variables with 'final' modifier or not.public int[] getDefaultTokens()
AbstractCheck
getDefaultTokens
in class AbstractCheck
TokenTypes
public int[] getAcceptableTokens()
AbstractCheck
getAcceptableTokens
in class AbstractCheck
TokenTypes
public int[] getRequiredTokens()
AbstractCheck
getRequiredTokens
in class AbstractCheck
TokenTypes
public void visitToken(DetailAST ast)
AbstractCheck
visitToken
in class AbstractCheck
ast
- the token to processCopyright © 2001–2018. All rights reserved.