View Javadoc
1   /*
2   NeedBraces
3   allowSingleLineStatement = true
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  public class InputNeedBracesSingleLineStatements
13  {
14      private static class SomeClass {
15          boolean flag = true;
16          private static boolean test(boolean k) {
17              return k;
18          }
19      }
20  
21      private int foo() {
22          if (SomeClass.test(true) == true) return 4; //No warning if 'mAllowSingleLineIf' is true
23          return 0;
24      }
25  
26      private int foo1() {
27          if (SomeClass.test(true)) return 4; int k = 3; //No warning if 'mAllowSingleLineIf' is true
28          return 0;
29      }
30  
31      private int foo2() {
32          if (SomeClass.test(true) == true) // violation ''if' construct must use '{}'s'
33              return 4;
34          return 0;
35      }
36  
37      private int foo3() {
38          // violation below ''if' construct must use '{}'s'
39          if (SomeClass.test(true) == true) if (true) return 4;
40          return 0;
41      }
42  
43      private void foo(Object o) {
44          if (o != null) this.notify();
45      }
46  
47      private void foo2(Object o) {
48          if (o != null) // violation ''if' construct must use '{}'s'
49              this.notify();
50      }
51  
52      private void loopTest(Object o) {
53          while (o != null) {
54              this.notify();
55          }
56          while (o != null) // violation ''while' construct must use '{}'s'
57              this.notify();
58          while (o != null) this.notify();
59          do {
60              this.notify();
61          } while (o != null);
62          do this.notify(); while (o != null);
63          do // violation ''do' construct must use '{}'s'
64              this.notify();
65          while (o != null);
66          for (;;) // violation ''for' construct must use '{}'s'
67              break;
68          for (;;) break;
69          for (int i = 0; i < 10; i++) {
70               this.notify();
71          }
72          for (int i = 0; i < 10; i++) // violation ''for' construct must use '{}'s'
73               this.notify();
74          for (int i = 0; ; ) this.notify();
75      }
76  
77      private int getSmth(int num)
78      {
79          int counter = 0;
80          switch (num) {
81              case 1: counter++; break;
82              case 2:
83                  counter += 2;
84                  break;
85              case 3:
86                  counter += 3;
87                  break;
88              case 6: counter += 10; break;
89              default: counter = 100; break;
90          }
91          return counter;
92      }
93  
94      private void testElse(int k) {
95          if (k == 4) System.identityHashCode("yes");
96          else System.identityHashCode("no");
97          for (;;);
98      }
99  
100     private int testMissingWarnings() {
101         if (true) // violation ''if' construct must use '{}'s'
102             throw new RuntimeException();
103         if (true) {
104             return 1;
105         } else // violation ''else' construct must use '{}'s'
106             return 2;
107     }
108 
109     void enhancedForLoop(int[] array) {
110         for (int value: array) return;
111     }
112 
113     int[] sourceLocators;
114 
115     private class StateInfo {
116         public boolean isInitial() {
117             // violation below ''if' construct must use '{}'s'
118             for (int locator: sourceLocators) if (locator != 0) return false;
119             return true;
120         }
121     }
122 
123     private void forEachLoop() {
124         for (String s: new String[]{""}) break;
125         for (String s: new String[]{""}) // violation ''for' construct must use '{}'s'
126             break;
127         for (;;)
128         ;
129     }
130     private void method(){
131         if(false) {
132             switch (0) {
133                 case -1:
134                     return;
135                 default:
136                     return;
137             }
138         }
139         switch(1){
140             case 1: return;
141             default: throw new RuntimeException("");
142         }
143     }
144 }