1 package com.puppycrawl.tools.checkstyle.checks.j2ee; 20 21 import com.puppycrawl.tools.checkstyle.api.DetailAST; 22 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 23 24 28 public class Utils 29 { 30 37 public static boolean hasPublicMethod(DetailAST aAST, String aName) 38 { 39 final DetailAST objBlock = aAST.findFirstToken(TokenTypes.OBJBLOCK); 40 if (objBlock != null) { 41 DetailAST child = (DetailAST) objBlock.getFirstChild(); 42 while (child != null) { 43 if ((child.getType() == TokenTypes.METHOD_DEF) 44 && Utils.isPublicMethod(child, aName)) 45 { 46 return true; 47 } 48 child = (DetailAST) child.getNextSibling(); 49 } 50 } 51 return false; 52 } 53 54 62 public static boolean hasPublicMethod( 63 DetailAST aAST, 64 String aName, 65 boolean aIsVoid) 66 { 67 final DetailAST objBlock = aAST.findFirstToken(TokenTypes.OBJBLOCK); 68 if (objBlock != null) { 69 DetailAST child = (DetailAST) objBlock.getFirstChild(); 70 while (child != null) { 71 if ((child.getType() == TokenTypes.METHOD_DEF) 72 && Utils.isPublicMethod( 73 child, 74 aName, 75 aIsVoid)) 76 { 77 return true; 78 } 79 child = (DetailAST) child.getNextSibling(); 80 } 81 } 82 return false; 83 } 84 85 94 public static boolean hasPublicMethod( 95 DetailAST aAST, 96 String aName, 97 boolean aIsVoid, 98 int aParameterCount) 99 { 100 final DetailAST objBlock = aAST.findFirstToken(TokenTypes.OBJBLOCK); 101 if (objBlock != null) { 102 DetailAST child = (DetailAST) objBlock.getFirstChild(); 103 while (child != null) { 104 if ((child.getType() == TokenTypes.METHOD_DEF) 105 && Utils.isPublicMethod( 106 child, 107 aName, 108 aIsVoid, 109 aParameterCount)) 110 { 111 return true; 112 } 113 child = (DetailAST) child.getNextSibling(); 114 } 115 } 116 return false; 117 } 118 119 127 public static boolean hasPublicConstructor( 128 DetailAST aAST, 129 int aParameterCount) 130 { 131 final DetailAST objBlock = aAST.findFirstToken(TokenTypes.OBJBLOCK); 132 if (objBlock == null) { 133 return false; 134 } 135 int constructorCount = 0; 136 DetailAST child = (DetailAST) objBlock.getFirstChild(); 137 while (child != null) { 138 if (child.getType() == TokenTypes.CTOR_DEF) { 139 constructorCount++; 140 final DetailAST parameters = 141 child.findFirstToken(TokenTypes.PARAMETERS); 142 if (Utils.isPublic(child) 143 && (parameters.getChildCount() == aParameterCount)) 144 { 145 return true; 146 } 147 } 148 child = (DetailAST) child.getNextSibling(); 149 } 150 return ((constructorCount == 0) && (aParameterCount == 0)); 152 } 153 154 161 public static boolean implementsEntityBean(DetailAST aAST) 162 { 163 final DetailAST definer = getDefiner(aAST); 164 return ((definer != null) 165 && Utils.hasImplements(definer, "javax.ejb.EntityBean")); 166 } 167 168 175 public static boolean implementsSessionBean(DetailAST aAST) 176 { 177 final DetailAST definer = getDefiner(aAST); 178 return ((definer != null) 179 && Utils.hasImplements(definer, "javax.ejb.SessionBean")); 180 } 181 182 189 public static boolean isInEJB(DetailAST aAST) 190 { 191 final DetailAST definer = getDefiner(aAST); 192 return ( 193 (definer != null) 194 && (Utils.hasImplements(definer, "javax.ejb.SessionBean") 195 || Utils.hasImplements(definer, "javax.ejb.EntityBean") 196 || (Utils 197 .hasImplements(definer, "javax.ejb.MessageDrivenBean") 198 && Utils.hasImplements( 199 definer, 200 "javax.jms.MessageListener")))); 201 } 202 203 208 private static DetailAST getDefiner(DetailAST aAST) 209 { 210 DetailAST definer = aAST.getParent(); 211 while ((definer != null)) { 212 if (definer.getType() == TokenTypes.CLASS_DEF) { 213 break; 214 } 215 definer = definer.getParent(); 216 } 217 return definer; 218 } 219 220 225 public static boolean isAbstract(DetailAST aAST) 226 { 227 final DetailAST mods = aAST.findFirstToken(TokenTypes.MODIFIERS); 228 return ((mods != null) 229 && mods.branchContains(TokenTypes.ABSTRACT)); 230 } 231 232 237 public static boolean isFinal(DetailAST aAST) 238 { 239 final DetailAST mods = aAST.findFirstToken(TokenTypes.MODIFIERS); 240 return ((mods != null) 241 && mods.branchContains(TokenTypes.FINAL)); 242 } 243 244 249 public static boolean isPublic(DetailAST aAST) 250 { 251 final DetailAST mods = aAST.findFirstToken(TokenTypes.MODIFIERS); 252 return ((mods != null) 253 && mods.branchContains(TokenTypes.LITERAL_PUBLIC)); 254 } 255 256 261 public static boolean isStatic(DetailAST aAST) 262 { 263 final DetailAST mods = aAST.findFirstToken(TokenTypes.MODIFIERS); 264 return ((mods != null) 265 && mods.branchContains(TokenTypes.LITERAL_STATIC)); 266 } 267 268 273 public static boolean isVoid(DetailAST aAST) 274 { 275 final DetailAST type = aAST.findFirstToken(TokenTypes.TYPE); 276 return ((type != null) 277 && type.branchContains(TokenTypes.LITERAL_VOID)); 278 } 279 280 288 public static boolean hasImplements(DetailAST aAST, String aInterface) 289 { 290 final String shortName = 291 com.puppycrawl.tools.checkstyle.api.Utils.baseClassname(aInterface); 292 final DetailAST implementsAST = 293 aAST.findFirstToken(TokenTypes.IMPLEMENTS_CLAUSE); 294 if (implementsAST != null) { 295 DetailAST child = (DetailAST) implementsAST.getFirstChild(); 296 while (child != null) { 297 if ((child.getType() == TokenTypes.IDENT) 298 || (child.getType() == TokenTypes.DOT)) 299 { 300 final String name = Utils.constructDottedName(child); 301 if (name.equals(aInterface) 302 || name.equals(shortName)) 303 { 304 return true; 305 } 306 } 307 child = (DetailAST) child.getNextSibling(); 308 } 309 } 310 return false; 311 } 312 313 321 public static boolean hasExtends(DetailAST aAST, String aClassOrInterface) 322 { 323 final String shortName = 324 com.puppycrawl.tools.checkstyle.api.Utils.baseClassname( 325 aClassOrInterface); 326 final DetailAST extendsAST = 327 aAST.findFirstToken(TokenTypes.EXTENDS_CLAUSE); 328 if (extendsAST != null) { 329 DetailAST child = (DetailAST) extendsAST.getFirstChild(); 330 while (child != null) { 331 if ((child.getType() == TokenTypes.IDENT) 332 || (child.getType() == TokenTypes.DOT)) 333 { 334 final String name = Utils.constructDottedName(child); 335 if (name.equals(aClassOrInterface) 336 || name.equals(shortName)) 337 { 338 return true; 339 } 340 } 341 child = (DetailAST) child.getNextSibling(); 342 } 343 } 344 return false; 345 } 346 347 354 public static boolean hasThrows(DetailAST aAST, String aException) 355 { 356 final String shortName = 357 com.puppycrawl.tools.checkstyle.api.Utils.baseClassname( 358 aException); 359 final DetailAST throwsAST = 360 aAST.findFirstToken(TokenTypes.LITERAL_THROWS); 361 if (throwsAST != null) { 362 DetailAST child = (DetailAST) throwsAST.getFirstChild(); 363 while (child != null) { 364 if ((child.getType() == TokenTypes.IDENT) 365 || (child.getType() == TokenTypes.DOT)) 366 { 367 final String name = Utils.constructDottedName(child); 368 if (name.equals(aException) 369 || name.equals(shortName)) 370 { 371 return true; 372 } 373 } 374 child = (DetailAST) child.getNextSibling(); 375 } 376 } 377 return false; 378 } 379 380 389 public static boolean isPublicMethod( 390 DetailAST aAST, 391 String aName, 392 boolean aIsVoid, 393 int aParameterCount) 394 { 395 final DetailAST nameNode = aAST.findFirstToken(TokenTypes.IDENT); 396 if (nameNode != null) { 397 final String name = nameNode.getText(); 398 if (name.equals(aName) 399 && isPublic(aAST) 400 && (isVoid(aAST) == aIsVoid)) 401 { 402 final DetailAST parameters = 403 aAST.findFirstToken(TokenTypes.PARAMETERS); 404 if (parameters.getChildCount() == aParameterCount) { 405 return true; 406 } 407 } 408 } 409 return false; 410 } 411 412 420 public static boolean isPublicMethod( 421 DetailAST aAST, 422 String aName, 423 boolean aIsVoid) 424 { 425 final DetailAST nameNode = aAST.findFirstToken(TokenTypes.IDENT); 426 if (nameNode != null) { 427 final String name = nameNode.getText(); 428 if (name.equals(aName) 429 && isPublic(aAST) 430 && (isVoid(aAST) == aIsVoid)) 431 { 432 return true; 433 } 434 } 435 return false; 436 } 437 438 445 public static boolean isPublicMethod(DetailAST aAST, String aName) 446 { 447 final DetailAST nameNode = aAST.findFirstToken(TokenTypes.IDENT); 448 if (nameNode != null) { 449 final String name = nameNode.getText(); 450 if (name.equals(aName) && isPublic(aAST)) { 451 return true; 452 } 453 } 454 return false; 455 } 456 457 465 public static String constructDottedName(DetailAST aAST) 466 { 467 String result; 468 469 if (aAST.getType() == TokenTypes.DOT) { 470 final DetailAST left = (DetailAST) aAST.getFirstChild(); 471 final DetailAST right = (DetailAST) left.getNextSibling(); 472 473 result = 474 constructDottedName(left) + "." + constructDottedName(right); 475 } 476 else { 477 result = aAST.getText(); 478 } 479 480 return result; 481 } 482 483 491 public static boolean sameParameters( 492 DetailAST aMethodAST1, 493 DetailAST aMethodAST2) 494 { 495 final DetailAST params1 = 496 aMethodAST1.findFirstToken(TokenTypes.PARAMETERS); 497 final DetailAST params2 = 498 aMethodAST2.findFirstToken(TokenTypes.PARAMETERS); 499 if (params1.getChildCount() != params2.getChildCount()) { 500 return false; 501 } 502 DetailAST param1 = (DetailAST) params1.getFirstChild(); 503 DetailAST param2 = (DetailAST) params2.getFirstChild(); 504 while (param1 != null) { 505 if ((param1.getType() == TokenTypes.PARAMETER_DEF) 506 && (param2.getType() == TokenTypes.PARAMETER_DEF)) 507 { 508 final DetailAST type1 = param1.findFirstToken(TokenTypes.TYPE); 509 final DetailAST type2 = param2.findFirstToken(TokenTypes.TYPE); 510 if (!equalTypes(type1, type2)) { 511 return false; 512 } 513 } 514 param1 = (DetailAST) param1.getNextSibling(); 515 param2 = (DetailAST) param2.getNextSibling(); 516 } 517 return true; 518 } 519 520 526 public static boolean equalTypes( 527 DetailAST aTypeAST1, 528 DetailAST aTypeAST2) 529 { 530 final DetailAST child1 = (DetailAST) aTypeAST1.getFirstChild(); 531 final DetailAST child2 = (DetailAST) aTypeAST2.getFirstChild(); 532 final String name1 = Utils.constructDottedName(child1); 533 final String name2 = Utils.constructDottedName(child2); 534 return name1.equals(name2); 535 } 536 } 537 | Popular Tags |