langtools/src/share/classes/com/sun/tools/javac/processing/PrintingProcessor.java
author darcy
Thu, 17 Jul 2014 09:47:25 -0700
changeset 25690 b1dac768ab79
parent 22170 62da5257a0a7
permissions -rw-r--r--
8050430: Provided new utility visitors supporting SourceVersion.RELEASE_9 Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
22153
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents: 10192
diff changeset
     2
 * Copyright (c) 2005, 2013, 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.*;
22153
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents: 10192
diff changeset
    39
import com.sun.tools.javac.util.StringUtils;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
 * A processor which prints out elements.  Used to implement the
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 * -Xprint option; the included visitor class is used to implement
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 * Elements.printElements.
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5841
diff changeset
    46
 * <p><b>This is NOT part of any supported API.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
 * If you write code that depends on this, you do so at your own risk.
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
 * This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
 * deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
@SupportedAnnotationTypes("*")
22170
62da5257a0a7 8031360: Update langtools code base to use RELEASE_9
darcy
parents: 22163
diff changeset
    52
@SupportedSourceVersion(SourceVersion.RELEASE_9)
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
25690
b1dac768ab79 8050430: Provided new utility visitors supporting SourceVersion.RELEASE_9
darcy
parents: 22170
diff changeset
    86
        extends SimpleElementVisitor9<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:
22153
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents: 10192
diff changeset
   206
                    writer.print(StringUtils.toLowerCase(kind.toString()));
10
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) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22153
diff changeset
   230
                List<Element> enclosedElements = new ArrayList<>(e.getEnclosedElements());
5218
066372a81ca9 6937417: javac -Xprint returns IndexOutOfBoundsException
darcy
parents: 3763
diff changeset
   231
                // Handle any enum constants specially before other entities.
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22153
diff changeset
   232
                List<Element> enumConstants = new ArrayList<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
                for(Element element : enclosedElements) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
                    if (element.getKind() == ENUM_CONSTANT)
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
                        enumConstants.add(element);
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
                }
