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