View Javadoc
1   package com.google.checkstyle.test.chapter4formatting.rule411optionalbracesusage;
2   
3   class InputUseOfOptionalBraces {
4     /**
5      * some javadoc..
6      *
7      * @return helper func *
8      */
9     boolean condition() {
10      return false;
11    }
12  
13    /** Test do/while loops. */
14    void testDoWhile() {
15      // Valid
16      do {
17        testDoWhile();
18      } while (condition());
19  
20      // Invalid
21      // violation below ''do' construct must use '{}'s.'
22      do testDoWhile();
23      while (condition());
24    }
25  
26    /** Test while loops. */
27    void testWhile() {
28      // Valid
29      while (condition()) {
30        testWhile();
31      }
32  
33      // Invalid
34      // violation 2 lines below ''while' construct must use '{}'s.'
35      // violation 3 lines below ''while' construct must use '{}'s.'
36      while (condition())
37        ;
38      while (condition()) testWhile();
39      // violation 2 lines below ''while' construct must use '{}'s.'
40      // violation below ''if' construct must use '{}'s.'
41      while (condition()) if (condition()) testWhile();
42      String k = "testing";
43      // violation below ''if' construct must use '{}'s.'
44      if (k != null) k = "ss";
45    }
46  
47    /** Test for loops. */
48    void testFor() {
49      // Valid
50      for (int i = 1; i < 5; i++) {
51        testFor();
52      }
53  
54      // Invalid
55      // violation below ''for' construct must use '{}'s.'
56      for (int i = 1; i < 5; i++)
57        ;
58      // violation below ''for' construct must use '{}'s.'
59      for (int i = 1; i < 5; i++) testFor();
60      // violation 2 lines below ''for' construct must use '{}'s.'
61      // violation below ''if' construct must use '{}'s.'
62      for (int i = 1; i < 5; i++) if (i > 2) testFor();
63    }
64  
65    /** Test if constructs. */
66    public void testIf() {
67      // Valid
68      if (condition()) {
69        testIf();
70      } else if (condition()) {
71        testIf();
72      } else {
73        testIf();
74      }
75  
76      // Invalid
77      // violation below ''if' construct must use '{}'s.'
78      if (condition())
79        ;
80      // violation below ''if' construct must use '{}'s.'
81      if (condition()) testIf();
82      // violation 2 lines below ''if' construct must use '{}'s.'
83      // violation 2 lines below ''else' construct must use '{}'s.'
84      if (condition()) testIf();
85      else testIf();
86      // violation below ''if' construct must use '{}'s.'
87      if (condition()) testIf();
88      else {
89        testIf();
90      }
91      if (condition()) {
92        testIf();
93        // violation below ''else' construct must use '{}'s.'
94      } else testIf();
95      // violation 2 lines below ''if' construct must use '{}'s.'
96      // violation below ''if' construct must use '{}'s.'
97      if (condition()) if (condition()) testIf();
98    }
99  
100   void whitespaceAfterSemi() {
101     // reject
102     int i = 1;
103     int j = 2;
104 
105     // accept
106     for (; ; ) {}
107   }
108 
109   /** Empty constructor block. */
110   public InputUseOfOptionalBraces() {}
111 
112   /** Empty method block. */
113   public void emptyImplementation() {}
114 
115   /** Testing Lambdas. */
116   static Runnable r2 = () -> String.CASE_INSENSITIVE_ORDER.equals("Hello world one!");
117 
118   static Runnable r3 = () -> String.CASE_INSENSITIVE_ORDER.equals("Hello world one!");
119   static Runnable r4 =
120       () -> {
121         String.CASE_INSENSITIVE_ORDER.equals("Hello world one!");
122       };
123   static Runnable r5 = () -> {};
124 
125   class EmptyBlocks {
126     boolean flag = true;
127     int[] abc = {
128       1, 2, 3, 4,
129     };
130 
131     void foo() {
132       // violation below ''while' construct must use '{}'s.'
133       while (flag)
134         ;
135       while (flag) {}
136       while (flag) {
137         /*foo*/
138       }
139       // violation below ''do' construct must use '{}'s.'
140       do
141         ;
142       while (flag);
143       do {} while (flag);
144       do {
145         /*foo*/
146       } while (flag);
147       // violation below ''if' construct must use '{}'s.'
148       if (flag)
149         ;
150       if (flag) {
151         /*foo*/
152       }
153       if (flag) {
154         /*foo*/
155       }
156       // violation 2 lines below ''if' construct must use '{}'s.'
157       // violation 3 lines below ''else' construct must use '{}'s.'
158       if (flag)
159         ;
160       else
161         ;
162       if (flag) {
163       } else {
164       }
165       if (flag) {
166         /*foo*/
167       } else {
168         /*foo*/
169       }
170       // violation below ''for' construct must use '{}'s.'
171       for (int i = 0; i < 10; i++)
172         ;
173       for (int i = 0; i < 10; i++) {}
174       for (int i = 0; i < 10; i++) {
175         /*foo*/
176       }
177       // violation below ''for' construct must use '{}'s.'
178       for (int b : abc)
179         ;
180       for (int b : abc) {}
181       for (int b : abc) {
182         /*foo*/
183       }
184     }
185 
186     class InnerEmptyBlocks {
187       boolean flag = true;
188       int[] abc = {
189         1, 2, 3, 4,
190       };
191 
192       void foo() {
193         // violation below ''while' construct must use '{}'s.'
194         while (flag)
195           ;
196         while (flag) {}
197         while (flag) {
198           /*foo*/
199         }
200         // violation below ''do' construct must use '{}'s.'
201         do
202           ;
203         while (flag);
204         do {} while (flag);
205         do {
206           /*foo*/
207         } while (flag);
208         // violation below ''if' construct must use '{}'s.'
209         if (flag)
210           ;
211         if (flag) {
212           /*foo*/
213         }
214         if (flag) {
215           /*foo*/
216         }
217         // violation 2 lines below ''if' construct must use '{}'s.'
218         // violation 3 lines below ''else' construct must use '{}'s.'
219         if (flag)
220           ;
221         else
222           ;
223         if (flag) {
224         } else {
225         }
226         if (flag) {
227           /*foo*/
228         } else {
229           /*foo*/
230         }
231         // violation below ''for' construct must use '{}'s.'
232         for (int i = 0; i < 10; i++)
233           ;
234         for (int i = 0; i < 10; i++) {}
235         for (int i = 0; i < 10; i++) {
236           /*foo*/
237         }
238         // violation below ''for' construct must use '{}'s.'
239         for (int b : abc)
240           ;
241         for (int b : abc) {}
242         for (int b : abc) {
243           /*foo*/
244         }
245       }
246     }
247 
248     InnerEmptyBlocks anon =
249         new InnerEmptyBlocks() {
250           boolean flag = true;
251           int[] abc = {
252             1, 2, 3, 4,
253           };
254 
255           void foo() {
256             // violation below ''while' construct must use '{}'s.'
257             while (flag)
258               ;
259             while (flag) {}
260             while (flag) {
261               /*foo*/
262             }
263             // violation below ''do' construct must use '{}'s.'
264             do
265               ;
266             while (flag);
267             do {} while (flag);
268             do {
269               /*foo*/
270             } while (flag);
271             // violation below ''if' construct must use '{}'s.'
272             if (flag)
273               ;
274             if (flag) {
275               /*foo*/
276             }
277             if (flag) {
278               /*foo*/
279             }
280             // violation 2 lines below ''if' construct must use '{}'s.'
281             // violation 3 lines below ''else' construct must use '{}'s.'
282             if (flag)
283               ;
284             else
285               ;
286             if (flag) {
287             } else {
288             }
289             if (flag) {
290               /*foo*/
291             } else {
292               /*foo*/
293             }
294             // violation below ''for' construct must use '{}'s.'
295             for (int i = 0; i < 10; i++)
296               ;
297             for (int i = 0; i < 10; i++) {}
298             for (int i = 0; i < 10; i++) {
299               /*foo*/
300             }
301             // violation below ''for' construct must use '{}'s.'
302             for (int b : abc)
303               ;
304             for (int b : abc) {}
305             for (int b : abc) {
306               /*foo*/
307             }
308           }
309         };
310   }
311 }