View Javadoc
1   package com.google.checkstyle.test.chapter4formatting.rule412nonemptyblocks;
2   
3   class InputNonemptyBlocksLeftRightCurly
4     { // violation ''{' at column 3 should be on the previous line.'
5       /**
6        * summary.
7        *
8        * @return helper func *
9        */
10      boolean condition()
11      { // violation ''{' at column 5 should be on the previous line.'
12        return false;
13      }
14  
15      /** Test do/while loops. */
16      void testDoWhile()
17      { // violation ''{' at column 5 should be on the previous line.'
18  
19        do {
20          testDoWhile();
21        } // violation ''}' at column 7 should be on the same line as the next part of .*'
22        while (condition());
23  
24        do {
25          testDoWhile();
26        } while (condition());
27      }
28  
29      /** Test while loops. */
30      void testWhile()
31      { // violation ''{' at column 5 should be on the previous line.'
32  
33        while (condition()) {
34          testWhile();
35        }
36  
37        while (condition()) {
38          /* foo */
39        }
40        while (condition()) {
41          testWhile();
42        }
43        while (condition()) {
44          if (condition()) {
45            testWhile();
46          }
47        }
48      }
49  
50      /** Test for loops. */
51      void testFor()
52      { // violation ''{' at column 5 should be on the previous line.'
53  
54        for (int i = 1; i < 5; i++) {
55          testFor();
56        }
57  
58        for (int i = 1; i < 5; i++) {
59          /* foo */
60        }
61        for (int i = 1; i < 5; i++) {
62          testFor();
63        }
64        for (int i = 1; i < 5; i++) {
65          if (i > 2) {
66            testFor();
67          }
68        }
69      }
70  
71      /** Test if constructs. */
72      public void testIf()
73      { // violation ''{' at column 5 should be on the previous line.'
74  
75        if (condition()) {
76          testIf();
77        } // violation ''}' at column 7 should be on the same line as the next part of .*'
78        else if (condition()) {
79          testIf();
80        } // violation ''}' at column 7 should be on the same line as the next part of .*'
81        else {
82          testIf();
83        }
84  
85        if (condition()) { /* foo */ }
86        if (condition()) {
87          testIf();
88        }
89        if (condition()) {
90          testIf();
91        } else {
92          testIf();
93        }
94        if (condition()) {
95          testIf();
96        } else {
97          testIf();
98        }
99        if (condition()) {
100         testIf();
101       } // violation ''}' at column 7 should be on the same line as the next part of .*'
102       else {
103         testIf();
104       }
105       if (condition()) {
106         if (condition()) {
107           testIf();
108         }
109       }
110     }
111 
112     void whitespaceAfterSemi()
113     { // violation ''{' at column 5 should be on the previous line.'
114 
115       int i = 1;
116       int j = 2;
117 
118       for (; ; ) {}
119     }
120 
121     /** Empty constructor block. */
122     public InputNonemptyBlocksLeftRightCurly() {}
123 
124     /** Empty method block. */
125     public void emptyImplementation() {}
126   }
127 
128 // violation below 'Top-level class EnumContainerLeft has to reside in its own source file.'
129 class EnumContainerLeft {
130   private enum Suit {
131     CLUBS,
132     HEARTS,
133     SPADES,
134     DIAMONDS
135   } // ok
136 }
137 
138 // violation below 'Top-level class WithArraysLeft has to reside in its own source file.'
139 class WithArraysLeft { // ok
140   String[] s1 = {""}; // ok
141   String[] empty = {}; // ok
142   String[] s2 = { // ok
143     "foo", "foo",
144   };
145   String[] s3 = { // ok
146     "foo", "foo",
147   };
148   String[] s4 = { // ok
149     "foo", "foo",
150   };
151   String[] s5 = {"foo", "foo"}; // ok
152 }
153 
154 // violation below 'Top-level class InputRightCurlyOther2 has to reside in its own source file.'
155 class InputRightCurlyOther2
156   { // violation ''{' at column 3 should be on the previous line.'
157     /**
158      * summary.
159      *
160      * @see test method *
161      */
162     int foo()
163         throws InterruptedException
164     { // violation ''{' at column 5 should be on the previous line.'
165       int x = 1;
166       int a = 2;
167       while (true)
168         { // violation ''{' at column 9 should be on the previous line.'
169           try
170           { // violation ''{' at column 11 should be on the previous line.'
171             if (x > 0)
172               { // violation ''{' at column 15 should be on the previous line.'
173                 break;
174               } else if (x < 0) { // ok
175 
176               ;
177             } // violation ''}' at column 13 should be on the same line as the next part.*'
178             else
179               { // violation ''{' at column 15 should be on the previous line.'
180                 break;
181               } // ok
182             switch (a)
183               { // violation ''{' at column 15 should be on the previous line.'
184               case 0:
185                 break;
186               default:
187                 break;
188               } // ok
189           } // violation ''}' at column 11 should be on the same line as the next part of .*'
190           catch (Exception e)
191           { // violation ''{' at column 11 should be on the previous line.'
192             break;
193           } // ok
194         } // ok
195 
196       synchronized (this)
197         { // violation ''{' at column 9 should be on the previous line.'
198           do
199           { // violation ''{' at column 11 should be on the previous line.'
200             x = 2;
201           } while (x == 2); // ok
202         } // ok
203 
204       this.wait(666); // Bizarre, but legal
205 
206       for (int k = 0; k < 1; k++)
207         { // violation ''{' at column 9 should be on the previous line.'
208           String innerBlockVariable = "";
209         } // ok
210 
211       if (System.currentTimeMillis() > 1000) {
212         return 1;
213       } else {
214         return 2;
215       }
216     } // ok
217 
218     static
219     { // violation ''{' at column 5 should be on the previous line.'
220       int x = 1;
221     } // ok
222 
223     /** some javadoc. */
224     public enum GreetingsEnum
225     { // violation ''{' at column 5 should be on the previous line.'
226       HELLO,
227       GOODBYE
228     } // ok
229 
230     void method2()
231     { // violation ''{' at column 5 should be on the previous line.'
232       boolean flag = true;
233       if (flag) {
234         System.identityHashCode("heh");
235       // 2 violations 3 lines below:
236       //  ''if' child has incorrect indentation level 6, expected level should be 8.'
237       //  ''}' at column 21 should have line break before.'
238       flag = !flag; } System
239         .identityHashCode("Xe-xe");
240 
241       if (flag) { System.identityHashCode("some foo"); }
242       // violation above ''{' at column 17 should have line break after.'
243     } // ok
244   } // ok
245 
246 /**
247  * Test input for closing brace if that brace terminates a statement or the body of a constructor.
248  */
249 // violation below 'Top-level class FooCtor has to reside in its own source file.'
250 class FooCtor
251   { // violation ''{' at column 3 should be on the previous line.'
252   int i3;
253 
254   public FooCtor()
255     { // violation ''{' at column 5 should be on the previous line.'
256       i3 = 1;
257     } } // violation ''}' at column 5 should be alone on a line.'
258 
259 /** Test input for closing brace if that brace terminates a statement or the body of a method. */
260 // violation below 'Top-level class FooMethod has to reside in its own source file.'
261 class FooMethod
262   { // violation ''{' at column 3 should be on the previous line.'
263     public void fooMethod()
264     { // violation ''{' at column 5 should be on the previous line.'
265       int i = 1;
266     } } // violation ''}' at column 5 should be alone on a line.'
267 
268 /**
269  * Test input for closing brace if that brace terminates a statement or the body of a named class.
270  */
271 // violation below 'Top-level class FooInner has to reside in its own source file.'
272 class FooInner
273   { // violation ''{' at column 3 should be on the previous line.'
274   class InnerFoo
275     { // violation ''{' at column 5 should be on the previous line.'
276       public void fooInnerMethod()
277         { // violation ''{' at column 9 should be on the previous line.'
278         }
279     }
280   } // ok
281 
282 // violation below 'Top-level class EnumContainer has to reside in its own source file.'
283 class EnumContainer {
284   private enum Suit {
285     CLUBS,
286     HEARTS,
287     SPADES,
288     DIAMONDS
289   } // ok
290 }
291 
292 // violation below 'Top-level class WithArrays has to reside in its own source file.'
293 class WithArrays {
294   String[] test = {""}; // ok
295   String[] empty = {}; // ok
296   String[] s1 = {
297     "foo", "foo",
298   }; // ok
299   String[] s2 = {
300     "foo", "foo",
301   }; // ok
302   String[] s3 = {
303     "foo", "foo",
304   }; // ok
305   String[] s4 = {"foo", "foo"}; // ok
306 }