View Javadoc
1   package com.google.checkstyle.test.chapter4formatting.rule43onestatement;
2   
3   // Two import statements on the same line are illegal.
4   
5   /** Some javadoc. */
6   public class InputFormattedOneStatementPerLine {
7   
8     /** Dummy variable to work on. */
9     private int one = 0;
10  
11    /** Dummy variable to work on. */
12    private int two = 0;
13  
14    /** Simple legal method. */
15    public void doLegal() {
16      one = 1;
17      two = 2;
18    }
19  
20    /** The illegal format is used within a String. Therefor the whole method is legal. */
21    public void doLegalString() {
22      one = 1;
23      two = 2;
24      System.identityHashCode("one = 1; two = 2");
25    }
26  
27    /** Within the for-header there are 3 Statements, but this is legal. */
28    public void doLegalForLoop() {
29      for (int i = 0, j = 0, k = 1; i < 20; i++) { // it's ok.
30        one = i;
31      }
32    }
33  
34    /** Simplest form of illegal layouts. */
35    public void doIllegal() {
36      one = 1;
37      two = 2;
38      if (one == 1) {
39        one++;
40        two++;
41      }
42      if (one != 1) {
43        one++;
44      } else {
45        one--;
46      }
47      int n = 10;
48  
49      doLegal();
50      doLegal();
51      while (one == 1) {
52        one++;
53        two--;
54      }
55    }
56  
57    /**
58     * While theoretically being distributed over two lines, this is a sample of 2 statements on one
59     * line.
60     */
61    public void doIllegal2() {
62      one = 1;
63      two = 2;
64    }
65  
66    class Inner {
67      /** Dummy variable to work on. */
68      private int one = 0;
69  
70      /** Dummy variable to work on. */
71      private int two = 0;
72  
73      /** Simple legal method. */
74      public void doLegal() {
75        one = 1;
76        two = 2;
77      }
78  
79      /** Simplest form a an illegal layout. */
80      public void doIllegal() {
81        one = 1;
82        two = 2;
83        if (one == 1) {
84          one++;
85          two++;
86        }
87        if (one != 1) {
88          one++;
89        } else {
90          one--;
91        }
92        int n = 10;
93  
94        doLegal();
95        doLegal();
96        while (one == 1) {
97          one++;
98          two--;
99        }
100     }
101   }
102 
103   /** Two declaration statements on the same line are illegal. */
104   int aaaa;
105 
106   int bbb;
107 
108   /** Two declaration statements which are not on the same line are legal. */
109   int ccc;
110 
111   int dddd;
112 
113   /** Two assignment (declaration) statements on the same line are illegal. */
114   int dog = 1;
115 
116   int cow = 2;
117 
118   /** Two assignment (declaration) statements on the different lines are legal. */
119   int test1 = 1;
120 
121   int test2 = 2;
122 
123   /** This method contains two object creation statements on the same line. */
124   private void foo() {
125     // Two object creation statements on the same line are illegal.
126     Object obj1 = new Object();
127     Object obj2 = new Object();
128   }
129 
130   int blah;
131 
132   int seven = 2;
133 
134   /** Two statements on the same line (they both are distributed over two lines) are illegal. */
135   int var6 = 5;
136 
137   /** Two statements on the same line (they both are distributed over two lines) are illegal. */
138   private void foo2() {
139     toString();
140     toString();
141   }
142 
143   int var11 = 2;
144 
145   /** Multiline for loop statement is legal. */
146   private void foo3() {
147     for (int n = 0, k = 1; n < 5; n++, k--) {}
148   }
149 
150   /** This method contains break and while loop statements. */
151   private void foo4() {
152     int var9 = 0;
153     int var10 = 0;
154 
155     do {
156       var9++;
157       if (var10 > 4) {
158         break; // legal
159       }
160       var11++;
161       var9++;
162     } while (var11 < 7); // legal
163 
164     /*
165      * One statement inside for block is legal
166      */
167     for (int i = 0; i < 10; i++) {
168       one = 5;
169     } // legal
170 
171     /*
172      * One statement inside for block where
173      * increment expression is empty is legal
174      */
175     for (int i = 0; i < 10; ) {
176       one = 5;
177     } // legal
178 
179     /*
180      One statement inside for block where
181      increment and conditional expressions are empty
182      (forever loop) is legal
183     */
184     for (int i = 0; ; ) {
185       one = 5;
186     } // legal
187   }
188 
189   /** Some javadoc. */
190   public void foo5() {
191     // a "forever" loop.
192     for (; ; ) {} // legal
193   }
194 
195   /** Some javadoc. */
196   public void foo6() {
197     // One statement inside for block is legal
198     for (; ; ) {
199       one = 5;
200     } // legal
201   }
202 
203   int var1 = 0;
204   int var2 = 0;
205 
206   /** One statement inside multiline for loop is legal. */
207   private void foo7() {
208     for (int n = 0, k = 1; n < 5; n++, k--) {
209       var1++;
210     } // legal
211   }
212 
213   /** Two statements on the same lne inside multiline for loop are illegal. */
214   private void foo8() {
215     for (int n = 0, k = 1; n < 5; n++, k--) {
216       var1++;
217       var2++;
218     }
219   }
220 }