View Javadoc
1   package com.puppycrawl.tools.checkstyle.checks.coding.finallocalvariable;
2   
3   public class InputFinalLocalVariable
4   {
5       private int m_ClassVariable = 0;
6       //static block
7       static
8       {
9           int i, j = 0;
10          Runnable runnable = new Runnable()
11          {
12              public void run()
13              {
14              }
15          };
16      }
17      /** constructor */
18      public InputFinalLocalVariable()
19      {
20          int i = 0;
21          // final variable
22          final int j = 2;
23  
24          int z;
25  
26          Object obj = new Object();
27  
28          int k = 0;
29  
30          String x = obj.toString();
31  
32          k++;
33  
34          k = 2;
35  
36          Runnable runnable = new Runnable()
37          {
38              public void run()
39              {
40                  int q = 0;
41              }
42          };
43      }
44  
45      public void method(int aArg, final int aFinal, int aArg2)
46      {
47          int z = 0;
48  
49          z++;
50  
51          aArg2++;
52      }
53  
54      public void aMethod()
55      {
56          int i = 0;
57  
58          final int j = 2;
59  
60          int z;
61  
62          Object obj = new Object();
63  
64          int k = 0;
65  
66          String x = obj.toString();
67  
68          k++;
69  
70          final class Inner
71          {
72              public Inner()
73              {
74                  int w = 0;
75                  Runnable runnable = new Runnable()
76                  {
77                      public void run()
78                      {
79                      }
80                  };
81              }
82          }
83      }
84  
85      public void anotherMethod()
86      {
87          boolean aBool = true;
88          for (int i = 0, j = 1, k = 1; j < 10 ; j++)
89          {
90              k++;
91              aBool = false;
92          }
93  
94          int l = 0;
95          {
96              int weird = 0;
97              int j = 0;
98              int k = 0;
99              {
100                l++;
101             }
102         }
103 
104         int weird = 0;
105         weird++;
106 
107         final InnerClass ic = new InnerClass();
108 
109         ic.mInner2 = 1;
110     }
111 
112     class InnerClass
113     {
114         private int mInner = 0;
115 
116         public int mInner2 = 0;
117     }
118 }
119 
120 interface Inter
121 {
122     void method(int aParam);
123 }
124 
125 abstract class AbstractClass
126 {
127     public abstract void abstractMethod(int aParam);
128 }
129 
130 class Blah
131 {
132     static
133     {
134         for(int a : getInts())
135         {
136 
137         }
138     }
139 
140     static int[] getInts() {
141         return null;
142     }
143 }
144 
145 class test_1241722
146 {
147     private Object o_;
148 
149     public void doSomething(Object _o)
150     {
151         System.identityHashCode(_o);
152     }
153 
154     public void doSomething2(Object _o1)
155     {
156         o_ = _o1;
157     }
158 }
159 
160 class class1
161 {
162     public class1(final int x){
163 
164     }
165 }
166 
167 class AA {
168     {
169         int y = 0;
170         y = 9;
171     }
172 }
173 
174 enum Enum1 {
175     ;
176 
177     {
178         int var = 0;
179         var = 1;
180     }
181 }
182 
183 class class2 {
184     public void method1(){
185         int x;
186         x = 3;
187     }
188     public void method2() {
189         for(int i=0;i<5;i++){
190             int x;
191             x = 3;
192         }
193         int y;
194         for(int i=0;i<5;i++) {
195             y = 3;
196         }
197         for(int i=0;i<5;i++) {
198             int z;
199             for(int j=0;j<5;j++) {
200                 z = 3;
201             }
202         }
203     }
204     public void method3() {
205         int m;
206         do {
207            m = 0;
208         } while (false);
209         do {
210             int n;
211            n = 0;
212         } while (true);        
213     }
214 
215     private void foo() {
216         int q;
217         int w;
218         int e;
219         q = 1;
220         w = 1;
221         e = 1;
222         e = 2;
223         class Local {
224             void bar() {
225                 int q;
226                 int w;
227                 int e;
228                 q = 1;
229                 q = 2;
230                 w = 1;
231                 e = 1;
232             }
233         }
234 
235         int i;
236         for (;; i = 1) { }
237     }
238 
239     public void method4() {
240         int m;
241         int i = 5;
242         while (i > 1) {
243             m = 0;
244             i++;
245         }
246         while (true) {
247             int n;
248             n = 0;
249         }
250     }
251 
252     int[] array = new int[10];
253     public void method5() {
254         int r;
255         for (int a: array) {
256             r = 0;
257         }
258         for (int a: array) {
259             int t;
260             t = 0;
261         }
262     }
263 }
264 
265 class classs3 {
266     public void method(final int i) {
267         switch (i) {
268             case 1:
269                 int foo = 1;    // Violation
270                 break;
271             default:
272         }
273         switch (i) {
274             case 1:
275                 int foo = 1;    // No Violation
276                 break;
277             case 2:
278                 foo = 2;
279                 break;
280             default:
281         }
282     }
283 }
284 
285 class Class3 {
286     public void test1() {
287         final boolean b = true;
288         int shouldBeFinal;        //Violation
289 
290         if (b) {
291             shouldBeFinal = 1;
292         }
293         else {
294             shouldBeFinal = 2;
295         }
296     }
297 
298     public void test2() {
299         final int b = 10;
300         int shouldBeFinal;        //Violation
301 
302         switch (b) {
303         case 0:
304             shouldBeFinal = 1;
305             break;
306         default:
307             shouldBeFinal = 2;
308             break;
309         }
310     }
311 
312     public void test3() {
313         int x;        // No Violation
314         try {
315             x = 0;
316         } catch (final Exception e) {
317             x = 1;
318         }
319 
320         int y;        // No Violation
321         try {
322             y = 0;
323         } finally {
324             y = 1;
325         }
326     }
327 
328     public void test4() {
329         final boolean b = false;
330         int x;        // No Violation
331         if (b) {
332             x = 1;
333         } else {
334             x = 2;
335         }
336         
337         if(b) {
338             x = 3;
339         }
340     }
341 
342     public void test5() {
343         final boolean b = false;
344         int shouldBeFinal;    //Violation
345         if(b) {
346         }
347         if (b) {
348             shouldBeFinal = 1;
349         } else {
350             shouldBeFinal = 2;
351         }
352     }
353 }
354 
355 class class4 {
356     public void foo() {
357         int shouldBeFinal;    //violation
358         class Bar {
359             void bar () {
360                 int shouldBeFinal;    //Violation
361                 final boolean b = false;
362                 if (b) {
363                     shouldBeFinal = 1;
364                 } else {
365                     shouldBeFinal = 2;
366                 }
367             }
368         }
369     }
370 }
371 
372 class class5 {
373     public void test1(){
374         final boolean b = false;
375         int shouldBeFinal;    //Violation
376         if(b){
377             if(b){
378                 shouldBeFinal = 1;
379             } else {
380                 shouldBeFinal = 2;
381             }
382         }
383     }
384     public void test2() {
385         final int b = 10;
386         int shouldBeFinal;        //Violation
387 
388         switch (b) {
389         case 0:
390             switch (b) {
391             case 0:
392                 shouldBeFinal = 1;
393                 break;
394             default:
395                 shouldBeFinal = 2;
396                 break;
397             }
398             break;
399         default:
400             shouldBeFinal = 3;
401             break;
402         }
403     }
404     public void test3() {
405         int x;    //No Violation
406         try {
407             x = 0;
408             try {
409                 x = 0;
410             } catch (final Exception e) {
411                 x = 1;
412             }
413         } catch (final Exception e) {
414             x = 1;
415         }
416     }
417     public void test4() {
418         int shouldBeFinal;    //violation
419         class Bar {
420             void bar () {
421                 int shouldBeFinal;    //Violation
422                 final boolean b = false;
423                 if (b) {
424                     if (b) {
425                         shouldBeFinal = 1;
426                     } else {
427                         shouldBeFinal = 2;
428                     }
429                 } else {
430                     shouldBeFinal = 2;
431                 }
432             }
433         }
434     }
435 }