View Javadoc
1   /*
2   FinalLocalVariable
3   validateEnhancedForLoopVariable = (default)false
4   tokens = (default)VARIABLE_DEF
5   
6   
7   */
8   
9   package com.puppycrawl.tools.checkstyle.checks.coding.finallocalvariable;
10  
11  import java.util.HashMap;
12  import java.util.Locale;
13  
14  
15  
16  public class InputFinalLocalVariableAssignedMultipleTimes {
17  
18      void foo1() {
19          final boolean some_condition = true;
20          int i;
21          if (some_condition) {
22              i = 1;
23          }
24          i = 2;
25      }
26  
27      void foo2() {
28          final boolean some_condition = true;
29          int i;
30          if (some_condition) {
31              i = 1;
32          }
33          else {
34  
35          }
36          i = 2;
37      }
38  
39      void foo3() {
40          final boolean some_condition = true;
41          int i;
42          if (some_condition) {
43              i = 1;
44              if (i >= 1) {
45  
46              }
47              else {
48  
49              }
50          }
51          i = 2;
52      }
53  
54  
55      void foo4() {
56          final boolean some_condition = true;
57          int i; // violation, "Variable 'i' should be declared final"
58          if (some_condition) {
59              if (true) {
60              }
61              else {
62              }
63              i = 1;
64          }
65          else {
66              i = 2;
67          }
68          if (true) {
69  
70          }
71          else {
72          }
73  
74      }
75  
76      void foo5() {
77          final boolean some_condition = true;
78          int i;
79  
80          {
81              i = 2;
82          }
83  
84          if (some_condition) {
85              i = 1;
86          }
87      }
88  
89      void foo6() {
90          final boolean some_condition = true;
91          int i;
92  
93          {
94              i = 2;
95          }
96  
97          if (some_condition) {
98              i = 1;
99          }
100         else {
101             i = 6;
102         }
103     }
104 
105     void foo7() {
106         final boolean some_condition = true;
107         int i;
108         if (some_condition) {
109             i = 1;
110         }
111         else {
112             i = 1;
113         }
114         i = 2;
115     }
116 
117     void foo8() {
118         final boolean some_condition = true;
119         final int i;
120         if (some_condition) {
121             i = 1;
122         }
123         else {
124 
125         }
126     }
127 
128     // Taken from findbugs
129     public static String foo9(String filePath, String project) {
130         String path = new String(filePath); // violation, "Variable 'path' should be declared final"
131         String commonPath;
132         if (project != null) {
133             commonPath = "";
134             // violation below "Variable 'relativePath' should be declared final"
135             String relativePath = "";
136             if (!relativePath.equals(path)) {
137                 return relativePath;
138             }
139         }
140         commonPath = "";
141         return commonPath;
142     }
143 
144     // Taken from findbugs
145     public int foo10(String factory1, String factory2) {
146         int result = 0;
147         String s1, s2;
148         switch (result) {
149             case 1:
150                 s1 = "Java";
151                 s2 = "C#";
152                 break;
153             case 2:
154                 s1 = "C++";
155                 s2 = "Pascal";
156                 ;
157                 break;
158             case 3:
159                 s1 = "Basic";
160                 s2 = "Angol";
161                 break;
162             case 4:
163                 s1 = "JavaScript";
164                 s2 = "Go";
165                 break;
166             case 5:
167             default:
168                 s1 = "F#";
169                 s2 = "Objective C";
170                 break;
171         }
172         if (s1 == null) {
173             s1 = "incorrect language";
174         }
175         if (s2 == null) {
176             s2 = "incorrect language";
177         }
178         result = s1.compareTo(s2);
179 
180         if (result == 0) {
181             switch (result) {
182                 case 1:
183                     s1 = "Java";
184                     s2 = "C#";
185                     break;
186                 case 2:
187                 default:
188                     s1 = "C++";
189                     s2 = "C";
190                     break;
191             }
192             result = s1.compareTo(s2);
193         }
194         else if (result == 1) {
195             result = -8;
196         }
197         return result;
198     }
199 
200     // Taken from findbugs
201     public String foo11(final Object o, boolean getMinimal) {
202 
203         String n = System.lineSeparator();
204         if (n != null) {
205             return n;
206         }
207 
208         try {
209 
210             String className;
211             int kind; // violation, "Variable 'kind' should be declared final"
212             boolean isParameterToInitMethodofAnonymousInnerClass = false;
213             boolean isSyntheticMethod = false;
214             if (o instanceof String || o instanceof Integer) {
215 
216                 String m; // violation, "Variable 'm' should be declared final"
217                 if (o instanceof String) {
218                     m = (String) o;
219                     isSyntheticMethod = m.equals("");
220                     kind = 1;
221                     className = this.getClass().getName();
222                 }
223                 else if (o instanceof String) {
224                     m = "";
225                     // Don't
226                     isSyntheticMethod = m.equals("");
227                     className = this.getClass().getName();
228                     kind = 2;
229                     if ("<init>".equals(m.toLowerCase(Locale.getDefault()))) {
230                         final int i = className.lastIndexOf('$');
231                         if (i + 1 < className.length()) {
232                             isParameterToInitMethodofAnonymousInnerClass = true;
233                         }
234                     }
235                 }
236                 else {
237                     throw new IllegalStateException("impossible");
238                 }
239 
240                 if (!m.equals("") && !"<init>".equals(m.toLowerCase(Locale.getDefault()))) {
241                     final String c = "className";
242                     // get inherited annotation
243                     String inheritedAnnotations = new String();
244                     if (c.charAt(1) > 0) {
245 
246                         n = "";
247                         if (n != null) {
248                             inheritedAnnotations += "";
249                         }
250                     }
251                     for (int i = 5; i < 10; i++) {
252                         n = new String("");
253                         if (n != null) {
254                             inheritedAnnotations += "";
255                         }
256                     }
257                     if (n == null) {
258                         String.CASE_INSENSITIVE_ORDER.equals("#" + inheritedAnnotations.length());
259                     }
260                     if (!inheritedAnnotations.isEmpty()) {
261                         if (inheritedAnnotations.length() == 1) {
262                             return inheritedAnnotations;
263                         }
264                         if (!getMinimal) {
265                             return inheritedAnnotations;
266                         }
267 
268                         String min = inheritedAnnotations;
269                         if (min.length() == 0) {
270                             inheritedAnnotations = null;
271                             min = inheritedAnnotations;
272                         }
273                         return min;
274                     }
275                     // check to see if method is defined in this class;
276                     // if not, on't consider default annotations
277                     if (inheritedAnnotations == null) {
278                         return null;
279                     }
280                     if (inheritedAnnotations.equals("")) {
281                         String.CASE_INSENSITIVE_ORDER.equals("l" + " defines " + m);
282                     }
283                 } // if not static
284             } // associated with method
285             else if (o instanceof String) {
286 
287                 className = ((String) o).getClass().getName();
288                 kind = 3;
289             }
290             else if (o instanceof String) {
291                 assert false;
292                 className = (String) o;
293                 kind = 4;
294             }
295             else {
296                 throw new IllegalArgumentException("Can't" + o.getClass().getName());
297             }
298 
299             // <init> method parameters for inner classes don't inherit default
300             // annotations
301             // since some of them are synthetic
302             if (isParameterToInitMethodofAnonymousInnerClass) {
303                 return null;
304             }
305 
306             // synthetic elements should not inherit default annotations
307             if (isSyntheticMethod) {
308                 return null;
309             }
310             try {
311                 final String c = new String(className);
312 
313                 if (c != null && c.equals("")) {
314                     return null;
315                 }
316             }
317             catch (Exception e) {
318                 assert true;
319             }
320 
321             // look for default annotation
322             n = new String(className);
323             if (n == null) {
324                 String.CASE_INSENSITIVE_ORDER.equals("Default annotation for " + kind + " is " + n);
325             }
326             if (n != null) {
327                 return n;
328             }
329 
330             n = new String(className);
331             if (n == null) {
332                 String.CASE_INSENSITIVE_ORDER.equals("Default annotation for any is " + n);
333             }
334             if (n != null) {
335                 return n;
336             }
337 
338             final int p = className.lastIndexOf('.');
339             className = className.substring(0, p + 1) + "package-info";
340             n = new String(className);
341             if (n == null) {
342                 String.CASE_INSENSITIVE_ORDER.equals("Default annotation for " + kind + " is " + n);
343             }
344             if (n != null) {
345                 return n;
346             }
347 
348             n = new String(className);
349             if (n == null) {
350                 String.CASE_INSENSITIVE_ORDER.equals("Default annotation for any is " + n);
351             }
352             if (n != null) {
353                 return n;
354             }
355 
356             return n;
357         }
358         catch (Exception e) {
359             String.CASE_INSENSITIVE_ORDER.equals(e);
360             ;
361             return null;
362         }
363 
364     }
365 
366     // Taken from findbugs
367     private void foo12(Long start, Long end) {
368         HashMap<Object, Object> headMap;
369         if (end < Long.MAX_VALUE) {
370             headMap = new HashMap<>();
371             Long tailEnd = 1L;
372             if (tailEnd != null) {
373                 end = tailEnd;
374             }
375             if (!headMap.isEmpty()) {
376                 tailEnd = (Long) headMap.get(headMap.size());
377                 if (tailEnd > end) {
378                     end = tailEnd;
379                 }
380             }
381         }
382         headMap = new HashMap<>();
383         if (!headMap.isEmpty()) {
384             final int headStart = headMap.size();
385             final Long headEnd = (Long) headMap.get(headStart);
386             if (headEnd >= start - 1) {
387                 headMap.remove(headStart);
388                 start = Long.valueOf(headStart);
389             }
390         }
391         headMap.clear();
392         headMap.remove(end);
393         headMap.put(start, end);
394     }
395 
396     // Taken from Guava
397     public static int foo13(int p, int q, int mode) {
398         String.CASE_INSENSITIVE_ORDER.equals(mode);
399         ;
400         if (q == 0) {
401             throw new ArithmeticException("/ by zero"); // for GWT
402         }
403         final int div = p / q;
404         final int rem = p - q * div; // equal to p % q
405 
406         if (rem == 0) {
407             return div;
408         }
409 
410       /*
411       * Normal Java division rounds towards 0, consistently with RoundingMode.DOWN. We just have to
412       * deal with the cases where rounding towards 0 is wrong,which typically depends on the sign of
413       * p / q.
414       *
415       * signum is 1 if p and q are both nonnegative or both negative, and -1 otherwise.
416       */
417         final int signum = 1 | ((p ^ q) >> (Integer.SIZE - 1));
418         boolean increment; // violation, "Variable 'increment' should be declared final"
419         switch (mode) {
420             case 1:
421                 String.CASE_INSENSITIVE_ORDER.equals(rem == 0);
422                 // fall through
423             case 2:
424                 increment = false;
425                 break;
426             case 3:
427                 increment = true;
428                 break;
429             case 4:
430                 increment = signum > 0;
431                 break;
432             case 5:
433                 increment = signum < 0;
434                 break;
435             case 6:
436             case 7:
437             case 8:
438                 final int absRem = 1;
439                 final int cmpRemToHalfDivisor = absRem - (1 - absRem);
440                 // subtracting two nonnegative ints can't overflow
441                 // cmpRemToHalfDivisor has the same sign as compare(abs(rem), abs(q) / 2).
442                 if (cmpRemToHalfDivisor == 0) { // exactly on the half mark
443                     increment = (mode == 1) || (mode == 2 & (div & 1) != 0);
444                 }
445                 else {
446                     increment = cmpRemToHalfDivisor > 0; // closer to the UP value
447                 }
448                 break;
449             default:
450                 throw new AssertionError();
451         }
452         return increment ? div + signum : div;
453     }
454 }