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
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
43          while (condition()) // violation
44              testWhile();
45          while (condition()) // violation
46              if (condition()) // violation
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
60          for (int i = 1; i < 5; i++) // violation
61              testFor();
62          for (int i = 1; i < 5; // violation
63               i++)
64              if (i > 2) // violation
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
84          if (condition()) // violation
85              testIf();
86          if (condition()) // violation
87              testIf();
88          else // violation
89              testIf();
90          if (condition()) // violation
91              testIf();
92          else {
93              testIf();
94          }
95          if (condition()) {
96              testIf();
97          }
98          else // violation
99              testIf();
100         if (condition()) // violation
101             if (condition()) // violation
102                 testIf();
103 
104         if (condition()) // violation
105             while (condition()) testWhile(); // violation
106         if (condition()) // violation
107             do testDoWhile(); while (condition()); // violation
108         if (condition()) // violation
109             for (int i = 0; i < 1; i++) testFor(); // violation
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 }