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