1 11 package org.eclipse.jdt.core.dom; 12 13 import java.util.Iterator ; 14 import java.util.List ; 15 16 37 class NaiveASTFlattener extends ASTVisitor { 38 39 43 private StringBuffer buffer; 44 45 private int indent = 0; 46 47 50 NaiveASTFlattener() { 51 this.buffer = new StringBuffer (); 52 } 53 54 59 public String getResult() { 60 return this.buffer.toString(); 61 } 62 63 66 public void reset() { 67 this.buffer.setLength(0); 68 } 69 70 void printIndent() { 71 for (int i = 0; i < this.indent; i++) 72 this.buffer.append(" "); } 74 75 82 void printModifiers(List ext) { 83 for (Iterator it = ext.iterator(); it.hasNext(); ) { 84 ASTNode p = (ASTNode) it.next(); 85 p.accept(this); 86 this.buffer.append(" "); } 88 } 89 90 96 void printModifiers(int modifiers) { 97 if (Modifier.isPublic(modifiers)) { 98 this.buffer.append("public "); } 100 if (Modifier.isProtected(modifiers)) { 101 this.buffer.append("protected "); } 103 if (Modifier.isPrivate(modifiers)) { 104 this.buffer.append("private "); } 106 if (Modifier.isStatic(modifiers)) { 107 this.buffer.append("static "); } 109 if (Modifier.isAbstract(modifiers)) { 110 this.buffer.append("abstract "); } 112 if (Modifier.isFinal(modifiers)) { 113 this.buffer.append("final "); } 115 if (Modifier.isSynchronized(modifiers)) { 116 this.buffer.append("synchronized "); } 118 if (Modifier.isVolatile(modifiers)) { 119 this.buffer.append("volatile "); } 121 if (Modifier.isNative(modifiers)) { 122 this.buffer.append("native "); } 124 if (Modifier.isStrictfp(modifiers)) { 125 this.buffer.append("strictfp "); } 127 if (Modifier.isTransient(modifiers)) { 128 this.buffer.append("transient "); } 130 } 131 132 136 public boolean visit(AnnotationTypeDeclaration node) { 137 if (node.getJavadoc() != null) { 138 node.getJavadoc().accept(this); 139 } 140 printIndent(); 141 printModifiers(node.modifiers()); 142 this.buffer.append("@interface "); node.getName().accept(this); 144 this.buffer.append(" {"); for (Iterator it = node.bodyDeclarations().iterator(); it.hasNext(); ) { 146 BodyDeclaration d = (BodyDeclaration) it.next(); 147 d.accept(this); 148 } 149 this.buffer.append("}\n"); return false; 151 } 152 153 157 public boolean visit(AnnotationTypeMemberDeclaration node) { 158 if (node.getJavadoc() != null) { 159 node.getJavadoc().accept(this); 160 } 161 printIndent(); 162 printModifiers(node.modifiers()); 163 node.getType().accept(this); 164 this.buffer.append(" "); node.getName().accept(this); 166 this.buffer.append("()"); if (node.getDefault() != null) { 168 this.buffer.append(" default "); node.getDefault().accept(this); 170 } 171 this.buffer.append(";\n"); return false; 173 } 174 175 178 public boolean visit(AnonymousClassDeclaration node) { 179 this.buffer.append("{\n"); this.indent++; 181 for (Iterator it = node.bodyDeclarations().iterator(); it.hasNext(); ) { 182 BodyDeclaration b = (BodyDeclaration) it.next(); 183 b.accept(this); 184 } 185 this.indent--; 186 printIndent(); 187 this.buffer.append("}\n"); return false; 189 } 190 191 194 public boolean visit(ArrayAccess node) { 195 node.getArray().accept(this); 196 this.buffer.append("["); node.getIndex().accept(this); 198 this.buffer.append("]"); return false; 200 } 201 202 205 public boolean visit(ArrayCreation node) { 206 this.buffer.append("new "); ArrayType at = node.getType(); 208 int dims = at.getDimensions(); 209 Type elementType = at.getElementType(); 210 elementType.accept(this); 211 for (Iterator it = node.dimensions().iterator(); it.hasNext(); ) { 212 this.buffer.append("["); Expression e = (Expression) it.next(); 214 e.accept(this); 215 this.buffer.append("]"); dims--; 217 } 218 for (int i= 0; i < dims; i++) { 220 this.buffer.append("[]"); } 222 if (node.getInitializer() != null) { 223 node.getInitializer().accept(this); 224 } 225 return false; 226 } 227 228 231 public boolean visit(ArrayInitializer node) { 232 this.buffer.append("{"); for (Iterator it = node.expressions().iterator(); it.hasNext(); ) { 234 Expression e = (Expression) it.next(); 235 e.accept(this); 236 if (it.hasNext()) { 237 this.buffer.append(","); } 239 } 240 this.buffer.append("}"); return false; 242 } 243 244 247 public boolean visit(ArrayType node) { 248 node.getComponentType().accept(this); 249 this.buffer.append("[]"); return false; 251 } 252 253 256 public boolean visit(AssertStatement node) { 257 printIndent(); 258 this.buffer.append("assert "); node.getExpression().accept(this); 260 if (node.getMessage() != null) { 261 this.buffer.append(" : "); node.getMessage().accept(this); 263 } 264 this.buffer.append(";\n"); return false; 266 } 267 268 271 public boolean visit(Assignment node) { 272 node.getLeftHandSide().accept(this); 273 this.buffer.append(node.getOperator().toString()); 274 node.getRightHandSide().accept(this); 275 return false; 276 } 277 278 281 public boolean visit(Block node) { 282 this.buffer.append("{\n"); this.indent++; 284 for (Iterator it = node.statements().iterator(); it.hasNext(); ) { 285 Statement s = (Statement) it.next(); 286 s.accept(this); 287 } 288 this.indent--; 289 printIndent(); 290 this.buffer.append("}\n"); return false; 292 } 293 294 298 public boolean visit(BlockComment node) { 299 printIndent(); 300 this.buffer.append("/* */"); return false; 302 } 303 304 307 public boolean visit(BooleanLiteral node) { 308 if (node.booleanValue() == true) { 309 this.buffer.append("true"); } else { 311 this.buffer.append("false"); } 313 return false; 314 } 315 316 319 public boolean visit(BreakStatement node) { 320 printIndent(); 321 this.buffer.append("break"); if (node.getLabel() != null) { 323 this.buffer.append(" "); node.getLabel().accept(this); 325 } 326 this.buffer.append(";\n"); return false; 328 } 329 330 333 public boolean visit(CastExpression node) { 334 this.buffer.append("("); node.getType().accept(this); 336 this.buffer.append(")"); node.getExpression().accept(this); 338 return false; 339 } 340 341 344 public boolean visit(CatchClause node) { 345 this.buffer.append("catch ("); node.getException().accept(this); 347 this.buffer.append(") "); node.getBody().accept(this); 349 return false; 350 } 351 352 355 public boolean visit(CharacterLiteral node) { 356 this.buffer.append(node.getEscapedValue()); 357 return false; 358 } 359 360 363 public boolean visit(ClassInstanceCreation node) { 364 if (node.getExpression() != null) { 365 node.getExpression().accept(this); 366 this.buffer.append("."); } 368 this.buffer.append("new "); if (node.getAST().apiLevel() == AST.JLS2_INTERNAL) { 370 node.internalGetName().accept(this); 371 } 372 if (node.getAST().apiLevel() >= AST.JLS3) { 373 if (!node.typeArguments().isEmpty()) { 374 this.buffer.append("<"); for (Iterator it = node.typeArguments().iterator(); it.hasNext(); ) { 376 Type t = (Type) it.next(); 377 t.accept(this); 378 if (it.hasNext()) { 379 this.buffer.append(","); } 381 } 382 this.buffer.append(">"); } 384 node.getType().accept(this); 385 } 386 this.buffer.append("("); for (Iterator it = node.arguments().iterator(); it.hasNext(); ) { 388 Expression e = (Expression) it.next(); 389 e.accept(this); 390 if (it.hasNext()) { 391 this.buffer.append(","); } 393 } 394 this.buffer.append(")"); if (node.getAnonymousClassDeclaration() != null) { 396 node.getAnonymousClassDeclaration().accept(this); 397 } 398 return false; 399 } 400 401 404 public boolean visit(CompilationUnit node) { 405 if (node.getPackage() != null) { 406 node.getPackage().accept(this); 407 } 408 for (Iterator it = node.imports().iterator(); it.hasNext(); ) { 409 ImportDeclaration d = (ImportDeclaration) it.next(); 410 d.accept(this); 411 } 412 for (Iterator it = node.types().iterator(); it.hasNext(); ) { 413 AbstractTypeDeclaration d = (AbstractTypeDeclaration) it.next(); 414 d.accept(this); 415 } 416 return false; 417 } 418 419 422 public boolean visit(ConditionalExpression node) { 423 node.getExpression().accept(this); 424 this.buffer.append(" ? "); node.getThenExpression().accept(this); 426 this.buffer.append(" : "); node.getElseExpression().accept(this); 428 return false; 429 } 430 431 434 public boolean visit(ConstructorInvocation node) { 435 printIndent(); 436 if (node.getAST().apiLevel() >= AST.JLS3) { 437 if (!node.typeArguments().isEmpty()) { 438 this.buffer.append("<"); for (Iterator it = node.typeArguments().iterator(); it.hasNext(); ) { 440 Type t = (Type) it.next(); 441 t.accept(this); 442 if (it.hasNext()) { 443 this.buffer.append(","); } 445 } 446 this.buffer.append(">"); } 448 } 449 this.buffer.append("this("); for (Iterator it = node.arguments().iterator(); it.hasNext(); ) { 451 Expression e = (Expression) it.next(); 452 e.accept(this); 453 if (it.hasNext()) { 454 this.buffer.append(","); } 456 } 457 this.buffer.append(");\n"); return false; 459 } 460 461 464 public boolean visit(ContinueStatement node) { 465 printIndent(); 466 this.buffer.append("continue"); if (node.getLabel() != null) { 468 this.buffer.append(" "); node.getLabel().accept(this); 470 } 471 this.buffer.append(";\n"); return false; 473 } 474 475 478 public boolean visit(DoStatement node) { 479 printIndent(); 480 this.buffer.append("do "); node.getBody().accept(this); 482 this.buffer.append(" while ("); node.getExpression().accept(this); 484 this.buffer.append(");\n"); return false; 486 } 487 488 491 public boolean visit(EmptyStatement node) { 492 printIndent(); 493 this.buffer.append(";\n"); return false; 495 } 496 497 501 public boolean visit(EnhancedForStatement node) { 502 printIndent(); 503 this.buffer.append("for ("); node.getParameter().accept(this); 505 this.buffer.append(" : "); node.getExpression().accept(this); 507 this.buffer.append(") "); node.getBody().accept(this); 509 return false; 510 } 511 512 516 public boolean visit(EnumConstantDeclaration node) { 517 if (node.getJavadoc() != null) { 518 node.getJavadoc().accept(this); 519 } 520 printIndent(); 521 printModifiers(node.modifiers()); 522 node.getName().accept(this); 523 if (!node.arguments().isEmpty()) { 524 this.buffer.append("("); for (Iterator it = node.arguments().iterator(); it.hasNext(); ) { 526 Expression e = (Expression) it.next(); 527 e.accept(this); 528 if (it.hasNext()) { 529 this.buffer.append(","); } 531 } 532 this.buffer.append(")"); } 534 if (node.getAnonymousClassDeclaration() != null) { 535 node.getAnonymousClassDeclaration().accept(this); 536 } 537 return false; 538 } 539 540 544 public boolean visit(EnumDeclaration node) { 545 if (node.getJavadoc() != null) { 546 node.getJavadoc().accept(this); 547 } 548 printIndent(); 549 printModifiers(node.modifiers()); 550 this.buffer.append("enum "); node.getName().accept(this); 552 this.buffer.append(" "); if (!node.superInterfaceTypes().isEmpty()) { 554 this.buffer.append("implements "); for (Iterator it = node.superInterfaceTypes().iterator(); it.hasNext(); ) { 556 Type t = (Type) it.next(); 557 t.accept(this); 558 if (it.hasNext()) { 559 this.buffer.append(", "); } 561 } 562 this.buffer.append(" "); } 564 this.buffer.append("{"); for (Iterator it = node.enumConstants().iterator(); it.hasNext(); ) { 566 EnumConstantDeclaration d = (EnumConstantDeclaration) it.next(); 567 d.accept(this); 568 if (it.hasNext()) { 570 this.buffer.append(", "); } 573 } 574 if (!node.bodyDeclarations().isEmpty()) { 575 this.buffer.append("; "); for (Iterator it = node.bodyDeclarations().iterator(); it.hasNext(); ) { 577 BodyDeclaration d = (BodyDeclaration) it.next(); 578 d.accept(this); 579 } 581 } 582 this.buffer.append("}\n"); return false; 584 } 585 586 589 public boolean visit(ExpressionStatement node) { 590 printIndent(); 591 node.getExpression().accept(this); 592 this.buffer.append(";\n"); return false; 594 } 595 596 599 public boolean visit(FieldAccess node) { 600 node.getExpression().accept(this); 601 this.buffer.append("."); node.getName().accept(this); 603 return false; 604 } 605 606 609 public boolean visit(FieldDeclaration node) { 610 if (node.getJavadoc() != null) { 611 node.getJavadoc().accept(this); 612 } 613 printIndent(); 614 if (node.getAST().apiLevel() == AST.JLS2_INTERNAL) { 615 printModifiers(node.getModifiers()); 616 } 617 if (node.getAST().apiLevel() >= AST.JLS3) { 618 printModifiers(node.modifiers()); 619 } 620 node.getType().accept(this); 621 this.buffer.append(" "); for (Iterator it = node.fragments().iterator(); it.hasNext(); ) { 623 VariableDeclarationFragment f = (VariableDeclarationFragment) it.next(); 624 f.accept(this); 625 if (it.hasNext()) { 626 this.buffer.append(", "); } 628 } 629 this.buffer.append(";\n"); return false; 631 } 632 633 636 public boolean visit(ForStatement node) { 637 printIndent(); 638 this.buffer.append("for ("); for (Iterator it = node.initializers().iterator(); it.hasNext(); ) { 640 Expression e = (Expression) it.next(); 641 e.accept(this); 642 if (it.hasNext()) buffer.append(", "); } 644 this.buffer.append("; "); if (node.getExpression() != null) { 646 node.getExpression().accept(this); 647 } 648 this.buffer.append("; "); for (Iterator it = node.updaters().iterator(); it.hasNext(); ) { 650 Expression e = (Expression) it.next(); 651 e.accept(this); 652 if (it.hasNext()) buffer.append(", "); } 654 this.buffer.append(") "); node.getBody().accept(this); 656 return false; 657 } 658 659 662 public boolean visit(IfStatement node) { 663 printIndent(); 664 this.buffer.append("if ("); node.getExpression().accept(this); 666 this.buffer.append(") "); node.getThenStatement().accept(this); 668 if (node.getElseStatement() != null) { 669 this.buffer.append(" else "); node.getElseStatement().accept(this); 671 } 672 return false; 673 } 674 675 678 public boolean visit(ImportDeclaration node) { 679 printIndent(); 680 this.buffer.append("import "); if (node.getAST().apiLevel() >= AST.JLS3) { 682 if (node.isStatic()) { 683 this.buffer.append("static "); } 685 } 686 node.getName().accept(this); 687 if (node.isOnDemand()) { 688 this.buffer.append(".*"); } 690 this.buffer.append(";\n"); return false; 692 } 693 694 697 public boolean visit(InfixExpression node) { 698 node.getLeftOperand().accept(this); 699 this.buffer.append(' '); this.buffer.append(node.getOperator().toString()); 701 this.buffer.append(' '); 702 node.getRightOperand().accept(this); 703 final List extendedOperands = node.extendedOperands(); 704 if (extendedOperands.size() != 0) { 705 this.buffer.append(' '); 706 for (Iterator it = extendedOperands.iterator(); it.hasNext(); ) { 707 this.buffer.append(node.getOperator().toString()).append(' '); 708 Expression e = (Expression) it.next(); 709 e.accept(this); 710 } 711 } 712 return false; 713 } 714 715 718 public boolean visit(InstanceofExpression node) { 719 node.getLeftOperand().accept(this); 720 this.buffer.append(" instanceof "); node.getRightOperand().accept(this); 722 return false; 723 } 724 725 728 public boolean visit(Initializer node) { 729 if (node.getJavadoc() != null) { 730 node.getJavadoc().accept(this); 731 } 732 if (node.getAST().apiLevel() == AST.JLS2_INTERNAL) { 733 printModifiers(node.getModifiers()); 734 } 735 if (node.getAST().apiLevel() >= AST.JLS3) { 736 printModifiers(node.modifiers()); 737 } 738 node.getBody().accept(this); 739 return false; 740 } 741 742 745 public boolean visit(Javadoc node) { 746 printIndent(); 747 this.buffer.append("/** "); for (Iterator it = node.tags().iterator(); it.hasNext(); ) { 749 ASTNode e = (ASTNode) it.next(); 750 e.accept(this); 751 } 752 this.buffer.append("\n */\n"); return false; 754 } 755 756 759 public boolean visit(LabeledStatement node) { 760 printIndent(); 761 node.getLabel().accept(this); 762 this.buffer.append(": "); node.getBody().accept(this); 764 return false; 765 } 766 767 771 public boolean visit(LineComment node) { 772 this.buffer.append("//\n"); return false; 774 } 775 776 780 public boolean visit(MarkerAnnotation node) { 781 this.buffer.append("@"); node.getTypeName().accept(this); 783 return false; 784 } 785 786 790 public boolean visit(MemberRef node) { 791 if (node.getQualifier() != null) { 792 node.getQualifier().accept(this); 793 } 794 this.buffer.append("#"); node.getName().accept(this); 796 return false; 797 } 798 799 803 public boolean visit(MemberValuePair node) { 804 node.getName().accept(this); 805 this.buffer.append("="); node.getValue().accept(this); 807 return false; 808 } 809 810 814 public boolean visit(MethodRef node) { 815 if (node.getQualifier() != null) { 816 node.getQualifier().accept(this); 817 } 818 this.buffer.append("#"); node.getName().accept(this); 820 this.buffer.append("("); for (Iterator it = node.parameters().iterator(); it.hasNext(); ) { 822 MethodRefParameter e = (MethodRefParameter) it.next(); 823 e.accept(this); 824 if (it.hasNext()) { 825 this.buffer.append(","); } 827 } 828 this.buffer.append(")"); return false; 830 } 831 832 836 public boolean visit(MethodRefParameter node) { 837 node.getType().accept(this); 838 if (node.getAST().apiLevel() >= AST.JLS3) { 839 if (node.isVarargs()) { 840 this.buffer.append("..."); } 842 } 843 if (node.getName() != null) { 844 this.buffer.append(" "); node.getName().accept(this); 846 } 847 return false; 848 } 849 850 853 public boolean visit(MethodDeclaration node) { 854 if (node.getJavadoc() != null) { 855 node.getJavadoc().accept(this); 856 } 857 printIndent(); 858 if (node.getAST().apiLevel() == AST.JLS2_INTERNAL) { 859 printModifiers(node.getModifiers()); 860 } 861 if (node.getAST().apiLevel() >= AST.JLS3) { 862 printModifiers(node.modifiers()); 863 if (!node.typeParameters().isEmpty()) { 864 this.buffer.append("<"); for (Iterator it = node.typeParameters().iterator(); it.hasNext(); ) { 866 TypeParameter t = (TypeParameter) it.next(); 867 t.accept(this); 868 if (it.hasNext()) { 869 this.buffer.append(","); } 871 } 872 this.buffer.append(">"); } 874 } 875 if (!node.isConstructor()) { 876 if (node.getAST().apiLevel() == AST.JLS2_INTERNAL) { 877 node.internalGetReturnType().accept(this); 878 } else { 879 if (node.getReturnType2() != null) { 880 node.getReturnType2().accept(this); 881 } else { 882 this.buffer.append("void"); } 885 } 886 this.buffer.append(" "); } 888 node.getName().accept(this); 889 this.buffer.append("("); for (Iterator it = node.parameters().iterator(); it.hasNext(); ) { 891 SingleVariableDeclaration v = (SingleVariableDeclaration) it.next(); 892 v.accept(this); 893 if (it.hasNext()) { 894 this.buffer.append(","); } 896 } 897 this.buffer.append(")"); for (int i = 0; i < node.getExtraDimensions(); i++) { 899 this.buffer.append("[]"); } 901 if (!node.thrownExceptions().isEmpty()) { 902 this.buffer.append(" throws "); for (Iterator it = node.thrownExceptions().iterator(); it.hasNext(); ) { 904 Name n = (Name) it.next(); 905 n.accept(this); 906 if (it.hasNext()) { 907 this.buffer.append(", "); } 909 } 910 this.buffer.append(" "); } 912 if (node.getBody() == null) { 913 this.buffer.append(";\n"); } else { 915 node.getBody().accept(this); 916 } 917 return false; 918 } 919 920 923 public boolean visit(MethodInvocation node) { 924 if (node.getExpression() != null) { 925 node.getExpression().accept(this); 926 this.buffer.append("."); } 928 if (node.getAST().apiLevel() >= AST.JLS3) { 929 if (!node.typeArguments().isEmpty()) { 930 this.buffer.append("<"); for (Iterator it = node.typeArguments().iterator(); it.hasNext(); ) { 932 Type t = (Type) it.next(); 933 t.accept(this); 934 if (it.hasNext()) { 935 this.buffer.append(","); } 937 } 938 this.buffer.append(">"); } 940 } 941 node.getName().accept(this); 942 this.buffer.append("("); for (Iterator it = node.arguments().iterator(); it.hasNext(); ) { 944 Expression e = (Expression) it.next(); 945 e.accept(this); 946 if (it.hasNext()) { 947 this.buffer.append(","); } 949 } 950 this.buffer.append(")"); return false; 952 } 953 954 958 public boolean visit(Modifier node) { 959 this.buffer.append(node.getKeyword().toString()); 960 return false; 961 } 962 963 967 public boolean visit(NormalAnnotation node) { 968 this.buffer.append("@"); node.getTypeName().accept(this); 970 this.buffer.append("("); for (Iterator it = node.values().iterator(); it.hasNext(); ) { 972 MemberValuePair p = (MemberValuePair) it.next(); 973 p.accept(this); 974 if (it.hasNext()) { 975 this.buffer.append(","); } 977 } 978 this.buffer.append(")"); return false; 980 } 981 982 985 public boolean visit(NullLiteral node) { 986 this.buffer.append("null"); return false; 988 } 989 990 993 public boolean visit(NumberLiteral node) { 994 this.buffer.append(node.getToken()); 995 return false; 996 } 997 998 1001 public boolean visit(PackageDeclaration node) { 1002 if (node.getAST().apiLevel() >= AST.JLS3) { 1003 if (node.getJavadoc() != null) { 1004 node.getJavadoc().accept(this); 1005 } 1006 for (Iterator it = node.annotations().iterator(); it.hasNext(); ) { 1007 Annotation p = (Annotation) it.next(); 1008 p.accept(this); 1009 this.buffer.append(" "); } 1011 } 1012 printIndent(); 1013 this.buffer.append("package "); node.getName().accept(this); 1015 this.buffer.append(";\n"); return false; 1017 } 1018 1019 1023 public boolean visit(ParameterizedType node) { 1024 node.getType().accept(this); 1025 this.buffer.append("<"); for (Iterator it = node.typeArguments().iterator(); it.hasNext(); ) { 1027 Type t = (Type) it.next(); 1028 t.accept(this); 1029 if (it.hasNext()) { 1030 this.buffer.append(","); } 1032 } 1033 this.buffer.append(">"); return false; 1035 } 1036 1037 1040 public boolean visit(ParenthesizedExpression node) { 1041 this.buffer.append("("); node.getExpression().accept(this); 1043 this.buffer.append(")"); return false; 1045 } 1046 1047 1050 public boolean visit(PostfixExpression node) { 1051 node.getOperand().accept(this); 1052 this.buffer.append(node.getOperator().toString()); 1053 return false; 1054 } 1055 1056 1059 public boolean visit(PrefixExpression node) { 1060 this.buffer.append(node.getOperator().toString()); 1061 node.getOperand().accept(this); 1062 return false; 1063 } 1064 1065 1068 public boolean visit(PrimitiveType node) { 1069 this.buffer.append(node.getPrimitiveTypeCode().toString()); 1070 return false; 1071 } 1072 1073 1076 public boolean visit(QualifiedName node) { 1077 node.getQualifier().accept(this); 1078 this.buffer.append("."); node.getName().accept(this); 1080 return false; 1081 } 1082 1083 1087 public boolean visit(QualifiedType node) { 1088 node.getQualifier().accept(this); 1089 this.buffer.append("."); node.getName().accept(this); 1091 return false; 1092 } 1093 1094 1097 public boolean visit(ReturnStatement node) { 1098 printIndent(); 1099 this.buffer.append("return"); if (node.getExpression() != null) { 1101 this.buffer.append(" "); node.getExpression().accept(this); 1103 } 1104 this.buffer.append(";\n"); return false; 1106 } 1107 1108 1111 public boolean visit(SimpleName node) { 1112 this.buffer.append(node.getIdentifier()); 1113 return false; 1114 } 1115 1116 1119 public boolean visit(SimpleType node) { 1120 return true; 1121 } 1122 1123 1127 public boolean visit(SingleMemberAnnotation node) { 1128 this.buffer.append("@"); node.getTypeName().accept(this); 1130 this.buffer.append("("); node.getValue().accept(this); 1132 this.buffer.append(")"); return false; 1134 } 1135 1136 1139 public boolean visit(SingleVariableDeclaration node) { 1140 printIndent(); 1141 if (node.getAST().apiLevel() == AST.JLS2_INTERNAL) { 1142 printModifiers(node.getModifiers()); 1143 } 1144 if (node.getAST().apiLevel() >= AST.JLS3) { 1145 printModifiers(node.modifiers()); 1146 } 1147 node.getType().accept(this); 1148 if (node.getAST().apiLevel() >= AST.JLS3) { 1149 if (node.isVarargs()) { 1150 this.buffer.append("..."); } 1152 } 1153 this.buffer.append(" "); node.getName().accept(this); 1155 for (int i = 0; i < node.getExtraDimensions(); i++) { 1156 this.buffer.append("[]"); } 1158 if (node.getInitializer() != null) { 1159 this.buffer.append("="); node.getInitializer().accept(this); 1161 } 1162 return false; 1163 } 1164 1165 1168 public boolean visit(StringLiteral node) { 1169 this.buffer.append(node.getEscapedValue()); 1170 return false; 1171 } 1172 1173 1176 public boolean visit(SuperConstructorInvocation node) { 1177 printIndent(); 1178 if (node.getExpression() != null) { 1179 node.getExpression().accept(this); 1180 this.buffer.append("."); } 1182 if (node.getAST().apiLevel() >= AST.JLS3) { 1183 if (!node.typeArguments().isEmpty()) { 1184 this.buffer.append("<"); for (Iterator it = node.typeArguments().iterator(); it.hasNext(); ) { 1186 Type t = (Type) it.next(); 1187 t.accept(this); 1188 if (it.hasNext()) { 1189 this.buffer.append(","); } 1191 } 1192 this.buffer.append(">"); } 1194 } 1195 this.buffer.append("super("); for (Iterator it = node.arguments().iterator(); it.hasNext(); ) { 1197 Expression e = (Expression) it.next(); 1198 e.accept(this); 1199 if (it.hasNext()) { 1200 this.buffer.append(","); } 1202 } 1203 this.buffer.append(");\n"); return false; 1205 } 1206 1207 1210 public boolean visit(SuperFieldAccess node) { 1211 if (node.getQualifier() != null) { 1212 node.getQualifier().accept(this); 1213 this.buffer.append("."); } 1215 this.buffer.append("super."); node.getName().accept(this); 1217 return false; 1218 } 1219 1220 1223 public boolean visit(SuperMethodInvocation node) { 1224 if (node.getQualifier() != null) { 1225 node.getQualifier().accept(this); 1226 this.buffer.append("."); } 1228 this.buffer.append("super."); if (node.getAST().apiLevel() >= AST.JLS3) { 1230 if (!node.typeArguments().isEmpty()) { 1231 this.buffer.append("<"); for (Iterator it = node.typeArguments().iterator(); it.hasNext(); ) { 1233 Type t = (Type) it.next(); 1234 t.accept(this); 1235 if (it.hasNext()) { 1236 this.buffer.append(","); } 1238 } 1239 this.buffer.append(">"); } 1241 } 1242 node.getName().accept(this); 1243 this.buffer.append("("); for (Iterator it = node.arguments().iterator(); it.hasNext(); ) { 1245 Expression e = (Expression) it.next(); 1246 e.accept(this); 1247 if (it.hasNext()) { 1248 this.buffer.append(","); } 1250 } 1251 this.buffer.append(")"); return false; 1253 } 1254 1255 1258 public boolean visit(SwitchCase node) { 1259 if (node.isDefault()) { 1260 this.buffer.append("default :\n"); } else { 1262 this.buffer.append("case "); node.getExpression().accept(this); 1264 this.buffer.append(":\n"); } 1266 this.indent++; return false; 1268 } 1269 1270 1273 public boolean visit(SwitchStatement node) { 1274 this.buffer.append("switch ("); node.getExpression().accept(this); 1276 this.buffer.append(") "); this.buffer.append("{\n"); this.indent++; 1279 for (Iterator it = node.statements().iterator(); it.hasNext(); ) { 1280 Statement s = (Statement) it.next(); 1281 s.accept(this); 1282 this.indent--; } 1284 this.indent--; 1285 printIndent(); 1286 this.buffer.append("}\n"); return false; 1288 } 1289 1290 1293 public boolean visit(SynchronizedStatement node) { 1294 this.buffer.append("synchronized ("); node.getExpression().accept(this); 1296 this.buffer.append(") "); node.getBody().accept(this); 1298 return false; 1299 } 1300 1301 1305 public boolean visit(TagElement node) { 1306 if (node.isNested()) { 1307 this.buffer.append("{"); } else { 1310 this.buffer.append("\n * "); } 1313 boolean previousRequiresWhiteSpace = false; 1314 if (node.getTagName() != null) { 1315 this.buffer.append(node.getTagName()); 1316 previousRequiresWhiteSpace = true; 1317 } 1318 boolean previousRequiresNewLine = false; 1319 for (Iterator it = node.fragments().iterator(); it.hasNext(); ) { 1320 ASTNode e = (ASTNode) it.next(); 1321 boolean currentIncludesWhiteSpace = (e instanceof TextElement); 1324 if (previousRequiresNewLine && currentIncludesWhiteSpace) { 1325 this.buffer.append("\n * "); } 1327 previousRequiresNewLine = currentIncludesWhiteSpace; 1328 if (previousRequiresWhiteSpace && !currentIncludesWhiteSpace) { 1330 this.buffer.append(" "); } 1332 e.accept(this); 1333 previousRequiresWhiteSpace = !currentIncludesWhiteSpace && !(e instanceof TagElement); 1334 } 1335 if (node.isNested()) { 1336 this.buffer.append("}"); } 1338 return false; 1339 } 1340 1341 1345 public boolean visit(TextElement node) { 1346 this.buffer.append(node.getText()); 1347 return false; 1348 } 1349 1350 1353 public boolean visit(ThisExpression node) { 1354 if (node.getQualifier() != null) { 1355 node.getQualifier().accept(this); 1356 this.buffer.append("."); } 1358 this.buffer.append("this"); return false; 1360 } 1361 1362 1365 public boolean visit(ThrowStatement node) { 1366 printIndent(); 1367 this.buffer.append("throw "); node.getExpression().accept(this); 1369 this.buffer.append(";\n"); return false; 1371 } 1372 1373 1376 public boolean visit(TryStatement node) { 1377 printIndent(); 1378 this.buffer.append("try "); node.getBody().accept(this); 1380 this.buffer.append(" "); for (Iterator it = node.catchClauses().iterator(); it.hasNext(); ) { 1382 CatchClause cc = (CatchClause) it.next(); 1383 cc.accept(this); 1384 } 1385 if (node.getFinally() != null) { 1386 this.buffer.append(" finally "); node.getFinally().accept(this); 1388 } 1389 return false; 1390 } 1391 1392 1395 public boolean visit(TypeDeclaration node) { 1396 if (node.getJavadoc() != null) { 1397 node.getJavadoc().accept(this); 1398 } 1399 if (node.getAST().apiLevel() == AST.JLS2_INTERNAL) { 1400 printModifiers(node.getModifiers()); 1401 } 1402 if (node.getAST().apiLevel() >= AST.JLS3) { 1403 printModifiers(node.modifiers()); 1404 } 1405 this.buffer.append(node.isInterface() ? "interface " : "class "); node.getName().accept(this); 1407 if (node.getAST().apiLevel() >= AST.JLS3) { 1408 if (!node.typeParameters().isEmpty()) { 1409 this.buffer.append("<"); for (Iterator it = node.typeParameters().iterator(); it.hasNext(); ) { 1411 TypeParameter t = (TypeParameter) it.next(); 1412 t.accept(this); 1413 if (it.hasNext()) { 1414 this.buffer.append(","); } 1416 } 1417 this.buffer.append(">"); } 1419 } 1420 this.buffer.append(" "); if (node.getAST().apiLevel() == AST.JLS2_INTERNAL) { 1422 if (node.internalGetSuperclass() != null) { 1423 this.buffer.append("extends "); node.internalGetSuperclass().accept(this); 1425 this.buffer.append(" "); } 1427 if (!node.internalSuperInterfaces().isEmpty()) { 1428 this.buffer.append(node.isInterface() ? "extends " : "implements "); for (Iterator it = node.internalSuperInterfaces().iterator(); it.hasNext(); ) { 1430 Name n = (Name) it.next(); 1431 n.accept(this); 1432 if (it.hasNext()) { 1433 this.buffer.append(", "); } 1435 } 1436 this.buffer.append(" "); } 1438 } 1439 if (node.getAST().apiLevel() >= AST.JLS3) { 1440 if (node.getSuperclassType() != null) { 1441 this.buffer.append("extends "); node.getSuperclassType().accept(this); 1443 this.buffer.append(" "); } 1445 if (!node.superInterfaceTypes().isEmpty()) { 1446 this.buffer.append(node.isInterface() ? "extends " : "implements "); for (Iterator it = node.superInterfaceTypes().iterator(); it.hasNext(); ) { 1448 Type t = (Type) it.next(); 1449 t.accept(this); 1450 if (it.hasNext()) { 1451 this.buffer.append(", "); } 1453 } 1454 this.buffer.append(" "); } 1456 } 1457 this.buffer.append("{\n"); this.indent++; 1459 for (Iterator it = node.bodyDeclarations().iterator(); it.hasNext(); ) { 1460 BodyDeclaration d = (BodyDeclaration) it.next(); 1461 d.accept(this); 1462 } 1463 this.indent--; 1464 printIndent(); 1465 this.buffer.append("}\n"); return false; 1467 } 1468 1469 1472 public boolean visit(TypeDeclarationStatement node) { 1473 if (node.getAST().apiLevel() == AST.JLS2_INTERNAL) { 1474 node.internalGetTypeDeclaration().accept(this); 1475 } 1476 if (node.getAST().apiLevel() >= AST.JLS3) { 1477 node.getDeclaration().accept(this); 1478 } 1479 return false; 1480 } 1481 1482 1485 public boolean visit(TypeLiteral node) { 1486 node.getType().accept(this); 1487 this.buffer.append(".class"); return false; 1489 } 1490 1491 1495 public boolean visit(TypeParameter node) { 1496 node.getName().accept(this); 1497 if (!node.typeBounds().isEmpty()) { 1498 this.buffer.append(" extends "); for (Iterator it = node.typeBounds().iterator(); it.hasNext(); ) { 1500 Type t = (Type) it.next(); 1501 t.accept(this); 1502 if (it.hasNext()) { 1503 this.buffer.append(" & "); } 1505 } 1506 } 1507 return false; 1508 } 1509 1510 1513 public boolean visit(VariableDeclarationExpression node) { 1514 if (node.getAST().apiLevel() == AST.JLS2_INTERNAL) { 1515 printModifiers(node.getModifiers()); 1516 } 1517 if (node.getAST().apiLevel() >= AST.JLS3) { 1518 printModifiers(node.modifiers()); 1519 } 1520 node.getType().accept(this); 1521 this.buffer.append(" "); for (Iterator it = node.fragments().iterator(); it.hasNext(); ) { 1523 VariableDeclarationFragment f = (VariableDeclarationFragment) it.next(); 1524 f.accept(this); 1525 if (it.hasNext()) { 1526 this.buffer.append(", "); } 1528 } 1529 return false; 1530 } 1531 1532 1535 public boolean visit(VariableDeclarationFragment node) { 1536 node.getName().accept(this); 1537 for (int i = 0; i < node.getExtraDimensions(); i++) { 1538 this.buffer.append("[]"); } 1540 if (node.getInitializer() != null) { 1541 this.buffer.append("="); node.getInitializer().accept(this); 1543 } 1544 return false; 1545 } 1546 1547 1550 public boolean visit(VariableDeclarationStatement node) { 1551 printIndent(); 1552 if (node.getAST().apiLevel() == AST.JLS2_INTERNAL) { 1553 printModifiers(node.getModifiers()); 1554 } 1555 if (node.getAST().apiLevel() >= AST.JLS3) { 1556 printModifiers(node.modifiers()); 1557 } 1558 node.getType().accept(this); 1559 this.buffer.append(" "); for (Iterator it = node.fragments().iterator(); it.hasNext(); ) { 1561 VariableDeclarationFragment f = (VariableDeclarationFragment) it.next(); 1562 f.accept(this); 1563 if (it.hasNext()) { 1564 this.buffer.append(", "); } 1566 } 1567 this.buffer.append(";\n"); return false; 1569 } 1570 1571 1575 public boolean visit(WildcardType node) { 1576 this.buffer.append("?"); Type bound = node.getBound(); 1578 if (bound != null) { 1579 if (node.isUpperBound()) { 1580 this.buffer.append(" extends "); } else { 1582 this.buffer.append(" super "); } 1584 bound.accept(this); 1585 } 1586 return false; 1587 } 1588 1589 1592 public boolean visit(WhileStatement node) { 1593 printIndent(); 1594 this.buffer.append("while ("); node.getExpression().accept(this); 1596 this.buffer.append(") "); node.getBody().accept(this); 1598 return false; 1599 } 1600 1601} 1602 | Popular Tags |