1 11 package org.eclipse.jdt.internal.core.dom.rewrite; 12 13 import java.util.List ; 14 15 import org.eclipse.jdt.core.dom.*; 16 import org.eclipse.jdt.internal.compiler.util.Util; 17 18 public class ASTRewriteFlattener extends ASTVisitor { 19 20 25 static final int JLS2_INTERNAL = AST.JLS2; 26 27 public static String asString(ASTNode node, RewriteEventStore store) { 28 ASTRewriteFlattener flattener= new ASTRewriteFlattener(store); 29 node.accept(flattener); 30 return flattener.getResult(); 31 } 32 33 protected StringBuffer result; 34 private RewriteEventStore store; 35 36 public ASTRewriteFlattener(RewriteEventStore store) { 37 this.store= store; 38 this.result= new StringBuffer (); 39 } 40 41 46 public String getResult() { 47 return new String (this.result.toString()); 49 } 50 51 54 public void reset() { 55 this.result.setLength(0); 56 } 57 58 64 public static void printModifiers(int modifiers, StringBuffer buf) { 65 if (Modifier.isPublic(modifiers)) { 66 buf.append("public "); } 68 if (Modifier.isProtected(modifiers)) { 69 buf.append("protected "); } 71 if (Modifier.isPrivate(modifiers)) { 72 buf.append("private "); } 74 if (Modifier.isStatic(modifiers)) { 75 buf.append("static "); } 77 if (Modifier.isAbstract(modifiers)) { 78 buf.append("abstract "); } 80 if (Modifier.isFinal(modifiers)) { 81 buf.append("final "); } 83 if (Modifier.isSynchronized(modifiers)) { 84 buf.append("synchronized "); } 86 if (Modifier.isVolatile(modifiers)) { 87 buf.append("volatile "); } 89 if (Modifier.isNative(modifiers)) { 90 buf.append("native "); } 92 if (Modifier.isStrictfp(modifiers)) { 93 buf.append("strictfp "); } 95 if (Modifier.isTransient(modifiers)) { 96 buf.append("transient "); } 98 } 99 100 protected List getChildList(ASTNode parent, StructuralPropertyDescriptor childProperty) { 101 return (List ) getAttribute(parent, childProperty); 102 } 103 104 protected ASTNode getChildNode(ASTNode parent, StructuralPropertyDescriptor childProperty) { 105 return (ASTNode) getAttribute(parent, childProperty); 106 } 107 108 protected int getIntAttribute(ASTNode parent, StructuralPropertyDescriptor childProperty) { 109 return ((Integer ) getAttribute(parent, childProperty)).intValue(); 110 } 111 112 protected boolean getBooleanAttribute(ASTNode parent, StructuralPropertyDescriptor childProperty) { 113 return ((Boolean ) getAttribute(parent, childProperty)).booleanValue(); 114 } 115 116 protected Object getAttribute(ASTNode parent, StructuralPropertyDescriptor childProperty) { 117 return this.store.getNewValue(parent, childProperty); 118 } 119 120 protected void visitList(ASTNode parent, StructuralPropertyDescriptor childProperty, String separator) { 121 List list= getChildList(parent, childProperty); 122 for (int i= 0; i < list.size(); i++) { 123 if (separator != null && i > 0) { 124 this.result.append(separator); 125 } 126 ((ASTNode) list.get(i)).accept(this); 127 } 128 } 129 130 protected void visitList(ASTNode parent, StructuralPropertyDescriptor childProperty, String separator, String lead, String post) { 131 List list= getChildList(parent, childProperty); 132 if (!list.isEmpty()) { 133 this.result.append(lead); 134 for (int i= 0; i < list.size(); i++) { 135 if (separator != null && i > 0) { 136 this.result.append(separator); 137 } 138 ((ASTNode) list.get(i)).accept(this); 139 } 140 this.result.append(post); 141 } 142 } 143 144 145 148 public boolean visit(AnonymousClassDeclaration node) { 149 this.result.append('{'); 150 visitList(node, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY, null); 151 this.result.append('}'); 152 return false; 153 } 154 155 158 public boolean visit(ArrayAccess node) { 159 getChildNode(node, ArrayAccess.ARRAY_PROPERTY).accept(this); 160 this.result.append('['); 161 getChildNode(node, ArrayAccess.INDEX_PROPERTY).accept(this); 162 this.result.append(']'); 163 return false; 164 } 165 166 169 public boolean visit(ArrayCreation node) { 170 this.result.append("new "); ArrayType arrayType= (ArrayType) getChildNode(node, ArrayCreation.TYPE_PROPERTY); 172 173 Type elementType= (Type) getChildNode(arrayType, ArrayType.COMPONENT_TYPE_PROPERTY); 175 int dimensions= 1; while (elementType.isArrayType()) { 177 dimensions++; 178 elementType = (Type) getChildNode(elementType, ArrayType.COMPONENT_TYPE_PROPERTY); 179 } 180 181 elementType.accept(this); 182 183 List list= getChildList(node, ArrayCreation.DIMENSIONS_PROPERTY); 184 for (int i= 0; i < list.size(); i++) { 185 this.result.append('['); 186 ((ASTNode) list.get(i)).accept(this); 187 this.result.append(']'); 188 dimensions--; 189 } 190 191 for (int i= 0; i < dimensions; i++) { 193 this.result.append("[]"); } 195 ASTNode initializer= getChildNode(node, ArrayCreation.INITIALIZER_PROPERTY); 196 if (initializer != null) { 197 getChildNode(node, ArrayCreation.INITIALIZER_PROPERTY).accept(this); 198 } 199 return false; 200 } 201 202 205 public boolean visit(ArrayInitializer node) { 206 this.result.append('{'); 207 visitList(node, ArrayInitializer.EXPRESSIONS_PROPERTY, String.valueOf(',')); 208 this.result.append('}'); 209 return false; 210 } 211 212 215 public boolean visit(ArrayType node) { 216 getChildNode(node, ArrayType.COMPONENT_TYPE_PROPERTY).accept(this); 217 this.result.append("[]"); return false; 219 } 220 221 224 public boolean visit(AssertStatement node) { 225 this.result.append("assert "); getChildNode(node, AssertStatement.EXPRESSION_PROPERTY).accept(this); 227 228 ASTNode message= getChildNode(node, AssertStatement.MESSAGE_PROPERTY); 229 if (message != null) { 230 this.result.append(':'); 231 message.accept(this); 232 } 233 this.result.append(';'); 234 return false; 235 } 236 237 240 public boolean visit(Assignment node) { 241 getChildNode(node, Assignment.LEFT_HAND_SIDE_PROPERTY).accept(this); 242 this.result.append(getAttribute(node, Assignment.OPERATOR_PROPERTY).toString()); 243 getChildNode(node, Assignment.RIGHT_HAND_SIDE_PROPERTY).accept(this); 244 return false; 245 } 246 247 248 249 252 public boolean visit(Block node) { 253 this.result.append('{'); 254 visitList(node, Block.STATEMENTS_PROPERTY, null); 255 this.result.append('}'); 256 return false; 257 } 258 259 262 public boolean visit(BooleanLiteral node) { 263 if (node.booleanValue() == true) { 264 this.result.append("true"); } else { 266 this.result.append("false"); } 268 return false; 269 } 270 271 274 public boolean visit(BreakStatement node) { 275 this.result.append("break"); ASTNode label= getChildNode(node, BreakStatement.LABEL_PROPERTY); 277 if (label != null) { 278 this.result.append(' '); 279 label.accept(this); 280 } 281 this.result.append(';'); 282 return false; 283 } 284 285 288 public boolean visit(CastExpression node) { 289 this.result.append('('); 290 getChildNode(node, CastExpression.TYPE_PROPERTY).accept(this); 291 this.result.append(')'); 292 getChildNode(node, CastExpression.EXPRESSION_PROPERTY).accept(this); 293 return false; 294 } 295 296 299 public boolean visit(CatchClause node) { 300 this.result.append("catch ("); getChildNode(node, CatchClause.EXCEPTION_PROPERTY).accept(this); 302 this.result.append(')'); 303 getChildNode(node, CatchClause.BODY_PROPERTY).accept(this); 304 return false; 305 } 306 307 310 public boolean visit(CharacterLiteral node) { 311 this.result.append(getAttribute(node, CharacterLiteral.ESCAPED_VALUE_PROPERTY)); 312 return false; 313 } 314 315 318 public boolean visit(ClassInstanceCreation node) { 319 ASTNode expression= getChildNode(node, ClassInstanceCreation.EXPRESSION_PROPERTY); 320 if (expression != null) { 321 expression.accept(this); 322 this.result.append('.'); 323 } 324 this.result.append("new "); if (node.getAST().apiLevel() == JLS2_INTERNAL) { 326 getChildNode(node, ClassInstanceCreation.NAME_PROPERTY).accept(this); 327 } else { 328 visitList(node, ClassInstanceCreation.TYPE_ARGUMENTS_PROPERTY, String.valueOf(','), String.valueOf('<'), String.valueOf('>')); 329 getChildNode(node, ClassInstanceCreation.TYPE_PROPERTY).accept(this); 330 } 331 332 this.result.append('('); 333 visitList(node, ClassInstanceCreation.ARGUMENTS_PROPERTY, String.valueOf(',')); 334 this.result.append(')'); 335 ASTNode decl= getChildNode(node, ClassInstanceCreation.ANONYMOUS_CLASS_DECLARATION_PROPERTY); 336 if (decl != null) { 337 decl.accept(this); 338 } 339 return false; 340 } 341 342 345 public boolean visit(CompilationUnit node) { 346 ASTNode pack= getChildNode(node, CompilationUnit.PACKAGE_PROPERTY); 347 if (pack != null) { 348 pack.accept(this); 349 } 350 visitList(node, CompilationUnit.IMPORTS_PROPERTY, null); 351 visitList(node, CompilationUnit.TYPES_PROPERTY, null); 352 return false; 353 } 354 355 358 public boolean visit(ConditionalExpression node) { 359 getChildNode(node, ConditionalExpression.EXPRESSION_PROPERTY).accept(this); 360 this.result.append('?'); 361 getChildNode(node, ConditionalExpression.THEN_EXPRESSION_PROPERTY).accept(this); 362 this.result.append(':'); 363 getChildNode(node, ConditionalExpression.ELSE_EXPRESSION_PROPERTY).accept(this); 364 return false; 365 } 366 367 370 public boolean visit(ConstructorInvocation node) { 371 if (node.getAST().apiLevel() >= AST.JLS3) { 372 visitList(node, ConstructorInvocation.TYPE_ARGUMENTS_PROPERTY, String.valueOf(','), String.valueOf('<'), String.valueOf('>')); 373 } 374 this.result.append("this("); visitList(node, ConstructorInvocation.ARGUMENTS_PROPERTY, String.valueOf(',')); 376 this.result.append(");"); return false; 378 } 379 380 383 public boolean visit(ContinueStatement node) { 384 this.result.append("continue"); ASTNode label= getChildNode(node, ContinueStatement.LABEL_PROPERTY); 386 if (label != null) { 387 this.result.append(' '); 388 label.accept(this); 389 } 390 this.result.append(';'); 391 return false; 392 } 393 394 397 public boolean visit(DoStatement node) { 398 this.result.append("do "); getChildNode(node, DoStatement.BODY_PROPERTY).accept(this); 400 this.result.append(" while ("); getChildNode(node, DoStatement.EXPRESSION_PROPERTY).accept(this); 402 this.result.append(");"); return false; 404 } 405 406 409 public boolean visit(EmptyStatement node) { 410 this.result.append(';'); 411 return false; 412 } 413 414 417 public boolean visit(ExpressionStatement node) { 418 getChildNode(node, ExpressionStatement.EXPRESSION_PROPERTY).accept(this); 419 this.result.append(';'); 420 return false; 421 } 422 423 426 public boolean visit(FieldAccess node) { 427 getChildNode(node, FieldAccess.EXPRESSION_PROPERTY).accept(this); 428 this.result.append('.'); 429 getChildNode(node, FieldAccess.NAME_PROPERTY).accept(this); 430 return false; 431 } 432 433 436 public boolean visit(FieldDeclaration node) { 437 ASTNode javadoc= getChildNode(node, FieldDeclaration.JAVADOC_PROPERTY); 438 if (javadoc != null) { 439 javadoc.accept(this); 440 } 441 if (node.getAST().apiLevel() == JLS2_INTERNAL) { 442 printModifiers(getIntAttribute(node, FieldDeclaration.MODIFIERS_PROPERTY), this.result); 443 } else { 444 visitList(node, FieldDeclaration.MODIFIERS2_PROPERTY, String.valueOf(' '), Util.EMPTY_STRING, String.valueOf(' ')); 445 } 446 getChildNode(node, FieldDeclaration.TYPE_PROPERTY).accept(this); 447 this.result.append(' '); 448 visitList(node, FieldDeclaration.FRAGMENTS_PROPERTY, String.valueOf(',')); 449 this.result.append(';'); 450 return false; 451 } 452 453 456 public boolean visit(ForStatement node) { 457 this.result.append("for ("); visitList(node, ForStatement.INITIALIZERS_PROPERTY, String.valueOf(',')); 459 this.result.append(';'); 460 ASTNode expression= getChildNode(node, ForStatement.EXPRESSION_PROPERTY); 461 if (expression != null) { 462 expression.accept(this); 463 } 464 this.result.append(';'); 465 visitList(node, ForStatement.UPDATERS_PROPERTY, String.valueOf(',')); 466 this.result.append(')'); 467 getChildNode(node, ForStatement.BODY_PROPERTY).accept(this); 468 return false; 469 } 470 471 474 public boolean visit(IfStatement node) { 475 this.result.append("if ("); getChildNode(node, IfStatement.EXPRESSION_PROPERTY).accept(this); 477 this.result.append(')'); 478 getChildNode(node, IfStatement.THEN_STATEMENT_PROPERTY).accept(this); 479 ASTNode elseStatement= getChildNode(node, IfStatement.ELSE_STATEMENT_PROPERTY); 480 if (elseStatement != null) { 481 this.result.append(" else "); elseStatement.accept(this); 483 } 484 return false; 485 } 486 487 490 public boolean visit(ImportDeclaration node) { 491 this.result.append("import "); if (node.getAST().apiLevel() >= AST.JLS3) { 493 if (getBooleanAttribute(node, ImportDeclaration.STATIC_PROPERTY)) { 494 this.result.append("static "); } 496 } 497 getChildNode(node, ImportDeclaration.NAME_PROPERTY).accept(this); 498 if (getBooleanAttribute(node, ImportDeclaration.ON_DEMAND_PROPERTY)) { 499 this.result.append(".*"); } 501 this.result.append(';'); 502 return false; 503 } 504 505 506 507 510 public boolean visit(InfixExpression node) { 511 getChildNode(node, InfixExpression.LEFT_OPERAND_PROPERTY).accept(this); 512 this.result.append(' '); 513 String operator= getAttribute(node, InfixExpression.OPERATOR_PROPERTY).toString(); 514 515 this.result.append(operator); 516 this.result.append(' '); 517 getChildNode(node, InfixExpression.RIGHT_OPERAND_PROPERTY).accept(this); 518 519 List list= getChildList(node, InfixExpression.EXTENDED_OPERANDS_PROPERTY); 520 for (int i= 0; i < list.size(); i++) { 521 this.result.append(operator); 522 ((ASTNode) list.get(i)).accept(this); 523 } 524 return false; 525 } 526 527 530 public boolean visit(InstanceofExpression node) { 531 getChildNode(node, InstanceofExpression.LEFT_OPERAND_PROPERTY).accept(this); 532 this.result.append(" instanceof "); getChildNode(node, InstanceofExpression.RIGHT_OPERAND_PROPERTY).accept(this); 534 return false; 535 } 536 537 540 public boolean visit(Initializer node) { 541 ASTNode javadoc= getChildNode(node, Initializer.JAVADOC_PROPERTY); 542 if (javadoc != null) { 543 javadoc.accept(this); 544 } 545 if (node.getAST().apiLevel() == JLS2_INTERNAL) { 546 printModifiers(getIntAttribute(node, Initializer.MODIFIERS_PROPERTY), this.result); 547 } else { 548 visitList(node, Initializer.MODIFIERS2_PROPERTY, String.valueOf(' '), Util.EMPTY_STRING, String.valueOf(' ')); 549 } 550 getChildNode(node, Initializer.BODY_PROPERTY).accept(this); 551 return false; 552 } 553 554 557 public boolean visit(Javadoc node) { 558 this.result.append("/**"); List list= getChildList(node, Javadoc.TAGS_PROPERTY); 560 for (int i= 0; i < list.size(); i++) { 561 this.result.append("\n * "); ((ASTNode) list.get(i)).accept(this); 563 } 564 this.result.append("\n */"); return false; 566 } 567 568 571 public boolean visit(LabeledStatement node) { 572 getChildNode(node, LabeledStatement.LABEL_PROPERTY).accept(this); 573 this.result.append(": "); getChildNode(node, LabeledStatement.BODY_PROPERTY).accept(this); 575 return false; 576 } 577 578 581 public boolean visit(MethodDeclaration node) { 582 ASTNode javadoc= getChildNode(node, MethodDeclaration.JAVADOC_PROPERTY); 583 if (javadoc != null) { 584 javadoc.accept(this); 585 } 586 if (node.getAST().apiLevel() == JLS2_INTERNAL) { 587 printModifiers(getIntAttribute(node, MethodDeclaration.MODIFIERS_PROPERTY), this.result); 588 } else { 589 visitList(node, MethodDeclaration.MODIFIERS2_PROPERTY, String.valueOf(' '), Util.EMPTY_STRING, String.valueOf(' ')); 590 visitList(node, MethodDeclaration.TYPE_PARAMETERS_PROPERTY, String.valueOf(','), String.valueOf('<'), String.valueOf('>')); 591 } 592 593 if (!getBooleanAttribute(node, MethodDeclaration.CONSTRUCTOR_PROPERTY)) { 594 if (node.getAST().apiLevel() == JLS2_INTERNAL) { 595 getChildNode(node, MethodDeclaration.RETURN_TYPE_PROPERTY).accept(this); 596 } else { 597 ASTNode returnType = getChildNode(node, MethodDeclaration.RETURN_TYPE2_PROPERTY); 598 if (returnType != null) { 599 returnType.accept(this); 600 } else { 601 this.result.append("void"); } 604 } 605 this.result.append(' '); 606 } 607 getChildNode(node, MethodDeclaration.NAME_PROPERTY).accept(this); 608 this.result.append('('); 609 visitList(node, MethodDeclaration.PARAMETERS_PROPERTY, String.valueOf(',')); 610 this.result.append(')'); 611 int extraDims= getIntAttribute(node, MethodDeclaration.EXTRA_DIMENSIONS_PROPERTY); 612 for (int i = 0; i < extraDims; i++) { 613 this.result.append("[]"); } 615 visitList(node, MethodDeclaration.THROWN_EXCEPTIONS_PROPERTY, String.valueOf(','), " throws ", Util.EMPTY_STRING); ASTNode body= getChildNode(node, MethodDeclaration.BODY_PROPERTY); 617 if (body == null) { 618 this.result.append(';'); 619 } else { 620 body.accept(this); 621 } 622 return false; 623 } 624 625 628 public boolean visit(MethodInvocation node) { 629 ASTNode expression= getChildNode(node, MethodInvocation.EXPRESSION_PROPERTY); 630 if (expression != null) { 631 expression.accept(this); 632 this.result.append('.'); 633 } 634 if (node.getAST().apiLevel() >= AST.JLS3) { 635 visitList(node, MethodInvocation.TYPE_ARGUMENTS_PROPERTY, String.valueOf(','), String.valueOf('<'), String.valueOf('>')); 636 } 637 638 getChildNode(node, MethodInvocation.NAME_PROPERTY).accept(this); 639 this.result.append('('); 640 visitList(node, MethodInvocation.ARGUMENTS_PROPERTY, String.valueOf(',')); 641 this.result.append(')'); 642 return false; 643 } 644 645 648 public boolean visit(NullLiteral node) { 649 this.result.append("null"); return false; 651 } 652 653 656 public boolean visit(NumberLiteral node) { 657 this.result.append(getAttribute(node, NumberLiteral.TOKEN_PROPERTY).toString()); 658 return false; 659 } 660 661 664 public boolean visit(PackageDeclaration node) { 665 if (node.getAST().apiLevel() >= AST.JLS3) { 666 ASTNode javadoc = getChildNode(node, PackageDeclaration.JAVADOC_PROPERTY); 667 if (javadoc != null) { 668 javadoc.accept(this); 669 } 670 visitList(node, PackageDeclaration.ANNOTATIONS_PROPERTY, String.valueOf(' ')); 671 } 672 this.result.append("package "); getChildNode(node, PackageDeclaration.NAME_PROPERTY).accept(this); 674 this.result.append(';'); 675 return false; 676 } 677 678 681 public boolean visit(ParenthesizedExpression node) { 682 this.result.append('('); 683 getChildNode(node, ParenthesizedExpression.EXPRESSION_PROPERTY).accept(this); 684 this.result.append(')'); 685 return false; 686 } 687 688 691 public boolean visit(PostfixExpression node) { 692 getChildNode(node, PostfixExpression.OPERAND_PROPERTY).accept(this); 693 this.result.append(getAttribute(node, PostfixExpression.OPERATOR_PROPERTY).toString()); 694 return false; 695 } 696 697 700 public boolean visit(PrefixExpression node) { 701 this.result.append(getAttribute(node, PrefixExpression.OPERATOR_PROPERTY).toString()); 702 getChildNode(node, PrefixExpression.OPERAND_PROPERTY).accept(this); 703 return false; 704 } 705 706 709 public boolean visit(PrimitiveType node) { 710 this.result.append(getAttribute(node, PrimitiveType.PRIMITIVE_TYPE_CODE_PROPERTY).toString()); 711 return false; 712 } 713 714 717 public boolean visit(QualifiedName node) { 718 getChildNode(node, QualifiedName.QUALIFIER_PROPERTY).accept(this); 719 this.result.append('.'); 720 getChildNode(node, QualifiedName.NAME_PROPERTY).accept(this); 721 return false; 722 } 723 724 727 public boolean visit(ReturnStatement node) { 728 this.result.append("return"); ASTNode expression= getChildNode(node, ReturnStatement.EXPRESSION_PROPERTY); 730 if (expression != null) { 731 this.result.append(' '); 732 expression.accept(this); 733 } 734 this.result.append(';'); 735 return false; 736 } 737 738 741 public boolean visit(SimpleName node) { 742 this.result.append(getAttribute(node, SimpleName.IDENTIFIER_PROPERTY)); 743 return false; 744 } 745 746 749 public boolean visit(SimpleType node) { 750 return true; 751 } 752 753 756 public boolean visit(SingleVariableDeclaration node) { 757 if (node.getAST().apiLevel() == JLS2_INTERNAL) { 758 printModifiers(getIntAttribute(node, SingleVariableDeclaration.MODIFIERS_PROPERTY), this.result); 759 } else { 760 visitList(node, SingleVariableDeclaration.MODIFIERS2_PROPERTY, String.valueOf(' '), Util.EMPTY_STRING, String.valueOf(' ')); 761 } 762 getChildNode(node, SingleVariableDeclaration.TYPE_PROPERTY).accept(this); 763 if (node.getAST().apiLevel() >= AST.JLS3) { 764 if (getBooleanAttribute(node, SingleVariableDeclaration.VARARGS_PROPERTY)) { 765 this.result.append("..."); } 767 } 768 this.result.append(' '); 769 getChildNode(node, SingleVariableDeclaration.NAME_PROPERTY).accept(this); 770 int extraDimensions= getIntAttribute(node, SingleVariableDeclaration.EXTRA_DIMENSIONS_PROPERTY); 771 for (int i = 0; i < extraDimensions; i++) { 772 this.result.append("[]"); } 774 ASTNode initializer= getChildNode(node, SingleVariableDeclaration.INITIALIZER_PROPERTY); 775 if (initializer != null) { 776 this.result.append('='); 777 initializer.accept(this); 778 } 779 return false; 780 } 781 782 785 public boolean visit(StringLiteral node) { 786 this.result.append(getAttribute(node, StringLiteral.ESCAPED_VALUE_PROPERTY)); 787 return false; 788 } 789 790 793 public boolean visit(SuperConstructorInvocation node) { 794 ASTNode expression= getChildNode(node, SuperConstructorInvocation.EXPRESSION_PROPERTY); 795 if (expression != null) { 796 expression.accept(this); 797 this.result.append('.'); 798 } 799 if (node.getAST().apiLevel() >= AST.JLS3) { 800 visitList(node, SuperConstructorInvocation.TYPE_ARGUMENTS_PROPERTY, String.valueOf(','), String.valueOf('<'), String.valueOf('>')); 801 } 802 this.result.append("super("); visitList(node, SuperConstructorInvocation.ARGUMENTS_PROPERTY, String.valueOf(',')); 804 this.result.append(");"); return false; 806 } 807 808 811 public boolean visit(SuperFieldAccess node) { 812 ASTNode qualifier= getChildNode(node, SuperFieldAccess.QUALIFIER_PROPERTY); 813 if (qualifier != null) { 814 qualifier.accept(this); 815 this.result.append('.'); 816 } 817 this.result.append("super."); getChildNode(node, SuperFieldAccess.NAME_PROPERTY).accept(this); 819 return false; 820 } 821 822 825 public boolean visit(SuperMethodInvocation node) { 826 ASTNode qualifier= getChildNode(node, SuperMethodInvocation.QUALIFIER_PROPERTY); 827 if (qualifier != null) { 828 qualifier.accept(this); 829 this.result.append('.'); 830 } 831 this.result.append("super."); if (node.getAST().apiLevel() >= AST.JLS3) { 833 visitList(node, SuperMethodInvocation.TYPE_ARGUMENTS_PROPERTY, String.valueOf(','), String.valueOf('<'), String.valueOf('>')); 834 } 835 getChildNode(node, SuperMethodInvocation.NAME_PROPERTY).accept(this); 836 this.result.append('('); 837 visitList(node, SuperMethodInvocation.ARGUMENTS_PROPERTY, String.valueOf(',')); 838 this.result.append(')'); 839 return false; 840 } 841 842 845 public boolean visit(SwitchCase node) { 846 ASTNode expression= getChildNode(node, SwitchCase.EXPRESSION_PROPERTY); 847 if (expression == null) { 848 this.result.append("default"); } else { 850 this.result.append("case "); expression.accept(this); 852 } 853 this.result.append(':'); 854 return false; 855 } 856 857 860 public boolean visit(SwitchStatement node) { 861 this.result.append("switch ("); getChildNode(node, SwitchStatement.EXPRESSION_PROPERTY).accept(this); 863 this.result.append(')'); 864 this.result.append('{'); 865 visitList(node, SwitchStatement.STATEMENTS_PROPERTY, null); 866 this.result.append('}'); 867 return false; 868 } 869 870 873 public boolean visit(SynchronizedStatement node) { 874 this.result.append("synchronized ("); getChildNode(node, SynchronizedStatement.EXPRESSION_PROPERTY).accept(this); 876 this.result.append(')'); 877 getChildNode(node, SynchronizedStatement.BODY_PROPERTY).accept(this); 878 return false; 879 } 880 881 884 public boolean visit(ThisExpression node) { 885 ASTNode qualifier= getChildNode(node, ThisExpression.QUALIFIER_PROPERTY); 886 if (qualifier != null) { 887 qualifier.accept(this); 888 this.result.append('.'); 889 } 890 this.result.append("this"); return false; 892 } 893 894 897 public boolean visit(ThrowStatement node) { 898 this.result.append("throw "); getChildNode(node, ThrowStatement.EXPRESSION_PROPERTY).accept(this); 900 this.result.append(';'); 901 return false; 902 } 903 904 907 public boolean visit(TryStatement node) { 908 this.result.append("try "); getChildNode(node, TryStatement.BODY_PROPERTY).accept(this); 910 this.result.append(' '); 911 visitList(node, TryStatement.CATCH_CLAUSES_PROPERTY, null); 912 ASTNode finallyClause= getChildNode(node, TryStatement.FINALLY_PROPERTY); 913 if (finallyClause != null) { 914 this.result.append(" finally "); finallyClause.accept(this); 916 } 917 return false; 918 } 919 920 923 public boolean visit(TypeDeclaration node) { 924 int apiLevel= node.getAST().apiLevel(); 925 926 ASTNode javadoc= getChildNode(node, TypeDeclaration.JAVADOC_PROPERTY); 927 if (javadoc != null) { 928 javadoc.accept(this); 929 } 930 931 if (apiLevel == JLS2_INTERNAL) { 932 printModifiers(getIntAttribute(node, TypeDeclaration.MODIFIERS_PROPERTY), this.result); 933 } else { 934 visitList(node, TypeDeclaration.MODIFIERS2_PROPERTY, String.valueOf(' '), Util.EMPTY_STRING, String.valueOf(' ')); 935 } 936 937 boolean isInterface= getBooleanAttribute(node, TypeDeclaration.INTERFACE_PROPERTY); 938 this.result.append(isInterface ? "interface " : "class "); getChildNode(node, TypeDeclaration.NAME_PROPERTY).accept(this); 940 if (apiLevel >= AST.JLS3) { 941 visitList(node, TypeDeclaration.TYPE_PARAMETERS_PROPERTY, String.valueOf(','), String.valueOf('<'), String.valueOf('>')); 942 } 943 944 this.result.append(' '); 945 946 ChildPropertyDescriptor superClassProperty= (apiLevel == JLS2_INTERNAL) ? TypeDeclaration.SUPERCLASS_PROPERTY : TypeDeclaration.SUPERCLASS_TYPE_PROPERTY; 947 ASTNode superclass= getChildNode(node, superClassProperty); 948 if (superclass != null) { 949 this.result.append("extends "); superclass.accept(this); 951 this.result.append(' '); 952 } 953 954 ChildListPropertyDescriptor superInterfaceProperty= (apiLevel == JLS2_INTERNAL) ? TypeDeclaration.SUPER_INTERFACES_PROPERTY : TypeDeclaration.SUPER_INTERFACE_TYPES_PROPERTY; 955 String lead= isInterface ? "extends " : "implements "; visitList(node, superInterfaceProperty, String.valueOf(','), lead, Util.EMPTY_STRING); 957 this.result.append('{'); 958 visitList(node, TypeDeclaration.BODY_DECLARATIONS_PROPERTY, null); 959 this.result.append('}'); 960 return false; 961 } 962 963 966 public boolean visit(TypeDeclarationStatement node) { 967 if (node.getAST().apiLevel() == JLS2_INTERNAL) { 968 getChildNode(node, TypeDeclarationStatement.TYPE_DECLARATION_PROPERTY).accept(this); 969 } else { 970 getChildNode(node, TypeDeclarationStatement.DECLARATION_PROPERTY).accept(this); 971 } 972 return false; 973 } 974 975 978 public boolean visit(TypeLiteral node) { 979 getChildNode(node, TypeLiteral.TYPE_PROPERTY).accept(this); 980 this.result.append(".class"); return false; 982 } 983 984 987 public boolean visit(VariableDeclarationExpression node) { 988 if (node.getAST().apiLevel() == JLS2_INTERNAL) { 989 printModifiers(getIntAttribute(node, VariableDeclarationExpression.MODIFIERS_PROPERTY), this.result); 990 } else { 991 visitList(node, VariableDeclarationExpression.MODIFIERS2_PROPERTY, String.valueOf(' '), Util.EMPTY_STRING, String.valueOf(' ')); 992 } 993 getChildNode(node, VariableDeclarationExpression.TYPE_PROPERTY).accept(this); 994 this.result.append(' '); 995 visitList(node, VariableDeclarationExpression.FRAGMENTS_PROPERTY, String.valueOf(',')); 996 return false; 997 } 998 999 1002 public boolean visit(VariableDeclarationFragment node) { 1003 getChildNode(node, VariableDeclarationFragment.NAME_PROPERTY).accept(this); 1004 int extraDimensions= getIntAttribute(node, VariableDeclarationFragment.EXTRA_DIMENSIONS_PROPERTY); 1005 for (int i = 0; i < extraDimensions; i++) { 1006 this.result.append("[]"); } 1008 ASTNode initializer= getChildNode(node, VariableDeclarationFragment.INITIALIZER_PROPERTY); 1009 if (initializer != null) { 1010 this.result.append('='); 1011 initializer.accept(this); 1012 } 1013 return false; 1014 } 1015 1016 1019 public boolean visit(VariableDeclarationStatement node) { 1020 if (node.getAST().apiLevel() == JLS2_INTERNAL) { 1021 printModifiers(getIntAttribute(node, VariableDeclarationStatement.MODIFIERS_PROPERTY), this.result); 1022 } else { 1023 visitList(node, VariableDeclarationStatement.MODIFIERS2_PROPERTY, String.valueOf(' '), Util.EMPTY_STRING, String.valueOf(' ')); 1024 } 1025 getChildNode(node, VariableDeclarationStatement.TYPE_PROPERTY).accept(this); 1026 this.result.append(' '); 1027 visitList(node, VariableDeclarationStatement.FRAGMENTS_PROPERTY, String.valueOf(',')); 1028 this.result.append(';'); 1029 return false; 1030 } 1031 1032 1035 public boolean visit(WhileStatement node) { 1036 this.result.append("while ("); getChildNode(node, WhileStatement.EXPRESSION_PROPERTY).accept(this); 1038 this.result.append(')'); 1039 getChildNode(node, WhileStatement.BODY_PROPERTY).accept(this); 1040 return false; 1041 } 1042 1043 1044 1047 public boolean visit(BlockComment node) { 1048 return false; } 1050 1053 public boolean visit(LineComment node) { 1054 return false; } 1056 1059 public boolean visit(MemberRef node) { 1060 ASTNode qualifier= getChildNode(node, MemberRef.QUALIFIER_PROPERTY); 1061 if (qualifier != null) { 1062 qualifier.accept(this); 1063 } 1064 this.result.append('#'); 1065 getChildNode(node, MemberRef.NAME_PROPERTY).accept(this); 1066 return false; 1067 } 1068 1071 public boolean visit(MethodRef node) { 1072 ASTNode qualifier= getChildNode(node, MethodRef.QUALIFIER_PROPERTY); 1073 if (qualifier != null) { 1074 qualifier.accept(this); 1075 } 1076 this.result.append('#'); 1077 getChildNode(node, MethodRef.NAME_PROPERTY).accept(this); 1078 this.result.append('('); 1079 visitList(node, MethodRef.PARAMETERS_PROPERTY, ","); this.result.append(')'); 1081 return false; 1082 } 1083 1086 public boolean visit(MethodRefParameter node) { 1087 getChildNode(node, MethodRefParameter.TYPE_PROPERTY).accept(this); 1088 if (node.getAST().apiLevel() >= AST.JLS3) { 1089 if (getBooleanAttribute(node, MethodRefParameter.VARARGS_PROPERTY)) { 1090 this.result.append("..."); } 1092 } 1093 ASTNode name= getChildNode(node, MethodRefParameter.NAME_PROPERTY); 1094 if (name != null) { 1095 this.result.append(' '); 1096 name.accept(this); 1097 } 1098 return false; 1099 } 1100 1103 public boolean visit(TagElement node) { 1104 Object tagName= getAttribute(node, TagElement.TAG_NAME_PROPERTY); 1105 if (tagName != null) { 1106 this.result.append((String ) tagName); 1107 } 1108 List list= getChildList(node, TagElement.FRAGMENTS_PROPERTY); 1109 for (int i= 0; i < list.size(); i++) { 1110 if (i > 0 || tagName != null) { 1111 this.result.append(' '); 1112 } 1113 ASTNode curr= (ASTNode) list.get(i); 1114 if (curr instanceof TagElement) { 1115 this.result.append('{'); 1116 curr.accept(this); 1117 this.result.append('}'); 1118 } else { 1119 curr.accept(this); 1120 } 1121 } 1122 return false; 1123 } 1124 1127 public boolean visit(TextElement node) { 1128 this.result.append(getAttribute(node, TextElement.TEXT_PROPERTY)); 1129 return false; 1130 } 1131 1135 public boolean visit(AnnotationTypeDeclaration node) { 1136 ASTNode javadoc= getChildNode(node, AnnotationTypeDeclaration.JAVADOC_PROPERTY); 1137 if (javadoc != null) { 1138 javadoc.accept(this); 1139 } 1140 visitList(node, AnnotationTypeDeclaration.MODIFIERS2_PROPERTY, String.valueOf(' '), Util.EMPTY_STRING, String.valueOf(' ')); 1141 this.result.append("@interface "); getChildNode(node, AnnotationTypeDeclaration.NAME_PROPERTY).accept(this); 1143 this.result.append('{'); 1144 visitList(node, AnnotationTypeDeclaration.BODY_DECLARATIONS_PROPERTY, Util.EMPTY_STRING); 1145 this.result.append('}'); 1146 return false; 1147 } 1148 1149 1153 public boolean visit(AnnotationTypeMemberDeclaration node) { 1154 ASTNode javadoc= getChildNode(node, AnnotationTypeMemberDeclaration.JAVADOC_PROPERTY); 1155 if (javadoc != null) { 1156 javadoc.accept(this); 1157 } 1158 visitList(node, AnnotationTypeMemberDeclaration.MODIFIERS2_PROPERTY, String.valueOf(' '), Util.EMPTY_STRING, String.valueOf(' ')); 1159 getChildNode(node, AnnotationTypeMemberDeclaration.TYPE_PROPERTY).accept(this); 1160 this.result.append(' '); 1161 getChildNode(node, AnnotationTypeMemberDeclaration.NAME_PROPERTY).accept(this); 1162 this.result.append("()"); ASTNode def= getChildNode(node, AnnotationTypeMemberDeclaration.DEFAULT_PROPERTY); 1164 if (def != null) { 1165 this.result.append(" default "); def.accept(this); 1167 } 1168 this.result.append(';'); 1169 return false; 1170 } 1171 1172 1176 public boolean visit(EnhancedForStatement node) { 1177 this.result.append("for ("); getChildNode(node, EnhancedForStatement.PARAMETER_PROPERTY).accept(this); 1179 this.result.append(':'); 1180 getChildNode(node, EnhancedForStatement.EXPRESSION_PROPERTY).accept(this); 1181 this.result.append(')'); 1182 getChildNode(node, EnhancedForStatement.BODY_PROPERTY).accept(this); 1183 return false; 1184 } 1185 1186 1190 public boolean visit(EnumConstantDeclaration node) { 1191 ASTNode javadoc= getChildNode(node, EnumConstantDeclaration.JAVADOC_PROPERTY); 1192 if (javadoc != null) { 1193 javadoc.accept(this); 1194 } 1195 visitList(node, EnumConstantDeclaration.MODIFIERS2_PROPERTY, String.valueOf(' '), Util.EMPTY_STRING, String.valueOf(' ')); 1196 getChildNode(node, EnumConstantDeclaration.NAME_PROPERTY).accept(this); 1197 visitList(node, EnumConstantDeclaration.ARGUMENTS_PROPERTY, String.valueOf(','), String.valueOf('('), String.valueOf(')')); 1198 ASTNode classDecl= getChildNode(node, EnumConstantDeclaration.ANONYMOUS_CLASS_DECLARATION_PROPERTY); 1199 if (classDecl != null) { 1200 classDecl.accept(this); 1201 } 1202 return false; 1203 } 1204 1205 1209 public boolean visit(EnumDeclaration node) { 1210 ASTNode javadoc= getChildNode(node, EnumDeclaration.JAVADOC_PROPERTY); 1211 if (javadoc != null) { 1212 javadoc.accept(this); 1213 } 1214 visitList(node, EnumDeclaration.MODIFIERS2_PROPERTY, String.valueOf(' '), Util.EMPTY_STRING, String.valueOf(' ')); 1215 this.result.append("enum "); getChildNode(node, EnumDeclaration.NAME_PROPERTY).accept(this); 1217 this.result.append(' '); 1218 visitList(node, EnumDeclaration.SUPER_INTERFACE_TYPES_PROPERTY, String.valueOf(','), "implements ", Util.EMPTY_STRING); 1220 this.result.append('{'); 1221 visitList(node, EnumDeclaration.ENUM_CONSTANTS_PROPERTY, String.valueOf(','), Util.EMPTY_STRING, Util.EMPTY_STRING); 1222 visitList(node, EnumDeclaration.BODY_DECLARATIONS_PROPERTY, Util.EMPTY_STRING, String.valueOf(';'), Util.EMPTY_STRING); 1223 this.result.append('}'); 1224 return false; 1225 } 1226 1230 public boolean visit(MarkerAnnotation node) { 1231 this.result.append('@'); 1232 getChildNode(node, MarkerAnnotation.TYPE_NAME_PROPERTY).accept(this); 1233 return false; 1234 } 1235 1236 1240 public boolean visit(MemberValuePair node) { 1241 getChildNode(node, MemberValuePair.NAME_PROPERTY).accept(this); 1242 this.result.append('='); 1243 getChildNode(node, MemberValuePair.VALUE_PROPERTY).accept(this); 1244 return false; 1245 } 1246 1250 public boolean visit(Modifier node) { 1251 this.result.append(getAttribute(node, Modifier.KEYWORD_PROPERTY).toString()); 1252 return false; 1253 } 1254 1255 1259 public boolean visit(NormalAnnotation node) { 1260 this.result.append('@'); 1261 getChildNode(node, NormalAnnotation.TYPE_NAME_PROPERTY).accept(this); 1262 this.result.append('('); 1263 visitList(node, NormalAnnotation.VALUES_PROPERTY, ", "); this.result.append(')'); 1265 return false; 1266 } 1267 1271 public boolean visit(ParameterizedType node) { 1272 getChildNode(node, ParameterizedType.TYPE_PROPERTY).accept(this); 1273 this.result.append('<'); 1274 visitList(node, ParameterizedType.TYPE_ARGUMENTS_PROPERTY, ", "); this.result.append('>'); 1276 return false; 1277 } 1278 1279 1283 public boolean visit(QualifiedType node) { 1284 getChildNode(node, QualifiedType.QUALIFIER_PROPERTY).accept(this); 1285 this.result.append('.'); 1286 getChildNode(node, QualifiedType.NAME_PROPERTY).accept(this); 1287 return false; 1288 } 1289 1290 1293 public boolean visit(SingleMemberAnnotation node) { 1294 this.result.append('@'); 1295 getChildNode(node, SingleMemberAnnotation.TYPE_NAME_PROPERTY).accept(this); 1296 this.result.append('('); 1297 getChildNode(node, SingleMemberAnnotation.VALUE_PROPERTY).accept(this); 1298 this.result.append(')'); 1299 return false; 1300 } 1301 1302 1305 public boolean visit(TypeParameter node) { 1306 getChildNode(node, TypeParameter.NAME_PROPERTY).accept(this); 1307 visitList(node, TypeParameter.TYPE_BOUNDS_PROPERTY, " & ", " extends ", Util.EMPTY_STRING); return false; 1309 } 1310 1311 1314 public boolean visit(WildcardType node) { 1315 this.result.append('?'); 1316 ASTNode bound = getChildNode(node, WildcardType.BOUND_PROPERTY); 1317 if (bound != null) { 1318 if (getBooleanAttribute(node, WildcardType.UPPER_BOUND_PROPERTY)) { 1319 this.result.append(" extends "); } else { 1321 this.result.append(" super "); } 1323 bound.accept(this); 1324 } 1325 return false; 1326 } 1327} 1328 | Popular Tags |