1 package com.google.checkstyle.test.chapter4formatting.rule462horizontalwhitespace; 2 3 /* 4 * Bug 806242 (NoWhitespaceBeforeCheck violation with an interface). 5 * 6 * @author o_sukhodolsky 7 * @version 1.0 8 */ 9 10 /** Class for testing whitespace issues. violation missing author tag. */ 11 class InputWhitespaceAroundBasic { 12 private final int var1= 1; // violation ''=' is not preceded with whitespace.' 13 private final int var2 =1; // violation ''=' is not followed by whitespace.' 14 15 /** Should be ok. */ 16 private final int var3 = 1; 17 18 /** skip blank lines between comment and code, should be ok. */ 19 private final int var4 = 1; 20 21 int xyz; // multiple space between content and double slash. 22 int abc; // multiple space between double slash and comment's text. 23 int pqr; // testing both. 24 25 /** bug 806243 (NoWhitespaceBeforeCheck violation for anonymous inner class). */ 26 private int test; 27 28 private int i4, i5, i6; 29 // violation above 'Each variable declaration must be in its own statement.' 30 31 /** method. */ 32 void method1() { 33 final int a = 1; 34 int b= 1; // violation ''=' is not preceded with whitespace.' 35 b= 1; // violation ''=' is not preceded with whitespace.' 36 b +=1; // violation ''\+=' is not followed by whitespace.' 37 b -=- 1 + (+ b); // violation ''-=' is not followed by whitespace.' 38 b = b++ + b--; // ok 39 b = ++ b - -- b; // ok 40 } 41 42 /** method. */ 43 void method2() { 44 synchronized(this) { 45 // 2 violations above: 46 // ''synchronized' is not followed by whitespace.' 47 // ''synchronized' is not followed by whitespace.' 48 } 49 try{ 50 // 3 violations above: 51 // ''try' is not followed by whitespace.' 52 // ''try' is not followed by whitespace.' 53 // ''{' is not preceded with whitespace.' 54 } catch (RuntimeException e) {// violation ''{' is not followed by whitespace.' 55 } 56 } 57 58 /** test WS after void return. */ 59 private void fastExit() { 60 boolean complicatedStuffNeeded = true; 61 // 2 violations 3 lines below: 62 // ''if' is not followed by whitespace.' 63 // ''if' is not followed by whitespace.' 64 if(!complicatedStuffNeeded) { 65 // should not complain about missing WS after return 66 } else { 67 // do complicated stuff 68 } 69 } 70 71 /** 72 * test WS after non void return. 73 * 74 * @return 2 75 */ 76 private int nonVoid() { 77 if (true) { 78 return(2); 79 // 2 violations above: 80 // ''return' is not followed by whitespace.' 81 // ''return' is not followed by whitespace.' 82 } else { 83 return 2; // this is ok 84 } 85 } 86 87 /** test casts. */ 88 private void testCasts() { 89 Object o = (Object) new Object(); // ok 90 o = (Object) o; // ok 91 o = ( Object ) o; // ok 92 o = (Object) 93 o; // ok 94 } 95 96 /** test questions. */ 97 private void testQuestions() { 98 99 boolean b = (1 ==2) ? false : true; // violation ''==' is not followed by whitespace.' 100 } 101 102 /** star test. */ 103 private void starTest() { 104 int x = 2 * 3* 4; // violation ''*' is not preceded with whitespace.' 105 } 106 107 /** boolean test. */ 108 private void boolTest() { 109 boolean a = true; 110 boolean x = !a; 111 int z = ~1 + ~2; 112 } 113 114 /** division test. */ 115 private void divTest() { 116 int a = 4 % 2; 117 int b = 4% 2; // violation ''%' is not preceded with whitespace.' 118 int c = 4 %2; // violation ''%' is not followed by whitespace.' 119 int d = 4% 2; // violation ''%' is not preceded with whitespace.' 120 int e = 4 / 2; 121 int f = 4/ 2; // violation ''/' is not preceded with whitespace.' 122 int g = 4 /2; // violation ''/' is not followed by whitespace.' 123 } 124 125 /** 126 * summary. 127 * 128 * @return dot test * 129 */ 130 private java.lang.String dotTest() { 131 Object o = new java.lang.Object(); 132 o.toString(); 133 o.toString(); 134 o.toString(); 135 return o.toString(); 136 } 137 138 /** assert statement test. */ 139 public void assertTest() { 140 // OK 141 assert true; 142 143 // OK 144 assert true : "Whups"; 145 146 // evil colons, should be OK 147 assert "OK".equals(null) ? false : true : "Whups"; 148 149 // missing WS around assert 150 assert(true); // violation ''assert' is not followed by whitespace.' 151 152 // missing WS around colon 153 assert true: "Whups"; // violation '':' is not preceded with whitespace.' 154 } 155 156 /** another check. */ 157 void donBradman(Runnable run) { 158 donBradman( 159 new Runnable() { 160 public void run() {} 161 }); 162 163 final Runnable r = 164 new Runnable() { 165 public void run() {} 166 }; 167 } 168 169 /** rfe 521323, detect whitespace before ';'. */ 170 void rfe521323() { 171 doStuff(); 172 for (int i = 0; i < 5; i++) {} 173 } 174 175 /** bug 806243 (NoWhitespaceBeforeCheck violation for anonymous inner class). */ 176 void bug806243() { 177 Object o = 178 new InputWhitespaceAroundBasic() { 179 private int test; 180 }; 181 } 182 183 void doStuff() {} 184 185 interface Foo { 186 void foo(); 187 } 188 189 /** 190 * Avoid Whitespace violations in for loop. 191 * 192 * @author lkuehne 193 * @version 1.0 194 */ 195 class SpecialCasesInForLoop { 196 void forIterator() { 197 // avoid conflict between WhiteSpaceAfter ';' and ParenPad(nospace) 198 for (int i = 0; i++ < 5; ) { 199 // ^ no whitespace 200 } 201 202 // bug 895072 203 // avoid conflict between ParenPad(space) and NoWhiteSpace before ';' 204 int i = 0; 205 for (; i < 5; i++) { 206 // ^ whitespace 207 } 208 for (int anInt : getSomeInts()) { 209 // Should be ignored 210 } 211 } 212 213 int[] getSomeInts() { 214 int i = 2 / 3; 215 return null; 216 } 217 218 void forColon() { 219 int[] ll = new int[10]; 220 for (int x:ll) {} 221 // 2 violations above: 222 // '':' is not followed by whitespace.' 223 // '':' is not preceded with whitespace.' 224 for (int x :ll) {} // violation '':' is not followed by whitespace.' 225 for (int x: ll) {} // violation '':' is not preceded with whitespace.' 226 for (int x : ll) {} // ok 227 } 228 } 229 230 /** Operators mentioned in Google Coding Standards 2016-07-12. */ 231 class NewGoogleOperators { 232 NewGoogleOperators() { 233 Runnable l; 234 235 l = ()-> {}; // violation ''->' is not preceded with whitespace.' 236 l = () ->{}; 237 // 2 violations above: 238 // ''->' is not followed by whitespace.' 239 // ''->' is not followed by whitespace.' 240 l = () -> {}; // ok 241 l = () -> {}; // ok 242 243 java.util.Arrays.sort(null, String::compareToIgnoreCase); 244 java.util.Arrays.sort(null, String::compareToIgnoreCase); 245 246 new Object().toString(); 247 new Object().toString(); 248 } 249 } 250 }