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