All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.antlrjavaparser.api.visitor.DumpVisitor Maven / Gradle / Ivy

There is a newer version: 1.0.17
Show newest version
/*
 * Copyright (C) 2007 J�lio Vilmar Gesser.
 *
 * This file is part of Java 1.5 parser and Abstract Syntax Tree.
 *
 * Java 1.5 parser and Abstract Syntax Tree is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Java 1.5 parser and Abstract Syntax Tree is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Java 1.5 parser and Abstract Syntax Tree.  If not, see .
 */
/*
 * Created on 05/10/2006
 */
package com.github.antlrjavaparser.api.visitor;

import com.github.antlrjavaparser.api.BlockComment;
import com.github.antlrjavaparser.api.Comment;
import com.github.antlrjavaparser.api.CompilationUnit;
import com.github.antlrjavaparser.api.ImportDeclaration;
import com.github.antlrjavaparser.api.LineComment;
import com.github.antlrjavaparser.api.PackageDeclaration;
import com.github.antlrjavaparser.api.TypeParameter;
import com.github.antlrjavaparser.api.body.AnnotationDeclaration;
import com.github.antlrjavaparser.api.body.AnnotationMemberDeclaration;
import com.github.antlrjavaparser.api.body.BodyDeclaration;
import com.github.antlrjavaparser.api.body.CatchParameter;
import com.github.antlrjavaparser.api.body.ClassOrInterfaceDeclaration;
import com.github.antlrjavaparser.api.body.ConstructorDeclaration;
import com.github.antlrjavaparser.api.body.EmptyMemberDeclaration;
import com.github.antlrjavaparser.api.body.EmptyTypeDeclaration;
import com.github.antlrjavaparser.api.body.EnumConstantDeclaration;
import com.github.antlrjavaparser.api.body.EnumDeclaration;
import com.github.antlrjavaparser.api.body.FieldDeclaration;
import com.github.antlrjavaparser.api.body.InitializerDeclaration;
import com.github.antlrjavaparser.api.body.JavadocComment;
import com.github.antlrjavaparser.api.body.MethodDeclaration;
import com.github.antlrjavaparser.api.body.ModifierSet;
import com.github.antlrjavaparser.api.body.Parameter;
import com.github.antlrjavaparser.api.body.Resource;
import com.github.antlrjavaparser.api.body.TypeDeclaration;
import com.github.antlrjavaparser.api.body.VariableDeclarator;
import com.github.antlrjavaparser.api.body.VariableDeclaratorId;
import com.github.antlrjavaparser.api.expr.AnnotationExpr;
import com.github.antlrjavaparser.api.expr.ArrayAccessExpr;
import com.github.antlrjavaparser.api.expr.ArrayCreationExpr;
import com.github.antlrjavaparser.api.expr.ArrayInitializerExpr;
import com.github.antlrjavaparser.api.expr.AssignExpr;
import com.github.antlrjavaparser.api.expr.BinaryExpr;
import com.github.antlrjavaparser.api.expr.BooleanLiteralExpr;
import com.github.antlrjavaparser.api.expr.CastExpr;
import com.github.antlrjavaparser.api.expr.CharLiteralExpr;
import com.github.antlrjavaparser.api.expr.ClassExpr;
import com.github.antlrjavaparser.api.expr.ConditionalExpr;
import com.github.antlrjavaparser.api.expr.DoubleLiteralExpr;
import com.github.antlrjavaparser.api.expr.EnclosedExpr;
import com.github.antlrjavaparser.api.expr.Expression;
import com.github.antlrjavaparser.api.expr.FieldAccessExpr;
import com.github.antlrjavaparser.api.expr.InstanceOfExpr;
import com.github.antlrjavaparser.api.expr.IntegerLiteralExpr;
import com.github.antlrjavaparser.api.expr.IntegerLiteralMinValueExpr;
import com.github.antlrjavaparser.api.expr.LongLiteralExpr;
import com.github.antlrjavaparser.api.expr.LongLiteralMinValueExpr;
import com.github.antlrjavaparser.api.expr.MarkerAnnotationExpr;
import com.github.antlrjavaparser.api.expr.MemberValuePair;
import com.github.antlrjavaparser.api.expr.MethodCallExpr;
import com.github.antlrjavaparser.api.expr.NameExpr;
import com.github.antlrjavaparser.api.expr.NormalAnnotationExpr;
import com.github.antlrjavaparser.api.expr.NullLiteralExpr;
import com.github.antlrjavaparser.api.expr.ObjectCreationExpr;
import com.github.antlrjavaparser.api.expr.QualifiedNameExpr;
import com.github.antlrjavaparser.api.expr.SingleMemberAnnotationExpr;
import com.github.antlrjavaparser.api.expr.StringLiteralExpr;
import com.github.antlrjavaparser.api.expr.SuperExpr;
import com.github.antlrjavaparser.api.expr.ThisExpr;
import com.github.antlrjavaparser.api.expr.UnaryExpr;
import com.github.antlrjavaparser.api.expr.VariableDeclarationExpr;
import com.github.antlrjavaparser.api.stmt.AssertStmt;
import com.github.antlrjavaparser.api.stmt.BlockStmt;
import com.github.antlrjavaparser.api.stmt.BreakStmt;
import com.github.antlrjavaparser.api.stmt.CatchClause;
import com.github.antlrjavaparser.api.stmt.ContinueStmt;
import com.github.antlrjavaparser.api.stmt.DoStmt;
import com.github.antlrjavaparser.api.stmt.EmptyStmt;
import com.github.antlrjavaparser.api.stmt.ExplicitConstructorInvocationStmt;
import com.github.antlrjavaparser.api.stmt.ExpressionStmt;
import com.github.antlrjavaparser.api.stmt.ForStmt;
import com.github.antlrjavaparser.api.stmt.ForeachStmt;
import com.github.antlrjavaparser.api.stmt.IfStmt;
import com.github.antlrjavaparser.api.stmt.LabeledStmt;
import com.github.antlrjavaparser.api.stmt.ReturnStmt;
import com.github.antlrjavaparser.api.stmt.Statement;
import com.github.antlrjavaparser.api.stmt.SwitchEntryStmt;
import com.github.antlrjavaparser.api.stmt.SwitchStmt;
import com.github.antlrjavaparser.api.stmt.SynchronizedStmt;
import com.github.antlrjavaparser.api.stmt.ThrowStmt;
import com.github.antlrjavaparser.api.stmt.TryStmt;
import com.github.antlrjavaparser.api.stmt.TypeDeclarationStmt;
import com.github.antlrjavaparser.api.stmt.WhileStmt;
import com.github.antlrjavaparser.api.type.ClassOrInterfaceType;
import com.github.antlrjavaparser.api.type.PrimitiveType;
import com.github.antlrjavaparser.api.type.ReferenceType;
import com.github.antlrjavaparser.api.type.Type;
import com.github.antlrjavaparser.api.type.VoidType;
import com.github.antlrjavaparser.api.type.WildcardType;

