View Javadoc
1   /*
2   FallThrough
3   checkLastCaseGroup = true
4   reliefPattern = (default)falls?[ -]?thr(u|ough)
5   
6   
7   */
8   
9   package com.puppycrawl.tools.checkstyle.checks.coding.fallthrough;
10  
11  public class InputFallThrough
12  {
13      void method(int i, int j, boolean cond) {
14          while (true) {
15              switch (i) {
16              case 0:
17              case 1:
18                  i++;
19                  break;
20              case 2:
21                  i++;
22              case 3: // violation 'Fall\ through from previous branch of the switch statement.'
23                  i++;
24                  break;
25              case 4:
26                  return;
27              case 5:
28                  throw new RuntimeException("");
29              case 6:
30                  continue;
31              case 7: {
32                  break;
33              }
34              case 8: {
35                  return;
36              }
37              case 9: {
38                  throw new RuntimeException("");
39              }
40              case 10: {
41                  continue;
42              }
43              case 11: {
44                  i++;
45              }
46              case 12: // violation 'Fall\ through from previous branch of the switch statement.'
47                  if (false)
48                      break;
49                  else
50                      break;
51              case 13:
52                  if (true) {
53                      return;
54                  }
55              case 14: // violation 'Fall\ through from previous branch of the switch statement.'
56                  if (true) {
57                      return;
58                  } else {
59                      //do nothing
60                  }
61              case 15: // violation 'Fall\ through from previous branch of the switch statement.'
62                  do {
63                      System.identityHashCode("something");
64                      return;
65                  } while(true);
66              case 16:
67                  for (int j1 = 0; j1 < 10; j1++) {
68                      String.valueOf("something");
69                      return;
70                  }
71              case 17:
72                  while (true)
73                      throw new RuntimeException("");
74              case 18:
75                  while(cond) {
76                      break;
77                  }
78              case 19: // violation 'Fall\ through from previous branch of the switch statement.'
79                  try {
80                      i++;
81                      break;
82                  } catch (RuntimeException e) {
83                      break;
84                  } catch (Error e) {
85                      return;
86                  }
87              case 20:
88                  try {
89                      i++;
90                      break;
91                  } catch (RuntimeException e) {
92                  } catch (Error e) {
93                      return;
94                  }
95              case 21: // violation 'Fall\ through from previous branch of the switch statement.'
96                  try {
97                      i++;
98                  } catch (RuntimeException e) {
99                      i--;
100                 } finally {
101                     break;
102                 }
103             case 22:
104                 try {
105                     i++;
106                     break;
107                 } catch (RuntimeException e) {
108                     i--;
109                     break;
110                 } finally {
111                     i++;
112                 }
113             case 23:
114                 switch (j) {
115                 case 1:
116                     continue;
117                 case 2:
118                     return;
119                 default:
120                     return;
121                 }
122             case 24:
123                 switch (j) {
124                 case 1:
125                     continue;
126                 case 2:
127                     break;
128                 default:
129                     return;
130                 }
131             default: // 2 violations
132                 // this is the last label
133                 i++;
134             }
135         }
136     }
137 
138 
139 
140     /* Like above, but all fall throughs with relief comment */
141     void methodFallThru(int i, int j, boolean cond) {
142       while (true) {
143           switch (i) {
144           case -1: // FALLTHRU
145 
146           case 0:
147           case 1:
148               i++;
149               break;
150           case 2:
151               i++;
152               // fallthru
153           case 3:
154               i++;
155               break;
156           case 4:
157               return;
158           case 5:
159               throw new RuntimeException("");
160           case 6:
161               continue;
162           case 7: {
163               break;
164           }
165           case 8: {
166               return;
167           }
168           case 9: {
169               throw new RuntimeException("");
170           }
171           case 10: {
172               continue;
173           }
174           case 11: {
175               i++;
176           }
177           // fallthru
178           case 12:
179               if (false)
180                   break;
181               else
182                   break;
183           case 13:
184               if (true) {
185                   return;
186               }
187           case 14: // violation 'Fall\ through from previous branch of the switch statement.'
188               if (true) {
189                   return;
190               } else {
191                   //do nothing
192               }
193               // fallthru
194           case 15:
195               do {
196                   System.identityHashCode("something");
197                   return;
198               } while(true);
199           case 16:
200               for (int j1 = 0; j1 < 10; j1++) {
201                   String.valueOf("something");
202                   return;
203               }
204           case 17:
205               while (cond)
206                   throw new RuntimeException("");
207           case 18:
208               while(cond) {
209                   break;
210               }
211               // fallthru
212           case 19:
213               try {
214                   i++;
215                   break;
216               } catch (RuntimeException e) {
217                   break;
218               } catch (Error e) {
219                   return;
220               }
221           case 20:
222               try {
223                   i++;
224                   break;
225               } catch (RuntimeException e) {
226               } catch (Error e) {
227                   return;
228               }
229               // fallthru
230           case 21:
231               try {
232                   i++;
233               } catch (RuntimeException e) {
234                   i--;
235               } finally {
236                   break;
237               }
238           case 22:
239               try {
240                   i++;
241                   break;
242               } catch (RuntimeException e) {
243                   i--;
244                   break;
245               } finally {
246                   i++;
247               }
248 
249           case 23:
250               switch (j) {
251               case 1:
252                   continue;
253               case 2:
254                   return;
255               default:
256                   return;
257               }
258           case 24:
259               i++;
260           /* fallthru */ case 25:
261               i++;
262               break;
263 
264           case 26:
265               switch (j) {
266               case 1:
267                   continue;
268               case 2:
269                   break;
270               default:
271                   return;
272               }
273               // fallthru
274           default:
275               // this is the last label
276               i++;
277           // fallthru
278          }
279       }
280    }
281 
282    /* Test relief comment. */
283    void methodFallThruCC(int i, int j, boolean cond) {
284       while (true) {
285           switch (i){
286           case 0:
287               i++; // fallthru
288 
289           case 1:
290               i++;
291           // fallthru
292           case 2: {
293               i++;
294           }
295           // fallthru
296           case 3:
297               i++;
298           /* fallthru */case 4:
299                 break;
300           case 5:
301               i++;
302           // fallthru
303           }
304       }
305    }
306 
307    /* Like above, but C-style comments. */
308    void methodFallThruC(int i, int j, boolean cond) {
309       while (true) {
310           switch (i){
311           case 0:
312               i++; /* fallthru */
313 
314           case 1:
315               i++;
316           /* fallthru */
317           case 2:
318               i++;
319           /* fallthru */case 3:
320                 break;
321           case 4:
322               i++;
323           /* fallthru */
324           }
325       }
326    }
327 
328    /* Like above, but C-style comments with no spaces. */
329    void methodFallThruC2(int i, int j, boolean cond) {
330       while (true) {
331           switch (i){
332           case 0:
333               i++; /*fallthru*/
334 
335           case 1:
336               i++;
337           /*fallthru*/
338           case 2:
339               i++;
340           /*fallthru*/case 3:
341                 break;
342           case 4:
343               i++;
344           /*fallthru*/
345           }
346       }
347    }
348 
349    /* C-style comments with other default fallthru-comment. */
350    void methodFallThruCOtherWords(int i, int j, boolean cond) {
351       while (true) {
352           switch (i){
353           case 0:
354               i++; /* falls through */
355 
356           case 1:
357               i++;
358           /* falls through */
359           case 2:
360               i++;
361           /* falls through */case 3:
362                 break;
363           case 4:
364               i++;
365           /* falls through */
366           }
367       }
368    }
369 
370    /* C-style comments with custom fallthru-comment. */
371    void methodFallThruCCustomWords(int i, int j, boolean cond) {
372       while (true) {
373           switch (i){
374           case 0:
375               i++; /* Continue with next case */
376 
377           case 1: // violation 'Fall\ through from previous branch of the switch statement.'
378               i++;
379           /* Continue with next case */
380           case 2: // violation 'Fall\ through from previous branch of the switch statement.'
381               i++;
382           /* Continue with next case */case 3: // violation 'Fall\ through from prev.*'
383                 break;
384           case 4: // violation 'Fall .* from the last branch of the switch statement.'
385               i++;
386           /* Continue with next case */
387           }
388       }
389    }
390 
391    void methodFallThruLastCaseGroup(int i, int j, boolean cond) {
392        while (true) {
393            switch (i){
394            case 0:
395                i++; // fallthru
396            }
397            switch (i){
398            case 0:
399                i++;
400                // fallthru
401            }
402            switch (i){
403            case 0:
404                i++;
405            /* fallthru */ }
406        }
407     }
408 
409     void method1472228(int i) {
410         switch(i) {
411         case 2:
412             // do nothing
413             break;
414         default:
415         }
416     }
417 
418     void nestedSwitches() {
419         switch (hashCode()) {
420             case 1:
421                 switch (hashCode()) { // causing NullPointerException in the past
422                     case 1:
423                 }
424             default: // violation 'Fall\ through from previous branch of the switch statement.'
425         }
426     }
427 
428     void nextedSwitches2() {
429         switch(hashCode()) {
430         case 1:
431             switch(hashCode()){}
432         case 2: // violation 'Fall\ through from previous branch of the switch statement.'
433             System.lineSeparator();
434             break;
435         }
436     }
437 
438     void ifWithoutBreak() {
439         switch(hashCode()) {
440         case 1:
441             if (true) {
442                 System.lineSeparator();
443             }
444         case 2: // violation 'Fall\ through from previous branch of the switch statement.'
445             System.lineSeparator();
446             break;
447         }
448     }
449 
450     void noCommentAtTheEnd() {
451         switch(hashCode()) {
452         case 1: System.lineSeparator();
453 
454         case 2: // violation 'Fall\ through from previous branch of the switch statement.'
455             System.lineSeparator();
456             break;
457         }
458     }
459 
460     void synchronizedStatement() {
461        switch (hashCode()) {
462            case 1:
463                synchronized (this) {
464                    break;
465                }
466            case 2:
467                // synchronized nested in if
468                if (true) {
469                    synchronized (this) {
470                        break;
471                    }
472                } else {
473                    synchronized (this) {
474                        break;
475                    }
476                }
477            case 3:
478                synchronized (this) {
479                }
480                // fallthru
481            default:
482                break;
483        }
484     }
485 
486     void multipleCasesOnOneLine() {
487         int i = 0;
488         switch (i) {
489         case 0: case 1: i *= i; // fall through
490         case 2: case 3: i *= i;
491         case 4: case 5: i *= i; // violation 'Fall\ through from prev.* br.* switch statement.'
492         case 6: case 7: i *= i; // violation 'Fall\ through from prev.* br.* switch statement.'
493             break;
494         default:
495             throw new RuntimeException();
496         }
497     }
498 }