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