import java.util.Iterator;
import java.util.List;

/**
 * @author Julio Vilmar Gesser
 */

public final class DumpVisitor implements VoidVisitor {

    private static final CommentFormatter commentFormatter = new CommentFormatter();

    private static class SourcePrinter {

        private int level = 0;

        private boolean indented = false;

        private final StringBuilder buf = new StringBuilder();

        public void indent() {
            level++;
        }

        public void unindent() {
            level--;
        }

        private void makeIndent() {
            for (int i = 0; i < level; i++) {
                buf.append("    ");
            }
        }

        public void print(String arg) {
            if (!indented) {
                makeIndent();
                indented = true;
            }
            buf.append(arg);
        }

        public void printRaw(String arg, boolean stillIndented) {
            buf.append(arg);

            indented = stillIndented;
        }

        public void printRaw(String arg) {
            buf.append(arg);
        }

        public void printLn(String arg) {
            print(arg);
            printLn();
        }

        public void printLn() {
            buf.append("\n");
            indented = false;
        }

        public String getSource() {
            return buf.toString();
        }

        @Override
        public String toString() {
            return getSource();
        }

        public int getLevel() {
            return level;
        }
    }

    private final SourcePrinter printer = new SourcePrinter();

    public String getSource() {
        return printer.getSource();
    }

    private void printModifiers(int modifiers) {
        if (ModifierSet.isPrivate(modifiers)) {
            printer.print("private ");
        }
        if (ModifierSet.isProtected(modifiers)) {
            printer.print("protected ");
        }
        if (ModifierSet.isPublic(modifiers)) {
            printer.print("public ");
        }
        if (ModifierSet.isAbstract(modifiers)) {
            printer.print("abstract ");
        }
        if (ModifierSet.isStatic(modifiers)) {
            printer.print("static ");
        }
        if (ModifierSet.isFinal(modifiers)) {
            printer.print("final ");
        }
        if (ModifierSet.isNative(modifiers)) {
            printer.print("native ");
        }
        if (ModifierSet.isStrictfp(modifiers)) {
            printer.print("strictfp ");
        }
        if (ModifierSet.isSynchronized(modifiers)) {
            printer.print("synchronized ");
        }
        if (ModifierSet.isTransient(modifiers)) {
            printer.print("transient ");
        }
        if (ModifierSet.isVolatile(modifiers)) {
            printer.print("volatile ");
        }
    }

