| 1 11 package org.eclipse.jdt.core.dom; 12 13 import java.util.Iterator ; 14 import java.util.List ; 15 16 46 public class ASTMatcher { 47 48 52 private boolean matchDocTags; 53 54 63 public ASTMatcher() { 64 this(false); 65 } 66 67 75 public ASTMatcher(boolean matchDocTags) { 76 this.matchDocTags = matchDocTags; 77 } 78 79 95 public final boolean safeSubtreeListMatch(List list1, List list2) { 96 int size1 = list1.size(); 97 int size2 = list2.size(); 98 if (size1 != size2) { 99 return false; 100 } 101 for (Iterator it1 = list1.iterator(), it2 = list2.iterator(); it1.hasNext();) { 102 ASTNode n1 = (ASTNode) it1.next(); 103 ASTNode n2 = (ASTNode) it2.next(); 104 if (!n1.subtreeMatch(this, n2)) { 105 return false; 106 } 107 } 108 return true; 109 } 110 111 130 public final boolean safeSubtreeMatch(Object node1, Object node2) { 131 if (node1 == null && node2 == null) { 132 return true; 133 } 134 if (node1 == null || node2 == null) { 135 return false; 136 } 137 return ((ASTNode) node1).subtreeMatch(this, node2); 139 } 140 141 152 public static boolean safeEquals(Object o1, Object o2) { 153 if (o1 == o2) { 154 return true; 155 } 156 if (o1 == null || o2 == null) { 157 return false; 158 } 159 return o1.equals(o2); 160 } 161 162 177 public boolean match(AnnotationTypeDeclaration node, Object other) { 178 if (!(other instanceof AnnotationTypeDeclaration)) { 179 return false; 180 } 181 AnnotationTypeDeclaration o = (AnnotationTypeDeclaration) other; 182 return (safeSubtreeMatch(node.getJavadoc(), o.getJavadoc()) 184 && safeSubtreeListMatch(node.modifiers(), o.modifiers()) 185 && safeSubtreeMatch(node.getName(), o.getName()) 186 && safeSubtreeListMatch(node.bodyDeclarations(), o.bodyDeclarations())); 187 } 188 189 204 public boolean match(AnnotationTypeMemberDeclaration node, Object other) { 205 if (!(other instanceof AnnotationTypeMemberDeclaration)) { 206 return false; 207 } 208 AnnotationTypeMemberDeclaration o = (AnnotationTypeMemberDeclaration) other; 209 return (safeSubtreeMatch(node.getJavadoc(), o.getJavadoc()) 211 && safeSubtreeListMatch(node.modifiers(), o.modifiers()) 212 && safeSubtreeMatch(node.getType(), o.getType()) 213 && safeSubtreeMatch(node.getName(), o.getName()) 214 && safeSubtreeMatch(node.getDefault(), o.getDefault())); 215 } 216 217 231 public boolean match(AnonymousClassDeclaration node, Object other) { 232 if (!(other instanceof AnonymousClassDeclaration)) { 233 return false; 234 } 235 AnonymousClassDeclaration o = (AnonymousClassDeclaration) other; 236 return safeSubtreeListMatch(node.bodyDeclarations(), o.bodyDeclarations()); 237 } 238 239 253 public boolean match(ArrayAccess node, Object other) { 254 if (!(other instanceof ArrayAccess)) { 255 return false; 256 } 257 ArrayAccess o = (ArrayAccess) other; 258 return ( 259 safeSubtreeMatch(node.getArray(), o.getArray()) 260 && safeSubtreeMatch(node.getIndex(), o.getIndex())); 261 } 262 263 277 public boolean match(ArrayCreation node, Object other) { 278 if (!(other instanceof ArrayCreation)) { 279 return false; 280 } 281 ArrayCreation o = (ArrayCreation) other; 282 return ( 283 safeSubtreeMatch(node.getType(), o.getType()) 284 && safeSubtreeListMatch(node.dimensions(), o.dimensions()) 285 && safeSubtreeMatch(node.getInitializer(), o.getInitializer())); 286 } 287 288 302 public boolean match(ArrayInitializer node, Object other) { 303 if (!(other instanceof ArrayInitializer)) { 304 return false; 305 } 306 ArrayInitializer o = (ArrayInitializer) other; 307 return safeSubtreeListMatch(node.expressions(), o.expressions()); 308 } 309 310 324 public boolean match(ArrayType node, Object other) { 325 if (!(other instanceof ArrayType)) { 326 return false; 327 } 328 ArrayType o = (ArrayType) other; 329 return safeSubtreeMatch(node.getComponentType(), o.getComponentType()); 330 } 331 332 346 public boolean match(AssertStatement node, Object other) { 347 if (!(other instanceof AssertStatement)) { 348 return false; 349 } 350 AssertStatement o = (AssertStatement) other; 351 return ( 352 safeSubtreeMatch(node.getExpression(), o.getExpression()) 353 && safeSubtreeMatch(node.getMessage(), o.getMessage())); 354 } 355 356 370 public boolean match(Assignment node, Object other) { 371 if (!(other instanceof Assignment)) { 372 return false; 373 } 374 Assignment o = (Assignment) other; 375 return ( 376 node.getOperator().equals(o.getOperator()) 377 && safeSubtreeMatch(node.getLeftHandSide(), o.getLeftHandSide()) 378 && safeSubtreeMatch(node.getRightHandSide(), o.getRightHandSide())); 379 } 380 381 395 public boolean match(Block node, Object other) { 396 if (!(other instanceof Block)) { 397 return false; 398 } 399 Block o = (Block) other; 400 return safeSubtreeListMatch(node.statements(), o.statements()); 401 } 402 403 423 public boolean match(BlockComment node, Object other) { 424 if (!(other instanceof BlockComment)) { 425 return false; 426 } 427 return true; 428 } 429 430 444 public boolean match(BooleanLiteral node, Object other) { 445 if (!(other instanceof BooleanLiteral)) { 446 return false; 447 } 448 BooleanLiteral o = (BooleanLiteral) other; 449 return node.booleanValue() == o.booleanValue(); 450 } 451 452 466 public boolean match(BreakStatement node, Object other) { 467 if (!(other instanceof BreakStatement)) { 468 return false; 469 } 470 BreakStatement o = (BreakStatement) other; 471 return safeSubtreeMatch(node.getLabel(), o.getLabel()); 472 } 473 474 488 public boolean match(CastExpression node, Object other) { 489 if (!(other instanceof CastExpression)) { 490 return false; 491 } 492 CastExpression o = (CastExpression) other; 493 return ( 494 safeSubtreeMatch(node.getType(), o.getType()) 495 && safeSubtreeMatch(node.getExpression(), o.getExpression())); 496 } 497 498 512 public boolean match(CatchClause node, Object other) { 513 if (!(other instanceof CatchClause)) { 514 return false; 515 } 516 CatchClause o = (CatchClause) other; 517 return ( 518 safeSubtreeMatch(node.getException(), o.getException()) 519 && safeSubtreeMatch(node.getBody(), o.getBody())); 520 } 521 522 536 public boolean match(CharacterLiteral node, Object other) { 537 if (!(other instanceof CharacterLiteral)) { 538 return false; 539 } 540 CharacterLiteral o = (CharacterLiteral) other; 541 return safeEquals(node.getEscapedValue(), o.getEscapedValue()); 542 } 543 544 558 public boolean match(ClassInstanceCreation node, Object other) { 559 if (!(other instanceof ClassInstanceCreation)) { 560 return false; 561 } 562 ClassInstanceCreation o = (ClassInstanceCreation) other; 563 int level = node.getAST().apiLevel; 564 if (level == AST.JLS2_INTERNAL) { 565 if (!safeSubtreeMatch(node.internalGetName(), o.internalGetName())) { 566 return false; 567 } 568 } 569 if (level >= AST.JLS3) { 570 if (!safeSubtreeListMatch(node.typeArguments(), o.typeArguments())) { 571 return false; 572 } 573 if (!safeSubtreeMatch(node.getType(), o.getType())) { 574 return false; 575 } 576 } 577 return 578 safeSubtreeMatch(node.getExpression(), o.getExpression()) 579 && safeSubtreeListMatch(node.arguments(), o.arguments()) 580 && safeSubtreeMatch( 581 node.getAnonymousClassDeclaration(), 582 o.getAnonymousClassDeclaration()); 583 } 584 585 599 public boolean match(CompilationUnit node, Object other) { 600 if (!(other instanceof CompilationUnit)) { 601 return false; 602 } 603 CompilationUnit o = (CompilationUnit) other; 604 return ( 605 safeSubtreeMatch(node.getPackage(), o.getPackage()) 606 && safeSubtreeListMatch(node.imports(), o.imports()) 607 && safeSubtreeListMatch(node.types(), o.types())); 608 } 609 610 624 public boolean match(ConditionalExpression node, Object other) { 625 if (!(other instanceof ConditionalExpression)) { 626 return false; 627 } 628 ConditionalExpression o = (ConditionalExpression) other; 629 return ( 630 safeSubtreeMatch(node.getExpression(), o.getExpression()) 631 && safeSubtreeMatch(node.getThenExpression(), o.getThenExpression()) 632 && safeSubtreeMatch(node.getElseExpression(), o.getElseExpression())); 633 } 634 635 649 public boolean match(ConstructorInvocation node, Object other) { 650 if (!(other instanceof ConstructorInvocation)) { 651 return false; 652 } 653 ConstructorInvocation o = (ConstructorInvocation) other; 654 if (node.getAST().apiLevel >= AST.JLS3) { 655 if (!safeSubtreeListMatch(node.typeArguments(), o.typeArguments())) { 656 return false; 657 } 658 } 659 return safeSubtreeListMatch(node.arguments(), o.arguments()); 660 } 661 662 676 public boolean match(ContinueStatement node, Object other) { 677 if (!(other instanceof ContinueStatement)) { 678 return false; 679 } 680 ContinueStatement o = (ContinueStatement) other; 681 return safeSubtreeMatch(node.getLabel(), o.getLabel()); 682 } 683 684 698 public boolean match(DoStatement node, Object other) { 699 if (!(other instanceof DoStatement)) { 700 return false; 701 } 702 DoStatement o = (DoStatement) other; 703 return ( 704 safeSubtreeMatch(node.getExpression(), o.getExpression()) 705 && safeSubtreeMatch(node.getBody(), o.getBody())); 706 } 707 708 722 public boolean match(EmptyStatement node, Object other) { 723 if (!(other instanceof EmptyStatement)) { 724 return false; 725 } 726 return true; 727 } 728 729 744 public boolean match(EnhancedForStatement node, Object other) { 745 if (!(other instanceof EnhancedForStatement)) { 746 return false; 747 } 748 EnhancedForStatement o = (EnhancedForStatement) other; 749 return ( 750 safeSubtreeMatch(node.getParameter(), o.getParameter()) 751 && safeSubtreeMatch(node.getExpression(), o.getExpression()) 752 && safeSubtreeMatch(node.getBody(), o.getBody())); 753 } 754 755 770 public boolean match(EnumConstantDeclaration node, Object other) { 771 if (!(other instanceof EnumConstantDeclaration)) { 772 return false; 773 } 774 EnumConstantDeclaration o = (EnumConstantDeclaration) other; 775 return ( 776 safeSubtreeMatch(node.getJavadoc(), o.getJavadoc()) 777 && safeSubtreeListMatch(node.modifiers(), o.modifiers()) 778 && safeSubtreeMatch(node.getName(), o.getName()) 779 && safeSubtreeListMatch(node.arguments(), o.arguments()) 780 && safeSubtreeMatch( 781 node.getAnonymousClassDeclaration(), 782 o.getAnonymousClassDeclaration())); 783 } 784 785 800 public boolean match(EnumDeclaration node, Object other) { 801 if (!(other instanceof EnumDeclaration)) { 802 return false; 803 } 804 EnumDeclaration o = (EnumDeclaration) other; 805 return ( 806 safeSubtreeMatch(node.getJavadoc(), o.getJavadoc()) 807 && safeSubtreeListMatch(node.modifiers(), o.modifiers()) 808 && safeSubtreeMatch(node.getName(), o.getName()) 809 && safeSubtreeListMatch(node.superInterfaceTypes(), o.superInterfaceTypes()) 810 && safeSubtreeListMatch(node.enumConstants(), o.enumConstants()) 811 && safeSubtreeListMatch( 812 node.bodyDeclarations(), 813 o.bodyDeclarations())); 814 } 815 816 830 public boolean match(ExpressionStatement node, Object other) { 831 if (!(other instanceof ExpressionStatement)) { 832 return false; 833 } 834 ExpressionStatement o = (ExpressionStatement) other; 835 return safeSubtreeMatch(node.getExpression(), o.getExpression()); 836 } 837 838 852 public boolean match(FieldAccess node, Object other) { 853 if (!(other instanceof FieldAccess)) { 854 return false; 855 } 856 FieldAccess o = (FieldAccess) other; 857 return ( 858 safeSubtreeMatch(node.getExpression(), o.getExpression()) 859 && safeSubtreeMatch(node.getName(), o.getName())); 860 } 861 862 |