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
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
131         String commonPath;
132         if (project != null) {
133             commonPath = "";
134             String relativePath = ""; // violation
135             if (!relativePath.equals(path)) {
136                 return relativePath;
137             }
138         }
139         commonPath = "";
140         return commonPath;
141     }
142 
143     // Taken from findbugs
144     public int foo10(String factory1, String factory2) {
145         int result = 0;
146         String s1, s2;
147         switch (result) {
148             case 1:
149                 s1 = "Java";
150                 s2 = "C#";
151                 break;
152             case 2:
153                 s1 = "C++";
154                 s2 = "Pascal";
155                 ;
156                 break;
157             case 3:
158                 s1 = "Basic";
159                 s2 = "Angol";
160                 break;
161             case 4:
162                 s1 = "JavaScript";
163                 s2 = "Go";
164                 break;
165             case 5:
166             default:
167                 s1 = "F#";
168                 s2 = "Objective C";
169                 break;
170         }
171         if (s1 == null) {
172             s1 = "incorrect language";
173         }
174         if (s2 == null) {
175             s2 = "incorrect language";
176         }
177         result = s1.compareTo(s2);
178 
179         if (result == 0) {
180             switch (result) {
181                 case 1:
182                     s1 = "Java";
183                     s2 = "C#";
184                     break;
185                 case 2:
186                 default:
187                     s1 = "C++";
188                     s2 = "C";
189                     break;
190             }
191             result = s1.compareTo(s2);
192         }
193         else if (result == 1) {
194             result = -8;
195         }
196         return result;
197     }
198 
199     // Taken from findbugs
200     public String foo11(final Object o, boolean getMinimal) {
201 
202         String n = System.lineSeparator();
203         if (n != null) {
204             return n;
205         }
206 
207         try {
208 
209             String className;
210             int kind; // violation
211             boolean isParameterToInitMethodofAnonymousInnerClass = false;
212             boolean isSyntheticMethod = false;
213             if (o instanceof String || o instanceof Integer) {
214 
215                 String m; // violation
216                 if (o instanceof String) {
217                     m = (String) o;
218                     isSyntheticMethod = m.equals("");
219                     kind = 1;
220                     className = this.getClass().getName();
221                 }
222                 else if (o instanceof String) {
223                     m = "";
224                     // Don't
225                     isSyntheticMethod = m.equals("");
226                     className = this.getClass().getName();
227                     kind = 2;
228                     if ("<init>".equals(m.toLowerCase(Locale.getDefault()))) {
229                         final int i = className.lastIndexOf('$');
230                         if (i + 1 < className.length()) {
231                             isParameterToInitMethodofAnonymousInnerClass = true;
232                         }
233                     }
234                 }
235                 else {
236                     throw new IllegalStateException("impossible");
237                 }
238 
239                 if (!m.equals("") && !"<init>".equals(m.toLowerCase(Locale.getDefault()))) {
240                     final String c = "className";
241                     // get inherited annotation
242                     String inheritedAnnotations = new String();
243                     if (c.charAt(1) > 0) {
244 
245                         n = "";
246                         if (n != null) {
247                             inheritedAnnotations += "";
248                         }
249                     }
250                     for (int i = 5; i < 10; i++) {
251                         n = new String("");
252                         if (n != null) {
253                             inheritedAnnotations += "";
254                         }
255                     }
256                     if (n == null) {
257                         String.CASE_INSENSITIVE_ORDER.equals("#" + inheritedAnnotations.length());
258                     }
259                     if (!inheritedAnnotations.isEmpty()) {
260                         if (inheritedAnnotations.length() == 1) {
261                             return inheritedAnnotations;
262                         }
263                         if (!getMinimal) {
264                             return inheritedAnnotations;
265                         }
266 
267                         String min = inheritedAnnotations;
268                         if (min.length() == 0) {
269                             inheritedAnnotations = null;
270                             min = inheritedAnnotations;
271                         }
272                         return min;
273                     }
274                     // check to see if method is defined in this class;
275                     // if not, on't consider default annotations
276                     if (inheritedAnnotations == null) {
277                         return null;
278                     }
279                     if (inheritedAnnotations.equals("")) {
280                         String.CASE_INSENSITIVE_ORDER.equals("l" + " defines " + m);
281                     }
282                 } // if not static
283             } // associated with method
284             else if (o instanceof String) {
285 
286                 className = ((String) o).getClass().getName();
287                 kind = 3;
288             }
289             else if (o instanceof String) {
290                 assert false;
291                 className = (String) o;
292                 kind = 4;
293             }
294             else {
295                 throw new IllegalArgumentException("Can't" + o.getClass().getName());
296             }
297 
298             // <init> method parameters for inner classes don't inherit default
299             // annotations
300             // since some of them are synthetic
301             if (isParameterToInitMethodofAnonymousInnerClass) {
302                 return null;
303             }
304 
305             // synthetic elements should not inherit default annotations
306             if (isSyntheticMethod) {
307                 return null;
308             }
309             try {
310                 final String c = new String(className);
311 
312                 if (c != null && c.equals("")) {
313                     return null;
314                 }
315             }
316             catch (Exception e) {
317                 assert true;
318             }
319 
320             // look for default annotation
321             n = new String(className);
322             if (n == null) {
323                 String.CASE_INSENSITIVE_ORDER.equals("Default annotation for " + kind + " is " + n);
324             }
325             if (n != null) {
326                 return n;
327             }
328 
329             n = new String(className);
330             if (n == null) {
331                 String.CASE_INSENSITIVE_ORDER.equals("Default annotation for any is " + n);
332             }
333             if (n != null) {
334                 return n;
335             }
336 
337             final int p = className.lastIndexOf('.');
338             className = className.substring(0, p + 1) + "package-info";
339             n = new String(className);
340             if (n == null) {
341                 String.CASE_INSENSITIVE_ORDER.equals("Default annotation for " + kind + " is " + n);
342             }
343             if (n != null) {
344                 return n;
345             }
346 
347             n = new String(className);
348             if (n == null) {
349                 String.CASE_INSENSITIVE_ORDER.equals("Default annotation for any is " + n);
350             }
351             if (n != null) {
352                 return n;
353             }
354 
355             return n;
356         }
357         catch (Exception e) {
358             String.CASE_INSENSITIVE_ORDER.equals(e);
359             ;
360             return null;
361         }
362 
363     }
364 
365     // Taken from findbugs
366     private void foo12(Long start, Long end) {
367         HashMap<Object, Object> headMap;
368         if (end < Long.MAX_VALUE) {
369             headMap = new HashMap<>();
370             Long tailEnd = 1L;
371             if (tailEnd != null) {
372                 end = tailEnd;
373             }
374             if (!headMap.isEmpty()) {
375                 tailEnd = (Long) headMap.get(headMap.size());
376                 if (tailEnd > end) {
377                     end = tailEnd;
378                 }
379             }
380         }
381         headMap = new HashMap<>();
382         if (!headMap.isEmpty()) {
383             final int headStart = headMap.size();
384             final Long headEnd = (Long) headMap.get(headStart);
385             if (headEnd >= start - 1) {
386                 headMap.remove(headStart);
387                 start = Long.valueOf(headStart);
388             }
389         }
390         headMap.clear();
391         headMap.remove(end);
392         headMap.put(start, end);
393     }
394 
395     // Taken from Guava
396     public static int foo13(int p, int q, int mode) {
397         String.CASE_INSENSITIVE_ORDER.equals(mode);
398         ;
399         if (q == 0) {
400             throw new ArithmeticException("/ by zero"); // for GWT
401         }
402         final int div = p / q;
403         final int rem = p - q * div; // equal to p % q
404 
405         if (rem == 0) {
406             return div;
407         }
408 
409       /*
410       * Normal Java division rounds towards 0, consistently with RoundingMode.DOWN. We just have to
411       * deal with the cases where rounding towards 0 is wrong,which typically depends on the sign of
412       * p / q.
413       *
414       * signum is 1 if p and q are both nonnegative or both negative, and -1 otherwise.
415       */
416         final int signum = 1 | ((p ^ q) >> (Integer.SIZE - 1));
417         boolean increment; // violation
418         switch (mode) {
419             case 1:
420                 String.CASE_INSENSITIVE_ORDER.equals(rem == 0);
421                 // fall through
422             case 2:
423                 increment = false;
424                 break;
425             case 3:
426                 increment = true;
427                 break;
428             case 4:
429                 increment = signum > 0;
430                 break;
431             case 5:
432                 increment = signum < 0;
433                 break;
434             case 6:
435             case 7:
436             case 8:
437                 final int absRem = 1;
438                 final int cmpRemToHalfDivisor = absRem - (1 - absRem);
439                 // subtracting two nonnegative ints can't overflow
440                 // cmpRemToHalfDivisor has the same sign as compare(abs(rem), abs(q) / 2).
441                 if (cmpRemToHalfDivisor == 0) { // exactly on the half mark
442                     increment = (mode == 1) || (mode == 2 & (div & 1) != 0);
443                 }
444                 else {
445                     increment = cmpRemToHalfDivisor > 0; // closer to the UP value
446                 }
447                 break;
448             default:
449                 throw new AssertionError();
450         }
451         return increment ? div + signum : div;
452     }
453 }