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