public class VariableDeclarationUsageDistanceCheck extends AbstractCheck
Checks the distance between declaration of variable and its first usage. Note : Variable declaration/initialization statements are not counted while calculating length.
allowedDistance - Specify distance between declaration
of variable and its first usage. Values should be greater than 0.
Type is int.
Default value is 3.
ignoreVariablePattern - Define RegExp to ignore distance calculation
for variables listed in this pattern.
Type is java.util.regex.Pattern.
Default value is "".
validateBetweenScopes - Allow to calculate the distance between
declaration of variable and its first usage in the different scopes.
Type is boolean.
Default value is false.
ignoreFinal - Allow to ignore variables with a 'final' modifier.
Type is boolean.
Default value is true.
To configure the check with default config:
<module name="VariableDeclarationUsageDistance"/>
Example:
public class Test {
public void foo1() {
int num; // violation, distance = 4
final int PI; // OK, final variables not checked
System.out.println("Statement 1");
System.out.println("Statement 2");
System.out.println("Statement 3");
num = 1;
PI = 3.14;
}
public void foo2() {
int a; // OK, used in different scope
int b; // OK, used in different scope
int count = 0; // OK, used in different scope
{
System.out.println("Inside inner scope");
a = 1;
b = 2;
count++;
}
}
}
Check can detect a block of initialization methods. If a variable is used in such a block and there are no other statements after variable declaration, 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.
To configure the check to set allowed distance:
<module name="VariableDeclarationUsageDistance"> <property name="allowedDistance" value="4"/> </module>
Example:
public class Test {
public void foo1() {
int num; // OK, distance = 4
final int PI; // OK, final variables not checked
System.out.println("Statement 1");
System.out.println("Statement 2");
System.out.println("Statement 3");
num = 1;
PI = 3.14;
}
public void foo2() {
int a; // OK, used in different scope
int b; // OK, used in different scope
int count = 0; // OK, used in different scope
{
System.out.println("Inside inner scope");
a = 1;
b = 2;
count++;
}
}
}
To configure the check to ignore certain variables:
<module name="VariableDeclarationUsageDistance"> <property name="ignoreVariablePattern" value="^num$"/> </module>
This configuration ignores variables named "num".
Example:
public class Test {
public void foo1() {
int num; // OK, variable ignored
final int PI; // OK, final variables not checked
System.out.println("Statement 1");
System.out.println("Statement 2");
System.out.println("Statement 3");
num = 1;
PI = 3.14;
}
public void foo2() {
int a; // OK, used in different scope
int b; // OK, used in different scope
int count = 0; // OK, used in different scope
{
System.out.println("Inside inner scope");
a = 1;
b = 2;
count++;
}
}
}
To configure the check to force validation between scopes:
<module name="VariableDeclarationUsageDistance"> <property name="validateBetweenScopes" value="true"/> </module>
Example:
public class Test {
public void foo1() {
int num; // violation, distance = 4
final int PI; // OK, final variables not checked
System.out.println("Statement 1");
System.out.println("Statement 2");
System.out.println("Statement 3");
num = 1;
PI = 3.14;
}
public void foo2() {
int a; // OK, distance = 2
int b; // OK, distance = 3
int count = 0; // violation, distance = 4
{
System.out.println("Inside inner scope");
a = 1;
b = 2;
count++;
}
}
}
To configure the check to check final variables:
<module name="VariableDeclarationUsageDistance"> <property name="ignoreFinal" value="false"/> </module>
Example:
public class Test {
public void foo1() {
int num; // violation, distance = 4
final int PI; // violation, distance = 5
System.out.println("Statement 1");
System.out.println("Statement 2");
System.out.println("Statement 3");
num = 1;
PI = 3.14;
}
public void foo2() {
int a; // OK, used in different scope
int b; // OK, used in different scope
int count = 0; // OK, used in different scope
{
System.out.println("Inside inner scope");
a = 1;
b = 2;
count++;
}
}
}
Parent is com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
variable.declaration.usage.distance
variable.declaration.usage.distance.extend
AutomaticBean.OutputStreamOptions| Modifier and Type | Field and Description |
|---|---|
static java.lang.String |
MSG_KEY
Warning message key.
|
static java.lang.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)
Setter to specify distance between declaration of variable and its first usage.
|
void |
setIgnoreFinal(boolean ignoreFinal)
Setter to allow to ignore variables with a 'final' modifier.
|
void |
setIgnoreVariablePattern(java.util.regex.Pattern pattern)
Setter to define RegExp to ignore distance calculation for variables listed in this pattern.
|
void |
setValidateBetweenScopes(boolean validateBetweenScopes)
Setter to allow to calculate the distance between declaration of
variable and its first usage in the different scopes.
|
void |
visitToken(DetailAST ast)
Called to process a token.
|
beginTree, clearViolations, destroy, finishTree, getFileContents, getLine, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokensfinishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityconfigure, contextualize, getConfiguration, setupChildpublic static final java.lang.String MSG_KEY
public static final java.lang.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(java.util.regex.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()
AbstractCheckgetDefaultTokens in class AbstractCheckTokenTypespublic int[] getAcceptableTokens()
AbstractCheckgetAcceptableTokens in class AbstractCheckTokenTypespublic int[] getRequiredTokens()
AbstractCheckgetRequiredTokens in class AbstractCheckTokenTypespublic void visitToken(DetailAST ast)
AbstractCheckvisitToken in class AbstractCheckast - the token to processCopyright © 2001-2021. All Rights Reserved.