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