0001: /*
0002: * Copyright (C) 2007 Júlio Vilmar Gesser.
0003: *
0004: * This file is part of Java 1.5 parser and Abstract Syntax Tree.
0005: *
0006: * Java 1.5 parser and Abstract Syntax Tree is free software: you can redistribute it and/or modify
0007: * it under the terms of the GNU Lesser General Public License as published by
0008: * the Free Software Foundation, either version 3 of the License, or
0009: * (at your option) any later version.
0010: *
0011: * Java 1.5 parser and Abstract Syntax Tree is distributed in the hope that it will be useful,
0012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0014: * GNU Lesser General Public License for more details.
0015: *
0016: * You should have received a copy of the GNU Lesser General Public License
0017: * along with Java 1.5 parser and Abstract Syntax Tree. If not, see <http://www.gnu.org/licenses/>.
0018: */
0019: /*
0020: * Created on 05/10/2006
0021: */
0022: package japa.parser.ast.visitor;
0023:
0024: import japa.parser.ast.CompilationUnit;
0025: import japa.parser.ast.ImportDeclaration;
0026: import japa.parser.ast.Node;
0027: import japa.parser.ast.PackageDeclaration;
0028: import japa.parser.ast.TypeParameter;
0029: import japa.parser.ast.body.AnnotationDeclaration;
0030: import japa.parser.ast.body.AnnotationMemberDeclaration;
0031: import japa.parser.ast.body.BodyDeclaration;
0032: import japa.parser.ast.body.ClassOrInterfaceDeclaration;
0033: import japa.parser.ast.body.ConstructorDeclaration;
0034: import japa.parser.ast.body.EmptyMemberDeclaration;
0035: import japa.parser.ast.body.EmptyTypeDeclaration;
0036: import japa.parser.ast.body.EnumConstantDeclaration;
0037: import japa.parser.ast.body.EnumDeclaration;
0038: import japa.parser.ast.body.FieldDeclaration;
0039: import japa.parser.ast.body.InitializerDeclaration;
0040: import japa.parser.ast.body.MethodDeclaration;
0041: import japa.parser.ast.body.ModifierSet;
0042: import japa.parser.ast.body.Parameter;
0043: import japa.parser.ast.body.TypeDeclaration;
0044: import japa.parser.ast.body.VariableDeclarator;
0045: import japa.parser.ast.body.VariableDeclaratorId;
0046: import japa.parser.ast.expr.AnnotationExpr;
0047: import japa.parser.ast.expr.ArrayAccessExpr;
0048: import japa.parser.ast.expr.ArrayCreationExpr;
0049: import japa.parser.ast.expr.ArrayInitializerExpr;
0050: import japa.parser.ast.expr.AssignExpr;
0051: import japa.parser.ast.expr.BinaryExpr;
0052: import japa.parser.ast.expr.BooleanLiteralExpr;
0053: import japa.parser.ast.expr.CastExpr;
0054: import japa.parser.ast.expr.CharLiteralExpr;
0055: import japa.parser.ast.expr.ClassExpr;
0056: import japa.parser.ast.expr.ConditionalExpr;
0057: import japa.parser.ast.expr.DoubleLiteralExpr;
0058: import japa.parser.ast.expr.EnclosedExpr;
0059: import japa.parser.ast.expr.Expression;
0060: import japa.parser.ast.expr.FieldAccessExpr;
0061: import japa.parser.ast.expr.InstanceOfExpr;
0062: import japa.parser.ast.expr.IntegerLiteralExpr;
0063: import japa.parser.ast.expr.IntegerLiteralMinValueExpr;
0064: import japa.parser.ast.expr.LongLiteralExpr;
0065: import japa.parser.ast.expr.LongLiteralMinValueExpr;
0066: import japa.parser.ast.expr.MarkerAnnotationExpr;
0067: import japa.parser.ast.expr.MemberValuePair;
0068: import japa.parser.ast.expr.MethodCallExpr;
0069: import japa.parser.ast.expr.NameExpr;
0070: import japa.parser.ast.expr.NormalAnnotationExpr;
0071: import japa.parser.ast.expr.NullLiteralExpr;
0072: import japa.parser.ast.expr.ObjectCreationExpr;
0073: import japa.parser.ast.expr.QualifiedNameExpr;
0074: import japa.parser.ast.expr.SingleMemberAnnotationExpr;
0075: import japa.parser.ast.expr.StringLiteralExpr;
0076: import japa.parser.ast.expr.SuperExpr;
0077: import japa.parser.ast.expr.SuperMemberAccessExpr;
0078: import japa.parser.ast.expr.ThisExpr;
0079: import japa.parser.ast.expr.UnaryExpr;
0080: import japa.parser.ast.expr.VariableDeclarationExpr;
0081: import japa.parser.ast.stmt.AssertStmt;
0082: import japa.parser.ast.stmt.BlockStmt;
0083: import japa.parser.ast.stmt.BreakStmt;
0084: import japa.parser.ast.stmt.CatchClause;
0085: import japa.parser.ast.stmt.ContinueStmt;
0086: import japa.parser.ast.stmt.DoStmt;
0087: import japa.parser.ast.stmt.EmptyStmt;
0088: import japa.parser.ast.stmt.ExplicitConstructorInvocationStmt;
0089: import japa.parser.ast.stmt.ExpressionStmt;
0090: import japa.parser.ast.stmt.ForStmt;
0091: import japa.parser.ast.stmt.ForeachStmt;
0092: import japa.parser.ast.stmt.IfStmt;
0093: import japa.parser.ast.stmt.LabeledStmt;
0094: import japa.parser.ast.stmt.ReturnStmt;
0095: import japa.parser.ast.stmt.Statement;
0096: import japa.parser.ast.stmt.SwitchEntryStmt;
0097: import japa.parser.ast.stmt.SwitchStmt;
0098: import japa.parser.ast.stmt.SynchronizedStmt;
0099: import japa.parser.ast.stmt.ThrowStmt;
0100: import japa.parser.ast.stmt.TryStmt;
0101: import japa.parser.ast.stmt.TypeDeclarationStmt;
0102: import japa.parser.ast.stmt.WhileStmt;
0103: import japa.parser.ast.type.ClassOrInterfaceType;
0104: import japa.parser.ast.type.PrimitiveType;
0105: import japa.parser.ast.type.ReferenceType;
0106: import japa.parser.ast.type.Type;
0107: import japa.parser.ast.type.VoidType;
0108: import japa.parser.ast.type.WildcardType;
0109:
0110: import java.util.Iterator;
0111: import java.util.List;
0112:
0113: /**
0114: * @author Julio Vilmar Gesser
0115: */
0116:
0117: public final class DumpVisitor implements VoidVisitor<Object> {
0118:
0119: private final SourcePrinter printer = new SourcePrinter();
0120:
0121: public String getSource() {
0122: return printer.getSource();
0123: }
0124:
0125: private void printModifiers(int modifiers) {
0126: if (ModifierSet.isPrivate(modifiers)) {
0127: printer.print("private ");
0128: }
0129: if (ModifierSet.isProtected(modifiers)) {
0130: printer.print("protected ");
0131: }
0132: if (ModifierSet.isPublic(modifiers)) {
0133: printer.print("public ");
0134: }
0135: if (ModifierSet.isAbstract(modifiers)) {
0136: printer.print("abstract ");
0137: }
0138: if (ModifierSet.isFinal(modifiers)) {
0139: printer.print("final ");
0140: }
0141: if (ModifierSet.isNative(modifiers)) {
0142: printer.print("native ");
0143: }
0144: if (ModifierSet.isStatic(modifiers)) {
0145: printer.print("static ");
0146: }
0147: if (ModifierSet.isStrictfp(modifiers)) {
0148: printer.print("strictfp ");
0149: }
0150: if (ModifierSet.isSynchronized(modifiers)) {
0151: printer.print("synchronized ");
0152: }
0153: if (ModifierSet.isTransient(modifiers)) {
0154: printer.print("transient ");
0155: }
0156: if (ModifierSet.isVolatile(modifiers)) {
0157: printer.print("volatile ");
0158: }
0159: }
0160:
0161: private void printMembers(List<BodyDeclaration> members, Object arg) {
0162: for (BodyDeclaration member : members) {
0163: printer.printLn();
0164: member.accept(this , arg);
0165: printer.printLn();
0166: }
0167: }
0168:
0169: private void printMemberAnnotations(
0170: List<AnnotationExpr> annotations, Object arg) {
0171: if (annotations != null) {
0172: for (AnnotationExpr a : annotations) {
0173: a.accept(this , arg);
0174: printer.printLn();
0175: }
0176: }
0177: }
0178:
0179: private void printAnnotations(List<AnnotationExpr> annotations,
0180: Object arg) {
0181: if (annotations != null) {
0182: for (AnnotationExpr a : annotations) {
0183: a.accept(this , arg);
0184: printer.print(" ");
0185: }
0186: }
0187: }
0188:
0189: private void printTypeArgs(List<Type> args, Object arg) {
0190: if (args != null) {
0191: printer.print("<");
0192: for (Iterator<Type> i = args.iterator(); i.hasNext();) {
0193: Type t = i.next();
0194: t.accept(this , arg);
0195: if (i.hasNext()) {
0196: printer.print(", ");
0197: }
0198: }
0199: printer.print(">");
0200: }
0201: }
0202:
0203: private void printTypeParameters(List<TypeParameter> args,
0204: Object arg) {
0205: if (args != null) {
0206: printer.print("<");
0207: for (Iterator<TypeParameter> i = args.iterator(); i
0208: .hasNext();) {
0209: TypeParameter t = i.next();
0210: t.accept(this , arg);
0211: if (i.hasNext()) {
0212: printer.print(", ");
0213: }
0214: }
0215: printer.print(">");
0216: }
0217: }
0218:
0219: public void visit(Node n, Object arg) {
0220: throw new IllegalStateException(n.getClass().getName());
0221: }
0222:
0223: public void visit(CompilationUnit n, Object arg) {
0224: if (n.pakage != null) {
0225: n.pakage.accept(this , arg);
0226: }
0227: if (n.imports != null) {
0228: for (ImportDeclaration i : n.imports) {
0229: i.accept(this , arg);
0230: }
0231: printer.printLn();
0232: }
0233: if (n.types != null) {
0234: for (TypeDeclaration i : n.types) {
0235: i.accept(this , arg);
0236: printer.printLn();
0237: }
0238: }
0239: }
0240:
0241: public void visit(PackageDeclaration n, Object arg) {
0242: printAnnotations(n.annotations, arg);
0243: printer.print("package ");
0244: n.name.accept(this , arg);
0245: printer.printLn(";");
0246: printer.printLn();
0247: }
0248:
0249: public void visit(NameExpr n, Object arg) {
0250: printer.print(n.name);
0251: }
0252:
0253: public void visit(QualifiedNameExpr n, Object arg) {
0254: n.qualifier.accept(this , arg);
0255: printer.print(".");
0256: printer.print(n.name);
0257: }
0258:
0259: public void visit(ImportDeclaration n, Object arg) {
0260: printer.print("import ");
0261: if (n.isStatic) {
0262: printer.print("static ");
0263: }
0264: n.name.accept(this , arg);
0265: if (n.isAsterisk) {
0266: printer.print(".*");
0267: }
0268: printer.printLn(";");
0269: }
0270:
0271: public void visit(ClassOrInterfaceDeclaration n, Object arg) {
0272: printMemberAnnotations(n.annotations, arg);
0273: printModifiers(n.modifiers);
0274:
0275: if (n.isInterface) {
0276: printer.print("interface ");
0277: } else {
0278: printer.print("class ");
0279: }
0280:
0281: printer.print(n.name);
0282:
0283: printTypeParameters(n.typeParameters, arg);
0284:
0285: if (n.extendsList != null) {
0286: printer.print(" extends ");
0287: for (Iterator<ClassOrInterfaceType> i = n.extendsList
0288: .iterator(); i.hasNext();) {
0289: ClassOrInterfaceType c = i.next();
0290: c.accept(this , arg);
0291: if (i.hasNext()) {
0292: printer.print(", ");
0293: }
0294: }
0295: }
0296:
0297: if (n.implements List != null) {
0298: printer.print(" implements ");
0299: for (Iterator<ClassOrInterfaceType> i = n.implements List
0300: .iterator(); i.hasNext();) {
0301: ClassOrInterfaceType c = i.next();
0302: c.accept(this , arg);
0303: if (i.hasNext()) {
0304: printer.print(", ");
0305: }
0306: }
0307: }
0308:
0309: printer.printLn(" {");
0310: printer.indent();
0311: if (n.members != null) {
0312: printMembers(n.members, arg);
0313: }
0314: printer.unindent();
0315: printer.print("}");
0316: }
0317:
0318: public void visit(EmptyTypeDeclaration n, Object arg) {
0319: printer.print(";");
0320: }
0321:
0322: public void visit(ClassOrInterfaceType n, Object arg) {
0323: if (n.scope != null) {
0324: n.scope.accept(this , arg);
0325: printer.print(".");
0326: }
0327: printer.print(n.name);
0328: printTypeArgs(n.typeArgs, arg);
0329: }
0330:
0331: public void visit(TypeParameter n, Object arg) {
0332: printer.print(n.name);
0333: if (n.typeBound != null) {
0334: printer.print(" extends ");
0335: for (Iterator<ClassOrInterfaceType> i = n.typeBound
0336: .iterator(); i.hasNext();) {
0337: ClassOrInterfaceType c = i.next();
0338: c.accept(this , arg);
0339: if (i.hasNext()) {
0340: printer.print(" & ");
0341: }
0342: }
0343: }
0344: }
0345:
0346: public void visit(PrimitiveType n, Object arg) {
0347: switch (n.type) {
0348: case Boolean:
0349: printer.print("boolean");
0350: break;
0351: case Byte:
0352: printer.print("byte");
0353: break;
0354: case Char:
0355: printer.print("char");
0356: break;
0357: case Double:
0358: printer.print("double");
0359: break;
0360: case Float:
0361: printer.print("float");
0362: break;
0363: case Int:
0364: printer.print("int");
0365: break;
0366: case Long:
0367: printer.print("long");
0368: break;
0369: case Short:
0370: printer.print("short");
0371: break;
0372: }
0373: }
0374:
0375: public void visit(ReferenceType n, Object arg) {
0376: n.type.accept(this , arg);
0377: for (int i = 0; i < n.arrayCount; i++) {
0378: printer.print("[]");
0379: }
0380: }
0381:
0382: public void visit(WildcardType n, Object arg) {
0383: printer.print("?");
0384: if (n.ext != null) {
0385: printer.print(" extends ");
0386: n.ext.accept(this , arg);
0387: }
0388: if (n.sup != null) {
0389: printer.print(" super ");
0390: n.sup.accept(this , arg);
0391: }
0392: }
0393:
0394: public void visit(FieldDeclaration n, Object arg) {
0395: printMemberAnnotations(n.annotations, arg);
0396: printModifiers(n.modifiers);
0397: n.type.accept(this , arg);
0398:
0399: printer.print(" ");
0400: for (Iterator<VariableDeclarator> i = n.variables.iterator(); i
0401: .hasNext();) {
0402: VariableDeclarator var = i.next();
0403: var.accept(this , arg);
0404: if (i.hasNext()) {
0405: printer.print(", ");
0406: }
0407: }
0408:
0409: printer.print(";");
0410: }
0411:
0412: public void visit(VariableDeclarator n, Object arg) {
0413: n.id.accept(this , arg);
0414: if (n.init != null) {
0415: printer.print(" = ");
0416: n.init.accept(this , arg);
0417: }
0418: }
0419:
0420: public void visit(VariableDeclaratorId n, Object arg) {
0421: printer.print(n.name);
0422: for (int i = 0; i < n.arrayCount; i++) {
0423: printer.print("[]");
0424: }
0425: }
0426:
0427: public void visit(ArrayInitializerExpr n, Object arg) {
0428: printer.print("{");
0429: if (n.values != null) {
0430: printer.print(" ");
0431: for (Iterator<Expression> i = n.values.iterator(); i
0432: .hasNext();) {
0433: Expression expr = i.next();
0434: expr.accept(this , arg);
0435: if (i.hasNext()) {
0436: printer.print(", ");
0437: }
0438: }
0439: printer.print(" ");
0440: }
0441: printer.print("}");
0442: }
0443:
0444: public void visit(VoidType n, Object arg) {
0445: printer.print("void");
0446: }
0447:
0448: public void visit(ArrayAccessExpr n, Object arg) {
0449: n.name.accept(this , arg);
0450: printer.print("[");
0451: n.index.accept(this , arg);
0452: printer.print("]");
0453: }
0454:
0455: public void visit(ArrayCreationExpr n, Object arg) {
0456: printer.print("new ");
0457: n.type.accept(this , arg);
0458: printTypeArgs(n.typeArgs, arg);
0459:
0460: if (n.dimensions != null) {
0461: for (Expression dim : n.dimensions) {
0462: printer.print("[");
0463: dim.accept(this , arg);
0464: printer.print("]");
0465: }
0466: for (int i = 0; i < n.arrayCount; i++) {
0467: printer.print("[]");
0468: }
0469: } else {
0470: for (int i = 0; i < n.arrayCount; i++) {
0471: printer.print("[]");
0472: }
0473: printer.print(" ");
0474: n.initializer.accept(this , arg);
0475: }
0476: }
0477:
0478: public void visit(AssignExpr n, Object arg) {
0479: n.target.accept(this , arg);
0480: printer.print(" ");
0481: switch (n.op) {
0482: case assign:
0483: printer.print("=");
0484: break;
0485: case and:
0486: printer.print("&=");
0487: break;
0488: case or:
0489: printer.print("|=");
0490: break;
0491: case xor:
0492: printer.print("^=");
0493: break;
0494: case plus:
0495: printer.print("+=");
0496: break;
0497: case minus:
0498: printer.print("-=");
0499: break;
0500: case rem:
0501: printer.print("%=");
0502: break;
0503: case slash:
0504: printer.print("/=");
0505: break;
0506: case star:
0507: printer.print("*=");
0508: break;
0509: case lShift:
0510: printer.print("<<=");
0511: break;
0512: case rSignedShift:
0513: printer.print(">>=");
0514: break;
0515: case rUnsignedShift:
0516: printer.print(">>>=");
0517: break;
0518: }
0519: printer.print(" ");
0520: n.value.accept(this , arg);
0521: }
0522:
0523: public void visit(BinaryExpr n, Object arg) {
0524: n.left.accept(this , arg);
0525: printer.print(" ");
0526: switch (n.op) {
0527: case or:
0528: printer.print("||");
0529: break;
0530: case and:
0531: printer.print("&&");
0532: break;
0533: case binOr:
0534: printer.print("|");
0535: break;
0536: case binAnd:
0537: printer.print("&");
0538: break;
0539: case xor:
0540: printer.print("^");
0541: break;
0542: case equals:
0543: printer.print("==");
0544: break;
0545: case notEquals:
0546: printer.print("!=");
0547: break;
0548: case less:
0549: printer.print("<");
0550: break;
0551: case greater:
0552: printer.print(">");
0553: break;
0554: case lessEquals:
0555: printer.print("<=");
0556: break;
0557: case greaterEquals:
0558: printer.print(">=");
0559: break;
0560: case lShift:
0561: printer.print("<<");
0562: break;
0563: case rSignedShift:
0564: printer.print(">>");
0565: break;
0566: case rUnsignedShift:
0567: printer.print(">>>");
0568: break;
0569: case plus:
0570: printer.print("+");
0571: break;
0572: case minus:
0573: printer.print("-");
0574: break;
0575: case times:
0576: printer.print("*");
0577: break;
0578: case divide:
0579: printer.print("/");
0580: break;
0581: case remainder:
0582: printer.print("%");
0583: break;
0584: }
0585: printer.print(" ");
0586: n.right.accept(this , arg);
0587: }
0588:
0589: public void visit(CastExpr n, Object arg) {
0590: printer.print("(");
0591: n.type.accept(this , arg);
0592: printer.print(") ");
0593: n.expr.accept(this , arg);
0594: }
0595:
0596: public void visit(ClassExpr n, Object arg) {
0597: n.type.accept(this , arg);
0598: printer.print(".class");
0599: }
0600:
0601: public void visit(ConditionalExpr n, Object arg) {
0602: n.condition.accept(this , arg);
0603: printer.print(" ? ");
0604: n.thenExpr.accept(this , arg);
0605: printer.print(" : ");
0606: n.elseExpr.accept(this , arg);
0607: }
0608:
0609: public void visit(EnclosedExpr n, Object arg) {
0610: printer.print("(");
0611: n.inner.accept(this , arg);
0612: printer.print(")");
0613: }
0614:
0615: public void visit(FieldAccessExpr n, Object arg) {
0616: n.scope.accept(this , arg);
0617: printer.print(".");
0618: printer.print(n.field);
0619: }
0620:
0621: public void visit(InstanceOfExpr n, Object arg) {
0622: n.expr.accept(this , arg);
0623: printer.print(" instanceof ");
0624: n.type.accept(this , arg);
0625: }
0626:
0627: public void visit(CharLiteralExpr n, Object arg) {
0628: printer.print("'");
0629: printer.print(n.value);
0630: printer.print("'");
0631: }
0632:
0633: public void visit(DoubleLiteralExpr n, Object arg) {
0634: printer.print(n.value);
0635: }
0636:
0637: public void visit(IntegerLiteralExpr n, Object arg) {
0638: printer.print(n.value);
0639: }
0640:
0641: public void visit(LongLiteralExpr n, Object arg) {
0642: printer.print(n.value);
0643: }
0644:
0645: public void visit(IntegerLiteralMinValueExpr n, Object arg) {
0646: printer.print(n.value);
0647: }
0648:
0649: public void visit(LongLiteralMinValueExpr n, Object arg) {
0650: printer.print(n.value);
0651: }
0652:
0653: public void visit(StringLiteralExpr n, Object arg) {
0654: printer.print("\"");
0655: printer.print(n.value);
0656: printer.print("\"");
0657: }
0658:
0659: public void visit(BooleanLiteralExpr n, Object arg) {
0660: printer.print(n.value.toString());
0661: }
0662:
0663: public void visit(NullLiteralExpr n, Object arg) {
0664: printer.print("null");
0665: }
0666:
0667: public void visit(ThisExpr n, Object arg) {
0668: if (n.classExpr != null) {
0669: n.classExpr.accept(this , arg);
0670: printer.print(".");
0671: }
0672: printer.print("this");
0673: }
0674:
0675: public void visit(SuperExpr n, Object arg) {
0676: if (n.classExpr != null) {
0677: n.classExpr.accept(this , arg);
0678: printer.print(".");
0679: }
0680: printer.print("super");
0681: }
0682:
0683: public void visit(MethodCallExpr n, Object arg) {
0684: if (n.scope != null) {
0685: n.scope.accept(this , arg);
0686: printer.print(".");
0687: }
0688: printTypeArgs(n.typeArgs, arg);
0689: printer.print(n.name);
0690: printer.print("(");
0691: if (n.args != null) {
0692: for (Iterator<Expression> i = n.args.iterator(); i
0693: .hasNext();) {
0694: Expression e = i.next();
0695: e.accept(this , arg);
0696: if (i.hasNext()) {
0697: printer.print(", ");
0698: }
0699: }
0700: }
0701: printer.print(")");
0702: }
0703:
0704: public void visit(ObjectCreationExpr n, Object arg) {
0705: if (n.scope != null) {
0706: n.scope.accept(this , arg);
0707: printer.print(".");
0708: }
0709:
0710: printer.print("new ");
0711:
0712: printTypeArgs(n.typeArgs, arg);
0713: n.type.accept(this , arg);
0714:
0715: printer.print("(");
0716: if (n.args != null) {
0717: for (Iterator<Expression> i = n.args.iterator(); i
0718: .hasNext();) {
0719: Expression e = i.next();
0720: e.accept(this , arg);
0721: if (i.hasNext()) {
0722: printer.print(", ");
0723: }
0724: }
0725: }
0726: printer.print(")");
0727:
0728: if (n.anonymousClassBody != null) {
0729: printer.printLn(" {");
0730: printer.indent();
0731: printMembers(n.anonymousClassBody, arg);
0732: printer.unindent();
0733: printer.print("}");
0734: }
0735: }
0736:
0737: public void visit(SuperMemberAccessExpr n, Object arg) {
0738: printer.print("super.");
0739: printer.print(n.name);
0740: }
0741:
0742: public void visit(UnaryExpr n, Object arg) {
0743: switch (n.op) {
0744: case positive:
0745: printer.print("+");
0746: break;
0747: case negative:
0748: printer.print("-");
0749: break;
0750: case inverse:
0751: printer.print("~");
0752: break;
0753: case not:
0754: printer.print("!");
0755: break;
0756: case preIncrement:
0757: printer.print("++");
0758: break;
0759: case preDecrement:
0760: printer.print("--");
0761: break;
0762: }
0763:
0764: n.expr.accept(this , arg);
0765:
0766: switch (n.op) {
0767: case posIncrement:
0768: printer.print("++");
0769: break;
0770: case posDecrement:
0771: printer.print("--");
0772: break;
0773: }
0774: }
0775:
0776: public void visit(ConstructorDeclaration n, Object arg) {
0777: printMemberAnnotations(n.annotations, arg);
0778: printModifiers(n.modifiers);
0779:
0780: printTypeParameters(n.typeParameters, arg);
0781: if (n.typeParameters != null) {
0782: printer.print(" ");
0783: }
0784: printer.print(n.name);
0785:
0786: printer.print("(");
0787: if (n.parameters != null) {
0788: for (Iterator<Parameter> i = n.parameters.iterator(); i
0789: .hasNext();) {
0790: Parameter p = i.next();
0791: p.accept(this , arg);
0792: if (i.hasNext()) {
0793: printer.print(", ");
0794: }
0795: }
0796: }
0797: printer.print(")");
0798:
0799: if (n.throws_ != null) {
0800: printer.print(" throws ");
0801: for (Iterator<NameExpr> i = n.throws_.iterator(); i
0802: .hasNext();) {
0803: NameExpr name = i.next();
0804: name.accept(this , arg);
0805: if (i.hasNext()) {
0806: printer.print(", ");
0807: }
0808: }
0809: }
0810: printer.print(" ");
0811: n.block.accept(this , arg);
0812: }
0813:
0814: public void visit(MethodDeclaration n, Object arg) {
0815: printMemberAnnotations(n.annotations, arg);
0816: printModifiers(n.modifiers);
0817:
0818: printTypeParameters(n.typeParameters, arg);
0819: if (n.typeParameters != null) {
0820: printer.print(" ");
0821: }
0822:
0823: n.type.accept(this , arg);
0824: printer.print(" ");
0825: printer.print(n.name);
0826:
0827: printer.print("(");
0828: if (n.parameters != null) {
0829: for (Iterator<Parameter> i = n.parameters.iterator(); i
0830: .hasNext();) {
0831: Parameter p = i.next();
0832: p.accept(this , arg);
0833: if (i.hasNext()) {
0834: printer.print(", ");
0835: }
0836: }
0837: }
0838: printer.print(")");
0839:
0840: for (int i = 0; i < n.arrayCount; i++) {
0841: printer.print("[]");
0842: }
0843:
0844: if (n.throws_ != null) {
0845: printer.print(" throws ");
0846: for (Iterator<NameExpr> i = n.throws_.iterator(); i
0847: .hasNext();) {
0848: NameExpr name = i.next();
0849: name.accept(this , arg);
0850: if (i.hasNext()) {
0851: printer.print(", ");
0852: }
0853: }
0854: }
0855: if (n.block == null) {
0856: printer.print(";");
0857: } else {
0858: printer.print(" ");
0859: n.block.accept(this , arg);
0860: }
0861: }
0862:
0863: public void visit(Parameter n, Object arg) {
0864: printAnnotations(n.annotations, arg);
0865: printModifiers(n.modifiers);
0866:
0867: n.type.accept(this , arg);
0868: if (n.isVarArgs) {
0869: printer.print("...");
0870: }
0871: printer.print(" ");
0872: n.id.accept(this , arg);
0873: }
0874:
0875: public void visit(ExplicitConstructorInvocationStmt n, Object arg) {
0876: if (n.isThis) {
0877: printTypeArgs(n.typeArgs, arg);
0878: printer.print("this");
0879: } else {
0880: if (n.expr != null) {
0881: n.expr.accept(this , arg);
0882: printer.print(".");
0883: }
0884: printTypeArgs(n.typeArgs, arg);
0885: printer.print("super");
0886: }
0887: printer.print("(");
0888: if (n.args != null) {
0889: for (Iterator<Expression> i = n.args.iterator(); i
0890: .hasNext();) {
0891: Expression e = i.next();
0892: e.accept(this , arg);
0893: if (i.hasNext()) {
0894: printer.print(", ");
0895: }
0896: }
0897: }
0898: printer.print(");");
0899: }
0900:
0901: public void visit(VariableDeclarationExpr n, Object arg) {
0902: printAnnotations(n.annotations, arg);
0903: printModifiers(n.modifiers);
0904:
0905: n.type.accept(this , arg);
0906: printer.print(" ");
0907:
0908: for (Iterator<VariableDeclarator> i = n.vars.iterator(); i
0909: .hasNext();) {
0910: VariableDeclarator v = i.next();
0911: v.accept(this , arg);
0912: if (i.hasNext()) {
0913: printer.print(", ");
0914: }
0915: }
0916: }
0917:
0918: public void visit(TypeDeclarationStmt n, Object arg) {
0919: n.typeDecl.accept(this , arg);
0920: }
0921:
0922: public void visit(AssertStmt n, Object arg) {
0923: printer.print("assert ");
0924: n.check.accept(this , arg);
0925: if (n.msg != null) {
0926: printer.print(" : ");
0927: n.msg.accept(this , arg);
0928: }
0929: printer.print(";");
0930: }
0931:
0932: public void visit(BlockStmt n, Object arg) {
0933: printer.printLn("{");
0934: if (n.stmts != null) {
0935: printer.indent();
0936: for (Statement s : n.stmts) {
0937: s.accept(this , arg);
0938: printer.printLn();
0939: }
0940: printer.unindent();
0941: }
0942: printer.print("}");
0943:
0944: }
0945:
0946: public void visit(LabeledStmt n, Object arg) {
0947: printer.print(n.label);
0948: printer.print(": ");
0949: n.stmt.accept(this , arg);
0950: }
0951:
0952: public void visit(EmptyStmt n, Object arg) {
0953: printer.print(";");
0954: }
0955:
0956: public void visit(ExpressionStmt n, Object arg) {
0957: n.expr.accept(this , arg);
0958: printer.print(";");
0959: }
0960:
0961: public void visit(SwitchStmt n, Object arg) {
0962: printer.print("switch(");
0963: n.selector.accept(this , arg);
0964: printer.printLn(") {");
0965: if (n.entries != null) {
0966: printer.indent();
0967: for (SwitchEntryStmt e : n.entries) {
0968: e.accept(this , arg);
0969: }
0970: printer.unindent();
0971: }
0972: printer.print("}");
0973:
0974: }
0975:
0976: public void visit(SwitchEntryStmt n, Object arg) {
0977: if (n.label != null) {
0978: printer.print("case ");
0979: n.label.accept(this , arg);
0980: printer.print(":");
0981: } else {
0982: printer.print("default:");
0983: }
0984: printer.printLn();
0985: printer.indent();
0986: if (n.stmts != null) {
0987: for (Statement s : n.stmts) {
0988: s.accept(this , arg);
0989: printer.printLn();
0990: }
0991: }
0992: printer.unindent();
0993: }
0994:
0995: public void visit(BreakStmt n, Object arg) {
0996: printer.print("break");
0997: if (n.id != null) {
0998: printer.print(" ");
0999: printer.print(n.id);
1000: }
1001: printer.print(";");
1002: }
1003:
1004: public void visit(ReturnStmt n, Object arg) {
1005: printer.print("return");
1006: if (n.expr != null) {
1007: printer.print(" ");
1008: n.expr.accept(this , arg);
1009: }
1010: printer.print(";");
1011: }
1012:
1013: public void visit(EnumDeclaration n, Object arg) {
1014: printMemberAnnotations(n.annotations, arg);
1015: printModifiers(n.modifiers);
1016:
1017: printer.print("enum ");
1018: printer.print(n.name);
1019:
1020: if (n.implements List != null) {
1021: printer.print(" implements ");
1022: for (Iterator<ClassOrInterfaceType> i = n.implements List
1023: .iterator(); i.hasNext();) {
1024: ClassOrInterfaceType c = i.next();
1025: c.accept(this , arg);
1026: if (i.hasNext()) {
1027: printer.print(", ");
1028: }
1029: }
1030: }
1031:
1032: printer.printLn(" {");
1033: printer.indent();
1034: if (n.entries != null) {
1035: printer.printLn();
1036: for (Iterator<EnumConstantDeclaration> i = n.entries
1037: .iterator(); i.hasNext();) {
1038: EnumConstantDeclaration e = i.next();
1039: e.accept(this , arg);
1040: if (i.hasNext()) {
1041: printer.print(", ");
1042: }
1043: }
1044: }
1045: if (n.members != null) {
1046: printer.printLn(";");
1047: printMembers(n.members, arg);
1048: } else {
1049: printer.printLn();
1050: }
1051: printer.unindent();
1052: printer.print("}");
1053: }
1054:
1055: public void visit(EnumConstantDeclaration n, Object arg) {
1056: printMemberAnnotations(n.annotations, arg);
1057: printer.print(n.name);
1058:
1059: if (n.args != null) {
1060: printer.print("(");
1061: for (Iterator<Expression> i = n.args.iterator(); i
1062: .hasNext();) {
1063: Expression e = i.next();
1064: e.accept(this , arg);
1065: if (i.hasNext()) {
1066: printer.print(", ");
1067: }
1068: }
1069: printer.print(")");
1070: }
1071:
1072: if (n.classBody != null) {
1073: printer.printLn(" {");
1074: printer.indent();
1075: printMembers(n.classBody, arg);
1076: printer.unindent();
1077: printer.printLn("}");
1078: }
1079: }
1080:
1081: public void visit(EmptyMemberDeclaration n, Object arg) {
1082: printer.print(";");
1083: }
1084:
1085: public void visit(InitializerDeclaration n, Object arg) {
1086: printer.print("static ");
1087: n.block.accept(this , arg);
1088: }
1089:
1090: public void visit(IfStmt n, Object arg) {
1091: printer.print("if (");
1092: n.condition.accept(this , arg);
1093: printer.print(") ");
1094: n.thenStmt.accept(this , arg);
1095: if (n.elseStmt != null) {
1096: printer.print(" else ");
1097: n.elseStmt.accept(this , arg);
1098: }
1099: }
1100:
1101: public void visit(WhileStmt n, Object arg) {
1102: printer.print("while (");
1103: n.condition.accept(this , arg);
1104: printer.print(") ");
1105: n.body.accept(this , arg);
1106: }
1107:
1108: public void visit(ContinueStmt n, Object arg) {
1109: printer.print("continue");
1110: if (n.id != null) {
1111: printer.print(" ");
1112: printer.print(n.id);
1113: }
1114: printer.print(";");
1115: }
1116:
1117: public void visit(DoStmt n, Object arg) {
1118: printer.print("do ");
1119: n.body.accept(this , arg);
1120: printer.print(" while (");
1121: n.condition.accept(this , arg);
1122: printer.print(");");
1123: }
1124:
1125: public void visit(ForeachStmt n, Object arg) {
1126: printer.print("for (");
1127: n.var.accept(this , arg);
1128: printer.print(" : ");
1129: n.iterable.accept(this , arg);
1130: printer.print(") ");
1131: n.body.accept(this , arg);
1132: }
1133:
1134: public void visit(ForStmt n, Object arg) {
1135: printer.print("for (");
1136: if (n.init != null) {
1137: for (Iterator<Expression> i = n.init.iterator(); i
1138: .hasNext();) {
1139: Expression e = i.next();
1140: e.accept(this , arg);
1141: if (i.hasNext()) {
1142: printer.print(", ");
1143: }
1144: }
1145: }
1146: printer.print("; ");
1147: if (n.compare != null) {
1148: n.compare.accept(this , arg);
1149: }
1150: printer.print("; ");
1151: if (n.update != null) {
1152: for (Iterator<Expression> i = n.update.iterator(); i
1153: .hasNext();) {
1154: Expression e = i.next();
1155: e.accept(this , arg);
1156: if (i.hasNext()) {
1157: printer.print(", ");
1158: }
1159: }
1160: }
1161: printer.print(") ");
1162: n.body.accept(this , arg);
1163: }
1164:
1165: public void visit(ThrowStmt n, Object arg) {
1166: printer.print("throw ");
1167: n.expr.accept(this , arg);
1168: printer.print(";");
1169: }
1170:
1171: public void visit(SynchronizedStmt n, Object arg) {
1172: printer.print("synchronized (");
1173: n.expr.accept(this , arg);
1174: printer.print(") ");
1175: n.block.accept(this , arg);
1176: }
1177:
1178: public void visit(TryStmt n, Object arg) {
1179: printer.print("try ");
1180: n.tryBlock.accept(this , arg);
1181: if (n.catchs != null) {
1182: for (CatchClause c : n.catchs) {
1183: c.accept(this , arg);
1184: }
1185: }
1186: if (n.finallyBlock != null) {
1187: printer.print(" finally ");
1188: n.finallyBlock.accept(this , arg);
1189: }
1190: }
1191:
1192: public void visit(CatchClause n, Object arg) {
1193: printer.print(" catch (");
1194: n.except.accept(this , arg);
1195: printer.print(") ");
1196: n.catchBlock.accept(this , arg);
1197:
1198: }
1199:
1200: public void visit(AnnotationDeclaration n, Object arg) {
1201: printMemberAnnotations(n.annotations, arg);
1202: printModifiers(n.modifiers);
1203:
1204: printer.print("@interface ");
1205: printer.print(n.name);
1206: printer.printLn(" {");
1207: printer.indent();
1208: if (n.members != null) {
1209: printMembers(n.members, arg);
1210: }
1211: printer.unindent();
1212: printer.print("}");
1213: }
1214:
1215: public void visit(AnnotationMemberDeclaration n, Object arg) {
1216: printMemberAnnotations(n.annotations, arg);
1217: printModifiers(n.modifiers);
1218:
1219: n.type.accept(this , arg);
1220: printer.print(" ");
1221: printer.print(n.name);
1222: printer.print("()");
1223: if (n.defaultValue != null) {
1224: printer.print(" default ");
1225: n.defaultValue.accept(this , arg);
1226: }
1227: printer.print(";");
1228: }
1229:
1230: public void visit(MarkerAnnotationExpr n, Object arg) {
1231: printer.print("@");
1232: n.name.accept(this , arg);
1233: }
1234:
1235: public void visit(SingleMemberAnnotationExpr n, Object arg) {
1236: printer.print("@");
1237: n.name.accept(this , arg);
1238: printer.print("(");
1239: n.memberValue.accept(this , arg);
1240: printer.print(")");
1241: }
1242:
1243: public void visit(NormalAnnotationExpr n, Object arg) {
1244: printer.print("@");
1245: n.name.accept(this , arg);
1246: printer.print("(");
1247: for (Iterator<MemberValuePair> i = n.pairs.iterator(); i
1248: .hasNext();) {
1249: MemberValuePair m = i.next();
1250: m.accept(this , arg);
1251: if (i.hasNext()) {
1252: printer.print(", ");
1253: }
1254: }
1255: printer.print(")");
1256: }
1257:
1258: public void visit(MemberValuePair n, Object arg) {
1259: printer.print(n.name);
1260: printer.print(" = ");
1261: n.value.accept(this, arg);
1262: }
1263: }
|