1 33 34 package edu.rice.cs.drjava.model.repl; 35 36 import java.util.*; 37 38 import koala.dynamicjava.tree.*; 39 import koala.dynamicjava.tree.visitor.*; 40 41 50 51 public class IdentityVisitor implements Visitor<Node> { 52 57 58 public Node visit(PackageDeclaration node) { return node; } 59 60 65 66 public Node visit(ImportDeclaration node) { return node; } 67 68 72 73 public Node visit(EmptyStatement node) { return node; } 74 75 79 public Node visit(WhileStatement node) { 80 node.setCondition((Expression) node.getCondition().acceptVisitor(this)); 81 node.setBody(node.getBody().acceptVisitor(this)); 82 return node; 83 } 84 85 89 public Node visit(ForStatement node) { 90 LinkedList<Node> init = null; if (node.getInitialization() != null) { 92 init = new LinkedList<Node>(); Iterator<Node> it = node.getInitialization().iterator(); 94 while (it.hasNext()) { 95 init.add(it.next().acceptVisitor(this)); 96 } 97 } 98 node.setInitialization(init); 99 Expression cond = null; 100 if (node.getCondition() != null) { 101 cond = (Expression)node.getCondition().acceptVisitor(this); 102 } 103 node.setCondition(cond); 104 LinkedList<Node> updt = null; if (node.getUpdate() != null) { 106 updt = new LinkedList<Node>(); Iterator<Node> it = node.getUpdate().iterator(); 108 while (it.hasNext()) { 109 updt.add(it.next().acceptVisitor(this)); 110 } 111 } 112 node.setUpdate(updt); 113 node.setBody(node.getBody().acceptVisitor(this)); 114 return node; 115 } 116 117 121 public Node visit(ForEachStatement node) { 122 FormalParameter param = node.getParameter(); 123 Expression collection = node.getCollection(); 124 Node stmt = node.getBody(); 125 126 node.setParameter((FormalParameter)param.acceptVisitor(this)); 127 node.setCollection((Expression)collection.acceptVisitor(this)); 128 node.setBody(stmt.acceptVisitor(this)); 129 130 return node; 131 } 132 133 137 public Node visit(DoStatement node) { 138 Expression cond = (Expression) node.getCondition().acceptVisitor(this); 139 node.setCondition(cond); 140 Node body = node.getBody().acceptVisitor(this); 141 node.setBody(body); 142 return node; 143 } 144 145 149 public Node visit(SwitchStatement node) { 150 Expression sel = (Expression) node.getSelector().acceptVisitor(this); 151 node.setSelector(sel); 152 LinkedList<SwitchBlock> cases = new LinkedList<SwitchBlock>(); Iterator<SwitchBlock> it = node.getBindings().iterator(); 154 while (it.hasNext()) { 155 cases.add((SwitchBlock)it.next().acceptVisitor(this)); 156 } 157 node.setBindings(cases); 158 return node; 159 } 160 161 165 public Node visit(SwitchBlock node) { 166 Expression e = null; 167 if (node.getExpression() != null) { 168 e = (Expression) node.getExpression().acceptVisitor(this); 169 } 170 node.setExpression(e); 171 LinkedList<Node> statements = null; if (node.getStatements() != null) { 173 statements = new LinkedList<Node>(); Iterator<Node> it = node.getStatements().iterator(); 175 while (it.hasNext()) { 176 statements.add(it.next().acceptVisitor(this)); 177 } 178 } 179 node.setStatements(statements); 180 return node; 181 } 182 183 187 public Node visit(LabeledStatement node) { 188 node.setStatement(node.getStatement().acceptVisitor(this)); 189 return node; 190 } 191 192 196 public Node visit(BreakStatement node) { return node; } 197 198 202 public Node visit(TryStatement node) { 203 Node tryBlock = node.getTryBlock().acceptVisitor(this); 204 LinkedList<CatchStatement> catchStatements = new LinkedList<CatchStatement>(); 205 Iterator<CatchStatement> it = node.getCatchStatements().iterator(); 206 while (it.hasNext()) { 207 catchStatements.add((CatchStatement)it.next().acceptVisitor(this)); 208 } 209 Node finallyBlock = null; 210 if (node.getFinallyBlock() != null) { 211 finallyBlock = node.getFinallyBlock().acceptVisitor(this); 212 } 213 node = new TryStatement(tryBlock, catchStatements, finallyBlock, null, 0, 0, 0, 0); 214 return node; 215 } 216 217 221 public Node visit(CatchStatement node) { 222 Node exp = node.getException().acceptVisitor(this); 223 Node block = node.getBlock().acceptVisitor(this); 224 node = new CatchStatement((FormalParameter)exp, block, null, 0, 0, 0, 0); 225 return node; 226 } 227 228 232 public Node visit(ThrowStatement node) { 233 node.setExpression((Expression)node.getExpression().acceptVisitor(this)); 234 return node; 235 } 236 237 241 public Node visit(ReturnStatement node) { 242 node.setExpression((Expression)node.getExpression().acceptVisitor(this)); 243 return node; 244 } 245 246 250 public Node visit(SynchronizedStatement node) { 251 node.setLock((Expression)node.getLock().acceptVisitor(this)); 252 node.setBody(node.getBody().acceptVisitor(this)); 253 return node; 254 } 255 256 260 public Node visit(ContinueStatement node) { 261 return node; 262 } 263 264 268 public Node visit(IfThenStatement node) { 269 node.setCondition((Expression)node.getCondition().acceptVisitor(this)); 270 node.setThenStatement(node.getThenStatement().acceptVisitor(this)); 271 return node; 272 } 273 274 278 public Node visit(IfThenElseStatement node) { 279 node.setCondition((Expression)node.getCondition().acceptVisitor(this)); 280 node.setThenStatement(node.getThenStatement().acceptVisitor(this)); 281 node.setElseStatement(node.getElseStatement().acceptVisitor(this)); 282 return node; 283 } 284 285 289 public Node visit(AssertStatement node) { 290 node.setCondition((Expression)node.getCondition().acceptVisitor(this)); 291 if (node.getFailString() != null) 292 node.setFailString((Expression)node.getFailString().acceptVisitor(this)); 293 return node; 294 } 295 296 300 public Node visit(Literal node) { return node; } 301 302 306 public Node visit(ThisExpression node) { return node; } 307 308 312 public Node visit(QualifiedName node) { 313 return node; 314 } 315 316 320 public Node visit(ObjectFieldAccess node) { 321 node.setExpression((Expression)node.getExpression().acceptVisitor(this)); 322 return node; 323 } 324 325 329 public Node visit(StaticFieldAccess node) { 330 node.setFieldType((ReferenceType)node.getFieldType().acceptVisitor(this)); 331 return node; 332 } 333 334 338 public Node visit(ArrayAccess node) { 339 node.setExpression((Expression)node.getExpression().acceptVisitor(this)); 340 node.setCellNumber((Expression)node.getCellNumber().acceptVisitor(this)); 341 return node; 342 } 343 344 348 public Node visit(SuperFieldAccess node) { 349 return node; 350 } 351 352 356 public Node visit(ObjectMethodCall node) { 357 if (node.getExpression() != null) { 358 node.setExpression((Expression)node.getExpression().acceptVisitor(this)); 359 } 360 LinkedList<Expression> arguments = null; if (node.getArguments() != null) { 362 arguments = new LinkedList<Expression>(); Iterator<Expression> it = node.getArguments().iterator(); 364 while (it.hasNext()) { 365 arguments.add((Expression) it.next().acceptVisitor(this)); 366 } 367 } 368 node.setArguments(arguments); 369 return node; 370 } 371 372 376 public Node visit(FunctionCall node) { 377 LinkedList<Expression> arguments = null; if (node.getArguments() != null) { 379 arguments = new LinkedList<Expression>(); Iterator<Expression> it = node.getArguments().iterator(); 381 while (it.hasNext()) { 382 arguments.add((Expression) it.next().acceptVisitor(this)); 383 } 384 } 385 node.setArguments(arguments); 386 return node; 387 } 388 389 393 public Node visit(StaticMethodCall node) { 394 node.setMethodType((ReferenceType)node.getMethodType().acceptVisitor(this)); 395 LinkedList<Expression> arguments = null; if (node.getArguments() != null) { 397 arguments = new LinkedList<Expression>(); Iterator<Expression> it = node.getArguments().iterator(); 399 while (it.hasNext()) { 400 arguments.add((Expression)it.next().acceptVisitor(this)); 401 } 402 } 403 node.setArguments(arguments); 404 return node; 405 } 406 407 411 public Node visit(ConstructorInvocation node) { 412 if (node.getExpression() != null) { 413 node.setExpression((Expression)node.getExpression().acceptVisitor(this)); 414 } 415 LinkedList<Expression> arguments = null; if (node.getArguments() != null) { 417 arguments = new LinkedList<Expression>(); Iterator<Expression> it = node.getArguments().iterator(); 419 while (it.hasNext()) { 420 arguments.add((Expression)it.next().acceptVisitor(this)); 421 } 422 } 423 node.setArguments(arguments); 424 return node; 425 } 426 427 431 public Node visit(SuperMethodCall node) { 432 LinkedList<Expression> arguments = null; if (node.getArguments() != null) { 434 arguments = new LinkedList<Expression>(); Iterator<Expression> it = node.getArguments().iterator(); 436 while (it.hasNext()) { 437 arguments.add((Expression)it.next().acceptVisitor(this)); 438 } 439 } 440 node.setArguments(arguments); 441 return node; 442 } 443 444 448 public Node visit(PrimitiveType node) { return node; } 449 450 454 public Node visit(ReferenceType node) { return node; } 455 456 460 public Node visit(ArrayType node) { 461 if (node.getElementType() != null) { 462 node.setElementType((Type)node.getElementType().acceptVisitor(this)); 463 } 464 return node; 465 } 466 467 471 public Node visit(TypeExpression node) { 472 node = new TypeExpression((Type)node.getType().acceptVisitor(this)); 477 return node; 478 } 479 480 484 public Node visit(PostIncrement node) { return _visitUnary(node); } 485 486 490 public Node visit(PostDecrement node) { return _visitUnary(node); } 491 492 496 public Node visit(PreIncrement node) { return _visitUnary(node); } 497 498 502 public Node visit(PreDecrement node) { return _visitUnary(node); } 503 504 508 public Node visit(ArrayInitializer node) { 509 LinkedList<Expression> cells = new LinkedList<Expression>(); Iterator<Expression> it = node.getCells().iterator(); 511 while (it.hasNext()) { 512 cells.add((Expression)it.next().acceptVisitor(this)); 513 } 514 node.setCells(cells); 515 node.setElementType((Type)node.getElementType().acceptVisitor(this)); 516 return node; 517 } 518 519 523 public Node visit(ArrayAllocation node) { 524 int dim = node.getDimension(); 525 Type creationType = (Type)node.getCreationType().acceptVisitor(this); 526 LinkedList<Expression> sizes = new LinkedList<Expression>(); Iterator<Expression> it = node.getSizes().iterator(); 528 while (it.hasNext()) { 529 sizes.add((Expression)it.next().acceptVisitor(this)); 530 } 531 ArrayInitializer ai = null; 532 if (node.getInitialization() != null) { 533 ai = (ArrayInitializer)node.getInitialization().acceptVisitor(this); 534 } 535 node = new ArrayAllocation(creationType, 536 new ArrayAllocation.TypeDescriptor(sizes, dim, ai, 0, 0)); 537 return node; 538 } 539 540 544 public Node visit(SimpleAllocation node) { 545 node.setCreationType((Type)node.getCreationType().acceptVisitor(this)); 546 LinkedList<Expression> arguments = null; if (node.getArguments() != null) { 548 arguments = new LinkedList<Expression>(); Iterator<Expression> it = node.getArguments().iterator(); 550 while (it.hasNext()) { 551 arguments.add((Expression)it.next().acceptVisitor(this)); 552 } 553 } 554 node.setArguments(arguments); 555 return node; 556 } 557 558 562 public Node visit(ClassAllocation node) { 563 node.setCreationType((Type)node.getCreationType().acceptVisitor(this)); 564 LinkedList<Expression> arguments = null; if (node.getArguments() != null) { 566 arguments = new LinkedList<Expression>(); Iterator<Expression> it = node.getArguments().iterator(); 568 while (it.hasNext()) { 569 arguments.add((Expression)it.next().acceptVisitor(this)); 570 } 571 } 572 node.setArguments(arguments); 573 LinkedList<Node> members = new LinkedList<Node>(); Iterator<Node> it = node.getMembers().iterator(); 575 while (it.hasNext()) { 576 members.add(it.next().acceptVisitor(this)); 577 } 578 node.setMembers(members); 579 return node; 580 } 581 582 586 public Node visit(InnerAllocation node) { 587 node.setExpression((Expression)node.getExpression().acceptVisitor(this)); 588 node.setCreationType((Type)node.getCreationType().acceptVisitor(this)); 589 LinkedList<Expression> arguments = null; if (node.getArguments() != null) { 591 arguments = new LinkedList<Expression>(); Iterator<Expression> it = node.getArguments().iterator(); 593 while (it.hasNext()) { 594 arguments.add((Expression)it.next().acceptVisitor(this)); 595 } 596 } 597 node.setArguments(arguments); 598 return node; 599 } 600 601 605 public Node visit(InnerClassAllocation node) { 606 node.setExpression((Expression)node.getExpression().acceptVisitor(this)); 607 node.setCreationType((Type)node.getCreationType().acceptVisitor(this)); 608 LinkedList<Expression> arguments = null; if (node.getArguments() != null) { 610 arguments = new LinkedList<Expression>(); Iterator<Expression> it = node.getArguments().iterator(); 612 while (it.hasNext()) { 613 arguments.add((Expression)it.next().acceptVisitor(this)); 614 } 615 } 616 node.setArguments(arguments); 617 LinkedList<Node> members = new LinkedList<Node>(); Iterator<Node> it = node.getMembers().iterator(); 619 while (it.hasNext()) { 620 members.add(it.next().acceptVisitor(this)); 621 } 622 node.setMembers(members); 623 return node; 624 } 625 626 630 public Node visit(CastExpression node) { 631 node.setTargetType((Type)node.getTargetType().acceptVisitor(this)); 632 node.setExpression((Expression)node.getExpression().acceptVisitor(this)); 633 return node; 634 } 635 636 640 public Node visit(NotExpression node) { 641 return _visitUnary(node); 642 } 643 644 648 public Node visit(ComplementExpression node) { 649 return _visitUnary(node); 650 } 651 652 656 public Node visit(PlusExpression node) { 657 return _visitUnary(node); 658 } 659 660 664 public Node visit(MinusExpression node) { 665 return _visitUnary(node); 666 } 667 668 672 public Node visit(MultiplyExpression node) { 673 return _visitBinary(node); 674 } 675 676 680 public Node visit(DivideExpression node) { 681 return _visitBinary(node); 682 } 683 684 688 public Node visit(RemainderExpression node) { 689 return _visitBinary(node); 690 } 691 692 696 public Node visit(AddExpression node) { 697 return _visitBinary(node); 698 } 699 700 704 public Node visit(SubtractExpression node) { 705 return _visitBinary(node); 706 } 707 708 712 public Node visit(ShiftLeftExpression node) { 713 return _visitBinary(node); 714 } 715 716 720 public Node visit(ShiftRightExpression node) { 721 return _visitBinary(node); 722 } 723 724 728 public Node visit(UnsignedShiftRightExpression node) { 729 return _visitBinary(node); 730 } 731 732 736 public Node visit(LessExpression node) { 737 return _visitBinary(node); 738 } 739 740 744 public Node visit(GreaterExpression node) { 745 return _visitBinary(node); 746 } 747 748 752 public Node visit(LessOrEqualExpression node) { 753 return _visitBinary(node); 754 } 755 756 760 public Node visit(GreaterOrEqualExpression node) { 761 return _visitBinary(node); 762 } 763 764 768 public Node visit(InstanceOfExpression node) { 769 node.setExpression((Expression)node.getExpression().acceptVisitor(this)); 770 node.setReferenceType((Type)node.getReferenceType().acceptVisitor(this)); 771 return node; 772 } 773 774 778 public Node visit(EqualExpression node) { 779 return _visitBinary(node); 780 } 781 782 786 public Node visit(NotEqualExpression node) { 787 return _visitBinary(node); 788 } 789 790 794 public Node visit(BitAndExpression node) { 795 return _visitBinary(node); 796 } 797 798 802 public Node visit(ExclusiveOrExpression node) { 803 return _visitBinary(node); 804 } 805 806 810 public Node visit(BitOrExpression node) { 811 return _visitBinary(node); 812 } 813 814 818 public Node visit(AndExpression node) { 819 return _visitBinary(node); 820 } 821 822 826 public Node visit(OrExpression node) { 827 return _visitBinary(node); 828 } 829 830 834 public Node visit(ConditionalExpression node) { 835 node.setConditionExpression((Expression)node.getConditionExpression().acceptVisitor(this)); 836 node.setIfTrueExpression((Expression)node.getIfTrueExpression().acceptVisitor(this)); 837 node.setIfFalseExpression((Expression)node.getIfFalseExpression().acceptVisitor(this)); 838 return node; 839 } 840 841 845 public Node visit(SimpleAssignExpression node) { 846 return _visitBinary(node); 847 } 848 849 853 public Node visit(MultiplyAssignExpression node) { 854 return _visitBinary(node); 855 } 856 857 861 public Node visit(DivideAssignExpression node) { 862 return _visitBinary(node); 863 } 864 865 869 public Node visit(RemainderAssignExpression node) { 870 return _visitBinary(node); 871 } 872 873 877 public Node visit(AddAssignExpression node) { 878 return _visitBinary(node); 879 } 880 881 885 public Node visit(SubtractAssignExpression node) { 886 return _visitBinary(node); 887 } 888 889 893 public Node visit(ShiftLeftAssignExpression node) { 894 return _visitBinary(node); 895 } 896 897 901 public Node visit(ShiftRightAssignExpression node) { 902 return _visitBinary(node); 903 } 904 905 909 public Node visit(UnsignedShiftRightAssignExpression node) { 910 return _visitBinary(node); 911 } 912 913 917 public Node visit(BitAndAssignExpression node) { 918 return _visitBinary(node); 919 } 920 921 925 public Node visit(ExclusiveOrAssignExpression node) { 926 return _visitBinary(node); 927 } 928 929 933 public Node visit(BitOrAssignExpression node) { 934 return _visitBinary(node); 935 } 936 937 941 public Node visit(BlockStatement node) { 942 LinkedList<Node> statements = new LinkedList<Node>(); Iterator<Node> it = node.getStatements().iterator(); 944 while (it.hasNext()) { 945 statements.add(it.next().acceptVisitor(this)); 946 } 947 node.setStatements(statements); 948 return node; 949 } 950 951 955 public Node visit(ClassDeclaration node) { 956 LinkedList<Node> members = new LinkedList<Node>(); Iterator<Node> it = node.getMembers().iterator(); 958 while (it.hasNext()) { 959 members.add(it.next().acceptVisitor(this)); 960 } 961 node.setMembers(members); 962 return node; 963 } 964 965 969 public Node visit(InterfaceDeclaration node) { 970 LinkedList<Node> members = new LinkedList<Node>(); Iterator<Node> it = node.getMembers().iterator(); 972 while (it.hasNext()) { 973 members.add(it.next().acceptVisitor(this)); 974 } 975 node.setMembers(members); 976 return node; 977 } 978 979 983 public Node visit(ConstructorDeclaration node) { 984 LinkedList<FormalParameter> parameters = new LinkedList<FormalParameter>(); Iterator<FormalParameter> it1 = node.getParameters().iterator(); 986 while (it1.hasNext()) { 987 parameters.add((FormalParameter)it1.next().acceptVisitor(this)); 988 } 989 node.setParameters(parameters); 990 if (node.getConstructorInvocation() != null) { 991 node.setConstructorInvocation((ConstructorInvocation)node.getConstructorInvocation().acceptVisitor(this)); 992 } 993 LinkedList<Node> statements = new LinkedList<Node>(); Iterator<Node> it2 = node.getStatements().iterator(); 995 while (it2.hasNext()) { 996 statements.add(it2.next().acceptVisitor(this)); 997 } 998 node.setStatements(statements); 999 return node; 1000 } 1001 1002 1006 public Node visit(MethodDeclaration node) { 1007 node.setReturnType((Type)node.getReturnType().acceptVisitor(this)); 1008 LinkedList<FormalParameter> parameters = new LinkedList<FormalParameter>(); Iterator<FormalParameter> it = node.getParameters().iterator(); 1010 while (it.hasNext()) { 1011 parameters.add((FormalParameter)it.next().acceptVisitor(this)); 1012 } 1013 node.setParameters(parameters); 1014 if (node.getBody() != null) { 1015 node.setBody((BlockStatement)node.getBody().acceptVisitor(this)); 1016 } 1017 return node; 1018 } 1019 1020 1024 public Node visit(FormalParameter node) { 1025 node.setType((Type)node.getType().acceptVisitor(this)); 1026 return node; 1027 } 1028 1029 1033 public Node visit(FieldDeclaration node) { 1034 node.setType((Type)node.getType().acceptVisitor(this)); 1035 if (node.getInitializer() != null) { 1036 node.setInitializer((Expression)node.getInitializer().acceptVisitor(this)); 1037 } 1038 return node; 1039 } 1040 1041 1045 public Node visit(VariableDeclaration node) { 1046 node.setType((Type)node.getType().acceptVisitor(this)); 1047 if (node.getInitializer() != null) { 1048 node.setInitializer((Expression)node.getInitializer().acceptVisitor(this)); 1049 } 1050 return node; 1051 } 1052 1053 1057 public Node visit(ClassInitializer node) { 1058 node.setBlock((BlockStatement)node.getBlock().acceptVisitor(this)); 1059 return node; 1060 } 1061 1062 1066 public Node visit(InstanceInitializer node) { 1067 node.setBlock((BlockStatement)node.getBlock().acceptVisitor(this)); 1068 return node; 1069 } 1070 1071 private Node _visitUnary(UnaryExpression node) { 1072 node.setExpression((Expression)node.getExpression().acceptVisitor(this)); 1073 return node; 1074 } 1075 1076 private Node _visitBinary(BinaryExpression node) { 1077 node.setLeftExpression((Expression)node.getLeftExpression().acceptVisitor(this)); 1078 node.setRightExpression((Expression)node.getRightExpression().acceptVisitor(this)); 1079 return node; 1080 } 1081} 1082 | Popular Tags |