langtools/src/share/classes/com/sun/tools/javac/processing/PrintingProcessor.java
author jjg
Thu, 10 Jun 2010 16:08:01 -0700
changeset 5847 1908176fd6e3
parent 5841 7a8448425bb7
child 7681 1f0819a3341f
permissions -rw-r--r--
6944312: Potential rebranding issues in openjdk/langtools repository sources Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5218
diff changeset
     2
 * Copyright (c) 2005, 2009, Oracle and/or its affiliates. All rights reserved.
10
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
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5218
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5218
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
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
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5218
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5218
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5218
diff changeset
    23
 * questions.
10
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
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5841
diff changeset
    45
 * <p><b>This is NOT part of any supported API.
10
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("*")
3763
be9e8bdd7ead 6872011: Update printing processor to support JSR 308
darcy
parents: 2212
diff changeset
    51
// TODO: Change to version 7 based visitors when available
be9e8bdd7ead 6872011: Update printing processor to support JSR 308
darcy
parents: 2212
diff changeset
    52
@SupportedSourceVersion(SourceVersion.RELEASE_7)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
public class PrintingProcessor extends AbstractProcessor {
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
    PrintWriter writer;
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
    public PrintingProcessor() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
        super();
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
        writer = new PrintWriter(System.out);
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
    public void setWriter(Writer w) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
        writer = new PrintWriter(w);
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
    @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
    public boolean process(Set<? extends TypeElement> tes,
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
                           RoundEnvironment renv) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
        for(Element element : renv.getRootElements()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
            print(element);
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
        // Just print the elements, nothing more to do.
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
    void print(Element element) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
        new PrintingElementVisitor(writer, processingEnv.getElementUtils()).
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
            visit(element).flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
     * Used for the -Xprint option and called by Elements.printElements
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    public static class PrintingElementVisitor
5841
7a8448425bb7 6933147: Provided new utility visitors supporting SourceVersion.RELEASE_7
darcy
parents: 5520
diff changeset
    86
        extends SimpleElementVisitor7<PrintingElementVisitor, Boolean> {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
        int indentation; // Indentation level;
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
        final PrintWriter writer;
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
        final Elements elementUtils;
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
        public PrintingElementVisitor(Writer w, Elements elementUtils) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
            super();
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
            this.writer = new PrintWriter(w);
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
            this.elementUtils = elementUtils;
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
            indentation = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
        protected PrintingElementVisitor defaultAction(Element e, Boolean newLine) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
            if (newLine != null && newLine)
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
                writer.println();
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
            printDocComment(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
            printModifiers(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
            return this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
        public PrintingElementVisitor visitExecutable(ExecutableElement e, Boolean p) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
            ElementKind kind = e.getKind();
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
            if (kind != STATIC_INIT &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
                kind != INSTANCE_INIT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
                Element enclosing = e.getEnclosingElement();
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
                // Don't print out the constructor of an anonymous class
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
                if (kind == CONSTRUCTOR &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
                    enclosing != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
                    NestingKind.ANONYMOUS ==
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
                    // Use an anonymous class to determine anonymity!
5841
7a8448425bb7 6933147: Provided new utility visitors supporting SourceVersion.RELEASE_7
darcy
parents: 5520
diff changeset
   120
                    (new SimpleElementVisitor7<NestingKind, Void>() {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
                        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
                        public NestingKind visitType(TypeElement e, Void p) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
                            return e.getNestingKind();
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
                    }).visit(enclosing))
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
                    return this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
                defaultAction(e, true);
2088
6e2c8594b2e5 6460529: Provide mixin interfaces for getQualifiedName and getTypeParameters
darcy
parents: 1264
diff changeset
   129
                printFormalTypeParameters(e, true);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
                switch(kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
                    case CONSTRUCTOR:
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
                    // Print out simple name of the class
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
                    writer.print(e.getEnclosingElement().getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
                    case METHOD:
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
                    writer.print(e.getReturnType().toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
                    writer.print(" ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
                    writer.print(e.getSimpleName().toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
                writer.print("(");
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
                printParameters(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
                writer.print(")");
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
                AnnotationValue defaultValue = e.getDefaultValue();
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
                if (defaultValue != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
                    writer.print(" default " + defaultValue);
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
                printThrows(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
                writer.println(";");
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
            return this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
        public PrintingElementVisitor visitType(TypeElement e, Boolean p) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
            ElementKind kind = e.getKind();
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
            NestingKind nestingKind = e.getNestingKind();
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
            if (NestingKind.ANONYMOUS == nestingKind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
                // Print out an anonymous class in the style of a
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
                // class instance creation expression rather than a
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
                // class declaration.
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
                writer.print("new ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
                // If the anonymous class implements an interface
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
                // print that name, otherwise print the superclass.
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
                List<? extends TypeMirror> interfaces = e.getInterfaces();
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
                if (!interfaces.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
                    writer.print(interfaces.get(0));
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
                    writer.print(e.getSuperclass());
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
                writer.print("(");
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
                // Anonymous classes that implement an interface can't
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
                // have any constructor arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
                if (interfaces.isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
                    // Print out the parameter list from the sole
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
                    // constructor.  For now, don't try to elide any
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
                    // synthetic parameters by determining if the
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
                    // anonymous class is in a static context, etc.
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
                    List<? extends ExecutableElement> constructors =
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
                        ElementFilter.constructorsIn(e.getEnclosedElements());
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
                    if (!constructors.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
                        printParameters(constructors.get(0));
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
                writer.print(")");
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
                if (nestingKind == TOP_LEVEL) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
                    PackageElement pkg = elementUtils.getPackageOf(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
                    if (!pkg.isUnnamed())
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
                        writer.print("package " + pkg.getQualifiedName() + ";\n");
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
                defaultAction(e, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
                switch(kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
                case ANNOTATION_TYPE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
                    writer.print("@interface");
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
                default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
                    writer.print(kind.toString().toLowerCase());
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
                writer.print(" ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
                writer.print(e.getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
2088
6e2c8594b2e5 6460529: Provide mixin interfaces for getQualifiedName and getTypeParameters
darcy
parents: 1264
diff changeset
   211
                printFormalTypeParameters(e, false);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
                // Print superclass information if informative
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
                if (kind == CLASS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
                    TypeMirror supertype = e.getSuperclass();
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
                    if (supertype.getKind() != TypeKind.NONE) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
                        TypeElement e2 = (TypeElement)
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
                            ((DeclaredType) supertype).asElement();
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
                        if (e2.getSuperclass().getKind() != TypeKind.NONE)
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
                            writer.print(" extends " + supertype);
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
                printInterfaces(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
            writer.println(" {");
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
            indentation++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
            if (kind == ENUM) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
                List<Element> enclosedElements =
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
                    new ArrayList<Element>(e.getEnclosedElements());
5218
066372a81ca9 6937417: javac -Xprint returns IndexOutOfBoundsException
darcy
parents: 3763
diff changeset
   232
                // Handle any enum constants specially before other entities.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
                List<Element> enumConstants = new ArrayList<Element>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
                for(Element element : enclosedElements) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
                    if (element.getKind() == ENUM_CONSTANT)
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
                        enumConstants.add(element);
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
                }
5218
066372a81ca9 6937417: javac -Xprint returns IndexOutOfBoundsException
darcy
parents: 3763
diff changeset
   238
                if (!enumConstants.isEmpty()) {
066372a81ca9 6937417: javac -Xprint returns IndexOutOfBoundsException
darcy
parents: 3763
diff changeset
   239
                    int i;
066372a81ca9 6937417: javac -Xprint returns IndexOutOfBoundsException
darcy
parents: 3763
diff changeset
   240
                    for(i = 0; i < enumConstants.size()-1; i++) {
066372a81ca9 6937417: javac -Xprint returns IndexOutOfBoundsException
darcy
parents: 3763
diff changeset
   241
                        this.visit(enumConstants.get(i), true);
066372a81ca9 6937417: javac -Xprint returns IndexOutOfBoundsException
darcy
parents: 3763
diff changeset
   242
                        writer.print(",");
066372a81ca9 6937417: javac -Xprint returns IndexOutOfBoundsException
darcy
parents: 3763
diff changeset
   243
                    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
                    this.visit(enumConstants.get(i), true);
5218
066372a81ca9 6937417: javac -Xprint returns IndexOutOfBoundsException
darcy
parents: 3763
diff changeset
   245
                    writer.println(";\n");
066372a81ca9 6937417: javac -Xprint returns IndexOutOfBoundsException
darcy
parents: 3763
diff changeset
   246
066372a81ca9 6937417: javac -Xprint returns IndexOutOfBoundsException
darcy
parents: 3763
diff changeset
   247
                    enclosedElements.removeAll(enumConstants);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
                for(Element element : enclosedElements)
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
                    this.visit(element);
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
                for(Element element : e.getEnclosedElements())
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
                    this.visit(element);
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
            indentation--;
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
            indent();
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
            writer.println("}");
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
            return this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
        public PrintingElementVisitor visitVariable(VariableElement e, Boolean newLine) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
            ElementKind kind = e.getKind();
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
            defaultAction(e, newLine);
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
            if (kind == ENUM_CONSTANT)
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
                writer.print(e.getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
            else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
                writer.print(e.asType().toString() + " " + e.getSimpleName() );
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
                Object constantValue  = e.getConstantValue();
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
                if (constantValue != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
                    writer.print(" = ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
                    writer.print(elementUtils.getConstantExpression(constantValue));
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
                writer.println(";");
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
            return this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
        public PrintingElementVisitor visitTypeParameter(TypeParameterElement e, Boolean p) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
            writer.print(e.getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
            return this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
        // Should we do more here?
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
        public PrintingElementVisitor visitPackage(PackageElement e, Boolean p) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
            defaultAction(e, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
            if (!e.isUnnamed())
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
                writer.println("package " + e.getQualifiedName() + ";");
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
            else
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
                writer.println("// Unnamed package");
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
            return this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
        public void flush() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
            writer.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
        private void printDocComment(Element e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
            String docComment = elementUtils.getDocComment(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
            if (docComment != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
                // Break comment into lines
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
                java.util.StringTokenizer st = new StringTokenizer(docComment,
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
                                                                  "\n\r");
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
                indent();
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
                writer.println("/**");
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
                while(st.hasMoreTokens()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
                    indent();
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
                    writer.print(" *");
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
                    writer.println(st.nextToken());
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
                indent();
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
                writer.println(" */");
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
        private void printModifiers(Element e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
            ElementKind kind = e.getKind();
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
            if (kind == PARAMETER) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
                printAnnotationsInline(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
                printAnnotations(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
                indent();
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
            if (kind == ENUM_CONSTANT)
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
                return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
            Set<Modifier> modifiers = new LinkedHashSet<Modifier>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
            modifiers.addAll(e.getModifiers());
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
            switch (kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
            case ANNOTATION_TYPE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
            case INTERFACE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
                modifiers.remove(Modifier.ABSTRACT);
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
            case ENUM:
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
                modifiers.remove(Modifier.FINAL);
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
                modifiers.remove(Modifier.ABSTRACT);
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
            case METHOD:
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
            case FIELD:
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
                Element enclosingElement = e.getEnclosingElement();
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
                if (enclosingElement != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
                    enclosingElement.getKind().isInterface()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
                    modifiers.remove(Modifier.PUBLIC);
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
                    modifiers.remove(Modifier.ABSTRACT); // only for methods
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
                    modifiers.remove(Modifier.STATIC);   // only for fields
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
                    modifiers.remove(Modifier.FINAL);    // only for fields
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
            for(Modifier m: modifiers) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
                writer.print(m.toString() + " ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
2088
6e2c8594b2e5 6460529: Provide mixin interfaces for getQualifiedName and getTypeParameters
darcy
parents: 1264
diff changeset
   369
        private void printFormalTypeParameters(Parameterizable e,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
                                               boolean pad) {
2088
6e2c8594b2e5 6460529: Provide mixin interfaces for getQualifiedName and getTypeParameters
darcy
parents: 1264
diff changeset
   371
            List<? extends TypeParameterElement> typeParams = e.getTypeParameters();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
            if (typeParams.size() > 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
                writer.print("<");
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
                boolean first = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
                for(TypeParameterElement tpe: typeParams) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
                    if (!first)
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
                        writer.print(", ");
3763
be9e8bdd7ead 6872011: Update printing processor to support JSR 308
darcy
parents: 2212
diff changeset
   379
                    printAnnotationsInline(tpe);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
                    writer.print(tpe.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
                    first = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
                writer.print(">");
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
                if (pad)
06bc494ca11e Initial load
duke
parents:
diff changeset
   386
                    writer.print(" ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   387
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   388
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
        private void printAnnotationsInline(Element e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
            List<? extends AnnotationMirror> annots = e.getAnnotationMirrors();
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
            for(AnnotationMirror annotationMirror : annots) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
                writer.print(annotationMirror);
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
                writer.print(" ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
        private void printAnnotations(Element e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
            List<? extends AnnotationMirror> annots = e.getAnnotationMirrors();
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
            for(AnnotationMirror annotationMirror : annots) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
                indent();
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
                writer.println(annotationMirror);
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
        // TODO: Refactor
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
        private void printParameters(ExecutableElement e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   408
            List<? extends VariableElement> parameters = e.getParameters();
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
            int size = parameters.size();
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
            switch (size) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
            case 0:
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
            case 1:
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
                for(VariableElement parameter: parameters) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
                    printModifiers(parameter);
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
                    if (e.isVarArgs() ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
                        TypeMirror tm = parameter.asType();
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
                        if (tm.getKind() != TypeKind.ARRAY)
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
                            throw new AssertionError("Var-args parameter is not an array type: " + tm);
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
                        writer.print((ArrayType.class.cast(tm)).getComponentType() );
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
                        writer.print("...");
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
                    } else
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
                        writer.print(parameter.asType());
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
                    writer.print(" " + parameter.getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   428
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
06bc494ca11e Initial load
duke
parents:
diff changeset
   431
            default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
                {
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
                    int i = 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
                    for(VariableElement parameter: parameters) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
                        if (i == 2)
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
                            indentation++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   437
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
                        if (i > 1)
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
                            indent();
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
                        printModifiers(parameter);
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
                        if (i == size && e.isVarArgs() ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
                            TypeMirror tm = parameter.asType();
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
                            if (tm.getKind() != TypeKind.ARRAY)
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
                                throw new AssertionError("Var-args parameter is not an array type: " + tm);
06bc494ca11e Initial load
duke
parents:
diff changeset
   447
                                    writer.print((ArrayType.class.cast(tm)).getComponentType() );
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
                            writer.print("...");
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
                        } else
06bc494ca11e Initial load
duke
parents:
diff changeset
   451
                            writer.print(parameter.asType());
06bc494ca11e Initial load
duke
parents:
diff changeset
   452
                        writer.print(" " + parameter.getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   453
06bc494ca11e Initial load
duke
parents:
diff changeset
   454
                        if (i < size)
06bc494ca11e Initial load
duke
parents:
diff changeset
   455
                            writer.println(",");
06bc494ca11e Initial load
duke
parents:
diff changeset
   456
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
                        i++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   458
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   459
06bc494ca11e Initial load
duke
parents:
diff changeset
   460
                    if (parameters.size() >= 2)
06bc494ca11e Initial load
duke
parents:
diff changeset
   461
                        indentation--;
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   466
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
        private void printInterfaces(TypeElement e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   468
            ElementKind kind = e.getKind();
06bc494ca11e Initial load
duke
parents:
diff changeset
   469
06bc494ca11e Initial load
duke
parents:
diff changeset
   470
            if(kind != ANNOTATION_TYPE) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   471
                List<? extends TypeMirror> interfaces = e.getInterfaces();
06bc494ca11e Initial load
duke
parents:
diff changeset
   472
                if (interfaces.size() > 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   473
                    writer.print((kind.isClass() ? " implements" : " extends"));
06bc494ca11e Initial load
duke
parents:
diff changeset
   474
06bc494ca11e Initial load
duke
parents:
diff changeset
   475
                    boolean first = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   476
                    for(TypeMirror interf: interfaces) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   477
                        if (!first)
06bc494ca11e Initial load
duke
parents:
diff changeset
   478
                            writer.print(",");
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
                        writer.print(" ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   480
                        writer.print(interf.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   481
                        first = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   482
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   483
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   484
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   485
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   486
06bc494ca11e Initial load
duke
parents:
diff changeset
   487
        private void printThrows(ExecutableElement e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   488
            List<? extends TypeMirror> thrownTypes = e.getThrownTypes();
06bc494ca11e Initial load
duke
parents:
diff changeset
   489
            final int size = thrownTypes.size();
06bc494ca11e Initial load
duke
parents:
diff changeset
   490
            if (size != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   491
                writer.print(" throws");
06bc494ca11e Initial load
duke
parents:
diff changeset
   492
06bc494ca11e Initial load
duke
parents:
diff changeset
   493
                int i = 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   494
                for(TypeMirror thrownType: thrownTypes) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   495
                    if (i == 1)
06bc494ca11e Initial load
duke
parents:
diff changeset
   496
                        writer.print(" ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   497
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
                    if (i == 2)
06bc494ca11e Initial load
duke
parents:
diff changeset
   499
                        indentation++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   500
06bc494ca11e Initial load
duke
parents:
diff changeset
   501
                    if (i >= 2)
06bc494ca11e Initial load
duke
parents:
diff changeset
   502
                        indent();
06bc494ca11e Initial load
duke
parents:
diff changeset
   503
06bc494ca11e Initial load
duke
parents:
diff changeset
   504
                    writer.print(thrownType);
06bc494ca11e Initial load
duke
parents:
diff changeset
   505
06bc494ca11e Initial load
duke
parents:
diff changeset
   506
                    if (i != size)
06bc494ca11e Initial load
duke
parents:
diff changeset
   507
                        writer.println(", ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   508
06bc494ca11e Initial load
duke
parents:
diff changeset
   509
                    i++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   511
06bc494ca11e Initial load
duke
parents:
diff changeset
   512
                if (size >= 2)
06bc494ca11e Initial load
duke
parents:
diff changeset
   513
                    indentation--;
06bc494ca11e Initial load
duke
parents:
diff changeset
   514
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   515
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   516
06bc494ca11e Initial load
duke
parents:
diff changeset
   517
        private static final String [] spaces = {
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
            "      ",
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
        private void indent() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   532
            int indentation = this.indentation;
06bc494ca11e Initial load
duke
parents:
diff changeset
   533
            if (indentation < 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   534
                return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   535
            final int maxIndex = spaces.length - 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   536
06bc494ca11e Initial load
duke
parents:
diff changeset
   537
            while (indentation > maxIndex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   538
                writer.print(spaces[maxIndex]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   539
                indentation -= maxIndex;
06bc494ca11e Initial load
duke
parents:
diff changeset
   540
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   541
            writer.print(spaces[indentation]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   542
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   543
06bc494ca11e Initial load
duke
parents:
diff changeset
   544
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   545
}