1 package com.puppycrawl.tools.checkstyle.checks.whitespace; 20 21 import com.puppycrawl.tools.checkstyle.api.Check; 22 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 23 import com.puppycrawl.tools.checkstyle.api.DetailAST; 24 25 122 public class WhitespaceAroundCheck extends Check 123 { 124 125 private boolean mAllowEmptyCtors; 126 127 private boolean mAllowEmptyMethods; 128 129 130 public int[] getDefaultTokens() 131 { 132 return new int[] { 133 TokenTypes.ASSIGN, 134 TokenTypes.BAND, 135 TokenTypes.BAND_ASSIGN, 136 TokenTypes.BOR, 137 TokenTypes.BOR_ASSIGN, 138 TokenTypes.BSR, 139 TokenTypes.BSR_ASSIGN, 140 TokenTypes.BXOR, 141 TokenTypes.BXOR_ASSIGN, 142 TokenTypes.COLON, 143 TokenTypes.DIV, 144 TokenTypes.DIV_ASSIGN, 145 TokenTypes.EQUAL, 146 TokenTypes.GE, 147 TokenTypes.GT, 148 TokenTypes.LAND, 149 TokenTypes.LCURLY, 150 TokenTypes.LE, 151 TokenTypes.LITERAL_CATCH, 152 TokenTypes.LITERAL_DO, 153 TokenTypes.LITERAL_ELSE, 154 TokenTypes.LITERAL_FINALLY, 155 TokenTypes.LITERAL_FOR, 156 TokenTypes.LITERAL_IF, 157 TokenTypes.LITERAL_RETURN, 158 TokenTypes.LITERAL_SYNCHRONIZED, 159 TokenTypes.LITERAL_TRY, 160 TokenTypes.LITERAL_WHILE, 161 TokenTypes.LOR, 162 TokenTypes.LT, 163 TokenTypes.MINUS, 164 TokenTypes.MINUS_ASSIGN, 165 TokenTypes.MOD, 166 TokenTypes.MOD_ASSIGN, 167 TokenTypes.NOT_EQUAL, 168 TokenTypes.PLUS, 169 TokenTypes.PLUS_ASSIGN, 170 TokenTypes.QUESTION, 171 TokenTypes.RCURLY, 172 TokenTypes.SL, 173 TokenTypes.SLIST, 174 TokenTypes.SL_ASSIGN, 175 TokenTypes.SR, 176 TokenTypes.SR_ASSIGN, 177 TokenTypes.STAR, 178 TokenTypes.STAR_ASSIGN, 179 TokenTypes.LITERAL_ASSERT, 180 TokenTypes.GENERIC_START, 181 TokenTypes.GENERIC_END, 182 TokenTypes.TYPE_EXTENSION_AND, 183 TokenTypes.WILDCARD_TYPE, 184 }; 185 } 186 187 191 public void setAllowEmptyMethods(boolean aAllow) 192 { 193 mAllowEmptyMethods = aAllow; 194 } 195 196 200 public void setAllowEmptyConstructors(boolean aAllow) 201 { 202 mAllowEmptyCtors = aAllow; 203 } 204 205 206 public void visitToken(DetailAST aAST) 207 { 208 final int type = aAST.getType(); 209 final int parentType = aAST.getParent().getType(); 210 211 if (((type == TokenTypes.RCURLY) || (type == TokenTypes.LCURLY)) 213 && (parentType == TokenTypes.ARRAY_INIT)) 214 { 215 return; 216 } 217 218 if ((type == TokenTypes.STAR) 220 && (parentType == TokenTypes.DOT)) 221 { 222 return; 223 } 224 225 if ((type == TokenTypes.SLIST) 227 && (parentType == TokenTypes.CASE_GROUP)) 228 { 229 return; 230 } 231 232 if ((type == TokenTypes.COLON) 234 && ((parentType == TokenTypes.LITERAL_DEFAULT) 235 || (parentType == TokenTypes.LITERAL_CASE))) 236 { 237 return; 238 } 239 240 if (emptyMethodBlockCheck(aAST, parentType) 242 || emptyCtorBlockCheck(aAST, parentType)) 243 { 244 return; 245 } 246 247 final String [] lines = getLines(); 248 final String line = lines[aAST.getLineNo() - 1]; 249 final int before = aAST.getColumnNo() - 1; 250 final int after = aAST.getColumnNo() + aAST.getText().length(); 251 252 if ((before >= 0) && !Character.isWhitespace(line.charAt(before))) { 253 log(aAST.getLineNo(), aAST.getColumnNo(), 254 "ws.notPreceded", new Object []{aAST.getText()}); 255 } 256 257 if (after >= line.length()) { 258 return; 259 } 260 261 final char nextChar = line.charAt(after); 262 if (!Character.isWhitespace(nextChar) 263 && !((type == TokenTypes.LITERAL_RETURN) 265 && (aAST.getFirstChild().getType() == TokenTypes.SEMI)) 266 && !((type == TokenTypes.RCURLY) 268 && ((nextChar == ')') 269 || (nextChar == ';') 270 || (nextChar == ',')))) 271 { 272 log( 273 aAST.getLineNo(), 274 aAST.getColumnNo() + aAST.getText().length(), 275 "ws.notFollowed", 276 new Object [] {aAST.getText()}); 277 } 278 } 279 280 288 private boolean emptyMethodBlockCheck(DetailAST aAST, int aParentType) 289 { 290 return mAllowEmptyMethods 291 && emptyBlockCheck(aAST, aParentType, TokenTypes.METHOD_DEF); 292 } 293 294 302 private boolean emptyCtorBlockCheck(DetailAST aAST, int aParentType) 303 { 304 return mAllowEmptyCtors 305 && emptyBlockCheck(aAST, aParentType, TokenTypes.CTOR_DEF); 306 } 307 308 323 private boolean emptyBlockCheck(DetailAST aAST, int aParentType, int aMatch) 324 { 325 final int type = aAST.getType(); 326 if (type == TokenTypes.RCURLY) { 327 final DetailAST grandParent = aAST.getParent().getParent(); 328 return (aParentType == TokenTypes.SLIST) 329 && (grandParent.getType() == aMatch); 330 } 331 332 return (type == TokenTypes.SLIST) 333 && (aParentType == aMatch) 334 && (aAST.getFirstChild().getType() == TokenTypes.RCURLY); 335 } 336 } 337 | Popular Tags |