View Javadoc
1   package com.puppycrawl.tools.checkstyle.checks.indentation.commentsindentation;
2   
3   import com.puppycrawl.tools.checkstyle.api.DetailAST;
4   import com.puppycrawl.tools.checkstyle.utils.CheckUtils;
5   
6   /**
7    * Contains examples of using comments at the end of the block.
8    */
9   public class InputCommentsIndentationCommentIsAtTheEndOfBlock {
10  
11      public void foo1() {
12          foo2();
13          // TODO: missing functionality
14      }
15  
16      public void foo2() {
17          foo3();
18                           // violation
19      }
20  
21      public void foo3() {
22          foo2();
23          // refreshDisplay();
24      }
25  
26      public void foo4() {
27          foooooooooooooooooooooooooooooooooooooooooo();
28          // ^-- some hint
29      }
30  
31      public void foooooooooooooooooooooooooooooooooooooooooo() { }
32  
33       /////////////////////////////// violation (a single line border to separate a group of methods)
34  
35      public void foo7() {
36          int a = 0;
37  // violation
38      }
39  
40      /////////////////////////////// (a single line border to separate a group of methods)
41  
42      public void foo8() {}
43  
44      public class TestClass {
45          public void test() {
46              int a = 0;
47                 // violation
48          }
49            // violation
50      }
51  
52      public void foo9() {
53          this.foo1();
54               // violation
55      }
56  
57      //    public void foo10() {
58      //
59      //    }
60  
61      public void foo11() {
62          CheckUtils
63              .getFirstNode(new DetailAST())
64              .getFirstChild()
65              .getNextSibling();
66          // comment
67      }
68  
69      public void foo12() {
70          CheckUtils
71              .getFirstNode(new DetailAST())
72              .getFirstChild()
73              .getNextSibling();
74                    // violation
75      }
76  
77      public void foo13() {
78          CheckUtils.getFirstNode(new DetailAST())
79                  .getFirstChild()
80                  .getNextSibling();
81          // comment
82      }
83  
84      public void foo14() {
85          CheckUtils.getFirstNode(new DetailAST())
86              .getFirstChild()
87              .getNextSibling();
88                                 // violation
89      }
90  
91      public void foo15() {
92          CheckUtils
93                .getFirstNode(new DetailAST());
94          // comment
95      }
96  
97      public void foo16() {
98          CheckUtils
99              .getFirstNode(new DetailAST());
100                      // violation
101     }
102 
103     public void foo17() {
104         CheckUtils
105             .getFirstNode(new DetailAST())
106             .getFirstChild()
107             // comment
108             .getNextSibling();
109     }
110 
111     public void foo18() {
112         CheckUtils
113             .getFirstNode(new DetailAST())
114             .getFirstChild()
115                              // violation
116             .getNextSibling();
117     }
118 
119     public void foo19() {
120         (new Thread(new Runnable() {
121             @Override
122             public void run() {
123 
124             }
125         })).
126             run();
127         // comment
128     }
129 
130     public void foo20() {
131         (new Thread(new Runnable() {
132             @Override
133             public void run() {
134 
135             }
136         })).
137             run();
138                           // violation
139     }
140 
141     public void foo21() {
142         int[] array = new int[5];
143 
144         java.util.List<String> expected = new java.util.ArrayList<>();
145         for (int i = 0; i < 5; i++) {
146         org.junit.Assert.assertEquals(expected.get(i), array[i]);
147         }
148         String s = String.format(java.util.Locale.ENGLISH, "The array element "
149                + "immediately following the end of the collection should be nulled",
150             array[1]);
151         // the above example was taken from hibernate-orm and was modified a bit
152     }
153 
154     public void foo22() {
155         int[] array = new int[5];
156 
157         java.util.List<String> expected = new java.util.ArrayList<>();
158         for (int i = 0; i < 5; i++) {
159             org.junit.Assert.assertEquals(expected.get(i), array[i]);
160         }
161         String s = String.format(java.util.Locale.ENGLISH, "The array element "
162                 + "immediately following the end of the collection should be nulled",
163             array[1]);
164                                  // violation
165     }
166 
167     public void foo23() {
168         new Object();
169         // comment
170     }
171 
172     public void foo24() {
173         new Object();
174                      // violation
175     }
176 
177     public String foo25() {
178         return String.format(java.util.Locale.ENGLISH, "%d",
179             1);
180         // comment
181     }
182 
183     public String foo26() {
184         return String.format(java.util.Locale.ENGLISH, "%d",
185             1);
186                                   // violation
187     }
188 
189     public void foo27() {
190         // comment
191         // block
192         foo17();
193 
194         // TODO
195     }
196 
197     public String foo28() {
198         int a = 5;
199         return String.format(java.util.Locale.ENGLISH, "%d",
200             1);
201         // comment
202     }
203 
204     public String foo29() {
205         int a = 5;
206         return String.format(java.util.Locale.ENGLISH, "%d",
207             1);
208                           // violation
209     }
210 
211     public void foo30() {
212         // comment
213         int a = 5;
214 //		violation
215     }
216 
217     public void foo31() {
218         String s = new String ("A"
219             + "B"
220             + "C");
221         // comment
222     }
223 
224     public void foo32() {
225         String s = new String ("A"
226             + "B"
227             + "C");
228             // violation
229     }
230 
231     public void foo33() {
232         // comment
233         this.foo22();
234 //		violation
235     }
236 
237     public void foo34() throws Exception {
238         throw new Exception("",
239             new Exception()
240             );
241         // comment
242     }
243 
244     public void foo35() throws Exception {
245         throw new Exception("",
246             new Exception()
247         );
248             // violation
249     }
250 
251     public void foo36() throws Exception {
252         throw new Exception("",
253             new Exception()
254         );
255 // violation
256     }
257 
258     public void foo37() throws Exception {
259         throw new Exception("", new Exception());
260         // comment
261     }
262 
263     public void foo38() throws Exception {
264         throw new Exception("", new Exception());
265               // violation
266     }
267 
268     public void foo39() throws Exception {
269         throw new Exception("",
270             new Exception());
271          // violation
272     }
273 
274     public void foo40() throws Exception {
275         int a = 88;
276         throw new Exception("", new Exception());
277          // violation
278     }
279 
280     public void foo41() throws Exception {
281         int a = 88;
282         throw new Exception("", new Exception());
283         // comment
284     }
285 
286     public void foo42() {
287         int a = 5;
288         if (a == 5) {
289             int b;
290             // comment
291         } else if (a ==6) {
292 
293         }
294     }
295 
296     public void foo43() {
297         try {
298             int a;
299             // comment
300         } catch (Exception e) {
301 
302         }
303     }
304 
305     public void foo44() {
306         int ar = 5;
307         // comment
308         ar = 6;
309         // comment
310     }
311 
312     public void foo45() {
313         int ar = 5;
314         // comment
315         ar = 6;
316          // violation
317     }
318 
319     public void foo46() {
320 // comment
321 // block
322 // violation
323     }
324 
325     public void foo47() {
326         int a = 5;
327         // comment
328         // block
329         // comment
330     }
331 
332     public void foo48() {
333         int a = 5;
334 // comment
335 // block
336 // violation
337     }
338 
339     public void foo49() {
340        // comment
341        // block
342        // ok
343     }
344 
345     public void foo50() {
346         return;
347 
348         // No NPE here!
349     }
350 
351     public String foo51() {
352         return String
353             .valueOf("11"
354             );
355          // violation
356     }
357 
358     public String foo52() {
359         return String
360             .valueOf("11"
361             );
362         // comment
363     }
364 
365     void foo53() {
366         // comment
367         new Object()
368             .toString();
369         // comment
370     }
371 
372     void foo54() {
373         /* comment */
374         new Object()
375             .toString();
376         // comment
377     }
378 
379     void foo55() {
380             /* violation */
381         new Object()
382             .toString();
383             // comment
384     }
385 
386     void foo56() {
387         new Object().toString();
388         // comment
389     }
390 
391     void foo57() {
392         new Object().toString();
393             // violation
394     }
395 
396     void foo58() {
397         /*
398            comment
399            */
400         // violation
401           foo1();
402           // comment
403     }
404 
405     void foo59() {
406         foo1();
407         /*
408          comment */
409         // comment
410     }
411 
412 
413     void foo61() {
414         foo1();
415         /*
416          * comment
417          */
418         /*
419          * comment
420          */
421     }
422 
423     void foo62() {
424         if (true) {
425             String.CASE_INSENSITIVE_ORDER.equals("");
426           }
427         else {
428 
429           }
430         /*
431          comment
432          */
433         /*
434          comment
435          */
436     }
437 
438     void foo63() {
439         try {
440             String.CASE_INSENSITIVE_ORDER.equals("");
441           }
442         catch (Exception e){
443 
444           }
445 
446         /*
447          comment
448          */
449         /*
450          comment
451          */
452     }
453 
454     void foo64()  {
455         foo1();
456 
457 //  violation
458     }
459 
460     void foo65() {
461         int i = 1
462             + 1
463             + 1;
464         // comment
465         // comment
466     }
467 
468     void foo66()  {
469         if (true) {
470             getClass();
471         }
472 
473           /* violation */
474     }
475 
476     void foo67()  {
477         try {
478             getClass();
479         } finally {
480             hashCode();
481         }
482 
483           /* violation */
484     }
485 
486     void foo68()  {
487         for (int i = 0; i < 0; i++) {
488             getClass();
489         }
490 
491           /* violation */
492     }
493 
494     void foo69()  {
495         while (true) {
496             getClass();
497         }
498 
499           /* violation */
500     }
501 
502     void foo70()  {
503         do {
504             getClass();
505         } while (true);
506 
507           /* violation */
508     }
509 
510     void foo71() {
511         switch("") {
512             case "!":
513                 break;
514             default:
515                 break;
516         }
517 
518           // violation
519     }
520 
521     void foo72() {
522         int u = 1;
523 
524 /* comment */
525 // violation
526     }
527 
528     void foo73() {
529         class Foo { }
530 
531 /* comment */
532 // violation
533     }
534 
535     interface Bar1 {
536         interface NestedBar { }
537 
538 // violation
539     }
540 
541     static class Bar2 {
542         enum Foo {
543             A;
544         }
545 
546     // violation
547     }
548 
549     static class Bar3 {
550         @interface Foo { }
551             // violation
552     }
553 
554     void foo74() {
555         getClass(); // comment
556 // comment
557 // comment
558     }
559 
560     void foo75() {
561         getClass();
562 // comment
563         // comment
564     }
565 
566     // We almost reached the end of the class here.
567 }
568 // The END of the class.