View Javadoc
1   package com.puppycrawl.tools.checkstyle.checks.coding.onestatementperline;
2   
3   /*
4       This class provides test input for OneStatementPerLineCheck with different
5       types of multiline statements.
6       A Java statement is the smallest unit that is a complete instruction.
7       Statements must end with a semicolon.
8       Statements generally contain expressions (expressions have a value).
9       One of the simplest is the Assignment Statement.
10  
11      <variable> = <expression>;
12   */
13  public class InputOneStatementPerLineMultiline {
14  
15      /**
16       * One multiline  assignment (declaration) statement
17       * is legal.
18       */
19      int e = 1, f = 2,
20          g = 5;
21  
22      /**
23       * One multiline  assignment (declaration) statement
24       * is legal.
25       */
26      int h = 1,
27          i = 2,
28          j = 5;
29  
30      /**
31       * One multiline  assignment (declaration) statement
32       * is legal.
33       */
34      int k = 1,
35          l = 2,
36          m = 5
37          ;
38  
39      /**
40       * Two multiline  assignment (declaration) statements
41       * on the same line are illegal.
42       */
43      int o = 1, p = 2,
44          r = 5; int t;
45  
46      /**
47       * Two assignment (declaration) statement
48       * which are not on the same line are legal.
49       */
50      int one = 1,
51          three = 5;
52      int two = 2;
53  
54      /**
55       * Two statements on the same line
56       * (they both are distributed over two lines)
57       * are illegal.
58       */
59      int var1 = 5,
60          var4 = 5; int var2 = 6,
61          var3 = 5;
62  
63      /**
64       * Two statements on the same line
65       * (the second is distributed over two lines)
66       * are illegal.
67       */
68      int var6 = 5; int var7 = 6,
69          var8 = 5;
70  
71      /**
72       * Two statements on the same line
73       * (they both are distributed over two lines)
74       * are illegal.
75       */
76      private void foo() {
77          toString(
78  
79          );toString(
80  
81          );
82      }
83  
84      /**
85       * While theoretically being distributed over three lines, this is a sample
86       * of 2 statements on one line.
87       */
88      int var9 = 1,
89          var10 = 5
90              ; int var11 = 2;
91  
92      /**
93       * Multiline for loop statement is legal.
94       */
95      private void foo2() {
96          for (int n = 0,
97               k = 1;
98               n < 5; n++,
99                   k--) {
100 
101         }
102     }
103 
104     /**
105      * Multiline for loop statement is legal.
106      */
107     private void foo3() {
108         for(int n = 0,
109             k = 1
110             ; n<5
111             ;
112             n++, k--) {}
113     }
114 
115     /**
116      * One statement inside multiline for loop block is legal.
117      */
118     private void foo4() {
119         for(int n = 0,
120             k = 1
121             ; n<5
122             ; ) { int a = 5,
123         b = 2;}
124     }
125 
126     /**
127      * Two statements on the same lne
128      * inside multiline for loop block are illegal.
129      */
130     private void foo5() {
131         for(int n = 0,
132             k = 1
133             ; n<5
134             ;
135             n++, k--) { var1++; var2++; }
136     }
137 
138     /**
139      * Multiple statements within try-with-resource on a separate line is legal.
140      * @see <a href="https://github.com/checkstyle/checkstyle/issues/2211">OneStatementPerLine: false match with try-with-resources</a>
141      */
142     private void issue2211pass() {
143         try(
144                 AutoCloseable i = new java.io.StringReader("");
145                 AutoCloseable k = new java.io.StringReader("");
146         ) {
147         } catch (Exception e1) {
148         }
149     }
150 
151     /**
152      * Multiple statements within try-with-resource on a separate line is legal. Added per PR comment:
153      * @see <a href="https://github.com/checkstyle/checkstyle/pull/2750#issuecomment-166032327">Please add UT cases</a>
154      */
155     private void issue2211pass2() {
156         try( AutoCloseable i = new java.io.StringReader("");
157             AutoCloseable k = new java.io.StringReader("");) {
158         } catch (Exception e1) {
159         }
160     }
161 
162     /**
163      * Multiple statements within try-with-resource on next line after try is illegal.
164      * @see <a href="https://github.com/checkstyle/checkstyle/issues/2211">OneStatementPerLine: false match with try-with-resources</a>
165      */
166     private void issue2211fail() {
167         try(
168                 AutoCloseable i = new java.io.StringReader("");AutoCloseable k = new java.io.StringReader("");
169         ) {
170         } catch (Exception e1) {
171         }
172     }
173 
174     /**
175      * Multiple statements within try-with-resource on a same line as try is illegal.  Added per PR comment:
176      * @see <a href="https://github.com/checkstyle/checkstyle/pull/2750#issuecomment-166032327">Please add UT cases</a>
177      */
178     private void issue2211fail2() {
179         try( AutoCloseable i = new java.io.StringReader("");AutoCloseable k = new java.io.StringReader("");) {
180         } catch (Exception e1) {
181         }
182     }
183 }