    private void printMembers(List members, Object arg) {
        for (BodyDeclaration member : members) {
            printer.printLn();
            member.accept(this, arg);
            printer.printLn();
        }
    }

    private void printMemberAnnotations(List annotations, Object arg) {
        if (annotations != null) {
            for (AnnotationExpr a : annotations) {
                a.accept(this, arg);
                printer.printLn();
            }
        }
    }

    private void printAnnotations(List annotations, Object arg) {
        if (annotations != null) {
            for (AnnotationExpr a : annotations) {
                a.accept(this, arg);
                printer.print(" ");
            }
        }
    }

    private void printTypeArgs(List args, Object arg) {
        if (args != null) {
            printer.print("<");
            for (Iterator i = args.iterator(); i.hasNext();) {
                Type t = i.next();
                t.accept(this, arg);
                if (i.hasNext()) {
                    printer.print(", ");
                }
            }
            printer.print(">");
        }
    }

    private void printTypeParameters(List args, Object arg) {
        if (args != null) {
            printer.print("<");
            for (Iterator i = args.iterator(); i.hasNext();) {
                TypeParameter t = i.next();
                t.accept(this, arg);
                if (i.hasNext()) {
                    printer.print(", ");
                }
            }
            printer.print(">");
        }
    }

    private void printArguments(List args, Object arg) {
        printer.print("(");
        if (args != null) {
            for (Iterator i = args.iterator(); i.hasNext();) {
                Expression e = i.next();
                e.accept(this, arg);
                if (i.hasNext()) {
                    printer.print(", ");
                }
            }
        }
        printer.print(")");
    }

    private void printJavadoc(JavadocComment javadoc, Object arg) {
        if (javadoc != null) {
            javadoc.accept(this, arg);
        }
    }

    public void visit(CompilationUnit n, Object arg) {
        if (n.getPackage() != null) {
            n.getPackage().accept(this, arg);
        }
        if (n.getImports() != null) {
            for (ImportDeclaration i : n.getImports()) {
                i.accept(this, arg);
            }
            printer.printLn();
        }
        if (n.getTypes() != null) {
            for (Iterator i = n.getTypes().iterator(); i.hasNext();) {
                i.next().accept(this, arg);
                printer.printLn();
                if (i.hasNext()) {
                    printer.printLn();
                }
            }
        }
    }

    public void visit(PackageDeclaration n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        printAnnotations(n.getAnnotations(), arg);
        printer.print("package ");
        n.getName().accept(this, arg);
        printer.print(";");
        printEndComments(n.getEndComments(), arg);
        printer.printLn();
    }

