0001: /*******************************************************************************
0002: * Copyright (c) 2000, 2007 IBM Corporation and others.
0003: * All rights reserved. This program and the accompanying materials
0004: * are made available under the terms of the Eclipse Public License v1.0
0005: * which accompanies this distribution, and is available at
0006: * http://www.eclipse.org/legal/epl-v10.html
0007: *
0008: * Contributors:
0009: * IBM Corporation - initial API and implementation
0010: *******************************************************************************/package org.eclipse.jdt.internal.corext.dom;
0011:
0012: import java.util.Iterator;
0013: import java.util.List;
0014:
0015: import org.eclipse.core.runtime.Assert;
0016:
0017: import org.eclipse.jdt.core.dom.AST;
0018: import org.eclipse.jdt.core.dom.ASTNode;
0019: import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
0020: import org.eclipse.jdt.core.dom.Annotation;
0021: import org.eclipse.jdt.core.dom.AnnotationTypeDeclaration;
0022: import org.eclipse.jdt.core.dom.AnnotationTypeMemberDeclaration;
0023: import org.eclipse.jdt.core.dom.AnonymousClassDeclaration;
0024: import org.eclipse.jdt.core.dom.ArrayAccess;
0025: import org.eclipse.jdt.core.dom.ArrayCreation;
0026: import org.eclipse.jdt.core.dom.ArrayInitializer;
0027: import org.eclipse.jdt.core.dom.ArrayType;
0028: import org.eclipse.jdt.core.dom.AssertStatement;
0029: import org.eclipse.jdt.core.dom.Assignment;
0030: import org.eclipse.jdt.core.dom.Block;
0031: import org.eclipse.jdt.core.dom.BlockComment;
0032: import org.eclipse.jdt.core.dom.BodyDeclaration;
0033: import org.eclipse.jdt.core.dom.BooleanLiteral;
0034: import org.eclipse.jdt.core.dom.BreakStatement;
0035: import org.eclipse.jdt.core.dom.CastExpression;
0036: import org.eclipse.jdt.core.dom.CatchClause;
0037: import org.eclipse.jdt.core.dom.CharacterLiteral;
0038: import org.eclipse.jdt.core.dom.ClassInstanceCreation;
0039: import org.eclipse.jdt.core.dom.CompilationUnit;
0040: import org.eclipse.jdt.core.dom.ConditionalExpression;
0041: import org.eclipse.jdt.core.dom.ConstructorInvocation;
0042: import org.eclipse.jdt.core.dom.ContinueStatement;
0043: import org.eclipse.jdt.core.dom.DoStatement;
0044: import org.eclipse.jdt.core.dom.EmptyStatement;
0045: import org.eclipse.jdt.core.dom.EnhancedForStatement;
0046: import org.eclipse.jdt.core.dom.EnumConstantDeclaration;
0047: import org.eclipse.jdt.core.dom.EnumDeclaration;
0048: import org.eclipse.jdt.core.dom.Expression;
0049: import org.eclipse.jdt.core.dom.ExpressionStatement;
0050: import org.eclipse.jdt.core.dom.FieldAccess;
0051: import org.eclipse.jdt.core.dom.FieldDeclaration;
0052: import org.eclipse.jdt.core.dom.ForStatement;
0053: import org.eclipse.jdt.core.dom.IfStatement;
0054: import org.eclipse.jdt.core.dom.ImportDeclaration;
0055: import org.eclipse.jdt.core.dom.InfixExpression;
0056: import org.eclipse.jdt.core.dom.Initializer;
0057: import org.eclipse.jdt.core.dom.InstanceofExpression;
0058: import org.eclipse.jdt.core.dom.Javadoc;
0059: import org.eclipse.jdt.core.dom.LabeledStatement;
0060: import org.eclipse.jdt.core.dom.LineComment;
0061: import org.eclipse.jdt.core.dom.MarkerAnnotation;
0062: import org.eclipse.jdt.core.dom.MemberRef;
0063: import org.eclipse.jdt.core.dom.MemberValuePair;
0064: import org.eclipse.jdt.core.dom.MethodDeclaration;
0065: import org.eclipse.jdt.core.dom.MethodInvocation;
0066: import org.eclipse.jdt.core.dom.MethodRef;
0067: import org.eclipse.jdt.core.dom.MethodRefParameter;
0068: import org.eclipse.jdt.core.dom.Modifier;
0069: import org.eclipse.jdt.core.dom.Name;
0070: import org.eclipse.jdt.core.dom.NormalAnnotation;
0071: import org.eclipse.jdt.core.dom.NullLiteral;
0072: import org.eclipse.jdt.core.dom.NumberLiteral;
0073: import org.eclipse.jdt.core.dom.PackageDeclaration;
0074: import org.eclipse.jdt.core.dom.ParameterizedType;
0075: import org.eclipse.jdt.core.dom.ParenthesizedExpression;
0076: import org.eclipse.jdt.core.dom.PostfixExpression;
0077: import org.eclipse.jdt.core.dom.PrefixExpression;
0078: import org.eclipse.jdt.core.dom.PrimitiveType;
0079: import org.eclipse.jdt.core.dom.QualifiedName;
0080: import org.eclipse.jdt.core.dom.QualifiedType;
0081: import org.eclipse.jdt.core.dom.ReturnStatement;
0082: import org.eclipse.jdt.core.dom.SimpleName;
0083: import org.eclipse.jdt.core.dom.SimpleType;
0084: import org.eclipse.jdt.core.dom.SingleMemberAnnotation;
0085: import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
0086: import org.eclipse.jdt.core.dom.Statement;
0087: import org.eclipse.jdt.core.dom.StringLiteral;
0088: import org.eclipse.jdt.core.dom.SuperConstructorInvocation;
0089: import org.eclipse.jdt.core.dom.SuperFieldAccess;
0090: import org.eclipse.jdt.core.dom.SuperMethodInvocation;
0091: import org.eclipse.jdt.core.dom.SwitchCase;
0092: import org.eclipse.jdt.core.dom.SwitchStatement;
0093: import org.eclipse.jdt.core.dom.SynchronizedStatement;
0094: import org.eclipse.jdt.core.dom.TagElement;
0095: import org.eclipse.jdt.core.dom.TextElement;
0096: import org.eclipse.jdt.core.dom.ThisExpression;
0097: import org.eclipse.jdt.core.dom.ThrowStatement;
0098: import org.eclipse.jdt.core.dom.TryStatement;
0099: import org.eclipse.jdt.core.dom.Type;
0100: import org.eclipse.jdt.core.dom.TypeDeclaration;
0101: import org.eclipse.jdt.core.dom.TypeDeclarationStatement;
0102: import org.eclipse.jdt.core.dom.TypeLiteral;
0103: import org.eclipse.jdt.core.dom.TypeParameter;
0104: import org.eclipse.jdt.core.dom.VariableDeclarationExpression;
0105: import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
0106: import org.eclipse.jdt.core.dom.VariableDeclarationStatement;
0107: import org.eclipse.jdt.core.dom.WhileStatement;
0108: import org.eclipse.jdt.core.dom.WildcardType;
0109:
0110: public class ASTFlattener extends GenericVisitor {
0111:
0112: /**
0113: * The string buffer into which the serialized representation of the AST is
0114: * written.
0115: */
0116: protected StringBuffer fBuffer;
0117:
0118: /**
0119: * Creates a new AST printer.
0120: */
0121: public ASTFlattener() {
0122: this .fBuffer = new StringBuffer();
0123: }
0124:
0125: /**
0126: * Returns the string accumulated in the visit.
0127: *
0128: * @return the serialized
0129: */
0130: public String getResult() {
0131: return this .fBuffer.toString();
0132: }
0133:
0134: /**
0135: * Resets this printer so that it can be used again.
0136: */
0137: public void reset() {
0138: this .fBuffer.setLength(0);
0139: }
0140:
0141: public static String asString(ASTNode node) {
0142: Assert.isTrue(node.getAST().apiLevel() == AST.JLS3);
0143:
0144: ASTFlattener flattener = new ASTFlattener();
0145: node.accept(flattener);
0146: return flattener.getResult();
0147: }
0148:
0149: protected boolean visitNode(ASTNode node) {
0150: Assert
0151: .isTrue(
0152: false,
0153: "No implementation to flatten node: " + node.toString()); //$NON-NLS-1$
0154: return false;
0155: }
0156:
0157: /**
0158: * Appends the text representation of the given modifier flags, followed by a single space.
0159: * Used for 3.0 modifiers and annotations.
0160: *
0161: * @param ext the list of modifier and annotation nodes
0162: * (element type: <code>IExtendedModifiers</code>)
0163: */
0164: private void printModifiers(List ext) {
0165: for (Iterator it = ext.iterator(); it.hasNext();) {
0166: ASTNode p = (ASTNode) it.next();
0167: p.accept(this );
0168: this .fBuffer.append(" ");//$NON-NLS-1$
0169: }
0170: }
0171:
0172: /*
0173: * @see ASTVisitor#visit(AnnotationTypeDeclaration)
0174: * @since 3.0
0175: */
0176: public boolean visit(AnnotationTypeDeclaration node) {
0177: if (node.getJavadoc() != null) {
0178: node.getJavadoc().accept(this );
0179: }
0180: printModifiers(node.modifiers());
0181: this .fBuffer.append("@interface ");//$NON-NLS-1$
0182: node.getName().accept(this );
0183: this .fBuffer.append(" {");//$NON-NLS-1$
0184: for (Iterator it = node.bodyDeclarations().iterator(); it
0185: .hasNext();) {
0186: BodyDeclaration d = (BodyDeclaration) it.next();
0187: d.accept(this );
0188: }
0189: this .fBuffer.append("}");//$NON-NLS-1$
0190: return false;
0191: }
0192:
0193: /*
0194: * @see ASTVisitor#visit(AnnotationTypeMemberDeclaration)
0195: * @since 3.0
0196: */
0197: public boolean visit(AnnotationTypeMemberDeclaration node) {
0198: if (node.getJavadoc() != null) {
0199: node.getJavadoc().accept(this );
0200: }
0201: printModifiers(node.modifiers());
0202: node.getType().accept(this );
0203: this .fBuffer.append(" ");//$NON-NLS-1$
0204: node.getName().accept(this );
0205: this .fBuffer.append("()");//$NON-NLS-1$
0206: if (node.getDefault() != null) {
0207: this .fBuffer.append(" default ");//$NON-NLS-1$
0208: node.getDefault().accept(this );
0209: }
0210: this .fBuffer.append(";");//$NON-NLS-1$
0211: return false;
0212: }
0213:
0214: /*
0215: * @see ASTVisitor#visit(AnonymousClassDeclaration)
0216: */
0217: public boolean visit(AnonymousClassDeclaration node) {
0218: this .fBuffer.append("{");//$NON-NLS-1$
0219: for (Iterator it = node.bodyDeclarations().iterator(); it
0220: .hasNext();) {
0221: BodyDeclaration b = (BodyDeclaration) it.next();
0222: b.accept(this );
0223: }
0224: this .fBuffer.append("}");//$NON-NLS-1$
0225: return false;
0226: }
0227:
0228: /*
0229: * @see ASTVisitor#visit(ArrayAccess)
0230: */
0231: public boolean visit(ArrayAccess node) {
0232: node.getArray().accept(this );
0233: this .fBuffer.append("[");//$NON-NLS-1$
0234: node.getIndex().accept(this );
0235: this .fBuffer.append("]");//$NON-NLS-1$
0236: return false;
0237: }
0238:
0239: /*
0240: * @see ASTVisitor#visit(ArrayCreation)
0241: */
0242: public boolean visit(ArrayCreation node) {
0243: this .fBuffer.append("new ");//$NON-NLS-1$
0244: ArrayType at = node.getType();
0245: int dims = at.getDimensions();
0246: Type elementType = at.getElementType();
0247: elementType.accept(this );
0248: for (Iterator it = node.dimensions().iterator(); it.hasNext();) {
0249: this .fBuffer.append("[");//$NON-NLS-1$
0250: Expression e = (Expression) it.next();
0251: e.accept(this );
0252: this .fBuffer.append("]");//$NON-NLS-1$
0253: dims--;
0254: }
0255: // add empty "[]" for each extra array dimension
0256: for (int i = 0; i < dims; i++) {
0257: this .fBuffer.append("[]");//$NON-NLS-1$
0258: }
0259: if (node.getInitializer() != null) {
0260: node.getInitializer().accept(this );
0261: }
0262: return false;
0263: }
0264:
0265: /*
0266: * @see ASTVisitor#visit(ArrayInitializer)
0267: */
0268: public boolean visit(ArrayInitializer node) {
0269: this .fBuffer.append("{");//$NON-NLS-1$
0270: for (Iterator it = node.expressions().iterator(); it.hasNext();) {
0271: Expression e = (Expression) it.next();
0272: e.accept(this );
0273: if (it.hasNext()) {
0274: this .fBuffer.append(",");//$NON-NLS-1$
0275: }
0276: }
0277: this .fBuffer.append("}");//$NON-NLS-1$
0278: return false;
0279: }
0280:
0281: /*
0282: * @see ASTVisitor#visit(ArrayType)
0283: */
0284: public boolean visit(ArrayType node) {
0285: node.getComponentType().accept(this );
0286: this .fBuffer.append("[]");//$NON-NLS-1$
0287: return false;
0288: }
0289:
0290: /*
0291: * @see ASTVisitor#visit(AssertStatement)
0292: */
0293: public boolean visit(AssertStatement node) {
0294: this .fBuffer.append("assert ");//$NON-NLS-1$
0295: node.getExpression().accept(this );
0296: if (node.getMessage() != null) {
0297: this .fBuffer.append(" : ");//$NON-NLS-1$
0298: node.getMessage().accept(this );
0299: }
0300: this .fBuffer.append(";");//$NON-NLS-1$
0301: return false;
0302: }
0303:
0304: /*
0305: * @see ASTVisitor#visit(Assignment)
0306: */
0307: public boolean visit(Assignment node) {
0308: node.getLeftHandSide().accept(this );
0309: this .fBuffer.append(node.getOperator().toString());
0310: node.getRightHandSide().accept(this );
0311: return false;
0312: }
0313:
0314: /*
0315: * @see ASTVisitor#visit(Block)
0316: */
0317: public boolean visit(Block node) {
0318: this .fBuffer.append("{");//$NON-NLS-1$
0319: for (Iterator it = node.statements().iterator(); it.hasNext();) {
0320: Statement s = (Statement) it.next();
0321: s.accept(this );
0322: }
0323: this .fBuffer.append("}");//$NON-NLS-1$
0324: return false;
0325: }
0326:
0327: /*
0328: * @see ASTVisitor#visit(BlockComment)
0329: * @since 3.0
0330: */
0331: public boolean visit(BlockComment node) {
0332: this .fBuffer.append("/* */");//$NON-NLS-1$
0333: return false;
0334: }
0335:
0336: /*
0337: * @see ASTVisitor#visit(BooleanLiteral)
0338: */
0339: public boolean visit(BooleanLiteral node) {
0340: if (node.booleanValue() == true) {
0341: this .fBuffer.append("true");//$NON-NLS-1$
0342: } else {
0343: this .fBuffer.append("false");//$NON-NLS-1$
0344: }
0345: return false;
0346: }
0347:
0348: /*
0349: * @see ASTVisitor#visit(BreakStatement)
0350: */
0351: public boolean visit(BreakStatement node) {
0352: this .fBuffer.append("break");//$NON-NLS-1$
0353: if (node.getLabel() != null) {
0354: this .fBuffer.append(" ");//$NON-NLS-1$
0355: node.getLabel().accept(this );
0356: }
0357: this .fBuffer.append(";");//$NON-NLS-1$
0358: return false;
0359: }
0360:
0361: /*
0362: * @see ASTVisitor#visit(CastExpression)
0363: */
0364: public boolean visit(CastExpression node) {
0365: this .fBuffer.append("(");//$NON-NLS-1$
0366: node.getType().accept(this );
0367: this .fBuffer.append(")");//$NON-NLS-1$
0368: node.getExpression().accept(this );
0369: return false;
0370: }
0371:
0372: /*
0373: * @see ASTVisitor#visit(CatchClause)
0374: */
0375: public boolean visit(CatchClause node) {
0376: this .fBuffer.append("catch (");//$NON-NLS-1$
0377: node.getException().accept(this );
0378: this .fBuffer.append(") ");//$NON-NLS-1$
0379: node.getBody().accept(this );
0380: return false;
0381: }
0382:
0383: /*
0384: * @see ASTVisitor#visit(CharacterLiteral)
0385: */
0386: public boolean visit(CharacterLiteral node) {
0387: this .fBuffer.append(node.getEscapedValue());
0388: return false;
0389: }
0390:
0391: /*
0392: * @see ASTVisitor#visit(ClassInstanceCreation)
0393: */
0394: public boolean visit(ClassInstanceCreation node) {
0395: if (node.getExpression() != null) {
0396: node.getExpression().accept(this );
0397: this .fBuffer.append(".");//$NON-NLS-1$
0398: }
0399: this .fBuffer.append("new ");//$NON-NLS-1$
0400: if (node.getAST().apiLevel() >= AST.JLS3) {
0401: if (!node.typeArguments().isEmpty()) {
0402: this .fBuffer.append("<");//$NON-NLS-1$
0403: for (Iterator it = node.typeArguments().iterator(); it
0404: .hasNext();) {
0405: Type t = (Type) it.next();
0406: t.accept(this );
0407: if (it.hasNext()) {
0408: this .fBuffer.append(",");//$NON-NLS-1$
0409: }
0410: }
0411: this .fBuffer.append(">");//$NON-NLS-1$
0412: }
0413: node.getType().accept(this );
0414: }
0415: this .fBuffer.append("(");//$NON-NLS-1$
0416: for (Iterator it = node.arguments().iterator(); it.hasNext();) {
0417: Expression e = (Expression) it.next();
0418: e.accept(this );
0419: if (it.hasNext()) {
0420: this .fBuffer.append(",");//$NON-NLS-1$
0421: }
0422: }
0423: this .fBuffer.append(")");//$NON-NLS-1$
0424: if (node.getAnonymousClassDeclaration() != null) {
0425: node.getAnonymousClassDeclaration().accept(this );
0426: }
0427: return false;
0428: }
0429:
0430: /*
0431: * @see ASTVisitor#visit(CompilationUnit)
0432: */
0433: public boolean visit(CompilationUnit node) {
0434: if (node.getPackage() != null) {
0435: node.getPackage().accept(this );
0436: }
0437: for (Iterator it = node.imports().iterator(); it.hasNext();) {
0438: ImportDeclaration d = (ImportDeclaration) it.next();
0439: d.accept(this );
0440: }
0441: for (Iterator it = node.types().iterator(); it.hasNext();) {
0442: AbstractTypeDeclaration d = (AbstractTypeDeclaration) it
0443: .next();
0444: d.accept(this );
0445: }
0446: return false;
0447: }
0448:
0449: /*
0450: * @see ASTVisitor#visit(ConditionalExpression)
0451: */
0452: public boolean visit(ConditionalExpression node) {
0453: node.getExpression().accept(this );
0454: this .fBuffer.append("?");//$NON-NLS-1$
0455: node.getThenExpression().accept(this );
0456: this .fBuffer.append(":");//$NON-NLS-1$
0457: node.getElseExpression().accept(this );
0458: return false;
0459: }
0460:
0461: /*
0462: * @see ASTVisitor#visit(ConstructorInvocation)
0463: */
0464: public boolean visit(ConstructorInvocation node) {
0465: if (node.getAST().apiLevel() >= AST.JLS3) {
0466: if (!node.typeArguments().isEmpty()) {
0467: this .fBuffer.append("<");//$NON-NLS-1$
0468: for (Iterator it = node.typeArguments().iterator(); it
0469: .hasNext();) {
0470: Type t = (Type) it.next();
0471: t.accept(this );
0472: if (it.hasNext()) {
0473: this .fBuffer.append(",");//$NON-NLS-1$
0474: }
0475: }
0476: this .fBuffer.append(">");//$NON-NLS-1$
0477: }
0478: }
0479: this .fBuffer.append("this(");//$NON-NLS-1$
0480: for (Iterator it = node.arguments().iterator(); it.hasNext();) {
0481: Expression e = (Expression) it.next();
0482: e.accept(this );
0483: if (it.hasNext()) {
0484: this .fBuffer.append(",");//$NON-NLS-1$
0485: }
0486: }
0487: this .fBuffer.append(");");//$NON-NLS-1$
0488: return false;
0489: }
0490:
0491: /*
0492: * @see ASTVisitor#visit(ContinueStatement)
0493: */
0494: public boolean visit(ContinueStatement node) {
0495: this .fBuffer.append("continue");//$NON-NLS-1$
0496: if (node.getLabel() != null) {
0497: this .fBuffer.append(" ");//$NON-NLS-1$
0498: node.getLabel().accept(this );
0499: }
0500: this .fBuffer.append(";");//$NON-NLS-1$
0501: return false;
0502: }
0503:
0504: /*
0505: * @see ASTVisitor#visit(DoStatement)
0506: */
0507: public boolean visit(DoStatement node) {
0508: this .fBuffer.append("do ");//$NON-NLS-1$
0509: node.getBody().accept(this );
0510: this .fBuffer.append(" while (");//$NON-NLS-1$
0511: node.getExpression().accept(this );
0512: this .fBuffer.append(");");//$NON-NLS-1$
0513: return false;
0514: }
0515:
0516: /*
0517: * @see ASTVisitor#visit(EmptyStatement)
0518: */
0519: public boolean visit(EmptyStatement node) {
0520: this .fBuffer.append(";");//$NON-NLS-1$
0521: return false;
0522: }
0523:
0524: /*
0525: * @see ASTVisitor#visit(EnhancedForStatement)
0526: * @since 3.0
0527: */
0528: public boolean visit(EnhancedForStatement node) {
0529: this .fBuffer.append("for (");//$NON-NLS-1$
0530: node.getParameter().accept(this );
0531: this .fBuffer.append(" : ");//$NON-NLS-1$
0532: node.getExpression().accept(this );
0533: this .fBuffer.append(") ");//$NON-NLS-1$
0534: node.getBody().accept(this );
0535: return false;
0536: }
0537:
0538: /*
0539: * @see ASTVisitor#visit(EnumConstantDeclaration)
0540: * @since 3.0
0541: */
0542: public boolean visit(EnumConstantDeclaration node) {
0543: if (node.getJavadoc() != null) {
0544: node.getJavadoc().accept(this );
0545: }
0546: printModifiers(node.modifiers());
0547: node.getName().accept(this );
0548: if (!node.arguments().isEmpty()) {
0549: this .fBuffer.append("(");//$NON-NLS-1$
0550: for (Iterator it = node.arguments().iterator(); it
0551: .hasNext();) {
0552: Expression e = (Expression) it.next();
0553: e.accept(this );
0554: if (it.hasNext()) {
0555: this .fBuffer.append(",");//$NON-NLS-1$
0556: }
0557: }
0558: this .fBuffer.append(")");//$NON-NLS-1$
0559: }
0560: if (node.getAnonymousClassDeclaration() != null) {
0561: node.getAnonymousClassDeclaration().accept(this );
0562: }
0563: return false;
0564: }
0565:
0566: /*
0567: * @see ASTVisitor#visit(EnumDeclaration)
0568: * @since 3.0
0569: */
0570: public boolean visit(EnumDeclaration node) {
0571: if (node.getJavadoc() != null) {
0572: node.getJavadoc().accept(this );
0573: }
0574: printModifiers(node.modifiers());
0575: this .fBuffer.append("enum ");//$NON-NLS-1$
0576: node.getName().accept(this );
0577: this .fBuffer.append(" ");//$NON-NLS-1$
0578: if (!node.super InterfaceTypes().isEmpty()) {
0579: this .fBuffer.append("implements ");//$NON-NLS-1$
0580: for (Iterator it = node.super InterfaceTypes().iterator(); it
0581: .hasNext();) {
0582: Type t = (Type) it.next();
0583: t.accept(this );
0584: if (it.hasNext()) {
0585: this .fBuffer.append(", ");//$NON-NLS-1$
0586: }
0587: }
0588: this .fBuffer.append(" ");//$NON-NLS-1$
0589: }
0590: this .fBuffer.append("{");//$NON-NLS-1$
0591: for (Iterator it = node.enumConstants().iterator(); it
0592: .hasNext();) {
0593: EnumConstantDeclaration d = (EnumConstantDeclaration) it
0594: .next();
0595: d.accept(this );
0596: // enum constant declarations do not include punctuation
0597: if (it.hasNext()) {
0598: // enum constant declarations are separated by commas
0599: this .fBuffer.append(", ");//$NON-NLS-1$
0600: }
0601: }
0602: if (!node.bodyDeclarations().isEmpty()) {
0603: this .fBuffer.append("; ");//$NON-NLS-1$
0604: for (Iterator it = node.bodyDeclarations().iterator(); it
0605: .hasNext();) {
0606: BodyDeclaration d = (BodyDeclaration) it.next();
0607: d.accept(this );
0608: // other body declarations include trailing punctuation
0609: }
0610: }
0611: this .fBuffer.append("}");//$NON-NLS-1$
0612: return false;
0613: }
0614:
0615: /*
0616: * @see ASTVisitor#visit(ExpressionStatement)
0617: */
0618: public boolean visit(ExpressionStatement node) {
0619: node.getExpression().accept(this );
0620: this .fBuffer.append(";");//$NON-NLS-1$
0621: return false;
0622: }
0623:
0624: /*
0625: * @see ASTVisitor#visit(FieldAccess)
0626: */
0627: public boolean visit(FieldAccess node) {
0628: node.getExpression().accept(this );
0629: this .fBuffer.append(".");//$NON-NLS-1$
0630: node.getName().accept(this );
0631: return false;
0632: }
0633:
0634: /*
0635: * @see ASTVisitor#visit(FieldDeclaration)
0636: */
0637: public boolean visit(FieldDeclaration node) {
0638: if (node.getJavadoc() != null) {
0639: node.getJavadoc().accept(this );
0640: }
0641: if (node.getAST().apiLevel() >= AST.JLS3) {
0642: printModifiers(node.modifiers());
0643: }
0644: node.getType().accept(this );
0645: this .fBuffer.append(" ");//$NON-NLS-1$
0646: for (Iterator it = node.fragments().iterator(); it.hasNext();) {
0647: VariableDeclarationFragment f = (VariableDeclarationFragment) it
0648: .next();
0649: f.accept(this );
0650: if (it.hasNext()) {
0651: this .fBuffer.append(", ");//$NON-NLS-1$
0652: }
0653: }
0654: this .fBuffer.append(";");//$NON-NLS-1$
0655: return false;
0656: }
0657:
0658: /*
0659: * @see ASTVisitor#visit(ForStatement)
0660: */
0661: public boolean visit(ForStatement node) {
0662: this .fBuffer.append("for (");//$NON-NLS-1$
0663: for (Iterator it = node.initializers().iterator(); it.hasNext();) {
0664: Expression e = (Expression) it.next();
0665: e.accept(this );
0666: }
0667: this .fBuffer.append("; ");//$NON-NLS-1$
0668: if (node.getExpression() != null) {
0669: node.getExpression().accept(this );
0670: }
0671: this .fBuffer.append("; ");//$NON-NLS-1$
0672: for (Iterator it = node.updaters().iterator(); it.hasNext();) {
0673: Expression e = (Expression) it.next();
0674: e.accept(this );
0675: }
0676: this .fBuffer.append(") ");//$NON-NLS-1$
0677: node.getBody().accept(this );
0678: return false;
0679: }
0680:
0681: /*
0682: * @see ASTVisitor#visit(IfStatement)
0683: */
0684: public boolean visit(IfStatement node) {
0685: this .fBuffer.append("if (");//$NON-NLS-1$
0686: node.getExpression().accept(this );
0687: this .fBuffer.append(") ");//$NON-NLS-1$
0688: node.getThenStatement().accept(this );
0689: if (node.getElseStatement() != null) {
0690: this .fBuffer.append(" else ");//$NON-NLS-1$
0691: node.getElseStatement().accept(this );
0692: }
0693: return false;
0694: }
0695:
0696: /*
0697: * @see ASTVisitor#visit(ImportDeclaration)
0698: */
0699: public boolean visit(ImportDeclaration node) {
0700: this .fBuffer.append("import ");//$NON-NLS-1$
0701: if (node.getAST().apiLevel() >= AST.JLS3) {
0702: if (node.isStatic()) {
0703: this .fBuffer.append("static ");//$NON-NLS-1$
0704: }
0705: }
0706: node.getName().accept(this );
0707: if (node.isOnDemand()) {
0708: this .fBuffer.append(".*");//$NON-NLS-1$
0709: }
0710: this .fBuffer.append(";");//$NON-NLS-1$
0711: return false;
0712: }
0713:
0714: /*
0715: * @see ASTVisitor#visit(InfixExpression)
0716: */
0717: public boolean visit(InfixExpression node) {
0718: node.getLeftOperand().accept(this );
0719: this .fBuffer.append(' '); // for cases like x= i - -1; or x= i++ + ++i;
0720: this .fBuffer.append(node.getOperator().toString());
0721: this .fBuffer.append(' ');
0722: node.getRightOperand().accept(this );
0723: final List extendedOperands = node.extendedOperands();
0724: if (extendedOperands.size() != 0) {
0725: this .fBuffer.append(' ');
0726: for (Iterator it = extendedOperands.iterator(); it
0727: .hasNext();) {
0728: this .fBuffer.append(node.getOperator().toString())
0729: .append(' ');
0730: Expression e = (Expression) it.next();
0731: e.accept(this );
0732: }
0733: }
0734: return false;
0735: }
0736:
0737: /*
0738: * @see ASTVisitor#visit(InstanceofExpression)
0739: */
0740: public boolean visit(InstanceofExpression node) {
0741: node.getLeftOperand().accept(this );
0742: this .fBuffer.append(" instanceof ");//$NON-NLS-1$
0743: node.getRightOperand().accept(this );
0744: return false;
0745: }
0746:
0747: /*
0748: * @see ASTVisitor#visit(Initializer)
0749: */
0750: public boolean visit(Initializer node) {
0751: if (node.getJavadoc() != null) {
0752: node.getJavadoc().accept(this );
0753: }
0754: if (node.getAST().apiLevel() >= AST.JLS3) {
0755: printModifiers(node.modifiers());
0756: }
0757: node.getBody().accept(this );
0758: return false;
0759: }
0760:
0761: /*
0762: * @see ASTVisitor#visit(Javadoc)
0763: */
0764: public boolean visit(Javadoc node) {
0765: this .fBuffer.append("/** ");//$NON-NLS-1$
0766: for (Iterator it = node.tags().iterator(); it.hasNext();) {
0767: ASTNode e = (ASTNode) it.next();
0768: e.accept(this );
0769: }
0770: this .fBuffer.append("\n */");//$NON-NLS-1$
0771: return false;
0772: }
0773:
0774: /*
0775: * @see ASTVisitor#visit(LabeledStatement)
0776: */
0777: public boolean visit(LabeledStatement node) {
0778: node.getLabel().accept(this );
0779: this .fBuffer.append(": ");//$NON-NLS-1$
0780: node.getBody().accept(this );
0781: return false;
0782: }
0783:
0784: /*
0785: * @see ASTVisitor#visit(LineComment)
0786: * @since 3.0
0787: */
0788: public boolean visit(LineComment node) {
0789: this .fBuffer.append("//\n");//$NON-NLS-1$
0790: return false;
0791: }
0792:
0793: /*
0794: * @see ASTVisitor#visit(MarkerAnnotation)
0795: * @since 3.0
0796: */
0797: public boolean visit(MarkerAnnotation node) {
0798: this .fBuffer.append("@");//$NON-NLS-1$
0799: node.getTypeName().accept(this );
0800: return false;
0801: }
0802:
0803: /*
0804: * @see ASTVisitor#visit(MemberRef)
0805: * @since 3.0
0806: */
0807: public boolean visit(MemberRef node) {
0808: if (node.getQualifier() != null) {
0809: node.getQualifier().accept(this );
0810: }
0811: this .fBuffer.append("#");//$NON-NLS-1$
0812: node.getName().accept(this );
0813: return false;
0814: }
0815:
0816: /*
0817: * @see ASTVisitor#visit(MemberValuePair)
0818: * @since 3.0
0819: */
0820: public boolean visit(MemberValuePair node) {
0821: node.getName().accept(this );
0822: this .fBuffer.append("=");//$NON-NLS-1$
0823: node.getValue().accept(this );
0824: return false;
0825: }
0826:
0827: /*
0828: * @see ASTVisitor#visit(MethodRef)
0829: * @since 3.0
0830: */
0831: public boolean visit(MethodRef node) {
0832: if (node.getQualifier() != null) {
0833: node.getQualifier().accept(this );
0834: }
0835: this .fBuffer.append("#");//$NON-NLS-1$
0836: node.getName().accept(this );
0837: this .fBuffer.append("(");//$NON-NLS-1$
0838: for (Iterator it = node.parameters().iterator(); it.hasNext();) {
0839: MethodRefParameter e = (MethodRefParameter) it.next();
0840: e.accept(this );
0841: if (it.hasNext()) {
0842: this .fBuffer.append(",");//$NON-NLS-1$
0843: }
0844: }
0845: this .fBuffer.append(")");//$NON-NLS-1$
0846: return false;
0847: }
0848:
0849: /*
0850: * @see ASTVisitor#visit(MethodRefParameter)
0851: * @since 3.0
0852: */
0853: public boolean visit(MethodRefParameter node) {
0854: node.getType().accept(this );
0855: if (node.getAST().apiLevel() >= AST.JLS3) {
0856: if (node.isVarargs()) {
0857: this .fBuffer.append("...");//$NON-NLS-1$
0858: }
0859: }
0860: if (node.getName() != null) {
0861: this .fBuffer.append(" ");//$NON-NLS-1$
0862: node.getName().accept(this );
0863: }
0864: return false;
0865: }
0866:
0867: /*
0868: * @see ASTVisitor#visit(MethodDeclaration)
0869: */
0870: public boolean visit(MethodDeclaration node) {
0871: if (node.getJavadoc() != null) {
0872: node.getJavadoc().accept(this );
0873: }
0874: if (node.getAST().apiLevel() >= AST.JLS3) {
0875: printModifiers(node.modifiers());
0876: if (!node.typeParameters().isEmpty()) {
0877: this .fBuffer.append("<");//$NON-NLS-1$
0878: for (Iterator it = node.typeParameters().iterator(); it
0879: .hasNext();) {
0880: TypeParameter t = (TypeParameter) it.next();
0881: t.accept(this );
0882: if (it.hasNext()) {
0883: this .fBuffer.append(",");//$NON-NLS-1$
0884: }
0885: }
0886: this .fBuffer.append("> ");//$NON-NLS-1$
0887: }
0888: }
0889: if (!node.isConstructor()) {
0890: if (node.getReturnType2() != null) {
0891: node.getReturnType2().accept(this );
0892: } else {
0893: // methods really ought to have a return type
0894: this .fBuffer.append("void");//$NON-NLS-1$
0895: }
0896: this .fBuffer.append(" ");//$NON-NLS-1$
0897: }
0898: node.getName().accept(this );
0899: this .fBuffer.append("(");//$NON-NLS-1$
0900: for (Iterator it = node.parameters().iterator(); it.hasNext();) {
0901: SingleVariableDeclaration v = (SingleVariableDeclaration) it
0902: .next();
0903: v.accept(this );
0904: if (it.hasNext()) {
0905: this .fBuffer.append(",");//$NON-NLS-1$
0906: }
0907: }
0908: this .fBuffer.append(")");//$NON-NLS-1$
0909: for (int i = 0; i < node.getExtraDimensions(); i++) {
0910: this .fBuffer.append("[]"); //$NON-NLS-1$
0911: }
0912: if (!node.thrownExceptions().isEmpty()) {
0913: this .fBuffer.append(" throws ");//$NON-NLS-1$
0914: for (Iterator it = node.thrownExceptions().iterator(); it
0915: .hasNext();) {
0916: Name n = (Name) it.next();
0917: n.accept(this );
0918: if (it.hasNext()) {
0919: this .fBuffer.append(", ");//$NON-NLS-1$
0920: }
0921: }
0922: this .fBuffer.append(" ");//$NON-NLS-1$
0923: }
0924: if (node.getBody() == null) {
0925: this .fBuffer.append(";");//$NON-NLS-1$
0926: } else {
0927: node.getBody().accept(this );
0928: }
0929: return false;
0930: }
0931:
0932: /*
0933: * @see ASTVisitor#visit(MethodInvocation)
0934: */
0935: public boolean visit(MethodInvocation node) {
0936: if (node.getExpression() != null) {
0937: node.getExpression().accept(this );
0938: this .fBuffer.append(".");//$NON-NLS-1$
0939: }
0940: if (node.getAST().apiLevel() >= AST.JLS3) {
0941: if (!node.typeArguments().isEmpty()) {
0942: this .fBuffer.append("<");//$NON-NLS-1$
0943: for (Iterator it = node.typeArguments().iterator(); it
0944: .hasNext();) {
0945: Type t = (Type) it.next();
0946: t.accept(this );
0947: if (it.hasNext()) {
0948: this .fBuffer.append(",");//$NON-NLS-1$
0949: }
0950: }
0951: this .fBuffer.append(">");//$NON-NLS-1$
0952: }
0953: }
0954: node.getName().accept(this );
0955: this .fBuffer.append("(");//$NON-NLS-1$
0956: for (Iterator it = node.arguments().iterator(); it.hasNext();) {
0957: Expression e = (Expression) it.next();
0958: e.accept(this );
0959: if (it.hasNext()) {
0960: this .fBuffer.append(",");//$NON-NLS-1$
0961: }
0962: }
0963: this .fBuffer.append(")");//$NON-NLS-1$
0964: return false;
0965: }
0966:
0967: /*
0968: * @see ASTVisitor#visit(Modifier)
0969: * @since 3.0
0970: */
0971: public boolean visit(Modifier node) {
0972: this .fBuffer.append(node.getKeyword().toString());
0973: return false;
0974: }
0975:
0976: /*
0977: * @see ASTVisitor#visit(NormalAnnotation)
0978: * @since 3.0
0979: */
0980: public boolean visit(NormalAnnotation node) {
0981: this .fBuffer.append("@");//$NON-NLS-1$
0982: node.getTypeName().accept(this );
0983: this .fBuffer.append("(");//$NON-NLS-1$
0984: for (Iterator it = node.values().iterator(); it.hasNext();) {
0985: MemberValuePair p = (MemberValuePair) it.next();
0986: p.accept(this );
0987: if (it.hasNext()) {
0988: this .fBuffer.append(",");//$NON-NLS-1$
0989: }
0990: }
0991: this .fBuffer.append(")");//$NON-NLS-1$
0992: return false;
0993: }
0994:
0995: /*
0996: * @see ASTVisitor#visit(NullLiteral)
0997: */
0998: public boolean visit(NullLiteral node) {
0999: this .fBuffer.append("null");//$NON-NLS-1$
1000: return false;
1001: }
1002:
1003: /*
1004: * @see ASTVisitor#visit(NumberLiteral)
1005: */
1006: public boolean visit(NumberLiteral node) {
1007: this .fBuffer.append(node.getToken());
1008: return false;
1009: }
1010:
1011: /*
1012: * @see ASTVisitor#visit(PackageDeclaration)
1013: */
1014: public boolean visit(PackageDeclaration node) {
1015: if (node.getAST().apiLevel() >= AST.JLS3) {
1016: if (node.getJavadoc() != null) {
1017: node.getJavadoc().accept(this );
1018: }
1019: for (Iterator it = node.annotations().iterator(); it
1020: .hasNext();) {
1021: Annotation p = (Annotation) it.next();
1022: p.accept(this );
1023: this .fBuffer.append(" ");//$NON-NLS-1$
1024: }
1025: }
1026: this .fBuffer.append("package ");//$NON-NLS-1$
1027: node.getName().accept(this );
1028: this .fBuffer.append(";");//$NON-NLS-1$
1029: return false;
1030: }
1031:
1032: /*
1033: * @see ASTVisitor#visit(ParameterizedType)
1034: * @since 3.0
1035: */
1036: public boolean visit(ParameterizedType node) {
1037: node.getType().accept(this );
1038: this .fBuffer.append("<");//$NON-NLS-1$
1039: for (Iterator it = node.typeArguments().iterator(); it
1040: .hasNext();) {
1041: Type t = (Type) it.next();
1042: t.accept(this );
1043: if (it.hasNext()) {
1044: this .fBuffer.append(",");//$NON-NLS-1$
1045: }
1046: }
1047: this .fBuffer.append(">");//$NON-NLS-1$
1048: return false;
1049: }
1050:
1051: /*
1052: * @see ASTVisitor#visit(ParenthesizedExpression)
1053: */
1054: public boolean visit(ParenthesizedExpression node) {
1055: this .fBuffer.append("(");//$NON-NLS-1$
1056: node.getExpression().accept(this );
1057: this .fBuffer.append(")");//$NON-NLS-1$
1058: return false;
1059: }
1060:
1061: /*
1062: * @see ASTVisitor#visit(PostfixExpression)
1063: */
1064: public boolean visit(PostfixExpression node) {
1065: node.getOperand().accept(this );
1066: this .fBuffer.append(node.getOperator().toString());
1067: return false;
1068: }
1069:
1070: /*
1071: * @see ASTVisitor#visit(PrefixExpression)
1072: */
1073: public boolean visit(PrefixExpression node) {
1074: this .fBuffer.append(node.getOperator().toString());
1075: node.getOperand().accept(this );
1076: return false;
1077: }
1078:
1079: /*
1080: * @see ASTVisitor#visit(PrimitiveType)
1081: */
1082: public boolean visit(PrimitiveType node) {
1083: this .fBuffer.append(node.getPrimitiveTypeCode().toString());
1084: return false;
1085: }
1086:
1087: /*
1088: * @see ASTVisitor#visit(QualifiedName)
1089: */
1090: public boolean visit(QualifiedName node) {
1091: node.getQualifier().accept(this );
1092: this .fBuffer.append(".");//$NON-NLS-1$
1093: node.getName().accept(this );
1094: return false;
1095: }
1096:
1097: /*
1098: * @see ASTVisitor#visit(QualifiedType)
1099: * @since 3.0
1100: */
1101: public boolean visit(QualifiedType node) {
1102: node.getQualifier().accept(this );
1103: this .fBuffer.append(".");//$NON-NLS-1$
1104: node.getName().accept(this );
1105: return false;
1106: }
1107:
1108: /*
1109: * @see ASTVisitor#visit(ReturnStatement)
1110: */
1111: public boolean visit(ReturnStatement node) {
1112: this .fBuffer.append("return");//$NON-NLS-1$
1113: if (node.getExpression() != null) {
1114: this .fBuffer.append(" ");//$NON-NLS-1$
1115: node.getExpression().accept(this );
1116: }
1117: this .fBuffer.append(";");//$NON-NLS-1$
1118: return false;
1119: }
1120:
1121: /*
1122: * @see ASTVisitor#visit(SimpleName)
1123: */
1124: public boolean visit(SimpleName node) {
1125: this .fBuffer.append(node.getIdentifier());
1126: return false;
1127: }
1128:
1129: /*
1130: * @see ASTVisitor#visit(SimpleType)
1131: */
1132: public boolean visit(SimpleType node) {
1133: return true;
1134: }
1135:
1136: /*
1137: * @see ASTVisitor#visit(SingleMemberAnnotation)
1138: * @since 3.0
1139: */
1140: public boolean visit(SingleMemberAnnotation node) {
1141: this .fBuffer.append("@");//$NON-NLS-1$
1142: node.getTypeName().accept(this );
1143: this .fBuffer.append("(");//$NON-NLS-1$
1144: node.getValue().accept(this );
1145: this .fBuffer.append(")");//$NON-NLS-1$
1146: return false;
1147: }
1148:
1149: /*
1150: * @see ASTVisitor#visit(SingleVariableDeclaration)
1151: */
1152: public boolean visit(SingleVariableDeclaration node) {
1153: if (node.getAST().apiLevel() >= AST.JLS3) {
1154: printModifiers(node.modifiers());
1155: }
1156: node.getType().accept(this );
1157: if (node.getAST().apiLevel() >= AST.JLS3) {
1158: if (node.isVarargs()) {
1159: this .fBuffer.append("...");//$NON-NLS-1$
1160: }
1161: }
1162: this .fBuffer.append(" ");//$NON-NLS-1$
1163: node.getName().accept(this );
1164: for (int i = 0; i < node.getExtraDimensions(); i++) {
1165: this .fBuffer.append("[]"); //$NON-NLS-1$
1166: }
1167: if (node.getInitializer() != null) {
1168: this .fBuffer.append("=");//$NON-NLS-1$
1169: node.getInitializer().accept(this );
1170: }
1171: return false;
1172: }
1173:
1174: /*
1175: * @see ASTVisitor#visit(StringLiteral)
1176: */
1177: public boolean visit(StringLiteral node) {
1178: this .fBuffer.append(node.getEscapedValue());
1179: return false;
1180: }
1181:
1182: /*
1183: * @see ASTVisitor#visit(SuperConstructorInvocation)
1184: */
1185: public boolean visit(SuperConstructorInvocation node) {
1186: if (node.getExpression() != null) {
1187: node.getExpression().accept(this );
1188: this .fBuffer.append(".");//$NON-NLS-1$
1189: }
1190: if (node.getAST().apiLevel() >= AST.JLS3) {
1191: if (!node.typeArguments().isEmpty()) {
1192: this .fBuffer.append("<");//$NON-NLS-1$
1193: for (Iterator it = node.typeArguments().iterator(); it
1194: .hasNext();) {
1195: Type t = (Type) it.next();
1196: t.accept(this );
1197: if (it.hasNext()) {
1198: this .fBuffer.append(",");//$NON-NLS-1$
1199: }
1200: }
1201: this .fBuffer.append(">");//$NON-NLS-1$
1202: }
1203: }
1204: this .fBuffer.append("super(");//$NON-NLS-1$
1205: for (Iterator it = node.arguments().iterator(); it.hasNext();) {
1206: Expression e = (Expression) it.next();
1207: e.accept(this );
1208: if (it.hasNext()) {
1209: this .fBuffer.append(",");//$NON-NLS-1$
1210: }
1211: }
1212: this .fBuffer.append(");");//$NON-NLS-1$
1213: return false;
1214: }
1215:
1216: /*
1217: * @see ASTVisitor#visit(SuperFieldAccess)
1218: */
1219: public boolean visit(SuperFieldAccess node) {
1220: if (node.getQualifier() != null) {
1221: node.getQualifier().accept(this );
1222: this .fBuffer.append(".");//$NON-NLS-1$
1223: }
1224: this .fBuffer.append("super.");//$NON-NLS-1$
1225: node.getName().accept(this );
1226: return false;
1227: }
1228:
1229: /*
1230: * @see ASTVisitor#visit(SuperMethodInvocation)
1231: */
1232: public boolean visit(SuperMethodInvocation node) {
1233: if (node.getQualifier() != null) {
1234: node.getQualifier().accept(this );
1235: this .fBuffer.append(".");//$NON-NLS-1$
1236: }
1237: this .fBuffer.append("super.");//$NON-NLS-1$
1238: if (node.getAST().apiLevel() >= AST.JLS3) {
1239: if (!node.typeArguments().isEmpty()) {
1240: this .fBuffer.append("<");//$NON-NLS-1$
1241: for (Iterator it = node.typeArguments().iterator(); it
1242: .hasNext();) {
1243: Type t = (Type) it.next();
1244: t.accept(this );
1245: if (it.hasNext()) {
1246: this .fBuffer.append(",");//$NON-NLS-1$
1247: }
1248: }
1249: this .fBuffer.append(">");//$NON-NLS-1$
1250: }
1251: }
1252: node.getName().accept(this );
1253: this .fBuffer.append("(");//$NON-NLS-1$
1254: for (Iterator it = node.arguments().iterator(); it.hasNext();) {
1255: Expression e = (Expression) it.next();
1256: e.accept(this );
1257: if (it.hasNext()) {
1258: this .fBuffer.append(",");//$NON-NLS-1$
1259: }
1260: }
1261: this .fBuffer.append(")");//$NON-NLS-1$
1262: return false;
1263: }
1264:
1265: /*
1266: * @see ASTVisitor#visit(SwitchCase)
1267: */
1268: public boolean visit(SwitchCase node) {
1269: if (node.isDefault()) {
1270: this .fBuffer.append("default :");//$NON-NLS-1$
1271: } else {
1272: this .fBuffer.append("case ");//$NON-NLS-1$
1273: node.getExpression().accept(this );
1274: this .fBuffer.append(":");//$NON-NLS-1$
1275: }
1276: return false;
1277: }
1278:
1279: /*
1280: * @see ASTVisitor#visit(SwitchStatement)
1281: */
1282: public boolean visit(SwitchStatement node) {
1283: this .fBuffer.append("switch (");//$NON-NLS-1$
1284: node.getExpression().accept(this );
1285: this .fBuffer.append(") ");//$NON-NLS-1$
1286: this .fBuffer.append("{");//$NON-NLS-1$
1287: for (Iterator it = node.statements().iterator(); it.hasNext();) {
1288: Statement s = (Statement) it.next();
1289: s.accept(this );
1290: }
1291: this .fBuffer.append("}");//$NON-NLS-1$
1292: return false;
1293: }
1294:
1295: /*
1296: * @see ASTVisitor#visit(SynchronizedStatement)
1297: */
1298: public boolean visit(SynchronizedStatement node) {
1299: this .fBuffer.append("synchronized (");//$NON-NLS-1$
1300: node.getExpression().accept(this );
1301: this .fBuffer.append(") ");//$NON-NLS-1$
1302: node.getBody().accept(this );
1303: return false;
1304: }
1305:
1306: /*
1307: * @see ASTVisitor#visit(TagElement)
1308: * @since 3.0
1309: */
1310: public boolean visit(TagElement node) {
1311: if (node.isNested()) {
1312: // nested tags are always enclosed in braces
1313: this .fBuffer.append("{");//$NON-NLS-1$
1314: } else {
1315: // top-level tags always begin on a new line
1316: this .fBuffer.append("\n * ");//$NON-NLS-1$
1317: }
1318: boolean previousRequiresWhiteSpace = false;
1319: if (node.getTagName() != null) {
1320: this .fBuffer.append(node.getTagName());
1321: previousRequiresWhiteSpace = true;
1322: }
1323: boolean previousRequiresNewLine = false;
1324: for (Iterator it = node.fragments().iterator(); it.hasNext();) {
1325: ASTNode e = (ASTNode) it.next();
1326: // assume text elements include necessary leading and trailing whitespace
1327: // but Name, MemberRef, MethodRef, and nested TagElement do not include white space
1328: boolean currentIncludesWhiteSpace = (e instanceof TextElement);
1329: if (previousRequiresNewLine && currentIncludesWhiteSpace) {
1330: this .fBuffer.append("\n * ");//$NON-NLS-1$
1331: }
1332: previousRequiresNewLine = currentIncludesWhiteSpace;
1333: // add space if required to separate
1334: if (previousRequiresWhiteSpace
1335: && !currentIncludesWhiteSpace) {
1336: this .fBuffer.append(" "); //$NON-NLS-1$
1337: }
1338: e.accept(this );
1339: previousRequiresWhiteSpace = !currentIncludesWhiteSpace
1340: && !(e instanceof TagElement);
1341: }
1342: if (node.isNested()) {
1343: this .fBuffer.append("}");//$NON-NLS-1$
1344: }
1345: return false;
1346: }
1347:
1348: /*
1349: * @see ASTVisitor#visit(TextElement)
1350: * @since 3.0
1351: */
1352: public boolean visit(TextElement node) {
1353: this .fBuffer.append(node.getText());
1354: return false;
1355: }
1356:
1357: /*
1358: * @see ASTVisitor#visit(ThisExpression)
1359: */
1360: public boolean visit(ThisExpression node) {
1361: if (node.getQualifier() != null) {
1362: node.getQualifier().accept(this );
1363: this .fBuffer.append(".");//$NON-NLS-1$
1364: }
1365: this .fBuffer.append("this");//$NON-NLS-1$
1366: return false;
1367: }
1368:
1369: /*
1370: * @see ASTVisitor#visit(ThrowStatement)
1371: */
1372: public boolean visit(ThrowStatement node) {
1373: this .fBuffer.append("throw ");//$NON-NLS-1$
1374: node.getExpression().accept(this );
1375: this .fBuffer.append(";");//$NON-NLS-1$
1376: return false;
1377: }
1378:
1379: /*
1380: * @see ASTVisitor#visit(TryStatement)
1381: */
1382: public boolean visit(TryStatement node) {
1383: this .fBuffer.append("try ");//$NON-NLS-1$
1384: node.getBody().accept(this );
1385: this .fBuffer.append(" ");//$NON-NLS-1$
1386: for (Iterator it = node.catchClauses().iterator(); it.hasNext();) {
1387: CatchClause cc = (CatchClause) it.next();
1388: cc.accept(this );
1389: }
1390: if (node.getFinally() != null) {
1391: this .fBuffer.append("finally ");//$NON-NLS-1$
1392: node.getFinally().accept(this );
1393: }
1394: return false;
1395: }
1396:
1397: /*
1398: * @see ASTVisitor#visit(TypeDeclaration)
1399: */
1400: public boolean visit(TypeDeclaration node) {
1401: if (node.getJavadoc() != null) {
1402: node.getJavadoc().accept(this );
1403: }
1404: if (node.getAST().apiLevel() >= AST.JLS3) {
1405: printModifiers(node.modifiers());
1406: }
1407: this .fBuffer
1408: .append(node.isInterface() ? "interface " : "class ");//$NON-NLS-2$//$NON-NLS-1$
1409: node.getName().accept(this );
1410: if (node.getAST().apiLevel() >= AST.JLS3) {
1411: if (!node.typeParameters().isEmpty()) {
1412: this .fBuffer.append("<");//$NON-NLS-1$
1413: for (Iterator it = node.typeParameters().iterator(); it
1414: .hasNext();) {
1415: TypeParameter t = (TypeParameter) it.next();
1416: t.accept(this );
1417: if (it.hasNext()) {
1418: this .fBuffer.append(",");//$NON-NLS-1$
1419: }
1420: }
1421: this .fBuffer.append(">");//$NON-NLS-1$
1422: }
1423: }
1424: this .fBuffer.append(" ");//$NON-NLS-1$
1425: if (node.getAST().apiLevel() >= AST.JLS3) {
1426: if (node.getSuperclassType() != null) {
1427: this .fBuffer.append("extends ");//$NON-NLS-1$
1428: node.getSuperclassType().accept(this );
1429: this .fBuffer.append(" ");//$NON-NLS-1$
1430: }
1431: if (!node.super InterfaceTypes().isEmpty()) {
1432: this .fBuffer
1433: .append(node.isInterface() ? "extends " : "implements ");//$NON-NLS-2$//$NON-NLS-1$
1434: for (Iterator it = node.super InterfaceTypes()
1435: .iterator(); it.hasNext();) {
1436: Type t = (Type) it.next();
1437: t.accept(this );
1438: if (it.hasNext()) {
1439: this .fBuffer.append(", ");//$NON-NLS-1$
1440: }
1441: }
1442: this .fBuffer.append(" ");//$NON-NLS-1$
1443: }
1444: }
1445: this .fBuffer.append("{");//$NON-NLS-1$
1446: BodyDeclaration prev = null;
1447: for (Iterator it = node.bodyDeclarations().iterator(); it
1448: .hasNext();) {
1449: BodyDeclaration d = (BodyDeclaration) it.next();
1450: if (prev instanceof EnumConstantDeclaration) {
1451: // enum constant declarations do not include punctuation
1452: if (d instanceof EnumConstantDeclaration) {
1453: // enum constant declarations are separated by commas
1454: this .fBuffer.append(", ");//$NON-NLS-1$
1455: } else {
1456: // semicolon separates last enum constant declaration from
1457: // first class body declarations
1458: this .fBuffer.append("; ");//$NON-NLS-1$
1459: }
1460: }
1461: d.accept(this );
1462: prev = d;
1463: }
1464: this .fBuffer.append("}");//$NON-NLS-1$
1465: return false;
1466: }
1467:
1468: /*
1469: * @see ASTVisitor#visit(TypeDeclarationStatement)
1470: */
1471: public boolean visit(TypeDeclarationStatement node) {
1472: if (node.getAST().apiLevel() >= AST.JLS3) {
1473: node.getDeclaration().accept(this );
1474: }
1475: return false;
1476: }
1477:
1478: /*
1479: * @see ASTVisitor#visit(TypeLiteral)
1480: */
1481: public boolean visit(TypeLiteral node) {
1482: node.getType().accept(this );
1483: this .fBuffer.append(".class");//$NON-NLS-1$
1484: return false;
1485: }
1486:
1487: /*
1488: * @see ASTVisitor#visit(TypeParameter)
1489: * @since 3.0
1490: */
1491: public boolean visit(TypeParameter node) {
1492: node.getName().accept(this );
1493: if (!node.typeBounds().isEmpty()) {
1494: this .fBuffer.append(" extends ");//$NON-NLS-1$
1495: for (Iterator it = node.typeBounds().iterator(); it
1496: .hasNext();) {
1497: Type t = (Type) it.next();
1498: t.accept(this );
1499: if (it.hasNext()) {
1500: this .fBuffer.append(" & ");//$NON-NLS-1$
1501: }
1502: }
1503: }
1504: return false;
1505: }
1506:
1507: /*
1508: * @see ASTVisitor#visit(VariableDeclarationExpression)
1509: */
1510: public boolean visit(VariableDeclarationExpression node) {
1511: if (node.getAST().apiLevel() >= AST.JLS3) {
1512: printModifiers(node.modifiers());
1513: }
1514: node.getType().accept(this );
1515: this .fBuffer.append(" ");//$NON-NLS-1$
1516: for (Iterator it = node.fragments().iterator(); it.hasNext();) {
1517: VariableDeclarationFragment f = (VariableDeclarationFragment) it
1518: .next();
1519: f.accept(this );
1520: if (it.hasNext()) {
1521: this .fBuffer.append(", ");//$NON-NLS-1$
1522: }
1523: }
1524: return false;
1525: }
1526:
1527: /*
1528: * @see ASTVisitor#visit(VariableDeclarationFragment)
1529: */
1530: public boolean visit(VariableDeclarationFragment node) {
1531: node.getName().accept(this );
1532: for (int i = 0; i < node.getExtraDimensions(); i++) {
1533: this .fBuffer.append("[]");//$NON-NLS-1$
1534: }
1535: if (node.getInitializer() != null) {
1536: this .fBuffer.append("=");//$NON-NLS-1$
1537: node.getInitializer().accept(this );
1538: }
1539: return false;
1540: }
1541:
1542: /*
1543: * @see ASTVisitor#visit(VariableDeclarationStatement)
1544: */
1545: public boolean visit(VariableDeclarationStatement node) {
1546: if (node.getAST().apiLevel() >= AST.JLS3) {
1547: printModifiers(node.modifiers());
1548: }
1549: node.getType().accept(this );
1550: this .fBuffer.append(" ");//$NON-NLS-1$
1551: for (Iterator it = node.fragments().iterator(); it.hasNext();) {
1552: VariableDeclarationFragment f = (VariableDeclarationFragment) it
1553: .next();
1554: f.accept(this );
1555: if (it.hasNext()) {
1556: this .fBuffer.append(", ");//$NON-NLS-1$
1557: }
1558: }
1559: this .fBuffer.append(";");//$NON-NLS-1$
1560: return false;
1561: }
1562:
1563: /*
1564: * @see ASTVisitor#visit(WildcardType)
1565: * @since 3.0
1566: */
1567: public boolean visit(WildcardType node) {
1568: this .fBuffer.append("?");//$NON-NLS-1$
1569: Type bound = node.getBound();
1570: if (bound != null) {
1571: if (node.isUpperBound()) {
1572: this .fBuffer.append(" extends ");//$NON-NLS-1$
1573: } else {
1574: this .fBuffer.append(" super ");//$NON-NLS-1$
1575: }
1576: bound.accept(this );
1577: }
1578: return false;
1579: }
1580:
1581: /*
1582: * @see ASTVisitor#visit(WhileStatement)
1583: */
1584: public boolean visit(WhileStatement node) {
1585: this .fBuffer.append("while (");//$NON-NLS-1$
1586: node.getExpression().accept(this );
1587: this .fBuffer.append(") ");//$NON-NLS-1$
1588: node.getBody().accept(this );
1589: return false;
1590: }
1591:
1592: }
|