1 package com.puppycrawl.tools.checkstyle.checks; 20 21 import java.util.Arrays ; 22 import java.util.Iterator ; 23 import java.util.Set ; 24 25 import antlr.collections.AST; 26 27 import com.puppycrawl.tools.checkstyle.api.Check; 28 import com.puppycrawl.tools.checkstyle.api.DetailAST; 29 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 30 31 168 public class DescendantTokenCheck extends Check 169 { 170 171 private int mMinimumDepth; 172 173 174 private int mMaximumDepth = Integer.MAX_VALUE; 175 176 177 private int mMinimumNumber; 178 179 180 private int mMaximumNumber = Integer.MAX_VALUE; 181 182 183 private int[] mLimitedTokens = new int[0]; 184 185 186 private String mMinimumMessage = "descendant.token.min"; 187 188 189 private String mMaximumMessage = "descendant.token.max"; 190 191 195 private int[] mCounts = new int[0]; 196 197 198 public int[] getDefaultTokens() 199 { 200 return new int[0]; 201 } 202 203 204 public void visitToken(DetailAST aAST) 205 { 206 Arrays.fill(mCounts, 0); 208 209 countTokens(aAST, 0); 210 211 final String name = TokenTypes.getTokenName(aAST.getType()); 213 214 for (int i = 0; i < mLimitedTokens.length; i++) { 215 final int tokenCount = mCounts[mLimitedTokens[i] - 1]; 216 if (tokenCount < mMinimumNumber) { 217 final String descendantName = 218 TokenTypes.getTokenName(mLimitedTokens[i]); 219 log(aAST.getLineNo(), 220 aAST.getColumnNo(), 221 mMinimumMessage, 222 new String [] { 223 "" + tokenCount, 224 "" + mMinimumNumber, 225 name, 226 descendantName, 227 }); 228 } 229 if (tokenCount > mMaximumNumber) { 230 final String descendantName = 231 TokenTypes.getTokenName(mLimitedTokens[i]); 232 log(aAST.getLineNo(), 233 aAST.getColumnNo(), 234 mMaximumMessage, 235 new String [] { 236 "" + tokenCount, 237 "" + mMaximumNumber, 238 name, 239 descendantName, 240 }); 241 } 242 } 243 } 244 245 250 private void countTokens(AST aAST, int aDepth) 251 { 252 if (aDepth <= mMaximumDepth) { 253 if (aDepth >= mMinimumDepth) { 255 final int type = aAST.getType(); 256 if (type <= mCounts.length) { 257 mCounts[type - 1]++; 258 } 259 } 260 AST child = aAST.getFirstChild(); 261 final int nextDepth = aDepth + 1; 262 while (child != null) { 263 countTokens(child, nextDepth); 264 child = child.getNextSibling(); 265 } 266 } 267 } 268 269 270 public int[] getAcceptableTokens() 271 { 272 final Set tokenNames = getTokenNames(); 274 final int[] result = new int[tokenNames.size()]; 275 int i = 0; 276 final Iterator it = tokenNames.iterator(); 277 while (it.hasNext()) { 278 final String name = (String ) it.next(); 279 result[i] = TokenTypes.getTokenId(name); 280 i++; 281 } 282 return result; 283 } 284 285 289 public void setLimitedTokens(String [] aLimitedTokens) 290 { 291 mLimitedTokens = new int[aLimitedTokens.length]; 292 293 int maxToken = 0; 294 for (int i = 0; i < aLimitedTokens.length; i++) { 295 mLimitedTokens[i] = TokenTypes.getTokenId(aLimitedTokens[i]); 296 if (mLimitedTokens[i] > maxToken) { 297 maxToken = mLimitedTokens[i]; 298 } 299 } 300 mCounts = new int[maxToken]; 301 } 302 303 307 public void setMinimumDepth(int aMinimumDepth) 308 { 309 mMinimumDepth = aMinimumDepth; 310 } 311 312 316 public void setMaximumDepth(int aMaximumDepth) 317 { 318 mMaximumDepth = aMaximumDepth; 319 } 320 321 325 public void setMinimumNumber(int aMinimumNumber) 326 { 327 mMinimumNumber = aMinimumNumber; 328 } 329 330 334 public void setMaximumNumber(int aMaximumNumber) 335 { 336 mMaximumNumber = aMaximumNumber; 337 } 338 339 350 public void setMinimumMessage(String aMessage) 351 { 352 mMinimumMessage = aMessage; 353 } 354 355 366 367 public void setMaximumMessage(String aMessage) 368 { 369 mMaximumMessage = aMessage; 370 } 371 } 372 | Popular Tags |