1 package com.google.checkstyle.test.chapter4formatting.rule43onestatement;
2
3 // Two import statements on the same line are illegal.
4 import java.io.BufferedReader; import java.io.EOFException;
5 // violation above 'Only one statement per line allowed.'
6
7 /** Some javadoc. */
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; // violation 'Only one statement per line allowed.'
51 if (one == 1) {
52 one++; two++; // violation 'Only one statement per line allowed.'
53 }
54 if (one != 1) { one++; } else { one--; }
55 // 3 violations above:
56 // ''{' at column 19 should have line break after.'
57 // ''{' at column 35 should have line break after.'
58 // 'Only one statement per line allowed.'
59 int n = 10;
60
61 doLegal(); doLegal(); // violation 'Only one statement per line allowed.'
62 while (one == 1) { one++; two--; }
63 // 3 violations above:
64 // ''{' at column 22 should have line break after.'
65 // 'Only one statement per line allowed.'
66 // ''}' at column 38 should be alone on a line.'
67
68 }
69
70 /**
71 * While theoretically being distributed over two lines, this is a sample
72 * of 2 statements on one line.
73 */
74 public void doIllegal2() {
75 one = 1
76 ; two = 2; // violation 'Only one statement per line allowed.'
77 }
78
79 class Inner {
80 /**
81 * Dummy variable to work on.
82 */
83 private int one = 0;
84
85 /**
86 * Dummy variable to work on.
87 */
88 private int two = 0;
89
90 /**
91 * Simple legal method.
92 */
93 public void doLegal() {
94 one = 1;
95 two = 2;
96 }
97
98 /**
99 * Simplest form a an illegal layout.
100 */
101 public void doIllegal() {
102 one = 1; two = 2; // violation 'Only one statement per line allowed.'
103 if (one == 1) {
104 one++; two++; // violation 'Only one statement per line allowed.'
105 }
106 if (one != 1) { one++; } else { one--; }
107 // 3 violations above:
108 // ''{' at column 21 should have line break after.'
109 // ''{' at column 37 should have line break after.'
110 // 'Only one statement per line allowed.'
111 int n = 10;
112
113 doLegal(); doLegal(); // violation 'Only one statement per line allowed.'
114 while (one == 1) {
115 one++; two--; // violation 'Only one statement per line allowed.'
116 }
117
118 }
119 }
120
121 /**
122 * Two declaration statements on the same line are illegal.
123 */
124 int aaaa; int bbb;
125 // 2 violations above:
126 // 'Only one variable definition per line allowed.'
127 // 'Only one statement per line allowed.'
128
129 /**
130 * Two declaration statements which are not on the same line
131 * are legal.
132 */
133 int ccc;
134 int dddd;
135
136 /**
137 * Two assignment (declaration) statements on the same line are illegal.
138 */
139 int dog = 1; int cow = 2;
140 // 2 violations above:
141 // 'Only one variable definition per line allowed.'
142 // 'Only one statement per line allowed.'
143
144 /**
145 * Two assignment (declaration) statements on the different lines
146 * are legal.
147 */
148 int test1 = 1;
149 int test2 = 2;
150
151 /**
152 * This method contains
153 * two object creation statements on the same line.
154 */
155 private void foo() {
156 //Two object creation statements on the same line are illegal.
157 Object obj1 = new Object(); Object obj2 = new Object();
158 // 2 violations above:
159 // 'Only one variable definition per line allowed.'
160 // 'Only one statement per line allowed.'
161 }
162
163 /**
164 * One multiline assignment (declaration) statement
165 * is legal.
166 */
167 int ijk = 1, jkl = 2, // violation 'Each variable declaration must be in its own statement.'
168 klm = 5;
169
170 /**
171 * One multiline assignment (declaration) statement
172 * is legal.
173 */
174 int lmn = 1, // violation 'Each variable declaration must be in its own statement.'
175 mnp = 2,
176 npq = 5;
177
178 /**
179 * One multiline assignment (declaration) statement
180 * is legal.
181 */
182 int wxy = 1, // violation 'Each variable declaration must be in its own statement.'
183 xyzz = 2,
184 yys = 5
185 ;
186
187 /**
188 * Two multiline assignment (declaration) statements
189 * are illegal.
190 */
191 // 2 violations 3 lines below:
192 // 'Each variable declaration must be in its own statement.'
193 // 'Only one variable definition per line allowed.'
194 int abc = 1, pqr = 2,
195 xyz = 5; int blah; // violation 'Only one statement per line allowed.'
196
197 /**
198 * Two assignment (declaration) statement
199 * which are not on the same lines and are legal.
200 */
201 int four = 1, // violation 'Each variable declaration must be in its own statement.'
202 five = 5
203 ;
204 int seven = 2;
205
206 /**
207 * Two statements on the same line
208 * (they both are distributed over two lines)
209 * are illegal.
210 */
211 // 2 violations 3 lines below:
212 // 'Each variable declaration must be in its own statement.'
213 // 'Only one variable definition per line allowed.'
214 int var1 = 5,
215 var4 = 5; int var2 = 6, // violation 'Each variable declaration must be in its own statement.'
216 var3 = 5; // violation 'Only one statement per line allowed.'
217
218 /**
219 * Two statements on the same line
220 * (they both are distributed over two lines)
221 * are illegal.
222 */
223 // 2 violations 3 lines below:
224 // 'Only one variable definition per line allowed.'
225 // 'Each variable declaration must be in its own statement.'
226 int var6 = 5; int var7 = 6,
227 var8 = 5; // violation 'Only one statement per line allowed.'
228
229 /**
230 * Two statements on the same line
231 * (they both are distributed over two lines)
232 * are illegal.
233 */
234 private void foo2() {
235 toString(
236
237 ); toString(// violation ''method call' .* incorrect indentation level 4, expected .* 6.'
238
239 );
240 // 2 violations above:
241 // ''method call rparen' has incorrect indentation level 8, expected level should be 4.'
242 // 'Only one statement per line allowed.'
243 }
244
245
246 /**
247 * While theoretically being distributed over two lines, this is a sample
248 * of 2 statements on one line.
249 */
250 int var9 = 1, var10 = 5 // violation 'Each variable declaration must be in its own statement.'
251 ; int var11 = 2; // violation 'Only one statement per line allowed.'
252
253
254 /**
255 * Multiline for loop statement is legal.
256 */
257 private void foo3() {
258 for (int n = 0,
259 k = 1;
260 n < 5; n++,
261 k--) {
262
263 }
264 }
265
266 /**
267 * Two multiline statements (which are not on the same line)
268 * are legal.
269 * Violations are not related to "OneStatementPerLine", but to "MultipleVariableDeclarations".
270 */
271 int var12, // violation 'Each variable declaration must be in its own statement.'
272 var13 = 12;
273 int var14 = 5, // violation 'Each variable declaration must be in its own statement.'
274 var15 = 6;
275
276 /**
277 * This method contains break and while loop statements.
278 */
279 private void foo4() {
280 do {
281 var9++;
282 if (var10 > 4) {
283 break; //legal
284 }
285 var11++;
286 var9++;
287 } while (var11 < 7); //legal
288
289 /*
290 * One statement inside for block is legal
291 */
292 for (int i = 0; i < 10; i++) { one = 5; } //legal
293 // 2 violations above:
294 // ''{' at column 34 should have line break after.'
295 // ''}' at column 45 should be alone on a line.'
296
297 /*
298 * One statement inside for block where
299 * increment expression is empty is legal
300 */
301 for (int i = 0; i < 10;) { one = 5; } //legal
302 // 2 violations above:
303 // ''{' at column 30 should have line break after.'
304 // ''}' at column 41 should be alone on a line.'
305
306 /*
307 One statement inside for block where
308 increment and conditional expressions are empty
309 (forever loop) is legal
310 */
311 for (int i = 0;;) { one = 5; } //legal
312 // 2 violations above:
313 // ''{' at column 23 should have line break after.'
314 // ''}' at column 34 should be alone on a line.'
315 }
316
317 /** Some javadoc. */
318 public void foo5() {
319 // a "forever" loop.
320 for (;;){} //legal
321 }
322
323 /** Some javadoc. */
324 public void foo6() {
325 // One statement inside for block is legal
326 for (;;) {
327 one = 5;
328 } //legal
329 }
330
331 /**
332 * One statement inside multiline for loop is legal.
333 */
334 private void foo7() {
335 for (int n = 0,
336 k = 1
337 ; n < 5
338 ;
339 n++, k--) {
340 var1++;
341 } //legal
342 }
343
344 /**
345 * Two statements on the same lne
346 * inside multiline for loop are illegal.
347 */
348 private void foo8() {
349 for (int n = 0,
350 k = 1
351 ; n < 5
352 ;
353 n++, k--) {
354 var1++; var2++; // violation 'Only one statement per line allowed.'
355 }
356 }
357 }