1 11 package org.eclipse.jdt.internal.corext.dom; 12 13 import java.util.Iterator ; 14 import java.util.List ; 15 16 import org.eclipse.core.runtime.Assert; 17 18 import org.eclipse.jdt.core.dom.*; 19 20 21 public class ASTFlattener extends GenericVisitor { 22 23 27 protected StringBuffer fBuffer; 28 29 32 public ASTFlattener() { 33 this.fBuffer= new StringBuffer (); 34 } 35 36 41 public String getResult() { 42 return this.fBuffer.toString(); 43 } 44 45 48 public void reset() { 49 this.fBuffer.setLength(0); 50 } 51 52 public static String asString(ASTNode node) { 53 ASTFlattener flattener= new ASTFlattener(); 54 node.accept(flattener); 55 return flattener.getResult(); 56 } 57 58 59 protected boolean visitNode(ASTNode node) { 60 Assert.isTrue(false, "No implementation to flatten node: " + node.toString()); return false; 62 } 63 64 71 private void printModifiers(List ext) { 72 for (Iterator it= ext.iterator(); it.hasNext();) { 73 ASTNode p= (ASTNode) it.next(); 74 p.accept(this); 75 this.fBuffer.append(" "); } 77 } 78 79 85 private void printModifiers(int modifiers) { 86 if (Modifier.isPublic(modifiers)) { 87 this.fBuffer.append("public "); } 89 if (Modifier.isProtected(modifiers)) { 90 this.fBuffer.append("protected "); } 92 if (Modifier.isPrivate(modifiers)) { 93 this.fBuffer.append("private "); } 95 if (Modifier.isStatic(modifiers)) { 96 this.fBuffer.append("static "); } 98 if (Modifier.isAbstract(modifiers)) { 99 this.fBuffer.append("abstract "); } 101 if (Modifier.isFinal(modifiers)) { 102 this.fBuffer.append("final "); } 104 if (Modifier.isSynchronized(modifiers)) { 105 this.fBuffer.append("synchronized "); } 107 if (Modifier.isVolatile(modifiers)) { 108 this.fBuffer.append("volatile "); } 110 if (Modifier.isNative(modifiers)) { 111 this.fBuffer.append("native "); } 113 if (Modifier.isStrictfp(modifiers)) { 114 this.fBuffer.append("strictfp "); } 116 if (Modifier.isTransient(modifiers)) { 117 this.fBuffer.append("transient "); } 119 } 120 121 125 public boolean visit(AnnotationTypeDeclaration node) { 126 if (node.getJavadoc() != null) { 127 node.getJavadoc().accept(this); 128 } 129 printModifiers(node.modifiers()); 130 this.fBuffer.append("@interface "); node.getName().accept(this); 132 this.fBuffer.append(" {"); for (Iterator it= node.bodyDeclarations().iterator(); it.hasNext();) { 134 BodyDeclaration d= (BodyDeclaration) it.next(); 135 d.accept(this); 136 } 137 this.fBuffer.append("}"); return false; 139 } 140 141 145 public boolean visit(AnnotationTypeMemberDeclaration node) { 146 if (node.getJavadoc() != null) { 147 node.getJavadoc().accept(this); 148 } 149 printModifiers(node.modifiers()); 150 node.getType().accept(this); 151 this.fBuffer.append(" "); node.getName().accept(this); 153 this.fBuffer.append("()"); if (node.getDefault() != null) { 155 this.fBuffer.append(" default "); node.getDefault().accept(this); 157 } 158 this.fBuffer.append(";"); return false; 160 } 161 162 165 public boolean visit(AnonymousClassDeclaration node) { 166 this.fBuffer.append("{"); for (Iterator it= node.bodyDeclarations().iterator(); it.hasNext();) { 168 BodyDeclaration b= (BodyDeclaration) it.next(); 169 b.accept(this); 170 } 171 this.fBuffer.append("}"); return false; 173 } 174 175 178 public boolean visit(ArrayAccess node) { 179 node.getArray().accept(this); 180 this.fBuffer.append("["); node.getIndex().accept(this); 182 this.fBuffer.append("]"); return false; 184 } 185 186 189 public boolean visit(ArrayCreation node) { 190 this.fBuffer.append("new "); ArrayType at= node.getType(); 192 int dims= at.getDimensions(); 193 Type elementType= at.getElementType(); 194 elementType.accept(this); 195 for (Iterator it= node.dimensions().iterator(); it.hasNext();) { 196 this.fBuffer.append("["); Expression e= (Expression) it.next(); 198 e.accept(this); 199 this.fBuffer.append("]"); dims--; 201 } 202 for (int i= 0; i < dims; i++) { 204 this.fBuffer.append("[]"); } 206 if (node.getInitializer() != null) { 207 node.getInitializer().accept(this); 208 } 209 return false; 210 } 211 212 215 public boolean visit(ArrayInitializer node) { 216 this.fBuffer.append("{"); for (Iterator it= node.expressions().iterator(); it.hasNext();) { 218 Expression e= (Expression) it.next(); 219 e.accept(this); 220 if (it.hasNext()) { 221 this.fBuffer.append(","); } 223 } 224 this.fBuffer.append("}"); return false; 226 } 227 228 231 public boolean visit(ArrayType node) { 232 node.getComponentType().accept(this); 233 this.fBuffer.append("[]"); return false; 235 } 236 237 240 public boolean visit(AssertStatement node) { 241 this.fBuffer.append("assert "); node.getExpression().accept(this); 243 if (node.getMessage() != null) { 244 this.fBuffer.append(" : "); node.getMessage().accept(this); 246 } 247 this.fBuffer.append(";"); return false; 249 } 250 251 254 public boolean visit(Assignment node) { 255 node.getLeftHandSide().accept(this); 256 this.fBuffer.append(node.getOperator().toString()); 257 node.getRightHandSide().accept(this); 258 return false; 259 } 260 261 264 public boolean visit(Block node) { 265 this.fBuffer.append("{"); for (Iterator it= node.statements().iterator(); it.hasNext();) { 267 Statement s= (Statement) it.next(); 268 s.accept(this); 269 } 270 this.fBuffer.append("}"); return false; 272 } 273 274 278 public boolean visit(BlockComment node) { 279 this.fBuffer.append("/* */"); return false; 281 } 282 283 286 public boolean visit(BooleanLiteral node) { 287 if (node.booleanValue() == true) { 288 this.fBuffer.append("true"); } else { 290 this.fBuffer.append("false"); } 292 return false; 293 } 294 295 298 public boolean visit(BreakStatement node) { 299 this.fBuffer.append("break"); if (node.getLabel() != null) { 301 this.fBuffer.append(" "); node.getLabel().accept(this); 303 } 304 this.fBuffer.append(";"); return false; 306 } 307 308 311 public boolean visit(CastExpression node) { 312 this.fBuffer.append("("); node.getType().accept(this); 314 this.fBuffer.append(")"); node.getExpression().accept(this); 316 return false; 317 } 318 319 322 public boolean visit(CatchClause node) { 323 this.fBuffer.append("catch ("); node.getException().accept(this); 325 this.fBuffer.append(") "); node.getBody().accept(this); 327 return false; 328 } 329 330 333 public boolean visit(CharacterLiteral node) { 334 this.fBuffer.append(node.getEscapedValue()); 335 return false; 336 } 337 338 341 public boolean visit(ClassInstanceCreation node) { 342 if (node.getExpression() != null) { 343 node.getExpression().accept(this); 344 this.fBuffer.append("."); } 346 this.fBuffer.append("new "); if (node.getAST().apiLevel() == AST.JLS2) { 348 node.getName().accept(this); 349 } 350 if (node.getAST().apiLevel() >= AST.JLS3) { 351 if (!node.typeArguments().isEmpty()) { 352 this.fBuffer.append("<"); for (Iterator it= node.typeArguments().iterator(); it.hasNext();) { 354 Type t= (Type) it.next(); 355 t.accept(this); 356 if (it.hasNext()) { 357 this.fBuffer.append(","); } 359 } 360 this.fBuffer.append(">"); } 362 node.getType().accept(this); 363 } 364 this.fBuffer.append("("); for (Iterator it= node.arguments().iterator(); it.hasNext();) { 366 Expression e= (Expression) it.next(); 367 e.accept(this); 368 if (it.hasNext()) { 369 this.fBuffer.append(","); } 371 } 372 this.fBuffer.append(")"); if (node.getAnonymousClassDeclaration() != null) { 374 node.getAnonymousClassDeclaration().accept(this); 375 } 376 return false; 377 } 378 379 382 public boolean visit(CompilationUnit node) { 383 if (node.getPackage() != null) { 384 node.getPackage().accept(this); 385 } 386 for (Iterator it= node.imports().iterator(); it.hasNext();) { 387 ImportDeclaration d= (ImportDeclaration) it.next(); 388 d.accept(this); 389 } 390 for (Iterator it= node.types().iterator(); it.hasNext();) { 391 AbstractTypeDeclaration d= (AbstractTypeDeclaration) it.next(); 392 d.accept(this); 393 } 394 return false; 395 } 396 397 400 public boolean visit(ConditionalExpression node) { 401 node.getExpression().accept(this); 402 this.fBuffer.append("?"); node.getThenExpression().accept(this); 404 this.fBuffer.append(":"); node.getElseExpression().accept(this); 406 return false; 407 } 408 409 412 public boolean visit(ConstructorInvocation node) { 413 if (node.getAST().apiLevel() >= AST.JLS3) { 414 if (!node.typeArguments().isEmpty()) { 415 this.fBuffer.append("<"); for (Iterator it= node.typeArguments().iterator(); it.hasNext();) { 417 Type t= (Type) it.next(); 418 t.accept(this); 419 if (it.hasNext()) { 420 this.fBuffer.append(","); } 422 } 423 this.fBuffer.append(">"); } 425 } 426 this.fBuffer.append("this("); for (Iterator it= node.arguments().iterator(); it.hasNext();) { 428 Expression e= (Expression) it.next(); 429 e.accept(this); 430 if (it.hasNext()) { 431 this.fBuffer.append(","); } 433 } 434 this.fBuffer.append(");"); return false; 436 } 437 438 441 public boolean visit(ContinueStatement node) { 442 this.fBuffer.append("continue"); if (node.getLabel() != null) { 444 this.fBuffer.append(" "); node.getLabel().accept(this); 446 } 447 this.fBuffer.append(";"); return false; 449 } 450 451 454 public boolean visit(DoStatement node) { 455 this.fBuffer.append("do "); node.getBody().accept(this); 457 this.fBuffer.append(" while ("); node.getExpression().accept(this); 459 this.fBuffer.append(");"); return false; 461 } 462 463 466 public boolean visit(EmptyStatement node) { 467 this.fBuffer.append(";"); return false; 469 } 470 471 475 public boolean visit(EnhancedForStatement node) { 476 this.fBuffer.append("for ("); node.getParameter().accept(this); 478 this.fBuffer.append(" : "); node.getExpression().accept(this); 480 this.fBuffer.append(") "); node.getBody().accept(this); 482 return false; 483 } 484 485 489 public boolean visit(EnumConstantDeclaration node) { 490 if (node.getJavadoc() != null) { 491 node.getJavadoc().accept(this); 492 } 493 printModifiers(node.modifiers()); 494 node.getName().accept(this); 495 if (!node.arguments().isEmpty()) { 496 this.fBuffer.append("("); for (Iterator it= node.arguments().iterator(); it.hasNext();) { 498 Expression e= (Expression) it.next(); 499 e.accept(this); 500 if (it.hasNext()) { 501 this.fBuffer.append(","); } 503 } 504 this.fBuffer.append(")"); } 506 if (node.getAnonymousClassDeclaration() != null) { 507 node.getAnonymousClassDeclaration().accept(this); 508 } 509 return false; 510 } 511 512 516 public boolean visit(EnumDeclaration node) { 517 if (node.getJavadoc() != null) { 518 node.getJavadoc().accept(this); 519 } 520 printModifiers(node.modifiers()); 521 this.fBuffer.append("enum "); node.getName().accept(this); 523 this.fBuffer.append(" "); if (!node.superInterfaceTypes().isEmpty()) { 525 this.fBuffer.append("implements "); for (Iterator it= node.superInterfaceTypes().iterator(); it.hasNext();) { 527 Type t= (Type) it.next(); 528 t.accept(this); 529 if (it.hasNext()) { 530 this.fBuffer.append(", "); } 532 } 533 this.fBuffer.append(" "); } 535 this.fBuffer.append("{"); for (Iterator it = node.enumConstants().iterator(); it.hasNext(); ) { 537 EnumConstantDeclaration d = (EnumConstantDeclaration) it.next(); 538 d.accept(this); 539 if (it.hasNext()) { 541 this.fBuffer.append(", "); } 544 } 545 if (!node.bodyDeclarations().isEmpty()) { 546 this.fBuffer.append("; "); for (Iterator it = node.bodyDeclarations().iterator(); it.hasNext(); ) { 548 BodyDeclaration d = (BodyDeclaration) it.next(); 549 d.accept(this); 550 } 552 } 553 this.fBuffer.append("}"); return false; 555 } 556 557 560 public boolean visit(ExpressionStatement node) { 561 node.getExpression().accept(this); 562 this.fBuffer.append(";"); return false; 564 } 565 566 569 public boolean visit(FieldAccess node) { 570 node.getExpression().accept(this); 571 this.fBuffer.append("."); node.getName().accept(this); 573 return false; 574 } 575 576 579 public boolean visit(FieldDeclaration node) { 580 if (node.getJavadoc() != null) { 581 node.getJavadoc().accept(this); 582 } 583 if (node.getAST().apiLevel() == AST.JLS2) { 584 printModifiers(node.getModifiers()); 585 } 586 if (node.getAST().apiLevel() >= AST.JLS3) { 587 printModifiers(node.modifiers()); 588 } 589 node.getType().accept(this); 590 this.fBuffer.append(" "); for (Iterator it= node.fragments().iterator(); it.hasNext();) { 592 VariableDeclarationFragment f= (VariableDeclarationFragment) it.next(); 593 f.accept(this); 594 if (it.hasNext()) { 595 this.fBuffer.append(", "); } 597 } 598 this.fBuffer.append(";"); return false; 600 } 601 602 605 public boolean visit(ForStatement node) { 606 this.fBuffer.append("for ("); for (Iterator it= node.initializers().iterator(); it.hasNext();) { 608 Expression e= (Expression) it.next(); 609 e.accept(this); 610 } 611 this.fBuffer.append("; "); if (node.getExpression() != null) { 613 node.getExpression().accept(this); 614 } 615 this.fBuffer.append("; "); for (Iterator it= node.updaters().iterator(); it.hasNext();) { 617 Expression e= (Expression) it.next(); 618 e.accept(this); 619 } 620 this.fBuffer.append(") "); node.getBody().accept(this); 622 return false; 623 } 624 625 628 public boolean visit(IfStatement node) { 629 this.fBuffer.append("if ("); node.getExpression().accept(this); 631 this.fBuffer.append(") "); node.getThenStatement().accept(this); 633 if (node.getElseStatement() != null) { 634 this.fBuffer.append(" else "); node.getElseStatement().accept(this); 636 } 637 return false; 638 } 639 640 643 public boolean visit(ImportDeclaration node) { 644 this.fBuffer.append("import "); if (node.getAST().apiLevel() >= AST.JLS3) { 646 if (node.isStatic()) { 647 this.fBuffer.append("static "); } 649 } 650 node.getName().accept(this); 651 if (node.isOnDemand()) { 652 this.fBuffer.append(".*"); } 654 this.fBuffer.append(";"); return false; 656 } 657 658 661 public boolean visit(InfixExpression node) { 662 node.getLeftOperand().accept(this); 663 this.fBuffer.append(' '); this.fBuffer.append(node.getOperator().toString()); 665 this.fBuffer.append(' '); 666 node.getRightOperand().accept(this); 667 final List extendedOperands = node.extendedOperands(); 668 if (extendedOperands.size() != 0) { 669 this.fBuffer.append(' '); 670 for (Iterator it = extendedOperands.iterator(); it.hasNext(); ) { 671 this.fBuffer.append(node.getOperator().toString()).append(' '); 672 Expression e = (Expression) it.next(); 673 e.accept(this); 674 } 675 } 676 return false; 677 } 678 679 682 public boolean visit(InstanceofExpression node) { 683 node.getLeftOperand().accept(this); 684 this.fBuffer.append(" instanceof "); node.getRightOperand().accept(this); 686 return false; 687 } 688 689 692 public boolean visit(Initializer node) { 693 if (node.getJavadoc() != null) { 694 node.getJavadoc().accept(this); 695 } 696 if (node.getAST().apiLevel() == AST.JLS2) { 697 printModifiers(node.getModifiers()); 698 } 699 if (node.getAST().apiLevel() >= AST.JLS3) { 700 printModifiers(node.modifiers()); 701 } 702 node.getBody().accept(this); 703 return false; 704 } 705 706 709 public boolean visit(Javadoc node) { 710 this.fBuffer.append("/** "); for (Iterator it= node.tags().iterator(); it.hasNext();) { 712 ASTNode e= (ASTNode) it.next(); 713 e.accept(this); 714 } 715 this.fBuffer.append("\n */"); return false; 717 } 718 719 722 public boolean visit(LabeledStatement node) { 723 node.getLabel().accept(this); 724 this.fBuffer.append(": "); node.getBody().accept(this); 726 return false; 727 } 728 729 733 public boolean visit(LineComment node) { 734 this.fBuffer.append("//\n"); return false; 736 } 737 738 742 public boolean visit(MarkerAnnotation node) { 743 this.fBuffer.append("@"); node.getTypeName().accept(this); 745 return false; 746 } 747 748 752 public boolean visit(MemberRef node) { 753 if (node.getQualifier() != null) { 754 node.getQualifier().accept(this); 755 } 756 this.fBuffer.append("#"); node.getName().accept(this); 758 return false; 759 } 760 761 765 public boolean visit(MemberValuePair node) { 766 node.getName().accept(this); 767 this.fBuffer.append("="); node.getValue().accept(this); 769 return false; 770 } 771 772 776 public boolean visit(MethodRef node) { 777 if (node.getQualifier() != null) { 778 node.getQualifier().accept(this); 779 } 780 this.fBuffer.append("#"); node.getName().accept(this); 782 this.fBuffer.append("("); for (Iterator it= node.parameters().iterator(); it.hasNext();) { 784 MethodRefParameter e= (MethodRefParameter) it.next(); 785 e.accept(this); 786 if (it.hasNext()) { 787 this.fBuffer.append(","); } 789 } 790 this.fBuffer.append(")"); return false; 792 } 793 794 798 public boolean visit(MethodRefParameter node) { 799 node.getType().accept(this); 800 if (node.getAST().apiLevel() >= AST.JLS3) { 801 if (node.isVarargs()) { 802 this.fBuffer.append("..."); } 804 } 805 if (node.getName() != null) { 806 this.fBuffer.append(" "); node.getName().accept(this); 808 } 809 return false; 810 } 811 812 815 public boolean visit(MethodDeclaration node) { 816 if (node.getJavadoc() != null) { 817 node.getJavadoc().accept(this); 818 } 819 if (node.getAST().apiLevel() == AST.JLS2) { 820 printModifiers(node.getModifiers()); 821 } 822 if (node.getAST().apiLevel() >= AST.JLS3) { 823 printModifiers(node.modifiers()); 824 if (!node.typeParameters().isEmpty()) { 825 this.fBuffer.append("<"); for (Iterator it= node.typeParameters().iterator(); it.hasNext();) { 827 TypeParameter t= (TypeParameter) it.next(); 828 t.accept(this); 829 if (it.hasNext()) { 830 this.fBuffer.append(","); } 832 } 833 this.fBuffer.append("> "); } 835 } 836 if (!node.isConstructor()) { 837 if (node.getAST().apiLevel() == AST.JLS2) { 838 node.getReturnType().accept(this); 839 } else { 840 if (node.getReturnType2() != null) { 841 node.getReturnType2().accept(this); 842 } else { 843 this.fBuffer.append("void"); } 846 } 847 this.fBuffer.append(" "); } 849 node.getName().accept(this); 850 this.fBuffer.append("("); for (Iterator it= node.parameters().iterator(); it.hasNext();) { 852 SingleVariableDeclaration v= (SingleVariableDeclaration) it.next(); 853 v.accept(this); 854 if (it.hasNext()) { 855 this.fBuffer.append(","); } 857 } 858 this.fBuffer.append(")"); for (int i= 0; i < node.getExtraDimensions(); i++) { 860 this.fBuffer.append("[]"); } 862 if (!node.thrownExceptions().isEmpty()) { 863 this.fBuffer.append(" throws "); for (Iterator it= node.thrownExceptions().iterator(); it.hasNext();) { 865 Name n= (Name) it.next(); 866 n.accept(this); 867 if (it.hasNext()) { 868 this.fBuffer.append(", "); } 870 } 871 this.fBuffer.append(" "); } 873 if (node.getBody() == null) { 874 this.fBuffer.append(";"); } else { 876 node.getBody().accept(this); 877 } 878 return false; 879 } 880 881 884 public boolean visit(MethodInvocation node) { 885 if (node.getExpression() != null) { 886 node.getExpression().accept(this); 887 this.fBuffer.append("."); } 889 if (node.getAST().apiLevel() >= AST.JLS3) { 890 if (!node.typeArguments().isEmpty()) { 891 this.fBuffer.append("<"); for (Iterator it= node.typeArguments().iterator(); it.hasNext();) { 893 Type t= (Type) it.next(); 894 t.accept(this); 895 if (it.hasNext()) { 896 this.fBuffer.append(","); } 898 } 899 this.fBuffer.append(">"); } 901 } 902 node.getName().accept(this); 903 this.fBuffer.append("("); for (Iterator it= node.arguments().iterator(); it.hasNext();) { 905 Expression e= (Expression) it.next(); 906 e.accept(this); 907 if (it.hasNext()) { 908 this.fBuffer.append(","); } 910 } 911 this.fBuffer.append(")"); return false; 913 } 914 915 919 public boolean visit(Modifier node) { 920 this.fBuffer.append(node.getKeyword().toString()); 921 return false; 922 } 923 924 928 public boolean visit(NormalAnnotation node) { 929 this.fBuffer.append("@"); node.getTypeName().accept(this); 931 this.fBuffer.append("("); for (Iterator it= node.values().iterator(); it.hasNext();) { 933 MemberValuePair p= (MemberValuePair) it.next(); 934 p.accept(this); 935 if (it.hasNext()) { 936 this.fBuffer.append(","); } 938 } 939 this.fBuffer.append(")"); return false; 941 } 942 943 946 public boolean visit(NullLiteral node) { 947 this.fBuffer.append("null"); return false; 949 } 950 951 954 public boolean visit(NumberLiteral node) { 955 this.fBuffer.append(node.getToken()); 956 return false; 957 } 958 959 962 public boolean visit(PackageDeclaration node) { 963 if (node.getAST().apiLevel() >= AST.JLS3) { 964 if (node.getJavadoc() != null) { 965 node.getJavadoc().accept(this); 966 } 967 for (Iterator it= node.annotations().iterator(); it.hasNext();) { 968 Annotation p= (Annotation) it.next(); 969 p.accept(this); 970 this.fBuffer.append(" "); } 972 } 973 this.fBuffer.append("package "); node.getName().accept(this); 975 this.fBuffer.append(";"); return false; 977 } 978 979 983 public boolean visit(ParameterizedType node) { 984 node.getType().accept(this); 985 this.fBuffer.append("<"); for (Iterator it= node.typeArguments().iterator(); it.hasNext();) { 987 Type t= (Type) it.next(); 988 t.accept(this); 989 if (it.hasNext()) { 990 this.fBuffer.append(","); } 992 } 993 this.fBuffer.append(">"); return false; 995 } 996 997 1000 public boolean visit(ParenthesizedExpression node) { 1001 this.fBuffer.append("("); node.getExpression().accept(this); 1003 this.fBuffer.append(")"); return false; 1005 } 1006 1007 1010 public boolean visit(PostfixExpression node) { 1011 node.getOperand().accept(this); 1012 this.fBuffer.append(node.getOperator().toString()); 1013 return false; 1014 } 1015 1016 1019 public boolean visit(PrefixExpression node) { 1020 this.fBuffer.append(node.getOperator().toString()); 1021 node.getOperand().accept(this); 1022 return false; 1023 } 1024 1025 1028 public boolean visit(PrimitiveType node) { 1029 this.fBuffer.append(node.getPrimitiveTypeCode().toString()); 1030 return false; 1031 } 1032 1033 1036 public boolean visit(QualifiedName node) { 1037 node.getQualifier().accept(this); 1038 this.fBuffer.append("."); node.getName().accept(this); 1040 return false; 1041 } 1042 1043 1047 public boolean visit(QualifiedType node) { 1048 node.getQualifier().accept(this); 1049 this.fBuffer.append("."); node.getName().accept(this); 1051 return false; 1052 } 1053 1054 1057 public boolean visit(ReturnStatement node) { 1058 this.fBuffer.append("return"); if (node.getExpression() != null) { 1060 this.fBuffer.append(" "); node.getExpression().accept(this); 1062 } 1063 this.fBuffer.append(";"); return false; 1065 } 1066 1067 1070 public boolean visit(SimpleName node) { 1071 this.fBuffer.append(node.getIdentifier()); 1072 return false; 1073 } 1074 1075 1078 public boolean visit(SimpleType node) { 1079 return true; 1080 } 1081 1082 1086 public boolean visit(SingleMemberAnnotation node) { 1087 this.fBuffer.append("@"); node.getTypeName().accept(this); 1089 this.fBuffer.append("("); node.getValue().accept(this); 1091 this.fBuffer.append(")"); return false; 1093 } 1094 1095 1098 public boolean visit(SingleVariableDeclaration node) { 1099 if (node.getAST().apiLevel() == AST.JLS2) { 1100 printModifiers(node.getModifiers()); 1101 } 1102 if (node.getAST().apiLevel() >= AST.JLS3) { 1103 printModifiers(node.modifiers()); 1104 } 1105 node.getType().accept(this); 1106 if (node.getAST().apiLevel() >= AST.JLS3) { 1107 if (node.isVarargs()) { 1108 this.fBuffer.append("..."); } 1110 } 1111 this.fBuffer.append(" "); node.getName().accept(this); 1113 for (int i= 0; i < node.getExtraDimensions(); i++) { 1114 this.fBuffer.append("[]"); } 1116 if (node.getInitializer() != null) { 1117 this.fBuffer.append("="); node.getInitializer().accept(this); 1119 } 1120 return false; 1121 } 1122 1123 1126 public boolean visit(StringLiteral node) { 1127 this.fBuffer.append(node.getEscapedValue()); 1128 return false; 1129 } 1130 1131 1134 public boolean visit(SuperConstructorInvocation node) { 1135 if (node.getExpression() != null) { 1136 node.getExpression().accept(this); 1137 this.fBuffer.append("."); } 1139 if (node.getAST().apiLevel() >= AST.JLS3) { 1140 if (!node.typeArguments().isEmpty()) { 1141 this.fBuffer.append("<"); for (Iterator it= node.typeArguments().iterator(); it.hasNext();) { 1143 Type t= (Type) it.next(); 1144 t.accept(this); 1145 if (it.hasNext()) { 1146 this.fBuffer.append(","); } 1148 } 1149 this.fBuffer.append(">"); } 1151 } 1152 this.fBuffer.append("super("); for (Iterator it= node.arguments().iterator(); it.hasNext();) { 1154 Expression e= (Expression) it.next(); 1155 e.accept(this); 1156 if (it.hasNext()) { 1157 this.fBuffer.append(","); } 1159 } 1160 this.fBuffer.append(");"); return false; 1162 } 1163 1164 1167 public boolean visit(SuperFieldAccess node) { 1168 if (node.getQualifier() != null) { 1169 node.getQualifier().accept(this); 1170 this.fBuffer.append("."); } 1172 this.fBuffer.append("super."); node.getName().accept(this); 1174 return false; 1175 } 1176 1177 1180 public boolean visit(SuperMethodInvocation node) { 1181 if (node.getQualifier() != null) { 1182 node.getQualifier().accept(this); 1183 this.fBuffer.append("."); } 1185 this.fBuffer.append("super."); if (node.getAST().apiLevel() >= AST.JLS3) { 1187 if (!node.typeArguments().isEmpty()) { 1188 this.fBuffer.append("<"); for (Iterator it= node.typeArguments().iterator(); it.hasNext();) { 1190 Type t= (Type) it.next(); 1191 t.accept(this); 1192 if (it.hasNext()) { 1193 this.fBuffer.append(","); } 1195 } 1196 this.fBuffer.append(">"); } 1198 } 1199 node.getName().accept(this); 1200 this.fBuffer.append("("); for (Iterator it= node.arguments().iterator(); it.hasNext();) { 1202 Expression e= (Expression) it.next(); 1203 e.accept(this); 1204 if (it.hasNext()) { 1205 this.fBuffer.append(","); } 1207 } 1208 this.fBuffer.append(")"); return false; 1210 } 1211 1212 1215 public boolean visit(SwitchCase node) { 1216 if (node.isDefault()) { 1217 this.fBuffer.append("default :"); } else { 1219 this.fBuffer.append("case "); node.getExpression().accept(this); 1221 this.fBuffer.append(":"); } 1223 return false; 1224 } 1225 1226 1229 public boolean visit(SwitchStatement node) { 1230 this.fBuffer.append("switch ("); node.getExpression().accept(this); 1232 this.fBuffer.append(") "); this.fBuffer.append("{"); for (Iterator it= node.statements().iterator(); it.hasNext();) { 1235 Statement s= (Statement) it.next(); 1236 s.accept(this); 1237 } 1238 this.fBuffer.append("}"); return false; 1240 } 1241 1242 1245 public boolean visit(SynchronizedStatement node) { 1246 this.fBuffer.append("synchronized ("); node.getExpression().accept(this); 1248 this.fBuffer.append(") "); node.getBody().accept(this); 1250 return false; 1251 } 1252 1253 1257 public boolean visit(TagElement node) { 1258 if (node.isNested()) { 1259 this.fBuffer.append("{"); } else { 1262 this.fBuffer.append("\n * "); } 1265 boolean previousRequiresWhiteSpace= false; 1266 if (node.getTagName() != null) { 1267 this.fBuffer.append(node.getTagName()); 1268 previousRequiresWhiteSpace= true; 1269 } 1270 boolean previousRequiresNewLine= false; 1271 for (Iterator it= node.fragments().iterator(); it.hasNext();) { 1272 ASTNode e= (ASTNode) it.next(); 1273 boolean currentIncludesWhiteSpace= (e instanceof TextElement); 1276 if (previousRequiresNewLine && currentIncludesWhiteSpace) { 1277 this.fBuffer.append("\n * "); } 1279 previousRequiresNewLine= currentIncludesWhiteSpace; 1280 if (previousRequiresWhiteSpace && !currentIncludesWhiteSpace) { 1282 this.fBuffer.append(" "); } 1284 e.accept(this); 1285 previousRequiresWhiteSpace= !currentIncludesWhiteSpace && !(e instanceof TagElement); 1286 } 1287 if (node.isNested()) { 1288 this.fBuffer.append("}"); } 1290 return false; 1291 } 1292 1293 1297 public boolean visit(TextElement node) { 1298 this.fBuffer.append(node.getText()); 1299 return false; 1300 } 1301 1302 1305 public boolean visit(ThisExpression node) { 1306 if (node.getQualifier() != null) { 1307 node.getQualifier().accept(this); 1308 this.fBuffer.append("."); } 1310 this.fBuffer.append("this"); return false; 1312 } 1313 1314 1317 public boolean visit(ThrowStatement node) { 1318 this.fBuffer.append("throw "); node.getExpression().accept(this); 1320 this.fBuffer.append(";"); return false; 1322 } 1323 1324 1327 public boolean visit(TryStatement node) { 1328 this.fBuffer.append("try "); node.getBody().accept(this); 1330 this.fBuffer.append(" "); for (Iterator it= node.catchClauses().iterator(); it.hasNext();) { 1332 CatchClause cc= (CatchClause) it.next(); 1333 cc.accept(this); 1334 } 1335 if (node.getFinally() != null) { 1336 this.fBuffer.append("finally "); node.getFinally().accept(this); 1338 } 1339 return false; 1340 } 1341 1342 1345 public boolean visit(TypeDeclaration node) { 1346 if (node.getJavadoc() != null) { 1347 node.getJavadoc().accept(this); 1348 } 1349 if (node.getAST().apiLevel() == AST.JLS2) { 1350 printModifiers(node.getModifiers()); 1351 } 1352 if (node.getAST().apiLevel() >= AST.JLS3) { 1353 printModifiers(node.modifiers()); 1354 } 1355 this.fBuffer.append(node.isInterface() ? "interface " : "class "); node.getName().accept(this); 1357 if (node.getAST().apiLevel() >= AST.JLS3) { 1358 if (!node.typeParameters().isEmpty()) { 1359 this.fBuffer.append("<"); for (Iterator it= node.typeParameters().iterator(); it.hasNext();) { 1361 TypeParameter t= (TypeParameter) it.next(); 1362 t.accept(this); 1363 if (it.hasNext()) { 1364 this.fBuffer.append(","); } 1366 } 1367 this.fBuffer.append(">"); } 1369 } 1370 this.fBuffer.append(" "); if (node.getAST().apiLevel() == AST.JLS2) { 1372 if (node.getSuperclass() != null) { 1373 this.fBuffer.append("extends "); node.getSuperclass().accept(this); 1375 this.fBuffer.append(" "); } 1377 if (!node.superInterfaces().isEmpty()) { 1378 this.fBuffer.append(node.isInterface() ? "extends " : "implements "); for (Iterator it= node.superInterfaces().iterator(); it.hasNext();) { 1380 Name n= (Name) it.next(); 1381 n.accept(this); 1382 if (it.hasNext()) { 1383 this.fBuffer.append(", "); } 1385 } 1386 this.fBuffer.append(" "); } 1388 } 1389 if (node.getAST().apiLevel() >= AST.JLS3) { 1390 if (node.getSuperclassType() != null) { 1391 this.fBuffer.append("extends "); node.getSuperclassType().accept(this); 1393 this.fBuffer.append(" "); } 1395 if (!node.superInterfaceTypes().isEmpty()) { 1396 this.fBuffer.append(node.isInterface() ? "extends " : "implements "); for (Iterator it= node.superInterfaceTypes().iterator(); it.hasNext();) { 1398 Type t= (Type) it.next(); 1399 t.accept(this); 1400 if (it.hasNext()) { 1401 this.fBuffer.append(", "); } 1403 } 1404 this.fBuffer.append(" "); } 1406 } 1407 this.fBuffer.append("{"); BodyDeclaration prev= null; 1409 for (Iterator it= node.bodyDeclarations().iterator(); it.hasNext();) { 1410 BodyDeclaration d= (BodyDeclaration) it.next(); 1411 if (prev instanceof EnumConstantDeclaration) { 1412 if (d instanceof EnumConstantDeclaration) { 1414 this.fBuffer.append(", "); } else { 1417 this.fBuffer.append("; "); } 1421 } 1422 d.accept(this); 1423 } 1424 this.fBuffer.append("}"); return false; 1426 } 1427 1428 1431 public boolean visit(TypeDeclarationStatement node) { 1432 if (node.getAST().apiLevel() == AST.JLS2) { 1433 node.getTypeDeclaration().accept(this); 1434 } 1435 if (node.getAST().apiLevel() >= AST.JLS3) { 1436 node.getDeclaration().accept(this); 1437 } 1438 return false; 1439 } 1440 1441 1444 public boolean visit(TypeLiteral node) { 1445 node.getType().accept(this); 1446 this.fBuffer.append(".class"); return false; 1448 } 1449 1450 1454 public boolean visit(TypeParameter node) { 1455 node.getName().accept(this); 1456 if (!node.typeBounds().isEmpty()) { 1457 this.fBuffer.append(" extends "); for (Iterator it= node.typeBounds().iterator(); it.hasNext();) { 1459 Type t= (Type) it.next(); 1460 t.accept(this); 1461 if (it.hasNext()) { 1462 this.fBuffer.append(" & "); } 1464 } 1465 } 1466 return false; 1467 } 1468 1469 1472 public boolean visit(VariableDeclarationExpression node) { 1473 if (node.getAST().apiLevel() == AST.JLS2) { 1474 printModifiers(node.getModifiers()); 1475 } 1476 if (node.getAST().apiLevel() >= AST.JLS3) { 1477 printModifiers(node.modifiers()); 1478 } 1479 node.getType().accept(this); 1480 this.fBuffer.append(" "); for (Iterator it= node.fragments().iterator(); it.hasNext();) { 1482 VariableDeclarationFragment f= (VariableDeclarationFragment) it.next(); 1483 f.accept(this); 1484 if (it.hasNext()) { 1485 this.fBuffer.append(", "); } 1487 } 1488 return false; 1489 } 1490 1491 1494 public boolean visit(VariableDeclarationFragment node) { 1495 node.getName().accept(this); 1496 for (int i= 0; i < node.getExtraDimensions(); i++) { 1497 this.fBuffer.append("[]"); } 1499 if (node.getInitializer() != null) { 1500 this.fBuffer.append("="); node.getInitializer().accept(this); 1502 } 1503 return false; 1504 } 1505 1506 1509 public boolean visit(VariableDeclarationStatement node) { 1510 if (node.getAST().apiLevel() == AST.JLS2) { 1511 printModifiers(node.getModifiers()); 1512 } 1513 if (node.getAST().apiLevel() >= AST.JLS3) { 1514 printModifiers(node.modifiers()); 1515 } 1516 node.getType().accept(this); 1517 this.fBuffer.append(" "); for (Iterator it= node.fragments().iterator(); it.hasNext();) { 1519 VariableDeclarationFragment f= (VariableDeclarationFragment) it.next(); 1520 f.accept(this); 1521 if (it.hasNext()) { 1522 this.fBuffer.append(", "); } 1524 } 1525 this.fBuffer.append(";"); return false; 1527 } 1528 1529 1533 public boolean visit(WildcardType node) { 1534 this.fBuffer.append("?"); Type bound= node.getBound(); 1536 if (bound != null) { 1537 if (node.isUpperBound()) { 1538 this.fBuffer.append(" extends "); } else { 1540 this.fBuffer.append(" super "); } 1542 bound.accept(this); 1543 } 1544 return false; 1545 } 1546 1547 1550 public boolean visit(WhileStatement node) { 1551 this.fBuffer.append("while ("); node.getExpression().accept(this); 1553 this.fBuffer.append(") "); node.getBody().accept(this); 1555 return false; 1556 } 1557 1558} 1559 | Popular Tags |