View Javadoc
1   package com.google.checkstyle.test.chapter4formatting.rule413emptyblocks;
2   
3   class InputFormattedEmptyBlocksAndCatchBlocks {
4     static {
5     }
6   
7     static {
8     }
9   
10    static {
11    }
12  
13    public void fooMethod() {
14      InputEmptyBlocksAndCatchBlocks r = new InputEmptyBlocksAndCatchBlocks();
15      int a = 1;
16      if (a == 1) {}
17      char[] s = {'1', '2'};
18      int index = 2;
19      if (doSideEffect() == 1) {}
20      Io in = new Io();
21      while ((r = in.read()) != null) {}
22      for (; index < s.length && s[index] != 'x'; index++) {}
23      if (a == 1) {
24      } else {
25        System.identityHashCode("a");
26      }
27      do {} while (a == 1);
28      switch (a) {
29      }
30      // violation 2 lines above 'switch without "default" clause.'
31      int[] z = {};
32    }
33  
34    public int doSideEffect() {
35      return 1;
36    }
37  
38    public void emptyMethod() {}
39  
40    void foo() throws Exception {
41      int a = 90;
42  
43      if (a == 1) {
44      } else {
45      }
46  
47      if (a == 1) {
48      } else {
49      }
50  
51      try (MyResource r = new MyResource()) {}
52      try (MyResource r = new MyResource()) {}
53      try (MyResource r = new MyResource()) {
54      } catch (Exception expected) {
55      }
56      try (MyResource r = new MyResource()) {
57      } catch (Exception expected) {
58      }
59      try (MyResource r = new MyResource()) {
60  
61      } catch (Exception expected) {
62      }
63      try (MyResource r = new MyResource()) {
64  
65      } catch (Exception expected) {
66      }
67      try (MyResource r = new MyResource()) {
68        ;
69      }
70    }
71  
72    /** some. */
73    public class MyResource implements AutoCloseable {
74      /** some. */
75      @Override
76      public void close() throws Exception {
77        System.out.println("Closed MyResource");
78      }
79    }
80  }
81  
82  // violation below 'Top-level class ExtraIo has to reside in its own source file.'
83  class ExtraIo {
84    public InputEmptyBlocksAndCatchBlocks read() {
85      return new InputEmptyBlocksAndCatchBlocks();
86    }
87  }
88  
89  // violation below 'Top-level class ExtraEmpty has to reside in its own source file.'
90  class ExtraEmpty {}
91  
92  // violation below 'Top-level class ExtraEmptyImplement has to reside in its own source file.'
93  interface ExtraEmptyImplement {}
94  
95  // violation below 'Top-level class ExtraWithInner has to reside in its own source file.'
96  class ExtraWithInner {
97    static {
98    }
99  
100   public void emptyMethod() {}
101 
102   public int doSideEffect() {
103     return 1;
104   }
105 
106   class Inner {
107     private void withEmpty() {
108       InputEmptyBlocksAndCatchBlocks r = new InputEmptyBlocksAndCatchBlocks();
109       int a = 1;
110       if (a == 1) {}
111       char[] s = {'1', '2'};
112       int index = 2;
113       if (doSideEffect() == 1) {}
114       Io in = new Io();
115       while ((r = in.read()) != null) {}
116       for (; index < s.length && s[index] != 'x'; index++) {}
117       if (a == 1) {
118       } else {
119         System.identityHashCode("a");
120       }
121       do {} while (a == 1);
122       switch (a) {
123       }
124       // violation 2 lines above 'switch without "default" clause.'
125       int[] z = {};
126     }
127   }
128 }
129 
130 // violation below 'Top-level class ExtraWithAnon has to reside in its own source file.'
131 class ExtraWithAnon {
132   interface AnonWithEmpty {
133     public void fooEmpty();
134   }
135 
136   void method() {
137     AnonWithEmpty foo =
138         new AnonWithEmpty() {
139 
140           public void emptyMethod() {}
141 
142           public void fooEmpty() {
143             InputEmptyBlocksAndCatchBlocks r = new InputEmptyBlocksAndCatchBlocks();
144             int a = 1;
145             if (a == 1) {}
146             char[] s = {'1', '2'};
147             int index = 2;
148             if (doSideEffect() == 1) {}
149             Io in = new Io();
150             while ((r = in.read()) != null) {}
151             for (; index < s.length && s[index] != 'x'; index++) {}
152             if (a == 1) {
153             } else {
154               System.identityHashCode("a");
155             }
156             do {} while (a == 1);
157             switch (a) {
158             }
159             // violation 2 lines above 'switch without "default" clause.'
160             int[] z = {};
161           }
162 
163           public int doSideEffect() {
164             return 1;
165           }
166         };
167   }
168 }
169 
170 // violation below 'Top-level class ExtraNewClass has to reside in its own source file.'
171 class ExtraNewClass {
172 
173   void foo() {
174     int a = 1;
175 
176     if (a == 1) {
177       System.identityHashCode("a");
178     } else {
179     }
180 
181     if (a == 1) {
182       System.identityHashCode("a");
183     } else {
184       /*ignore*/
185     }
186 
187     if (a == 1) {
188       /*ignore*/
189     } else {
190       System.identityHashCode("a");
191     }
192 
193     if (a == 1) {
194       System.identityHashCode("a");
195     } else if (a != 1) {
196       /*ignore*/
197     } else {
198       /*ignore*/
199     }
200 
201     if (a == 1) {
202       /*ignore*/
203     } else if (a != 1) {
204       System.identityHashCode("a");
205     } else {
206       /*ignore*/
207     }
208 
209     if (a == 1) {
210       /*ignore*/
211     } else if (a != 1) {
212       /*ignore*/
213     } else {
214       System.identityHashCode("a");
215     }
216 
217     if (a == 1) {
218       /*ignore*/
219     } else if (a != 1) {
220       /*ignore*/
221     } else {
222       /*ignore*/
223     }
224 
225     if (a == 1) {
226       /*ignore*/
227     } else if (a != 1) {
228     } else {
229     }
230 
231     if (a == 1) {
232     } else if (a != 1) {
233       /*ignore*/
234     } else {
235     }
236 
237     if (a == 1) {
238     } else if (a != 1) {
239     } else {
240       /*ignore*/
241     }
242   }
243 
244   class NewInner {
245 
246     void foo() {
247       int a = 1;
248 
249       if (a == 1) {
250         System.identityHashCode("a");
251       } else {
252       }
253 
254       if (a == 1) {
255         System.identityHashCode("a");
256       } else {
257         /*ignore*/
258       }
259 
260       if (a == 1) {
261         /*ignore*/
262       } else {
263         System.identityHashCode("a");
264       }
265 
266       if (a == 1) {
267         System.identityHashCode("a");
268       } else if (a != 1) {
269         /*ignore*/
270       } else {
271         /*ignore*/
272       }
273 
274       if (a == 1) {
275         /*ignore*/
276       } else if (a != 1) {
277         System.identityHashCode("a");
278       } else {
279         /*ignore*/
280       }
281 
282       if (a == 1) {
283         /*ignore*/
284       } else if (a != 1) {
285         /*ignore*/
286       } else {
287         System.identityHashCode("a");
288       }
289 
290       if (a == 1) {
291         /*ignore*/
292       } else if (a != 1) {
293         /*ignore*/
294       } else {
295         /*ignore*/
296       }
297 
298       if (a == 1) {
299         /*ignore*/
300       } else if (a != 1) {
301       } else {
302       }
303 
304       if (a == 1) {
305       } else if (a != 1) {
306         /*ignore*/
307       } else {
308       }
309 
310       if (a == 1) {
311       } else if (a != 1) {
312       } else {
313         /*ignore*/
314       }
315     }
316 
317     NewInner anon =
318         new NewInner() {
319 
320           void foo() {
321             int a = 1;
322 
323             if (a == 1) {
324               System.identityHashCode("a");
325             } else {
326             }
327 
328             if (a == 1) {
329               System.identityHashCode("a");
330             } else {
331               /*ignore*/
332             }
333 
334             if (a == 1) {
335               /*ignore*/
336             } else {
337               System.identityHashCode("a");
338             }
339 
340             if (a == 1) {
341               System.identityHashCode("a");
342             } else if (a != 1) {
343               /*ignore*/
344             } else {
345               /*ignore*/
346             }
347 
348             if (a == 1) {
349               /*ignore*/
350             } else if (a != 1) {
351               System.identityHashCode("a");
352             } else {
353               /*ignore*/
354             }
355 
356             if (a == 1) {
357               /*ignore*/
358             } else if (a != 1) {
359               /*ignore*/
360             } else {
361               System.identityHashCode("a");
362             }
363 
364             if (a == 1) {
365               /*ignore*/
366             } else if (a != 1) {
367               /*ignore*/
368             } else {
369               /*ignore*/
370             }
371 
372             if (a == 1) {
373               /*ignore*/
374             } else if (a != 1) {
375             } else {
376             }
377 
378             if (a == 1) {
379             } else if (a != 1) {
380               /*ignore*/
381             } else {
382             }
383 
384             if (a == 1) {
385             } else if (a != 1) {
386             } else {
387               /*ignore*/
388             }
389           }
390         };
391   }
392 }
393 
394 // violation below 'Top-level class ExtraExample has to reside in its own source file.'
395 class ExtraExample {
396 
397   void doNothing() {}
398 
399   void doNothingElse() {}
400 }
401 
402 // violation below '.* ExtraTestingEmptyBlockCatch has to reside in its own source file.'
403 class ExtraTestingEmptyBlockCatch {
404   boolean flag;
405 
406   void doSm() {}
407 
408   void foo() {
409     try {
410       if (!flag) {
411         doSm();
412       }
413     } catch (Exception e) {
414       /* ignore */
415     } finally {
416       /* ignore */
417     }
418   }
419 
420   void foo2() {
421     try {
422       if (!flag) {
423         doSm();
424       }
425       // violation below 'Empty catch block.'
426     } catch (Exception e) {
427     } finally {
428     }
429   }
430 
431   class Inner {
432     boolean flag;
433 
434     void doSm() {}
435 
436     void foo() {
437       try {
438         if (!flag) {
439           doSm();
440         }
441       } catch (Exception e) {
442         /* ignore */
443       } finally {
444         /* ignore */
445       }
446     }
447 
448     void foo2() {
449       try {
450         if (!flag) {
451           doSm();
452         }
453         // violation below 'Empty catch block.'
454       } catch (Exception e) {
455       } finally {
456       }
457     }
458   }
459 
460   Inner anon =
461       new Inner() {
462         boolean flag;
463 
464         void doSm() {}
465 
466         void foo() {
467           try {
468             if (!flag) {
469               doSm();
470             }
471           } catch (Exception e) {
472             /* ignore */
473           } finally {
474             /* ignore */
475           }
476         }
477 
478         void foo2() {
479           try {
480             if (!flag) {
481               doSm();
482             }
483             // violation below 'Empty catch block.'
484           } catch (Exception e) {
485           } finally {
486           }
487         }
488       };
489 }