    public void visit(NameExpr n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        printer.print(n.getName());
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(QualifiedNameExpr n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        n.getQualifier().accept(this, arg);
        printer.print(".");
        printer.print(n.getName());
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(ImportDeclaration n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        printer.print("import ");
        if (n.isStatic()) {
            printer.print("static ");
        }
        n.getName().accept(this, arg);
        if (n.isAsterisk()) {
            printer.print(".*");
        }
        printer.print(";");
        printEndComments(n.getEndComments(), arg);
        printer.printLn();
    }

    public void visit(ClassOrInterfaceDeclaration n, Object arg) {
        printMemberAnnotations(n.getAnnotations(), arg);
        printBeginComments(n.getBeginComments(), arg);
        printModifiers(n.getModifiers());

        if (n.isInterface()) {
            printer.print("interface ");
        } else {
            printer.print("class ");
        }

        printer.print(n.getName());

        printTypeParameters(n.getTypeParameters(), arg);

        if (n.getExtends() != null) {
            printer.print(" extends ");
            for (Iterator i = n.getExtends().iterator(); i.hasNext();) {
                ClassOrInterfaceType c = i.next();
                c.accept(this, arg);
                if (i.hasNext()) {
                    printer.print(", ");
                }
            }
        }

        if (n.getImplements() != null) {
            printer.print(" implements ");
            for (Iterator i = n.getImplements().iterator(); i.hasNext();) {
                ClassOrInterfaceType c = i.next();
                c.accept(this, arg);
                if (i.hasNext()) {
                    printer.print(", ");
                }
            }
        }

        printer.printLn(" {");
        printer.indent();
        if (n.getMembers() != null) {
            printMembers(n.getMembers(), arg);
        }
        printer.unindent();
        printer.print("}");
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(EmptyTypeDeclaration n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        printJavadoc(n.getJavaDoc(), arg);
        printer.print(";");
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(JavadocComment n, Object arg) {
        printer.print("/**");
        printer.print(n.getContent());
        printer.printLn("*/");
    }

    public void visit(ClassOrInterfaceType n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        if (n.getScope() != null) {
            n.getScope().accept(this, arg);
            printer.print(".");
        }
        printer.print(n.getName());
        printTypeArgs(n.getTypeArgs(), arg);
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(TypeParameter n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        printer.print(n.getName());
        if (n.getTypeBound() != null) {
            printer.print(" extends ");
            for (Iterator i = n.getTypeBound().iterator(); i.hasNext();) {
                ClassOrInterfaceType c = i.next();
                c.accept(this, arg);
                if (i.hasNext()) {
                    printer.print(" & ");
                }
            }
        }
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(PrimitiveType n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        switch (n.getType()) {
            case Boolean:
                printer.print("boolean");
                break;
            case Byte:
                printer.print("byte");
                break;
            case Char:
                printer.print("char");
                break;
            case Double:
                printer.print("double");
                break;
            case Float:
                printer.print("float");
                break;
            case Int:
                printer.print("int");
                break;
            case Long:
                printer.print("long");
                break;
            case Short:
                printer.print("short");
                break;
        }
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(ReferenceType n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        n.getType().accept(this, arg);
        for (int i = 0; i < n.getArrayCount(); i++) {
            printer.print("[]");
        }
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(WildcardType n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        printer.print("?");
        if (n.getExtends() != null) {
            printer.print(" extends ");
            n.getExtends().accept(this, arg);
        }
        if (n.getSuper() != null) {
            printer.print(" super ");
            n.getSuper().accept(this, arg);
        }
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(FieldDeclaration n, Object arg) {
        printJavadoc(n.getJavaDoc(), arg);
        printMemberAnnotations(n.getAnnotations(), arg);
        printBeginComments(n.getBeginComments(), arg);
        printModifiers(n.getModifiers());
        n.getType().accept(this, arg);

        printer.print(" ");
        for (Iterator i = n.getVariables().iterator(); i.hasNext();) {
            VariableDeclarator var = i.next();
            var.accept(this, arg);
            if (i.hasNext()) {
                printer.print(", ");
            }
        }

        printer.print(";");
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(VariableDeclarator n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        n.getId().accept(this, arg);
        if (n.getInit() != null) {
            printer.print(" = ");
            n.getInit().accept(this, arg);
        }
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(VariableDeclaratorId n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        printer.print(n.getName());
        for (int i = 0; i < n.getArrayCount(); i++) {
            printer.print("[]");
        }
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(ArrayInitializerExpr n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        printer.print("{");
        if (n.getValues() != null) {
            printer.print(" ");
            for (Iterator i = n.getValues().iterator(); i.hasNext();) {
                Expression expr = i.next();
                expr.accept(this, arg);
                if (i.hasNext()) {
                    printer.print(", ");
                }
            }
            printer.print(" ");
        }
        printer.print("}");
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(VoidType n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        printer.print("void");
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(ArrayAccessExpr n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        n.getName().accept(this, arg);
        printer.print("[");
        n.getIndex().accept(this, arg);
        printer.print("]");
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(ArrayCreationExpr n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        printer.print("new ");
        n.getType().accept(this, arg);

        if (n.getDimensions() != null) {
            for (Expression dim : n.getDimensions()) {
                printer.print("[");
                dim.accept(this, arg);
                printer.print("]");
            }
            for (int i = 0; i < n.getArrayCount(); i++) {
                printer.print("[]");
            }
        } else {
            for (int i = 0; i < n.getArrayCount(); i++) {
                printer.print("[]");
            }
            printer.print(" ");
            n.getInitializer().accept(this, arg);
        }
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(AssignExpr n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        n.getTarget().accept(this, arg);
        printer.print(" ");
        switch (n.getOperator()) {
            case assign:
                printer.print("=");
                break;
            case and:
                printer.print("&=");
                break;
            case or:
                printer.print("|=");
                break;
            case xor:
                printer.print("^=");
                break;
            case plus:
                printer.print("+=");
                break;
            case minus:
                printer.print("-=");
                break;
            case rem:
                printer.print("%=");
                break;
            case slash:
                printer.print("/=");
                break;
            case star:
                printer.print("*=");
                break;
            case lShift:
                printer.print("<<=");
                break;
            case rSignedShift:
                printer.print(">>=");
                break;
            case rUnsignedShift:
                printer.print(">>>=");
                break;
        }
        printer.print(" ");
        n.getValue().accept(this, arg);
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(BinaryExpr n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        n.getLeft().accept(this, arg);
        printer.print(" ");
        switch (n.getOperator()) {
            case or:
                printer.print("||");
                break;
            case and:
                printer.print("&&");
                break;
            case binOr:
                printer.print("|");
                break;
            case binAnd:
                printer.print("&");
                break;
            case xor:
                printer.print("^");
                break;
            case equals:
                printer.print("==");
                break;
            case notEquals:
                printer.print("!=");
                break;
            case less:
                printer.print("<");
                break;
            case greater:
                printer.print(">");
                break;
            case lessEquals:
                printer.print("<=");
                break;
            case greaterEquals:
                printer.print(">=");
                break;
            case lShift:
                printer.print("<<");
                break;
            case rSignedShift:
                printer.print(">>");
                break;
            case rUnsignedShift:
                printer.print(">>>");
                break;
            case plus:
                printer.print("+");
                break;
            case minus:
                printer.print("-");
                break;
            case times:
                printer.print("*");
                break;
            case divide:
                printer.print("/");
                break;
            case remainder:
                printer.print("%");
                break;
        }
        printer.print(" ");
        n.getRight().accept(this, arg);
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(CastExpr n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        printer.print("(");
        n.getType().accept(this, arg);
        printer.print(") ");
        n.getExpr().accept(this, arg);
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(ClassExpr n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        n.getType().accept(this, arg);
        printer.print(".class");
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(ConditionalExpr n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        n.getCondition().accept(this, arg);
        printer.print(" ? ");
        n.getThenExpr().accept(this, arg);
        printer.print(" : ");
        n.getElseExpr().accept(this, arg);
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(EnclosedExpr n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        printer.print("(");
        n.getInner().accept(this, arg);
        printer.print(")");
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(FieldAccessExpr n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        n.getScope().accept(this, arg);
        printer.print(".");
        printer.print(n.getField());
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(InstanceOfExpr n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        n.getExpr().accept(this, arg);
        printer.print(" instanceof ");
        n.getType().accept(this, arg);
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(CharLiteralExpr n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        printer.print("'");
        printer.print(n.getValue());
        printer.print("'");
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(DoubleLiteralExpr n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        printer.print(n.getValue());
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(IntegerLiteralExpr n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        printer.print(n.getValue());
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(LongLiteralExpr n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        printer.print(n.getValue());
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(IntegerLiteralMinValueExpr n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        printer.print(n.getValue());
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(LongLiteralMinValueExpr n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        printer.print(n.getValue());
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(StringLiteralExpr n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        printer.print("\"");
        printer.print(n.getValue());
        printer.print("\"");
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(BooleanLiteralExpr n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        printer.print(String.valueOf(n.getValue()));
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(NullLiteralExpr n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        printer.print("null");
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(ThisExpr n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        if (n.getClassExpr() != null) {
            n.getClassExpr().accept(this, arg);
            printer.print(".");
        }
        printer.print("this");
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(SuperExpr n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        if (n.getClassExpr() != null) {
            n.getClassExpr().accept(this, arg);
            printer.print(".");
        }
        printer.print("super");
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(MethodCallExpr n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        if (n.getScope() != null) {
            n.getScope().accept(this, arg);
            printer.print(".");
        }
        printTypeArgs(n.getTypeArgs(), arg);
        printer.print(n.getName());
        printArguments(n.getArgs(), arg);
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(ObjectCreationExpr n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        if (n.getScope() != null) {
            n.getScope().accept(this, arg);
            printer.print(".");
        }

        printer.print("new ");

        printTypeArgs(n.getTypeArgs(), arg);
        n.getType().accept(this, arg);

        printArguments(n.getArgs(), arg);

        if (n.getAnonymousClassBody() != null) {
            printer.printLn(" {");
            printer.indent();
            printMembers(n.getAnonymousClassBody(), arg);
            printer.unindent();
            printer.print("}");
        }
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(UnaryExpr n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        switch (n.getOperator()) {
            case positive:
                printer.print("+");
                break;
            case negative:
                printer.print("-");
                break;
            case inverse:
                printer.print("~");
                break;
            case not:
                printer.print("!");
                break;
            case preIncrement:
                printer.print("++");
                break;
            case preDecrement:
                printer.print("--");
                break;
        }

        n.getExpr().accept(this, arg);

        switch (n.getOperator()) {
            case posIncrement:
                printer.print("++");
                break;
            case posDecrement:
                printer.print("--");
                break;
        }
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(ConstructorDeclaration n, Object arg) {
        printJavadoc(n.getJavaDoc(), arg);
        printMemberAnnotations(n.getAnnotations(), arg);
        printBeginComments(n.getBeginComments(), arg);
        printModifiers(n.getModifiers());

        printTypeParameters(n.getTypeParameters(), arg);
        if (n.getTypeParameters() != null) {
            printer.print(" ");
        }
        printer.print(n.getName());

        printer.print("(");
        if (n.getParameters() != null) {
            for (Iterator i = n.getParameters().iterator(); i.hasNext();) {
                Parameter p = i.next();
                p.accept(this, arg);
                if (i.hasNext()) {
                    printer.print(", ");
                }
            }
        }
        printer.print(")");

        if (n.getThrows() != null) {
            printer.print(" throws ");
            for (Iterator i = n.getThrows().iterator(); i.hasNext();) {
                NameExpr name = i.next();
                name.accept(this, arg);
                if (i.hasNext()) {
                    printer.print(", ");
                }
            }
        }
        printer.print(" ");
        n.getBlock().accept(this, arg);
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(MethodDeclaration n, Object arg) {
        printMemberAnnotations(n.getAnnotations(), arg);
        printBeginComments(n.getBeginComments(), arg);
        printModifiers(n.getModifiers());

        printTypeParameters(n.getTypeParameters(), arg);
        if (n.getTypeParameters() != null) {
            printer.print(" ");
        }

        n.getType().accept(this, arg);
        printer.print(" ");
        printer.print(n.getName());

        printer.print("(");
        if (n.getParameters() != null) {
            for (Iterator i = n.getParameters().iterator(); i.hasNext();) {
                Parameter p = i.next();
                p.accept(this, arg);
                if (i.hasNext()) {
                    printer.print(", ");
                }
            }
        }
        printer.print(")");

        for (int i = 0; i < n.getArrayCount(); i++) {
            printer.print("[]");
        }

        if (n.getThrows() != null) {
            printer.print(" throws ");
            for (Iterator i = n.getThrows().iterator(); i.hasNext();) {
                NameExpr name = i.next();
                name.accept(this, arg);
                if (i.hasNext()) {
                    printer.print(", ");
                }
            }
        }
        if (n.getBody() == null) {
            printer.print(";");
        } else {
            printer.print(" ");
            n.getBody().accept(this, arg);
        }

        printEndComments(n.getEndComments(), arg);
    }

    public void visit(Parameter n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        printAnnotations(n.getAnnotations(), arg);
        printModifiers(n.getModifiers());

        n.getType().accept(this, arg);
        if (n.isVarArgs()) {
            printer.print("...");
        }
        printer.print(" ");
        n.getId().accept(this, arg);
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(CatchParameter n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        printAnnotations(n.getAnnotations(), arg);
        printModifiers(n.getModifiers());

        n.getTypeList().get(0).accept(this, arg);
        for (int i = 1; i < n.getTypeList().size(); i++) {
            printer.print(" | ");
            n.getTypeList().get(i).accept(this, arg);
        }

        printer.print(" ");
        n.getId().accept(this, arg);
        printEndComments(n.getEndComments(), arg);
    }

    @Override
    public void visit(Resource n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        printAnnotations(n.getAnnotations(), arg);
        printModifiers(n.getModifiers());

        n.getType().accept(this, arg);
        printer.print(" ");
        n.getId().accept(this, arg);
        printer.print(" = ");
        n.getExpression().accept(this, arg);
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(ExplicitConstructorInvocationStmt n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        if (n.isThis()) {
            printTypeArgs(n.getTypeArgs(), arg);
            printer.print("this");
        } else {
            if (n.getExpr() != null) {
                n.getExpr().accept(this, arg);
                printer.print(".");
            }
            printTypeArgs(n.getTypeArgs(), arg);
            printer.print("super");
        }
        printArguments(n.getArgs(), arg);
        printer.print(";");
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(VariableDeclarationExpr n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        printAnnotations(n.getAnnotations(), arg);
        printModifiers(n.getModifiers());

        n.getType().accept(this, arg);
        printer.print(" ");

        for (Iterator i = n.getVars().iterator(); i.hasNext();) {
            VariableDeclarator v = i.next();
            v.accept(this, arg);
            if (i.hasNext()) {
                printer.print(", ");
            }
        }
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(TypeDeclarationStmt n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        n.getTypeDeclaration().accept(this, arg);
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(AssertStmt n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        printer.print("assert ");
        n.getCheck().accept(this, arg);
        if (n.getMessage() != null) {
            printer.print(" : ");
            n.getMessage().accept(this, arg);
        }
        printer.print(";");
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(BlockStmt n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        printer.printLn("{");
        printer.indent();
        printInternalComments(n.getInternalComments(), arg);
        printer.unindent();

        if (n.getStmts() != null) {
            printer.indent();
            for (Statement s : n.getStmts()) {
                s.accept(this, arg);
                printer.printLn();
            }
            printer.unindent();
        }
        printer.print("}");
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(LabeledStmt n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        printer.print(n.getLabel());
        printer.print(": ");
        n.getStmt().accept(this, arg);
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(EmptyStmt n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        printer.print(";");
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(ExpressionStmt n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        n.getExpression().accept(this, arg);
        printer.print(";");
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(SwitchStmt n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        printer.print("switch(");
        n.getSelector().accept(this, arg);
        printer.printLn(") {");
        if (n.getEntries() != null) {
            printer.indent();
            for (SwitchEntryStmt e : n.getEntries()) {
                e.accept(this, arg);
            }
            printer.unindent();
        }
        printer.print("}");
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(SwitchEntryStmt n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        if (n.getLabel() != null) {
            printer.print("case ");
            n.getLabel().accept(this, arg);
            printer.print(":");
        } else {
            printer.print("default:");
        }
        printer.printLn();
        printer.indent();
        if (n.getStmts() != null) {
            for (Statement s : n.getStmts()) {
                s.accept(this, arg);
                printer.printLn();
            }
        }
        printer.unindent();
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(BreakStmt n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        printer.print("break");
        if (n.getId() != null) {
            printer.print(" ");
            printer.print(n.getId());
        }
        printer.print(";");
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(ReturnStmt n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        printer.print("return");
        if (n.getExpr() != null) {
            printer.print(" ");
            n.getExpr().accept(this, arg);
        }
        printer.print(";");
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(EnumDeclaration n, Object arg) {
        printJavadoc(n.getJavaDoc(), arg);
        printMemberAnnotations(n.getAnnotations(), arg);
        printBeginComments(n.getBeginComments(), arg);
        printModifiers(n.getModifiers());

        printer.print("enum ");
        printer.print(n.getName());

        if (n.getImplements() != null) {
            printer.print(" implements ");
            for (Iterator i = n.getImplements().iterator(); i.hasNext();) {
                ClassOrInterfaceType c = i.next();
                c.accept(this, arg);
                if (i.hasNext()) {
                    printer.print(", ");
                }
            }
        }

        printer.printLn(" {");
        printer.indent();
        if (n.getEntries() != null) {
            printer.printLn();
            for (Iterator i = n.getEntries().iterator(); i.hasNext();) {
                EnumConstantDeclaration e = i.next();
                e.accept(this, arg);
                if (i.hasNext()) {
                    printer.print(", ");
                }
            }
        }
        if (n.getMembers() != null) {
            printer.printLn(";");
            printMembers(n.getMembers(), arg);
        } else {
            if (n.getEntries() != null) {
                printer.printLn();
            }
        }
        printer.unindent();
        printer.print("}");
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(EnumConstantDeclaration n, Object arg) {
        printJavadoc(n.getJavaDoc(), arg);
        printMemberAnnotations(n.getAnnotations(), arg);
        printBeginComments(n.getBeginComments(), arg);
        printer.print(n.getName());

        if (n.getArgs() != null) {
            printArguments(n.getArgs(), arg);
        }

        if (n.getClassBody() != null) {
            printer.printLn(" {");
            printer.indent();
            printMembers(n.getClassBody(), arg);
            printer.unindent();
            printer.printLn("}");
        }
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(EmptyMemberDeclaration n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        printJavadoc(n.getJavaDoc(), arg);
        printer.print(";");
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(InitializerDeclaration n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        printJavadoc(n.getJavaDoc(), arg);
        if (n.isStatic()) {
            printer.print("static ");
        }
        n.getBlock().accept(this, arg);
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(IfStmt n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        printer.print("if (");
        n.getCondition().accept(this, arg);
        printer.print(") ");
        n.getThenStmt().accept(this, arg);
        if (n.getElseStmt() != null) {
            printer.print(" else ");
            n.getElseStmt().accept(this, arg);
        }
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(WhileStmt n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        printer.print("while (");
        n.getCondition().accept(this, arg);
        printer.print(") ");
        n.getBody().accept(this, arg);
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(ContinueStmt n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        printer.print("continue");
        if (n.getId() != null) {
            printer.print(" ");
            printer.print(n.getId());
        }
        printer.print(";");
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(DoStmt n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        printer.print("do ");
        n.getBody().accept(this, arg);
        printer.print(" while (");
        n.getCondition().accept(this, arg);
        printer.print(");");
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(ForeachStmt n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        printer.print("for (");
        n.getVariable().accept(this, arg);
        printer.print(" : ");
        n.getIterable().accept(this, arg);
        printer.print(") ");
        n.getBody().accept(this, arg);
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(ForStmt n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        printer.print("for (");
        if (n.getInit() != null) {
            for (Iterator i = n.getInit().iterator(); i.hasNext();) {
                Expression e = i.next();
                e.accept(this, arg);
                if (i.hasNext()) {
                    printer.print(", ");
                }
            }
        }
        printer.print("; ");
        if (n.getCompare() != null) {
            n.getCompare().accept(this, arg);
        }
        printer.print("; ");
        if (n.getUpdate() != null) {
            for (Iterator i = n.getUpdate().iterator(); i.hasNext();) {
                Expression e = i.next();
                e.accept(this, arg);
                if (i.hasNext()) {
                    printer.print(", ");
                }
            }
        }
        printer.print(") ");
        n.getBody().accept(this, arg);
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(ThrowStmt n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        printer.print("throw ");
        n.getExpr().accept(this, arg);
        printer.print(";");
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(SynchronizedStmt n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        printer.print("synchronized (");
        n.getExpr().accept(this, arg);
        printer.print(") ");
        n.getBlock().accept(this, arg);
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(TryStmt n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        printer.print("try ");

        if (n.getResources() != null && n.getResources().size() > 0) {
            printer.print("(");

            n.getResources().get(0).accept(this, arg);
            for (int i = 1; i < n.getResources().size(); i++) {
                printer.print("; ");
                n.getResources().get(i).accept(this, arg);
            }

            printer.print(")");
        }

        n.getTryBlock().accept(this, arg);
        if (n.getCatchs() != null) {
            for (CatchClause c : n.getCatchs()) {
                c.accept(this, arg);
            }
        }
        if (n.getFinallyBlock() != null) {
            printer.print(" finally ");
            n.getFinallyBlock().accept(this, arg);
        }
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(CatchClause n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        printer.print(" catch (");
        n.getExcept().accept(this, arg);
        printer.print(") ");
        n.getCatchBlock().accept(this, arg);
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(AnnotationDeclaration n, Object arg) {
        printJavadoc(n.getJavaDoc(), arg);
        printMemberAnnotations(n.getAnnotations(), arg);
        printBeginComments(n.getBeginComments(), arg);
        printModifiers(n.getModifiers());

        printer.print("@interface ");
        printer.print(n.getName());
        printer.printLn(" {");
        printer.indent();
        if (n.getMembers() != null) {
            printMembers(n.getMembers(), arg);
        }
        printer.unindent();
        printer.print("}");
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(AnnotationMemberDeclaration n, Object arg) {
        printJavadoc(n.getJavaDoc(), arg);
        printMemberAnnotations(n.getAnnotations(), arg);
        printBeginComments(n.getBeginComments(), arg);
        printModifiers(n.getModifiers());

        n.getType().accept(this, arg);
        printer.print(" ");
        printer.print(n.getName());
        printer.print("()");
        if (n.getDefaultValue() != null) {
            printer.print(" default ");
            n.getDefaultValue().accept(this, arg);
        }
        printer.print(";");
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(MarkerAnnotationExpr n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        printer.print("@");
        n.getName().accept(this, arg);
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(SingleMemberAnnotationExpr n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        printer.print("@");
        n.getName().accept(this, arg);
        printer.print("(");
        n.getMemberValue().accept(this, arg);
        printer.print(")");
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(NormalAnnotationExpr n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        printer.print("@");
        n.getName().accept(this, arg);
        printer.print("(");
        if (n.getPairs() != null) {
            for (Iterator i = n.getPairs().iterator(); i.hasNext();) {
                MemberValuePair m = i.next();
                m.accept(this, arg);
                if (i.hasNext()) {
                    printer.print(", ");
                }
            }
        }
        printer.print(")");
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(MemberValuePair n, Object arg) {
        printBeginComments(n.getBeginComments(), arg);
        printer.print(n.getName());
        printer.print(" = ");
        n.getValue().accept(this, arg);
        printEndComments(n.getEndComments(), arg);
    }

    public void visit(LineComment n, Object arg) {
        // No longer used
    }

    public void visit(BlockComment n, Object arg) {
        // No longer used
    }

    @Override
    public void visit(Comment n, Object arg) {
        // No longer used
    }

    private void printBeginComments(List beginComments, Object arg) {
        if (beginComments == null) {
            return;
        }

        for (Comment comment : beginComments) {
            printComment(comment, printer.getLevel(), CommentFormatter.CommentLocation.BEGINNING);
        }
    }

    private void printInternalComments(List internalComments, Object arg) {
        if (internalComments == null) {
            return;
        }

        for (Comment comment : internalComments) {
            printComment(comment, printer.getLevel(), CommentFormatter.CommentLocation.INTERNAL);
        }
    }

    private void printEndComments(List endComments, Object arg) {
        if (endComments == null) {
            return;
        }

        for (Comment comment : endComments) {
            printComment(comment, 0, CommentFormatter.CommentLocation.END);
        }
    }

    private void printComment(Comment comment, int indentLevel, CommentFormatter.CommentLocation commentLocation) {

        // Return if there's nothing to do
        if (comment == null || comment.getContent() == null) {
            return;
        }

        String commentAsString = commentFormatter.format(comment, indentLevel, commentLocation);

        // Comment ends with newline
        boolean endsWithNewline = (commentAsString.endsWith("\r\n") || commentAsString.endsWith("\n"));

        printer.printRaw(commentAsString, !endsWithNewline);
    }
}