28
28
</tr >
29
29
<tr >
30
30
<td >allowedDistance</td >
31
- <td >Specify distance between declaration of variable and its first usage. Values should be greater than 0.</td >
31
+ <td >Specify the maximum distance between a variable's declaration and its first usage. Value should be greater than 0.</td >
32
32
<td ><a href =" ../../property_types.html#int" >int</a ></td >
33
33
<td ><code >3</code ></td >
34
34
<td >5.8</td >
49
49
</tr >
50
50
<tr >
51
51
<td >validateBetweenScopes</td >
52
- <td >Allow to calculate the distance between declaration of variable and its first usage in the different scopes.</td >
52
+ <td >Allow to calculate the distance between a variable's declaration and its first usage across different scopes.</td >
53
53
<td ><a href =" ../../property_types.html#boolean" >boolean</a ></td >
54
54
<td ><code >false</code ></td >
55
55
<td >5.8</td >
@@ -96,46 +96,59 @@ public class Example1 {
96
96
}
97
97
}
98
98
}
99
- </code ></pre ></div >
99
+ </code ></pre ></div >< hr class = " example-separator " />
100
100
<p >
101
- Check can detect a block of initialization methods. If a variable is used in
102
- such a block and there are no other statements after variable declaration,
103
- then distance = 1.
101
+ Check is able to detect a block of initialization methods, as a single point for distance.
102
+ If a variable is used in such a block and there are no other statements after
103
+ variable declaration, then distance = 1.
104
104
</p >
105
- <p >
106
- Case #1 :
105
+ <p id = " Example2-config " >
106
+ Configure the check to default :
107
107
</p >
108
- <source >
109
- int minutes = 5;
110
- Calendar cal = Calendar.getInstance();
111
- cal.setTimeInMillis(timeNow);
112
- cal.set(Calendar.SECOND, 0);
113
- cal.set(Calendar.MILLISECOND, 0);
114
- cal.set(Calendar.HOUR_OF_DAY, hh);
115
- cal.set(Calendar.MINUTE, minutes);
116
- </source >
108
+ <div class =" wrapper" ><pre class =" prettyprint" ><code class =" language-xml" >
109
+ < module name="Checker">
110
+ < module name="TreeWalker">
111
+ < module name="VariableDeclarationUsageDistance"/>
112
+ < /module>
113
+ < /module>
114
+ </code ></pre ></div >
115
+ <p id =" Example2-code" >Example:</p >
116
+ <div class =" wrapper" ><pre class =" prettyprint" ><code class =" language-java" >
117
+ public class Example2 {
118
+
119
+ public void case1(long timeNow, int hh, int min) {
120
+ int minutes = min + 5; // Ok, No violation reported
121
+ Calendar cal = Calendar.getInstance();
122
+ cal.setTimeInMillis(timeNow);
123
+ cal.set(Calendar.SECOND, 0);
124
+ cal.set(Calendar.MILLISECOND, 0);
125
+ cal.set(Calendar.HOUR_OF_DAY, hh);
126
+ cal.set(Calendar.MINUTE, minutes);
127
+ }
128
+
129
+ public void case2(long timeNow, int hh, int min){
130
+ // violation below, 'variable 'minutes' declaration and its first usage is 6.'
131
+ int minutes = min + 5000;
132
+ Calendar cal = Calendar.getInstance();
133
+ cal.setTimeInMillis(timeNow);
134
+ cal.set(Calendar.SECOND, 0);
135
+ cal.set(Calendar.MILLISECOND, 0);
136
+ cal.set(Calendar.HOUR_OF_DAY, hh);
137
+ System.out.println("Hello World");
138
+ cal.set(Calendar.MINUTE, minutes);
139
+ }
140
+ }
141
+ </code ></pre ></div >
117
142
<p >
118
- The distance for the variable " minutes" is 1 even
143
+ The distance for the variable " minutes" in the method < code >case1</ code > is 1 even
119
144
though this variable is used in the fifth method's call.
120
145
</p >
121
146
<p >
122
- Case #2:
123
- </p >
124
- <source >
125
- int minutes = 5;
126
- Calendar cal = Calendar.getInstance();
127
- cal.setTimeInMillis(timeNow);
128
- cal.set(Calendar.SECOND, 0);
129
- cal.set(Calendar.MILLISECOND, 0);
130
- System.out.println(cal);
131
- cal.set(Calendar.HOUR_OF_DAY, hh);
132
- cal.set(Calendar.MINUTE, minutes);
133
- </source >
134
- <p >
135
- The distance for the variable " minutes" is 6 because there is one more expression
136
- (except the initialization block) between the declaration of this variable and its usage.
147
+ The distance for the variable " minutes" in the method <code >case2</code > is 6
148
+ because there is one more expression (except the initialization block) between the
149
+ declaration of this variable and its usage.
137
150
</p ><hr class =" example-separator" />
138
- <p id =" Example2 -config" >
151
+ <p id =" Example3 -config" >
139
152
To configure the check to set allowed distance:
140
153
</p >
141
154
<div class =" wrapper" ><pre class =" prettyprint" ><code class =" language-xml" >
@@ -147,9 +160,9 @@ cal.set(Calendar.MINUTE, minutes);
147
160
< /module>
148
161
< /module>
149
162
</code ></pre ></div >
150
- <p id =" Example2 -code" >Example:</p >
163
+ <p id =" Example3 -code" >Example:</p >
151
164
<div class =" wrapper" ><pre class =" prettyprint" ><code class =" language-java" >
152
- public class Example2 {
165
+ public class Example3 {
153
166
154
167
public void foo1() {
155
168
int num; // OK, distance = 4
@@ -175,7 +188,7 @@ public class Example2 {
175
188
}
176
189
}
177
190
</code ></pre ></div ><hr class =" example-separator" />
178
- <p id =" Example3 -config" >
191
+ <p id =" Example4 -config" >
179
192
To configure the check to ignore certain variables:
180
193
</p >
181
194
<div class =" wrapper" ><pre class =" prettyprint" ><code class =" language-xml" >
@@ -190,9 +203,9 @@ public class Example2 {
190
203
<p >
191
204
This configuration ignores variables named " num" .
192
205
</p >
193
- <p id =" Example3 -code" >Example:</p >
206
+ <p id =" Example4 -code" >Example:</p >
194
207
<div class =" wrapper" ><pre class =" prettyprint" ><code class =" language-java" >
195
- public class Example3 {
208
+ public class Example4 {
196
209
197
210
public void foo1() {
198
211
int num; // OK, variable ignored
@@ -218,7 +231,7 @@ public class Example3 {
218
231
}
219
232
}
220
233
</code ></pre ></div ><hr class =" example-separator" />
221
- <p id =" Example4 -config" >
234
+ <p id =" Example5 -config" >
222
235
To configure the check to force validation between scopes:
223
236
</p >
224
237
<div class =" wrapper" ><pre class =" prettyprint" ><code class =" language-xml" >
@@ -230,9 +243,9 @@ public class Example3 {
230
243
< /module>
231
244
< /module>
232
245
</code ></pre ></div >
233
- <p id =" Example4 -code" >Example:</p >
246
+ <p id =" Example5 -code" >Example:</p >
234
247
<div class =" wrapper" ><pre class =" prettyprint" ><code class =" language-java" >
235
- public class Example4 {
248
+ public class Example5 {
236
249
237
250
public void foo1() {
238
251
int num; // violation, distance = 4
@@ -258,7 +271,7 @@ public class Example4 {
258
271
}
259
272
}
260
273
</code ></pre ></div ><hr class =" example-separator" />
261
- <p id =" Example5 -config" >
274
+ <p id =" Example6 -config" >
262
275
To configure the check to check final variables:
263
276
</p >
264
277
<div class =" wrapper" ><pre class =" prettyprint" ><code class =" language-xml" >
@@ -270,9 +283,9 @@ public class Example4 {
270
283
< /module>
271
284
< /module>
272
285
</code ></pre ></div >
273
- <p id =" Example5 -code" >Example:</p >
286
+ <p id =" Example6 -code" >Example:</p >
274
287
<div class =" wrapper" ><pre class =" prettyprint" ><code class =" language-java" >
275
- public class Example5 {
288
+ public class Example6 {
276
289
277
290
public void foo1() {
278
291
int num; // violation, distance = 4
0 commit comments