View Javadoc
1   /*
2   NeedBraces
3   allowSingleLineStatement = (default)false
4   allowEmptyLoopBody = (default)false
5   tokens = (default)LITERAL_DO, LITERAL_ELSE, LITERAL_FOR, LITERAL_IF, LITERAL_WHILE
6   
7   
8   */
9   
10  package com.puppycrawl.tools.checkstyle.checks.blocks.needbraces;
11  
12  class InputNeedBracesTestIt
13  {
14      /** @return helper func **/
15      boolean condition()
16      {
17          return false;
18      }
19  
20      /** Test do/while loops **/
21      void testDoWhile()
22      {
23          // Valid
24          do {
25              testDoWhile();
26          }
27          while (condition());
28  
29          // Invalid
30          do testDoWhile(); while (condition()); // violation ''do' construct must use '{}'s'
31      }
32  
33      /** Test while loops **/
34      void testWhile()
35      {
36          // Valid
37          while (condition()) {
38              testWhile();
39          }
40  
41          // Invalid
42          while(condition()); // violation ''while' construct must use '{}'s'
43          while (condition()) // violation ''while' construct must use '{}'s'
44              testWhile();
45          while (condition()) // violation ''while' construct must use '{}'s'
46              if (condition()) // violation ''if' construct must use '{}'s'
47                  testWhile();
48      }
49  
50      /** Test for loops **/
51      void testFor()
52      {
53          // Valid
54          for (int i = 1; i < 5; i++) {
55              testFor();
56          }
57  
58          // Invalid
59          for(int i = 1;i < 5;i++); // violation ''for' construct must use '{}'s'
60          for (int i = 1; i < 5; i++) // violation ''for' construct must use '{}'s'
61              testFor();
62          for (int i = 1; i < 5; // violation ''for' construct must use '{}'s'
63               i++)
64              if (i > 2) // violation ''if' construct must use '{}'s'
65                  testFor();
66      }
67  
68      /** Test if constructs **/
69      public void testIf()
70      {
71          // Valid
72          if (condition()) {
73              testIf();
74          }
75          else if (condition()) {
76              testIf();
77          }
78          else {
79              testIf();
80          }
81  
82          // Invalid
83          if (condition()); // violation ''if' construct must use '{}'s'
84          if (condition()) // violation ''if' construct must use '{}'s'
85              testIf();
86          if (condition()) // violation ''if' construct must use '{}'s'
87              testIf();
88          else // violation ''else' construct must use '{}'s'
89              testIf();
90          if (condition()) // violation ''if' construct must use '{}'s'
91              testIf();
92          else {
93              testIf();
94          }
95          if (condition()) {
96              testIf();
97          }
98          else // violation ''else' construct must use '{}'s'
99              testIf();
100         if (condition()) // violation ''if' construct must use '{}'s'
101             if (condition()) // violation ''if' construct must use '{}'s'
102                 testIf();
103 
104         if (condition()) // violation ''if' construct must use '{}'s'
105             while (condition()) testWhile(); // violation ''while' construct must use '{}'s'
106         if (condition()) // violation ''if' construct must use '{}'s'
107             do testDoWhile(); while (condition()); // violation ''do' construct must use '{}'s'
108         if (condition()) // violation ''if' construct must use '{}'s'
109             for (int i = 0; i < 1; i++) testFor(); // violation ''for' construct must use '{}'s'
110         int a = 0;
111         switch (a) {default: {}}
112     }
113 
114     void whitespaceAfterSemi()
115     {
116         //reject
117         int i = 1;int j = 2;
118 
119         //accept
120         for (;;) {
121         }
122     }
123 
124     /** Empty constructor block. **/
125     public InputNeedBracesTestIt() {}
126 
127     /** Empty method block. **/
128     public void emptyImplementation() {}
129 }