Skip to content

Commit a45149f

Browse files
smita1078Z4ID-Reaper
authored andcommitted
Issue #13155: update the docs example for block initialization methods
1 parent 5d33265 commit a45149f

File tree

10 files changed

+195
-144
lines changed

10 files changed

+195
-144
lines changed

src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/VariableDeclarationUsageDistanceCheck.java

+12-12
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@
4242
* </div>
4343
* <ul>
4444
* <li>
45-
* Property {@code allowedDistance} - Specify distance between declaration
46-
* of variable and its first usage. Values should be greater than 0.
45+
* Property {@code allowedDistance} - Specify the maximum distance between a
46+
* variable's declaration and its first usage. Value should be greater than 0.
4747
* Type is {@code int}.
4848
* Default value is {@code 3}.
4949
* </li>
@@ -59,8 +59,8 @@
5959
* Default value is {@code ""}.
6060
* </li>
6161
* <li>
62-
* Property {@code validateBetweenScopes} - Allow to calculate the distance between
63-
* declaration of variable and its first usage in the different scopes.
62+
* Property {@code validateBetweenScopes} - Allow to calculate the distance between a
63+
* variable's declaration and its first usage across different scopes.
6464
* Type is {@code boolean}.
6565
* Default value is {@code false}.
6666
* </li>
@@ -104,8 +104,8 @@ public class VariableDeclarationUsageDistanceCheck extends AbstractCheck {
104104
private static final int DEFAULT_DISTANCE = 3;
105105

106106
/**
107-
* Specify distance between declaration of variable and its first usage.
108-
* Values should be greater than 0.
107+
* Specify the maximum distance between a variable's declaration and its first usage.
108+
* Value should be greater than 0.
109109
*/
110110
private int allowedDistance = DEFAULT_DISTANCE;
111111

@@ -116,17 +116,17 @@ public class VariableDeclarationUsageDistanceCheck extends AbstractCheck {
116116
private Pattern ignoreVariablePattern = Pattern.compile("");
117117

118118
/**
119-
* Allow to calculate the distance between declaration of variable and its
120-
* first usage in the different scopes.
119+
* Allow to calculate the distance between a variable's declaration and its first usage
120+
* across different scopes.
121121
*/
122122
private boolean validateBetweenScopes;
123123

124124
/** Allow to ignore variables with a 'final' modifier. */
125125
private boolean ignoreFinal = true;
126126

127127
/**
128-
* Setter to specify distance between declaration of variable and its first usage.
129-
* Values should be greater than 0.
128+
* Setter to specify the maximum distance between a variable's declaration and its first usage.
129+
* Value should be greater than 0.
130130
*
131131
* @param allowedDistance
132132
* Allowed distance between declaration of variable and its first
@@ -148,8 +148,8 @@ public void setIgnoreVariablePattern(Pattern pattern) {
148148
}
149149

150150
/**
151-
* Setter to allow to calculate the distance between declaration of
152-
* variable and its first usage in the different scopes.
151+
* Setter to allow to calculate the distance between a variable's declaration
152+
* and its first usage across different scopes.
153153
*
154154
* @param validateBetweenScopes
155155
* Defines if allow to calculate distance between declaration of

src/main/resources/com/puppycrawl/tools/checkstyle/meta/checks/coding/VariableDeclarationUsageDistanceCheck.xml

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
&lt;/div&gt;</description>
1212
<properties>
1313
<property default-value="3" name="allowedDistance" type="int">
14-
<description>Specify distance between declaration
15-
of variable and its first usage. Values should be greater than 0.</description>
14+
<description>Specify the maximum distance between a
15+
variable's declaration and its first usage. Value should be greater than 0.</description>
1616
</property>
1717
<property default-value="true" name="ignoreFinal" type="boolean">
1818
<description>Allow to ignore variables with a 'final' modifier.</description>
@@ -24,8 +24,8 @@
2424
for variables listed in this pattern.</description>
2525
</property>
2626
<property default-value="false" name="validateBetweenScopes" type="boolean">
27-
<description>Allow to calculate the distance between
28-
declaration of variable and its first usage in the different scopes.</description>
27+
<description>Allow to calculate the distance between a
28+
variable's declaration and its first usage across different scopes.</description>
2929
</property>
3030
</properties>
3131
<message-keys>

src/site/xdoc/checks/coding/variabledeclarationusagedistance.xml

+58-45
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
</tr>
2929
<tr>
3030
<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>
3232
<td><a href="../../property_types.html#int">int</a></td>
3333
<td><code>3</code></td>
3434
<td>5.8</td>
@@ -49,7 +49,7 @@
4949
</tr>
5050
<tr>
5151
<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>
5353
<td><a href="../../property_types.html#boolean">boolean</a></td>
5454
<td><code>false</code></td>
5555
<td>5.8</td>
@@ -96,46 +96,59 @@ public class Example1 {
9696
}
9797
}
9898
}
99-
</code></pre></div>
99+
</code></pre></div><hr class="example-separator"/>
100100
<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.
104104
</p>
105-
<p>
106-
Case #1:
105+
<p id="Example2-config">
106+
Configure the check to default:
107107
</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+
&lt;module name="Checker"&gt;
110+
&lt;module name="TreeWalker"&gt;
111+
&lt;module name="VariableDeclarationUsageDistance"/&gt;
112+
&lt;/module&gt;
113+
&lt;/module&gt;
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>
117142
<p>
118-
The distance for the variable &quot;minutes&quot; is 1 even
143+
The distance for the variable &quot;minutes&quot; in the method <code>case1</code> is 1 even
119144
though this variable is used in the fifth method's call.
120145
</p>
121146
<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 &quot;minutes&quot; 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 &quot;minutes&quot; 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.
137150
</p><hr class="example-separator"/>
138-
<p id="Example2-config">
151+
<p id="Example3-config">
139152
To configure the check to set allowed distance:
140153
</p>
141154
<div class="wrapper"><pre class="prettyprint"><code class="language-xml">
@@ -147,9 +160,9 @@ cal.set(Calendar.MINUTE, minutes);
147160
&lt;/module&gt;
148161
&lt;/module&gt;
149162
</code></pre></div>
150-
<p id="Example2-code">Example:</p>
163+
<p id="Example3-code">Example:</p>
151164
<div class="wrapper"><pre class="prettyprint"><code class="language-java">
152-
public class Example2 {
165+
public class Example3 {
153166

154167
public void foo1() {
155168
int num; // OK, distance = 4
@@ -175,7 +188,7 @@ public class Example2 {
175188
}
176189
}
177190
</code></pre></div><hr class="example-separator"/>
178-
<p id="Example3-config">
191+
<p id="Example4-config">
179192
To configure the check to ignore certain variables:
180193
</p>
181194
<div class="wrapper"><pre class="prettyprint"><code class="language-xml">
@@ -190,9 +203,9 @@ public class Example2 {
190203
<p>
191204
This configuration ignores variables named &quot;num&quot;.
192205
</p>
193-
<p id="Example3-code">Example:</p>
206+
<p id="Example4-code">Example:</p>
194207
<div class="wrapper"><pre class="prettyprint"><code class="language-java">
195-
public class Example3 {
208+
public class Example4 {
196209

197210
public void foo1() {
198211
int num; // OK, variable ignored
@@ -218,7 +231,7 @@ public class Example3 {
218231
}
219232
}
220233
</code></pre></div><hr class="example-separator"/>
221-
<p id="Example4-config">
234+
<p id="Example5-config">
222235
To configure the check to force validation between scopes:
223236
</p>
224237
<div class="wrapper"><pre class="prettyprint"><code class="language-xml">
@@ -230,9 +243,9 @@ public class Example3 {
230243
&lt;/module&gt;
231244
&lt;/module&gt;
232245
</code></pre></div>
233-
<p id="Example4-code">Example:</p>
246+
<p id="Example5-code">Example:</p>
234247
<div class="wrapper"><pre class="prettyprint"><code class="language-java">
235-
public class Example4 {
248+
public class Example5 {
236249

237250
public void foo1() {
238251
int num; // violation, distance = 4
@@ -258,7 +271,7 @@ public class Example4 {
258271
}
259272
}
260273
</code></pre></div><hr class="example-separator"/>
261-
<p id="Example5-config">
274+
<p id="Example6-config">
262275
To configure the check to check final variables:
263276
</p>
264277
<div class="wrapper"><pre class="prettyprint"><code class="language-xml">
@@ -270,9 +283,9 @@ public class Example4 {
270283
&lt;/module&gt;
271284
&lt;/module&gt;
272285
</code></pre></div>
273-
<p id="Example5-code">Example:</p>
286+
<p id="Example6-code">Example:</p>
274287
<div class="wrapper"><pre class="prettyprint"><code class="language-java">
275-
public class Example5 {
288+
public class Example6 {
276289

277290
public void foo1() {
278291
int num; // violation, distance = 4

0 commit comments

Comments
 (0)