1 //////////////////////////////////////////////////////////////////////////////// 2 //checkstyle: Checks Java source code for adherence to a set of rules. 3 //Copyright (C) 2001-2004 Oliver Burn 4 // 5 //This library is free software; you can redistribute it and/or 6 //modify it under the terms of the GNU Lesser General Public 7 //License as published by the Free Software Foundation; either 8 //version 2.1 of the License, or (at your option) any later version. 9 // 10 //This library is distributed in the hope that it will be useful, 11 //but WITHOUT ANY WARRANTY; without even the implied warranty of 12 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 //Lesser General Public License for more details. 14 // 15 //You should have received a copy of the GNU Lesser General Public 16 //License along with this library; if not, write to the Free Software 17 //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 //////////////////////////////////////////////////////////////////////////////// 19 package com.puppycrawl.tools.checkstyle.checks.coding.onestatementperline; 20 21 /** 22 * Two import statements on the same line are illegal. 23 */ 24 import java.io.EOFException; import java.io.BufferedReader; 25 26 /** 27 * This Class contains no logic, but serves as test-input for the unit tests for the 28 * <code>OneStatementPerLineCheck</code>-checkstyle enhancement. 29 * @author Alexander Jesse 30 * @see com.puppycrawl.tools.checkstyle.checks.coding.OneStatementPerLineCheck 31 */ 32 public class InputOneStatementPerLineSingleLine { 33 /** 34 * Dummy innerclass to test the behaviour in the case of a smalltalk-style 35 * statements (<code>myObject.firstMethod().secondMethod().thirdMethod()</code>). 36 * For this programming style each method must return the object itself <code>this</code>. 37 */ 38 class SmallTalkStyle { 39 SmallTalkStyle doSomething1() { 40 return this; 41 } 42 43 SmallTalkStyle doSomething2() { 44 return this; 45 } 46 47 SmallTalkStyle doSomething3() { 48 return this; 49 } 50 } 51 52 /** 53 * Dummy variable to work on. 54 */ 55 private int one = 0; 56 57 /** 58 * Dummy variable to work on. 59 */ 60 private int two = 0; 61 62 /** 63 * Simple legal method 64 */ 65 public void doLegal() { 66 one = 1; 67 two = 2; 68 } 69 70 /** 71 * The illegal format is used in a comment. Therefor the whole method is legal. 72 */ 73 public void doLegalComment() { 74 one = 1; 75 //one = 1; two = 2; 76 two = 2; 77 /* 78 * one = 1; two = 2; 79 */ 80 } 81 82 /** 83 * The illegal format is used within a String. Therefor the whole method is legal. 84 */ 85 public void doLegalString() { 86 one = 1; 87 two = 2; 88 System.identityHashCode("one = 1; two = 2"); 89 } 90 91 /** 92 * Within the for-header there are 3 Statements, but this is legal. 93 */ 94 public void doLegalForLoop() { 95 for (int i = 0; i < 20; i++) { 96 one = i; 97 } 98 } 99 100 /** 101 * Simplest form of an illegal layout. 102 */ 103 public void doIllegal() { 104 one = 1; two = 2; 105 } 106 107 /** 108 * Smalltalk-style is considered as one statement. 109 */ 110 public void doIllegalSmallTalk() { 111 SmallTalkStyle smalltalker = new SmallTalkStyle(); 112 smalltalker.doSomething1().doSomething2().doSomething3(); 113 } 114 115 /** 116 * Smalltalk-style is considered as one statement. 117 */ 118 public void doIllegalSmallTalk2() { 119 SmallTalkStyle smalltalker = new SmallTalkStyle(); 120 smalltalker.doSomething1() 121 .doSomething2() 122 .doSomething3(); 123 } 124 125 /** 126 * While theoretically being distributed over two lines, this is a sample 127 * of 2 statements on one line. 128 */ 129 public void doIllegal2() { 130 one = 1 131 ; two = 2; 132 } 133 134 /** 135 * The StringBuffer is a Java-API-class that permits smalltalk-style concatenation 136 * on the <code>append</code>-method. 137 */ 138 public void doStringBuffer() { 139 StringBuffer sb = new StringBuffer(); 140 sb.append("test "); 141 sb.append("test2 ").append("test3 "); 142 appendToSpringBuffer(sb, "test4"); 143 } 144 145 /** 146 * indirect stringbuffer-method. Used only internally. 147 * @param sb The stringbuffer we want to append something 148 * @param text The text to append 149 */ 150 private void appendToSpringBuffer(StringBuffer sb, String text) { 151 sb.append(text); 152 } 153 154 /** 155 * Two declaration statements on the same line are illegal. 156 */ 157 int a; int b; 158 159 /** 160 * Two declaration statements which are not on the same line 161 * are legal. 162 */ 163 int c; 164 int d; 165 166 /** 167 * Two assignment (declaration) statements on the same line are illegal. 168 */ 169 int e = 1; int f = 2; 170 171 /** 172 * Two assignment (declaration) statements on the different lines 173 * are legal. 174 */ 175 int g = 1; 176 int h = 2; 177 178 /** 179 * This method contains two increment statements 180 * and two object creation statements on the same line. 181 */ 182 private void foo() { 183 //This is two assignment (declaration) 184 //statements on different lines 185 int var1 = 1; 186 int var2 = 2; 187 188 //Two increment statements on the same line are illegal. 189 var1++; var2++; 190 191 //Two object creation statements on the same line are illegal. 192 Object obj1 = new Object(); Object obj2 = new Object(); 193 } 194 195 /** 196 * This method contains break, while-loop 197 * and for-loop statements. 198 */ 199 private void foo3() { 200 do { 201 one++; 202 if (two > 4) { 203 break; //legal 204 } 205 one++; 206 two++; 207 } while (two < 7); //legal 208 209 /** 210 * One statement inside for block is legal. 211 */ 212 for (int i = 0; i < 10; i++) one = 5; 213 214 /** 215 * One statement inside for block where 216 * increment expression is empty is legal. 217 */ 218 for (int i = 0; i < 10;) one = 5; 219 220 /** 221 * One statement inside for block where 222 * increment and conditional expressions are empty 223 * (forever loop) is legal 224 */ 225 for (int i = 0;;) one = 5; 226 } 227 228 public void foo4() { 229 /** 230 * a "forever" loop. 231 */ 232 for(;;){} //legal 233 } 234 235 public void foo5() { 236 /** 237 * One statement inside for block is legal 238 */ 239 for (;;) { one = 5; } 240 } 241 242 public void foo6() { 243 bar(() -> { 244 return;}, () -> {return;}); 245 } 246 247 void bar(Runnable r1, Runnable r2) { } 248 }