View Javadoc
1   package com.google.checkstyle.test.chapter4formatting.rule43onestatement;
2   
3   /**
4    * Two import statements on the same line are illegal.
5    */
6   import java.io.EOFException; import java.io.BufferedReader; //warn
7   
8   public class InputOneStatementPerLine {
9   
10    /**
11     * Dummy variable to work on.
12     */
13    private int one = 0;
14  
15    /**
16     * Dummy variable to work on.
17     */
18    private int two = 0;
19  
20    /**
21     * Simple legal method.
22     */
23    public void doLegal() {
24      one = 1;
25      two = 2;
26    }
27  
28    /**
29     * The illegal format is used within a String. Therefor the whole method is legal.
30     */
31    public void doLegalString() {
32      one = 1;
33      two = 2;
34      System.identityHashCode("one = 1; two = 2");
35    }
36  
37    /**
38     * Within the for-header there are 3 Statements, but this is legal.
39     */
40    public void doLegalForLoop() {
41      for (int i = 0, j = 0, k = 1; i < 20; i++) { //it's ok.
42        one = i;
43      }
44    }
45  
46    /**
47     * Simplest form of illegal layouts.
48     */
49    public void doIllegal() {
50      one = 1; two = 2; //warn
51      if (one == 1) {
52          one++; two++; //warn
53      }
54      if (one != 1) { one++; } else { one--; } //warn
55      int n = 10;
56  
57      doLegal(); doLegal(); //warn
58      while (one == 1) {one++; two--;} //warn
59  
60    }
61  
62    /**
63     * While theoretically being distributed over two lines, this is a sample
64     * of 2 statements on one line.
65     */
66    public void doIllegal2() {
67      one = 1
68      ; two = 2; //warn
69    }
70  
71    class Inner
72    {
73        /**
74         * Dummy variable to work on.
75         */
76        private int one = 0;
77  
78        /**
79         * Dummy variable to work on.
80         */
81        private int two = 0;
82  
83        /**
84         * Simple legal method.
85         */
86        public void doLegal() {
87          one = 1;
88          two = 2;
89        }
90  
91        /**
92         * Simplest form a an illegal layout.
93         */
94        public void doIllegal() {
95          one = 1; two = 2; //warn
96          if (one == 1) {
97              one++; two++; //warn
98          }
99          if (one != 1) { one++; } else { one--; } //warn
100         int n = 10;
101 
102         doLegal(); doLegal(); //warn
103         while (one == 1) {one++; two--;} //warn
104 
105       }
106   }
107 
108   /**
109    * Two declaration statements on the same line are illegal.
110    */
111   int a; int b; //warn
112 
113   /**
114    * Two declaration statements which are not on the same line
115    * are legal.
116    */
117   int c;
118   int d;
119 
120   /**
121    * Two assignment (declaration) statements on the same line are illegal.
122    */
123   int e = 1; int f = 2; //warn
124 
125   /**
126    * Two assignment (declaration) statements on the different lines
127    * are legal.
128    */
129   int g = 1;
130   int h = 2;
131 
132   /**
133    * This method contains
134    * two object creation statements on the same line.
135    */
136   private void foo() {
137     //Two object creation statements on the same line are illegal.
138     Object obj1 = new Object(); Object obj2 = new Object(); //warn
139   }
140 
141   /**
142    * One multiline  assignment (declaration) statement
143    * is legal.
144    */
145   int i = 1, j = 2,
146       k = 5;
147 
148   /**
149    * One multiline  assignment (declaration) statement
150    * is legal.
151    */
152   int l = 1,
153       m = 2,
154       n = 5;
155 
156   /**
157    * One multiline  assignment (declaration) statement
158    * is legal.
159    */
160   int w = 1,
161       x = 2,
162       y = 5
163           ;
164 
165   /**
166    * Two multiline  assignment (declaration) statements
167    * are illegal.
168    */
169   int o = 1, p = 2,
170       r = 5; int t; //warn
171 
172   /**
173    * Two assignment (declaration) statement
174    * which are not on the same lines and are legal.
175    */
176   int four = 1,
177       five = 5
178           ;
179   int seven = 2;
180 
181   /**
182    * Two statements on the same line
183    * (they both are distributed over two lines)
184    * are illegal.
185    */
186   int var1 = 5,
187       var4 = 5; int var2 = 6,
188       var3 = 5; //warn
189 
190   /**
191    * Two statements on the same line
192    * (they both are distributed over two lines)
193    * are illegal.
194    */
195   int var6 = 5; int var7 = 6,
196       var8 = 5; //warn
197 
198   /**
199    * Two statements on the same line
200    * (they both are distributed over two lines)
201    * are illegal.
202    */
203   private void foo2() {
204     toString(
205 
206     ); toString (
207 
208     ); //warn
209   }
210 
211 
212   /**
213    * While theoretically being distributed over two lines, this is a sample
214    * of 2 statements on one line.
215    */
216   int var9 = 1, var10 = 5
217       ; int var11 = 2; //warn
218 
219 
220   /**
221    * Multiline for loop statement is legal.
222    */
223   private void foo3() {
224     for (int n = 0,
225          k = 1;
226          n < 5; n++,
227              k--) {
228 
229     }
230   }
231 
232   /**
233    * Two multiline statements (which are not on the same line)
234    * are legal.
235    */
236   int var12,
237       var13 = 12;
238   int var14 = 5,
239       var15 = 6;
240 
241   /**
242    * This method contains break and while loop statements.
243    */
244   private void foo4() {
245     do {
246       var9++;
247       if (var10 > 4) {
248         break; //legal
249       }
250       var11++;
251       var9++;
252     } while (var11 < 7); //legal
253 
254     /**
255      * One statement inside for block is legal
256      */
257     for (int i = 0; i < 10; i++) one = 5; //legal
258 
259     /**
260      * One statement inside for block where
261      * increment expression is empty is legal
262      */
263     for (int i = 0; i < 10;) one = 5; //legal
264 
265     /**
266      * One statement inside for block where
267      * increment and conditional expressions are empty
268      * (forever loop) is legal
269      */
270     for (int i = 0;;) one = 5; //legal
271   }
272 
273   public void foo5() {
274     /**
275      * a "forever" loop.
276      */
277     for(;;){} //legal
278   }
279 
280   public void foo6() {
281   /**
282    * One statement inside for block is legal
283    */
284     for (;;) { one = 5; } //legal
285   }
286 
287   /**
288    * One statement inside multiline for loop is legal.
289    */
290   private void foo7() {
291     for(int n = 0,
292             k = 1
293             ; n<5
294             ;
295             n++, k--) { var1++; } //legal
296     }
297 
298   /**
299    * Two statements on the same lne
300    * inside multiline for loop are illegal.
301    */
302   private void foo8() {
303     for(int n = 0,
304             k = 1
305             ; n<5
306             ;
307             n++, k--) { var1++; var2++; } //warn
308     }
309 }