5218
066372a81ca9 6937417: javac -Xprint returns IndexOutOfBoundsException
darcy
parents: 3763
diff changeset
   237
                if (!enumConstants.isEmpty()) {
066372a81ca9 6937417: javac -Xprint returns IndexOutOfBoundsException
darcy
parents: 3763
diff changeset
   238
                    int i;
066372a81ca9 6937417: javac -Xprint returns IndexOutOfBoundsException
darcy
parents: 3763
diff changeset
   239
                    for(i = 0; i < enumConstants.size()-1; i++) {
066372a81ca9 6937417: javac -Xprint returns IndexOutOfBoundsException
darcy
parents: 3763
diff changeset
   240
                        this.visit(enumConstants.get(i), true);
066372a81ca9 6937417: javac -Xprint returns IndexOutOfBoundsException
darcy
parents: 3763
diff changeset
   241
                        writer.print(",");
066372a81ca9 6937417: javac -Xprint returns IndexOutOfBoundsException
darcy
parents: 3763
diff changeset
   242
                    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
                    this.visit(enumConstants.get(i), true);
5218
066372a81ca9 6937417: javac -Xprint returns IndexOutOfBoundsException
darcy
parents: 3763
diff changeset
   244
                    writer.println(";\n");
066372a81ca9 6937417: javac -Xprint returns IndexOutOfBoundsException
darcy
parents: 3763
diff changeset
   245
066372a81ca9 6937417: javac -Xprint returns IndexOutOfBoundsException
darcy
parents: 3763
diff changeset
   246
                    enclosedElements.removeAll(enumConstants);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
                for(Element element : enclosedElements)
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
                    this.visit(element);
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
                for(Element element : e.getEnclosedElements())
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
                    this.visit(element);
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
            indentation--;
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
            indent();
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
            writer.println("}");
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
            return this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
        public PrintingElementVisitor visitVariable(VariableElement e, Boolean newLine) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
            ElementKind kind = e.getKind();
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
            defaultAction(e, newLine);
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
            if (kind == ENUM_CONSTANT)
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
                writer.print(e.getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
            else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
                writer.print(e.asType().toString() + " " + e.getSimpleName() );
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
                Object constantValue  = e.getConstantValue();
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
                if (constantValue != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
                    writer.print(" = ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
                    writer.print(elementUtils.getConstantExpression(constantValue));
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
                writer.println(";");
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
            return this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
        public PrintingElementVisitor visitTypeParameter(TypeParameterElement e, Boolean p) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
            writer.print(e.getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
            return this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
        // Should we do more here?
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
        public PrintingElementVisitor visitPackage(PackageElement e, Boolean p) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
            defaultAction(e, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
            if (!e.isUnnamed())
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
                writer.println("package " + e.getQualifiedName() + ";");
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
            else
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
                writer.println("// Unnamed package");
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
            return this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
        public void flush() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
            writer.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
        private void printDocComment(Element e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
            String docComment = elementUtils.getDocComment(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
            if (docComment != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
                // Break comment into lines
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
                java.util.StringTokenizer st = new StringTokenizer(docComment,
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
                                                                  "\n\r");
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
                indent();
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
                writer.println("/**");
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
                while(st.hasMoreTokens()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
                    indent();
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
                    writer.print(" *");
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
                    writer.println(st.nextToken());
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
                indent();
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
                writer.println(" */");
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
        private void printModifiers(Element e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
            ElementKind kind = e.getKind();
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
            if (kind == PARAMETER) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
                printAnnotationsInline(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
                printAnnotations(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
                indent();
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
            if (kind == ENUM_CONSTANT)
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
                return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22153
diff changeset
   335
            Set<Modifier> modifiers = new LinkedHashSet<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
            modifiers.addAll(e.getModifiers());
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
            switch (kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
            case ANNOTATION_TYPE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
            case INTERFACE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
                modifiers.remove(Modifier.ABSTRACT);
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
            case ENUM:
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
                modifiers.remove(Modifier.FINAL);
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
                modifiers.remove(Modifier.ABSTRACT);
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
            case METHOD:
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
            case FIELD:
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
                Element enclosingElement = e.getEnclosingElement();
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
                if (enclosingElement != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
                    enclosingElement.getKind().isInterface()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
                    modifiers.remove(Modifier.PUBLIC);
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
                    modifiers.remove(Modifier.ABSTRACT); // only for methods
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
                    modifiers.remove(Modifier.STATIC);   // only for fields
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
                    modifiers.remove(Modifier.FINAL);    // only for fields
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
            for(Modifier m: modifiers) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
                writer.print(m.toString() + " ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
2088
6e2c8594b2e5 6460529: Provide mixin interfaces for getQualifiedName and getTypeParameters
darcy
parents: 1264
diff changeset
   368
        private void printFormalTypeParameters(Parameterizable e,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
                                               boolean pad) {
2088
6e2c8594b2e5 6460529: Provide mixin interfaces for getQualifiedName and getTypeParameters
darcy
parents: 1264
diff changeset
   370
            List<? extends TypeParameterElement> typeParams = e.getTypeParameters();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
            if (typeParams.size() > 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
                writer.print("<");
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
                boolean first = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
                for(TypeParameterElement tpe: typeParams) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
                    if (!first)
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
                        writer.print(", ");
3763
be9e8bdd7ead 6872011: Update printing processor to support JSR 308
darcy
parents: 2212
diff changeset
   378
                    printAnnotationsInline(tpe);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
                    writer.print(tpe.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
                    first = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
                writer.print(">");
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
                if (pad)
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
                    writer.print(" ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   386
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   387
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   388
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
        private void printAnnotationsInline(Element e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
            List<? extends AnnotationMirror> annots = e.getAnnotationMirrors();
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
            for(AnnotationMirror annotationMirror : annots) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
                writer.print(annotationMirror);
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
                writer.print(" ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
        private void printAnnotations(Element e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
            List<? extends AnnotationMirror> annots = e.getAnnotationMirrors();
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
            for(AnnotationMirror annotationMirror : annots) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
                indent();
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
                writer.println(annotationMirror);
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
        // TODO: Refactor
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
        private void printParameters(ExecutableElement e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
            List<? extends VariableElement> parameters = e.getParameters();
06bc494ca11e Initial load
duke
parents:
diff changeset
   408
            int size = parameters.size();
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
            switch (size) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
            case 0:
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
            case 1:
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
                for(VariableElement parameter: parameters) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
                    printModifiers(parameter);
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
                    if (e.isVarArgs() ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
                        TypeMirror tm = parameter.asType();
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
                        if (tm.getKind() != TypeKind.ARRAY)
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
                            throw new AssertionError("Var-args parameter is not an array type: " + tm);
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
                        writer.print((ArrayType.class.cast(tm)).getComponentType() );
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
                        writer.print("...");
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
                    } else
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
                        writer.print(parameter.asType());
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
                    writer.print(" " + parameter.getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   428
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
            default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   431
                {
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
                    int i = 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
                    for(VariableElement parameter: parameters) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
                        if (i == 2)
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
                            indentation++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
06bc494ca11e Initial load
duke
parents:
diff changeset
   437
                        if (i > 1)
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
                            indent();
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
                        printModifiers(parameter);
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
                        if (i == size && e.isVarArgs() ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
                            TypeMirror tm = parameter.asType();
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
                            if (tm.getKind() != TypeKind.ARRAY)
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
                                throw new AssertionError("Var-args parameter is not an array type: " + tm);
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
                                    writer.print((ArrayType.class.cast(tm)).getComponentType() );
06bc494ca11e Initial load
duke
parents:
diff changeset
   447
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
                            writer.print("...");
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
                        } else
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
                            writer.print(parameter.asType());
06bc494ca11e Initial load
duke
parents:
diff changeset
   451
                        writer.print(" " + parameter.getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   452
06bc494ca11e Initial load
duke
parents:
diff changeset
   453
                        if (i < size)
06bc494ca11e Initial load
duke
parents:
diff changeset
   454
                            writer.println(",");
06bc494ca11e Initial load
duke
parents:
diff changeset
   455
06bc494ca11e Initial load
duke
parents:
diff changeset
   456
                        i++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   458
06bc494ca11e Initial load
duke
parents:
diff changeset
   459
                    if (parameters.size() >= 2)
06bc494ca11e Initial load
duke
parents:
diff changeset
   460
                        indentation--;
06bc494ca11e Initial load
duke
parents:
diff changeset
   461
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
06bc494ca11e Initial load
duke
parents:
diff changeset
   466
        private void printInterfaces(TypeElement e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
            ElementKind kind = e.getKind();
06bc494ca11e Initial load
duke
parents:
diff changeset
   468
06bc494ca11e Initial load
duke
parents:
diff changeset
   469
            if(kind != ANNOTATION_TYPE) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   470
                List<? extends TypeMirror> interfaces = e.getInterfaces();
06bc494ca11e Initial load
duke
parents:
diff changeset
   471
                if (interfaces.size() > 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   472
                    writer.print((kind.isClass() ? " implements" : " extends"));
06bc494ca11e Initial load
duke
parents:
diff changeset
   473
06bc494ca11e Initial load
duke
parents:
diff changeset
   474
                    boolean first = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   475
                    for(TypeMirror interf: interfaces) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   476
                        if (!first)
06bc494ca11e Initial load
duke
parents:
diff changeset
   477
                            writer.print(",");
06bc494ca11e Initial load
duke
parents:
diff changeset
   478
                        writer.print(" ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
                        writer.print(interf.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   480
                        first = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   481
                    }
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
        private void printThrows(ExecutableElement e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   487
            List<? extends TypeMirror> thrownTypes = e.getThrownTypes();
06bc494ca11e Initial load
duke
parents:
diff changeset
   488
            final int size = thrownTypes.size();
06bc494ca11e Initial load
duke
parents:
diff changeset
   489
            if (size != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   490
                writer.print(" throws");
06bc494ca11e Initial load
duke
parents:
diff changeset
   491
06bc494ca11e Initial load
duke
parents:
diff changeset
   492
                int i = 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   493
                for(TypeMirror thrownType: thrownTypes) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   494
                    if (i == 1)
06bc494ca11e Initial load
duke
parents:
diff changeset
   495
                        writer.print(" ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   496
06bc494ca11e Initial load
duke
parents:
diff changeset
   497
                    if (i == 2)
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
                        indentation++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   499
06bc494ca11e Initial load
duke
parents:
diff changeset
   500
                    if (i >= 2)
06bc494ca11e Initial load
duke
parents:
diff changeset
   501
                        indent();
06bc494ca11e Initial load
duke
parents:
diff changeset
   502
06bc494ca11e Initial load
duke
parents:
diff changeset
   503
                    writer.print(thrownType);
06bc494ca11e Initial load
duke
parents:
diff changeset
   504
06bc494ca11e Initial load
duke
parents:
diff changeset
   505
                    if (i != size)
06bc494ca11e Initial load
duke
parents:
diff changeset
   506
                        writer.println(", ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   507
06bc494ca11e Initial load
duke
parents:
diff changeset
   508
                    i++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   509
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
06bc494ca11e Initial load
duke
parents:
diff changeset
   511
                if (size >= 2)
06bc494ca11e Initial load
duke
parents:
diff changeset
   512
                    indentation--;
06bc494ca11e Initial load
duke
parents:
diff changeset
   513
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   514
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   515
06bc494ca11e Initial load
duke
parents:
diff changeset
   516
        private static final String [] spaces = {
06bc494ca11e Initial load
duke
parents:
diff changeset
   517
            "",
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
        private void indent() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   531
            int indentation = this.indentation;
06bc494ca11e Initial load
duke
parents:
diff changeset
   532
            if (indentation < 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   533
                return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   534
            final int maxIndex = spaces.length - 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   535
06bc494ca11e Initial load
duke
parents:
diff changeset
   536
            while (indentation > maxIndex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   537
                writer.print(spaces[maxIndex]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   538
                indentation -= maxIndex;
06bc494ca11e Initial load
duke
parents:
diff changeset
   539
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   540
            writer.print(spaces[indentation]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   541
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   542
06bc494ca11e Initial load
duke
parents:
diff changeset
   543
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   544
}