langtools/src/share/classes/com/sun/tools/javac/processing/PrintingProcessor.java
author jjg
Fri, 29 Aug 2008 11:10:12 -0700
changeset 1206 3a05355982a9
parent 10 06bc494ca11e
child 1264 076a3cde30d5
permissions -rw-r--r--
6597471: unused imports in javax.tools.JavaCompiler 6597531: unused imports and unused private const. in com.sun.tools.javac.Server.java Reviewed-by: mcimadamore Contributed-by: davide.angelocola@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
 * have any questions.
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.javac.processing;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import javax.annotation.processing.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import javax.lang.model.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import javax.lang.model.element.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import static javax.lang.model.element.ElementKind.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import static javax.lang.model.element.NestingKind.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import javax.lang.model.type.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import javax.lang.model.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import java.io.PrintWriter;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import java.io.Writer;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import java.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 * A processor which prints out elements.  Used to implement the
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
 * -Xprint option; the included visitor class is used to implement
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 * Elements.printElements.
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
 * <p><b>This is NOT part of any API supported by Sun Microsystems.
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
 * If you write code that depends on this, you do so at your own risk.
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
 * This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
 * deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
@SupportedAnnotationTypes("*")
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
@SupportedSourceVersion(SourceVersion.RELEASE_6)
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
public class PrintingProcessor extends AbstractProcessor {
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
    PrintWriter writer;
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
    public PrintingProcessor() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
        super();
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
        writer = new PrintWriter(System.out);
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    public void setWriter(Writer w) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
        writer = new PrintWriter(w);
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
    @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
    public boolean process(Set<? extends TypeElement> tes,
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
                           RoundEnvironment renv) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
        for(Element element : renv.getRootElements()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
            print(element);
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
        // Just print the elements, nothing more to do.
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
    void print(Element element) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
        new PrintingElementVisitor(writer, processingEnv.getElementUtils()).
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
            visit(element).flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
     * Used for the -Xprint option and called by Elements.printElements
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
    public static class PrintingElementVisitor
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
        extends SimpleElementVisitor6<PrintingElementVisitor, Boolean> {
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
        int indentation; // Indentation level;
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
        final PrintWriter writer;
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
        final Elements elementUtils;
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
        public PrintingElementVisitor(Writer w, Elements elementUtils) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
            super();
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
            this.writer = new PrintWriter(w);
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
            this.elementUtils = elementUtils;
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
            indentation = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
        protected PrintingElementVisitor defaultAction(Element e, Boolean newLine) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
            if (newLine != null && newLine)
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
                writer.println();
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
            printDocComment(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
            printModifiers(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
            return this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
        public PrintingElementVisitor visitExecutable(ExecutableElement e, Boolean p) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
            ElementKind kind = e.getKind();
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
            if (kind != STATIC_INIT &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
                kind != INSTANCE_INIT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
                Element enclosing = e.getEnclosingElement();
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
                // Don't print out the constructor of an anonymous class
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
                if (kind == CONSTRUCTOR &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
                    enclosing != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
                    NestingKind.ANONYMOUS ==
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
                    // Use an anonymous class to determine anonymity!
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
                    (new SimpleElementVisitor6<NestingKind, Void>() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
                        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
                        public NestingKind visitType(TypeElement e, Void p) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
                            return e.getNestingKind();
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
                    }).visit(enclosing))
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
                    return this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
                defaultAction(e, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
                printFormalTypeParameters(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
                switch(kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
                    case CONSTRUCTOR:
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
                    // Print out simple name of the class
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
                    writer.print(e.getEnclosingElement().getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
                    case METHOD:
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
                    writer.print(e.getReturnType().toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
                    writer.print(" ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
                    writer.print(e.getSimpleName().toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
                writer.print("(");
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
                printParameters(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
                writer.print(")");
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
                AnnotationValue defaultValue = e.getDefaultValue();
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
                if (defaultValue != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
                    writer.print(" default " + defaultValue);
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
                printThrows(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
                writer.println(";");
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
            return this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
        public PrintingElementVisitor visitType(TypeElement e, Boolean p) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
            ElementKind kind = e.getKind();
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
            NestingKind nestingKind = e.getNestingKind();
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
            if (NestingKind.ANONYMOUS == nestingKind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
                // Print out an anonymous class in the style of a
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
                // class instance creation expression rather than a
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
                // class declaration.
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
                writer.print("new ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
                // If the anonymous class implements an interface
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
                // print that name, otherwise print the superclass.
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
                List<? extends TypeMirror> interfaces = e.getInterfaces();
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
                if (!interfaces.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
                    writer.print(interfaces.get(0));
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
                    writer.print(e.getSuperclass());
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
                writer.print("(");
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
                // Anonymous classes that implement an interface can't
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
                // have any constructor arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
                if (interfaces.isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
                    // Print out the parameter list from the sole
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
                    // constructor.  For now, don't try to elide any
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
                    // synthetic parameters by determining if the
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
                    // anonymous class is in a static context, etc.
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
                    List<? extends ExecutableElement> constructors =
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
                        ElementFilter.constructorsIn(e.getEnclosedElements());
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
                    if (!constructors.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
                        printParameters(constructors.get(0));
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
                writer.print(")");
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
                if (nestingKind == TOP_LEVEL) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
                    PackageElement pkg = elementUtils.getPackageOf(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
                    if (!pkg.isUnnamed())
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
                        writer.print("package " + pkg.getQualifiedName() + ";\n");
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
                defaultAction(e, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
                switch(kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
                case ANNOTATION_TYPE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
                    writer.print("@interface");
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
                default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
                    writer.print(kind.toString().toLowerCase());
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
                writer.print(" ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
                writer.print(e.getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
                printFormalTypeParameters(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
                // Print superclass information if informative
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
                if (kind == CLASS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
                    TypeMirror supertype = e.getSuperclass();
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
                    if (supertype.getKind() != TypeKind.NONE) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
                        TypeElement e2 = (TypeElement)
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
                            ((DeclaredType) supertype).asElement();
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
                        if (e2.getSuperclass().getKind() != TypeKind.NONE)
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
                            writer.print(" extends " + supertype);
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
                printInterfaces(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
            writer.println(" {");
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
            indentation++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
            if (kind == ENUM) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
                List<Element> enclosedElements =
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
                    new ArrayList<Element>(e.getEnclosedElements());
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
                List<Element> enumConstants = new ArrayList<Element>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
                for(Element element : enclosedElements) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
                    if (element.getKind() == ENUM_CONSTANT)
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
                        enumConstants.add(element);
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
                int i;
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
                for(i = 0; i < enumConstants.size()-1; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
                    this.visit(enumConstants.get(i), true);
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
                    writer.print(",");
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
                if (i >= 0 ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
                    this.visit(enumConstants.get(i), true);
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
                    writer.print(";");
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
                enclosedElements.removeAll(enumConstants);
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
                for(Element element : enclosedElements)
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
                    this.visit(element);
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
                for(Element element : e.getEnclosedElements())
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
                    this.visit(element);
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
            indentation--;
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
            indent();
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
            writer.println("}");
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
            return this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
        public PrintingElementVisitor visitVariable(VariableElement e, Boolean newLine) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
            ElementKind kind = e.getKind();
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
            defaultAction(e, newLine);
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
            if (kind == ENUM_CONSTANT)
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
                writer.print(e.getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
            else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
                writer.print(e.asType().toString() + " " + e.getSimpleName() );
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
                Object constantValue  = e.getConstantValue();
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
                if (constantValue != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
                    writer.print(" = ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
                    writer.print(elementUtils.getConstantExpression(constantValue));
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
                writer.println(";");
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
            return this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
        public PrintingElementVisitor visitTypeParameter(TypeParameterElement e, Boolean p) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
            writer.print(e.getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
            return this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
        // Should we do more here?
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
        public PrintingElementVisitor visitPackage(PackageElement e, Boolean p) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
            defaultAction(e, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
            if (!e.isUnnamed())
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
                writer.println("package " + e.getQualifiedName() + ";");
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
            else
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
                writer.println("// Unnamed package");
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
            return this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
        public void flush() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
            writer.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
        private void printDocComment(Element e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
            String docComment = elementUtils.getDocComment(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
            if (docComment != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
                // Break comment into lines
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
                java.util.StringTokenizer st = new StringTokenizer(docComment,
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
                                                                  "\n\r");
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
                indent();
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
                writer.println("/**");
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
                while(st.hasMoreTokens()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
                    indent();
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
                    writer.print(" *");
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
                    writer.println(st.nextToken());
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
                indent();
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
                writer.println(" */");
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
        private void printModifiers(Element e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
            ElementKind kind = e.getKind();
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
            if (kind == PARAMETER) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
                printAnnotationsInline(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
                printAnnotations(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
                indent();
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
            if (kind == ENUM_CONSTANT)
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
                return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
            Set<Modifier> modifiers = new LinkedHashSet<Modifier>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
            modifiers.addAll(e.getModifiers());
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
            switch (kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
            case ANNOTATION_TYPE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
            case INTERFACE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
                modifiers.remove(Modifier.ABSTRACT);
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
            case ENUM:
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
                modifiers.remove(Modifier.FINAL);
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
                modifiers.remove(Modifier.ABSTRACT);
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
            case METHOD:
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
            case FIELD:
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
                Element enclosingElement = e.getEnclosingElement();
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
                if (enclosingElement != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
                    enclosingElement.getKind().isInterface()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
                    modifiers.remove(Modifier.PUBLIC);
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
                    modifiers.remove(Modifier.ABSTRACT); // only for methods
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
                    modifiers.remove(Modifier.STATIC);   // only for fields
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
                    modifiers.remove(Modifier.FINAL);    // only for fields
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
            for(Modifier m: modifiers) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
                writer.print(m.toString() + " ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
        private void printFormalTypeParameters(ExecutableElement executable) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
            printFormalTypeParameters(executable.getTypeParameters(), true);
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
        private void printFormalTypeParameters(TypeElement type) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
            printFormalTypeParameters(type.getTypeParameters(), false);
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
        private void printFormalTypeParameters(List<? extends TypeParameterElement> typeParams,
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
                                               boolean pad) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
            if (typeParams.size() > 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
                writer.print("<");
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
                boolean first = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
                for(TypeParameterElement tpe: typeParams) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
                    if (!first)
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
                        writer.print(", ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
                    writer.print(tpe.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
                    first = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   386
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   387
06bc494ca11e Initial load
duke
parents:
diff changeset
   388
                writer.print(">");
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
                if (pad)
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
                    writer.print(" ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
        private void printAnnotationsInline(Element e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
            List<? extends AnnotationMirror> annots = e.getAnnotationMirrors();
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
            for(AnnotationMirror annotationMirror : annots) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
                writer.print(annotationMirror);
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
                writer.print(" ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
        private void printAnnotations(Element e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
            List<? extends AnnotationMirror> annots = e.getAnnotationMirrors();
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
            for(AnnotationMirror annotationMirror : annots) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
                indent();
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
                writer.println(annotationMirror);
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   408
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
        // TODO: Refactor
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
        private void printParameters(ExecutableElement e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
            List<? extends VariableElement> parameters = e.getParameters();
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
            int size = parameters.size();
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
            switch (size) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
            case 0:
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
            case 1:
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
                for(VariableElement parameter: parameters) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
                    printModifiers(parameter);
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
                    if (e.isVarArgs() ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
                        TypeMirror tm = parameter.asType();
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
                        if (tm.getKind() != TypeKind.ARRAY)
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
                            throw new AssertionError("Var-args parameter is not an array type: " + tm);
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
                        writer.print((ArrayType.class.cast(tm)).getComponentType() );
06bc494ca11e Initial load
duke
parents:
diff changeset
   428
                        writer.print("...");
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
                    } else
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
                        writer.print(parameter.asType());
06bc494ca11e Initial load
duke
parents:
diff changeset
   431
                    writer.print(" " + parameter.getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
            default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
                {
06bc494ca11e Initial load
duke
parents:
diff changeset
   437
                    int i = 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
                    for(VariableElement parameter: parameters) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
                        if (i == 2)
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
                            indentation++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
                        if (i > 1)
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
                            indent();
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
                        printModifiers(parameter);
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
06bc494ca11e Initial load
duke
parents:
diff changeset
   447
                        if (i == size && e.isVarArgs() ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
                            TypeMirror tm = parameter.asType();
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
                            if (tm.getKind() != TypeKind.ARRAY)
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
                                throw new AssertionError("Var-args parameter is not an array type: " + tm);
06bc494ca11e Initial load
duke
parents:
diff changeset
   451
                                    writer.print((ArrayType.class.cast(tm)).getComponentType() );
06bc494ca11e Initial load
duke
parents:
diff changeset
   452
06bc494ca11e Initial load
duke
parents:
diff changeset
   453
                            writer.print("...");
06bc494ca11e Initial load
duke
parents:
diff changeset
   454
                        } else
06bc494ca11e Initial load
duke
parents:
diff changeset
   455
                            writer.print(parameter.asType());
06bc494ca11e Initial load
duke
parents:
diff changeset
   456
                        writer.print(" " + parameter.getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
06bc494ca11e Initial load
duke
parents:
diff changeset
   458
                        if (i < size)
06bc494ca11e Initial load
duke
parents:
diff changeset
   459
                            writer.println(",");
06bc494ca11e Initial load
duke
parents:
diff changeset
   460
06bc494ca11e Initial load
duke
parents:
diff changeset
   461
                        i++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
                    if (parameters.size() >= 2)
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
                        indentation--;
06bc494ca11e Initial load
duke
parents:
diff changeset
   466
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   468
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   469
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   470
06bc494ca11e Initial load
duke
parents:
diff changeset
   471
        private void printInterfaces(TypeElement e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   472
            ElementKind kind = e.getKind();
06bc494ca11e Initial load
duke
parents:
diff changeset
   473
06bc494ca11e Initial load
duke
parents:
diff changeset
   474
            if(kind != ANNOTATION_TYPE) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   475
                List<? extends TypeMirror> interfaces = e.getInterfaces();
06bc494ca11e Initial load
duke
parents:
diff changeset
   476
                if (interfaces.size() > 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   477
                    writer.print((kind.isClass() ? " implements" : " extends"));
06bc494ca11e Initial load
duke
parents:
diff changeset
   478
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
                    boolean first = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   480
                    for(TypeMirror interf: interfaces) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   481
                        if (!first)
06bc494ca11e Initial load
duke
parents:
diff changeset
   482
                            writer.print(",");
06bc494ca11e Initial load
duke
parents:
diff changeset
   483
                        writer.print(" ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   484
                        writer.print(interf.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   485
                        first = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   486
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   487
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   488
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   489
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   490
06bc494ca11e Initial load
duke
parents:
diff changeset
   491
        private void printThrows(ExecutableElement e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   492
            List<? extends TypeMirror> thrownTypes = e.getThrownTypes();
06bc494ca11e Initial load
duke
parents:
diff changeset
   493
            final int size = thrownTypes.size();
06bc494ca11e Initial load
duke
parents:
diff changeset
   494
            if (size != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   495
                writer.print(" throws");
06bc494ca11e Initial load
duke
parents:
diff changeset
   496
06bc494ca11e Initial load
duke
parents:
diff changeset
   497
                int i = 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
                for(TypeMirror thrownType: thrownTypes) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   499
                    if (i == 1)
06bc494ca11e Initial load
duke
parents:
diff changeset
   500
                        writer.print(" ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   501
06bc494ca11e Initial load
duke
parents:
diff changeset
   502
                    if (i == 2)
06bc494ca11e Initial load
duke
parents:
diff changeset
   503
                        indentation++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   504
06bc494ca11e Initial load
duke
parents:
diff changeset
   505
                    if (i >= 2)
06bc494ca11e Initial load
duke
parents:
diff changeset
   506
                        indent();
06bc494ca11e Initial load
duke
parents:
diff changeset
   507
06bc494ca11e Initial load
duke
parents:
diff changeset
   508
                    writer.print(thrownType);
06bc494ca11e Initial load
duke
parents:
diff changeset
   509
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
                    if (i != size)
06bc494ca11e Initial load
duke
parents:
diff changeset
   511
                        writer.println(", ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   512
06bc494ca11e Initial load
duke
parents:
diff changeset
   513
                    i++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   514
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   515
06bc494ca11e Initial load
duke
parents:
diff changeset
   516
                if (size >= 2)
06bc494ca11e Initial load
duke
parents:
diff changeset
   517
                    indentation--;
06bc494ca11e Initial load
duke
parents:
diff changeset
   518
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   519
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   520
06bc494ca11e Initial load
duke
parents:
diff changeset
   521
        private static final String [] spaces = {
06bc494ca11e Initial load
duke
parents:
diff changeset
   522
            "",
06bc494ca11e Initial load
duke
parents:
diff changeset
   523
            "  ",
06bc494ca11e Initial load
duke
parents:
diff changeset
   524
            "    ",
06bc494ca11e Initial load
duke
parents:
diff changeset
   525
            "      ",
06bc494ca11e Initial load
duke
parents:
diff changeset
   526
            "        ",
06bc494ca11e Initial load
duke
parents:
diff changeset
   527
            "          ",
06bc494ca11e Initial load
duke
parents:
diff changeset
   528
            "            ",
06bc494ca11e Initial load
duke
parents:
diff changeset
   529
            "              ",
06bc494ca11e Initial load
duke
parents:
diff changeset
   530
            "                ",
06bc494ca11e Initial load
duke
parents:
diff changeset
   531
            "                  ",
06bc494ca11e Initial load
duke
parents:
diff changeset
   532
            "                    "
06bc494ca11e Initial load
duke
parents:
diff changeset
   533
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
   534
06bc494ca11e Initial load
duke
parents:
diff changeset
   535
        private void indent() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   536
            int indentation = this.indentation;
06bc494ca11e Initial load
duke
parents:
diff changeset
   537
            if (indentation < 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   538
                return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   539
            final int maxIndex = spaces.length - 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   540
06bc494ca11e Initial load
duke
parents:
diff changeset
   541
            while (indentation > maxIndex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   542
                writer.print(spaces[maxIndex]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   543
                indentation -= maxIndex;
06bc494ca11e Initial load
duke
parents:
diff changeset
   544
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   545
            writer.print(spaces[indentation]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   546
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   547
06bc494ca11e Initial load
duke
parents:
diff changeset
   548
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   549
}