langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/PrintingProcessor.java
author darcy
Mon, 30 Jan 2017 15:08:38 -0800
changeset 43375 0945845b891f
parent 26266 2d24bda701dc
child 43376 f165c71d9e03
permissions -rw-r--r--
8173609: Elements.printElements needs to support modules Reviewed-by: forax, jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
43375
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
     2
 * Copyright (c) 2005, 2017, 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.*;
43375
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
    33
import static javax.lang.model.element.ModuleElement.DirectiveKind.*;
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
    34
import static javax.lang.model.element.ModuleElement.*;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import javax.lang.model.type.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import javax.lang.model.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import java.io.PrintWriter;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
import java.io.Writer;
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
import java.util.*;
43375
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
    41
import java.util.stream.Collectors;
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
    42
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
    43
import com.sun.tools.javac.util.DefinedBy;
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
    44
import com.sun.tools.javac.util.DefinedBy.Api;
22153
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents: 10192
diff changeset
    45
import com.sun.tools.javac.util.StringUtils;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
 * A processor which prints out elements.  Used to implement the
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
 * -Xprint option; the included visitor class is used to implement
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
 * Elements.printElements.
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5841
diff changeset
    52
 * <p><b>This is NOT part of any supported API.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
 * If you write code that depends on this, you do so at your own risk.
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
 * This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
 * deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
@SupportedAnnotationTypes("*")
22170
62da5257a0a7 8031360: Update langtools code base to use RELEASE_9
darcy
parents: 22163
diff changeset
    58
@SupportedSourceVersion(SourceVersion.RELEASE_9)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
public class PrintingProcessor extends AbstractProcessor {
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    PrintWriter writer;
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    public PrintingProcessor() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
        super();
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
        writer = new PrintWriter(System.out);
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
    public void setWriter(Writer w) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
        writer = new PrintWriter(w);
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
    71
    @Override @DefinedBy(Api.ANNOTATION_PROCESSING)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
    public boolean process(Set<? extends TypeElement> tes,
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
                           RoundEnvironment renv) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
        for(Element element : renv.getRootElements()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
            print(element);
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
        // Just print the elements, nothing more to do.
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
    void print(Element element) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
        new PrintingElementVisitor(writer, processingEnv.getElementUtils()).
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
            visit(element).flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
     * Used for the -Xprint option and called by Elements.printElements
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
    public static class PrintingElementVisitor
25690
b1dac768ab79 8050430: Provided new utility visitors supporting SourceVersion.RELEASE_9
darcy
parents: 22170
diff changeset
    92
        extends SimpleElementVisitor9<PrintingElementVisitor, Boolean> {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
        int indentation; // Indentation level;
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
        final PrintWriter writer;
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
        final Elements elementUtils;
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
        public PrintingElementVisitor(Writer w, Elements elementUtils) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
            super();
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
            this.writer = new PrintWriter(w);
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
            this.elementUtils = elementUtils;
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
            indentation = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   104
        @Override @DefinedBy(Api.LANGUAGE_MODEL)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
        protected PrintingElementVisitor defaultAction(Element e, Boolean newLine) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
            if (newLine != null && newLine)
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
                writer.println();
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
            printDocComment(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
            printModifiers(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
            return this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   113
        @Override @DefinedBy(Api.LANGUAGE_MODEL)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
        public PrintingElementVisitor visitExecutable(ExecutableElement e, Boolean p) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
            ElementKind kind = e.getKind();
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
            if (kind != STATIC_INIT &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
                kind != INSTANCE_INIT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
                Element enclosing = e.getEnclosingElement();
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
                // Don't print out the constructor of an anonymous class
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
                if (kind == CONSTRUCTOR &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
                    enclosing != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
                    NestingKind.ANONYMOUS ==
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
                    // Use an anonymous class to determine anonymity!
5841
7a8448425bb7 6933147: Provided new utility visitors supporting SourceVersion.RELEASE_7
darcy
parents: 5520
diff changeset
   126
                    (new SimpleElementVisitor7<NestingKind, Void>() {
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   127
                        @Override @DefinedBy(Api.LANGUAGE_MODEL)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
                        public NestingKind visitType(TypeElement e, Void p) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
                            return e.getNestingKind();
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
                    }).visit(enclosing))
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
                    return this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
                defaultAction(e, true);
2088
6e2c8594b2e5 6460529: Provide mixin interfaces for getQualifiedName and getTypeParameters
darcy
parents: 1264
diff changeset
   135
                printFormalTypeParameters(e, true);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
                switch(kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
                    case CONSTRUCTOR:
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
                    // Print out simple name of the class
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
                    writer.print(e.getEnclosingElement().getSimpleName());
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
                    case METHOD:
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
                    writer.print(e.getReturnType().toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
                    writer.print(" ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
                    writer.print(e.getSimpleName().toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
                writer.print("(");
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
                printParameters(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
                writer.print(")");
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
                AnnotationValue defaultValue = e.getDefaultValue();
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
                if (defaultValue != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
                    writer.print(" default " + defaultValue);
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
                printThrows(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
                writer.println(";");
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
            return this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   164
        @Override @DefinedBy(Api.LANGUAGE_MODEL)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
        public PrintingElementVisitor visitType(TypeElement e, Boolean p) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
            ElementKind kind = e.getKind();
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
            NestingKind nestingKind = e.getNestingKind();
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
            if (NestingKind.ANONYMOUS == nestingKind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
                // Print out an anonymous class in the style of a
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
                // class instance creation expression rather than a
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
                // class declaration.
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
                writer.print("new ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
                // If the anonymous class implements an interface
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
                // print that name, otherwise print the superclass.
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
                List<? extends TypeMirror> interfaces = e.getInterfaces();
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
                if (!interfaces.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
                    writer.print(interfaces.get(0));
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
                    writer.print(e.getSuperclass());
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
                writer.print("(");
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
                // Anonymous classes that implement an interface can't
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
                // have any constructor arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
                if (interfaces.isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
                    // Print out the parameter list from the sole
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
                    // constructor.  For now, don't try to elide any
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
                    // synthetic parameters by determining if the
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
                    // anonymous class is in a static context, etc.
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
                    List<? extends ExecutableElement> constructors =
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
                        ElementFilter.constructorsIn(e.getEnclosedElements());
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
                    if (!constructors.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
                        printParameters(constructors.get(0));
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
                writer.print(")");
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
                if (nestingKind == TOP_LEVEL) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
                    PackageElement pkg = elementUtils.getPackageOf(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
                    if (!pkg.isUnnamed())
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
                        writer.print("package " + pkg.getQualifiedName() + ";\n");
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
                defaultAction(e, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
                switch(kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
                case ANNOTATION_TYPE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
                    writer.print("@interface");
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
                default:
22153
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents: 10192
diff changeset
   212
                    writer.print(StringUtils.toLowerCase(kind.toString()));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
                writer.print(" ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
                writer.print(e.getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
2088
6e2c8594b2e5 6460529: Provide mixin interfaces for getQualifiedName and getTypeParameters
darcy
parents: 1264
diff changeset
   217
                printFormalTypeParameters(e, false);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
                // Print superclass information if informative
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
                if (kind == CLASS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
                    TypeMirror supertype = e.getSuperclass();
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
                    if (supertype.getKind() != TypeKind.NONE) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
                        TypeElement e2 = (TypeElement)
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
                            ((DeclaredType) supertype).asElement();
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
                        if (e2.getSuperclass().getKind() != TypeKind.NONE)
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
                            writer.print(" extends " + supertype);
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
                printInterfaces(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
            writer.println(" {");
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
            indentation++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
            if (kind == ENUM) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22153
diff changeset
   236
                List<Element> enclosedElements = new ArrayList<>(e.getEnclosedElements());
5218
066372a81ca9 6937417: javac -Xprint returns IndexOutOfBoundsException
darcy
parents: 3763
diff changeset
   237
                // Handle any enum constants specially before other entities.
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22153
diff changeset
   238
                List<Element> enumConstants = new ArrayList<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
                for(Element element : enclosedElements) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
                    if (element.getKind() == ENUM_CONSTANT)
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
                        enumConstants.add(element);
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
                }
5218
066372a81ca9 6937417: javac -Xprint returns IndexOutOfBoundsException
darcy
parents: 3763
diff changeset
   243
                if (!enumConstants.isEmpty()) {
066372a81ca9 6937417: javac -Xprint returns IndexOutOfBoundsException
darcy
parents: 3763
diff changeset
   244
                    int i;
066372a81ca9 6937417: javac -Xprint returns IndexOutOfBoundsException
darcy
parents: 3763
diff changeset
   245
                    for(i = 0; i < enumConstants.size()-1; i++) {
066372a81ca9 6937417: javac -Xprint returns IndexOutOfBoundsException
darcy
parents: 3763
diff changeset
   246
                        this.visit(enumConstants.get(i), true);
066372a81ca9 6937417: javac -Xprint returns IndexOutOfBoundsException
darcy
parents: 3763
diff changeset
   247
                        writer.print(",");
066372a81ca9 6937417: javac -Xprint returns IndexOutOfBoundsException
darcy
parents: 3763
diff changeset
   248
                    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
                    this.visit(enumConstants.get(i), true);
5218
066372a81ca9 6937417: javac -Xprint returns IndexOutOfBoundsException
darcy
parents: 3763
diff changeset
   250
                    writer.println(";\n");
066372a81ca9 6937417: javac -Xprint returns IndexOutOfBoundsException
darcy
parents: 3763
diff changeset
   251
066372a81ca9 6937417: javac -Xprint returns IndexOutOfBoundsException
darcy
parents: 3763
diff changeset
   252
                    enclosedElements.removeAll(enumConstants);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
                for(Element element : enclosedElements)
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
                    this.visit(element);
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
                for(Element element : e.getEnclosedElements())
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
                    this.visit(element);
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
            indentation--;
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
            indent();
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
            writer.println("}");
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
            return this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   268
        @Override @DefinedBy(Api.LANGUAGE_MODEL)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
        public PrintingElementVisitor visitVariable(VariableElement e, Boolean newLine) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
            ElementKind kind = e.getKind();
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
            defaultAction(e, newLine);
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
            if (kind == ENUM_CONSTANT)
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
                writer.print(e.getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
            else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
                writer.print(e.asType().toString() + " " + e.getSimpleName() );
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
                Object constantValue  = e.getConstantValue();
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
                if (constantValue != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
                    writer.print(" = ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
                    writer.print(elementUtils.getConstantExpression(constantValue));
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
                writer.println(";");
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
            }
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
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   287
        @Override @DefinedBy(Api.LANGUAGE_MODEL)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
        public PrintingElementVisitor visitTypeParameter(TypeParameterElement e, Boolean p) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
            writer.print(e.getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
            return this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
        // Should we do more here?
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   294
        @Override @DefinedBy(Api.LANGUAGE_MODEL)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
        public PrintingElementVisitor visitPackage(PackageElement e, Boolean p) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
            defaultAction(e, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
            if (!e.isUnnamed())
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
                writer.println("package " + e.getQualifiedName() + ";");
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
            else
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
                writer.println("// Unnamed package");
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
            return this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
43375
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   304
        @Override @DefinedBy(Api.LANGUAGE_MODEL)
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   305
        public PrintingElementVisitor visitModule(ModuleElement e, Boolean p) {
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   306
            defaultAction(e, false);
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   307
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   308
            if (!e.isUnnamed()) {
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   309
                // TODO: openness of the module not currently exposed
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   310
                // by the language model API, but should be printed
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   311
                // here once available.
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   312
                writer.println("module " + e.getQualifiedName() + " {");
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   313
                indentation++;
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   314
                for (ModuleElement.Directive directive : e.getDirectives()) {
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   315
                    printDirective(directive);
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   316
                }
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   317
                indentation--;
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   318
                writer.println("}");
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   319
            } else
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   320
                writer.println("// Unnamed module"); // Should we do more here?
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   321
            return this;
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   322
        }
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   323
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   324
        private void printDirective(ModuleElement.Directive directive) {
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   325
            indent();
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   326
            switch (directive.getKind()) {
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   327
            case EXPORTS: // "exports package-name [to module-name-list]"
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   328
                {
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   329
                    ExportsDirective exportsDirective = (ExportsDirective) directive;
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   330
                    writer.print("exports ");
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   331
                    writer.print(exportsDirective.getPackage().getQualifiedName());
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   332
                    printModuleList(exportsDirective.getTargetModules());
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   333
                }
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   334
                break;
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   335
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   336
            case OPENS: // opens package-name [to module-name-list]
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   337
                {
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   338
                    OpensDirective opensDirective = (OpensDirective) directive;
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   339
                    writer.print("opens ");
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   340
                    writer.print(opensDirective.getPackage().getQualifiedName());
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   341
                    printModuleList(opensDirective.getTargetModules());
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   342
                }
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   343
                break;
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   344
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   345
            case PROVIDES: // provides service-name with implementation-name
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   346
                {
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   347
                    ProvidesDirective providesDirective = (ProvidesDirective) directive;
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   348
                    writer.print("provides ");
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   349
                    writer.print(providesDirective.getService().getQualifiedName());
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   350
                    writer.print(" with ");
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   351
                    printNameableList(providesDirective.getImplementations());
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   352
                }
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   353
                break;
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   354
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   355
            case REQUIRES: // requires (static|transitive)* module-name
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   356
                {
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   357
                    RequiresDirective requiresDirective = (RequiresDirective) directive;
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   358
                    writer.print("requires ");
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   359
                    if (requiresDirective.isStatic())
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   360
                        writer.print("static ");
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   361
                    if (requiresDirective.isTransitive())
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   362
                        writer.print("transitive ");
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   363
                    writer.print(requiresDirective.getDependency().getQualifiedName());
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   364
                }
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   365
                break;
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   366
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   367
            case USES: // uses service-name
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   368
                {
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   369
                    UsesDirective usesDirective = (UsesDirective) directive;
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   370
                    writer.print("uses ");
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   371
                    writer.print(usesDirective.getService().getQualifiedName());
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   372
                }
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   373
                break;
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   374
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   375
            default:
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   376
                throw new UnsupportedOperationException("unknown directive " + directive);
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   377
            }
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   378
            writer.println(";");
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   379
        }
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   380
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   381
        private void printModuleList(List<? extends ModuleElement> modules) {
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   382
            if (modules != null) {
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   383
                writer.print(" to ");
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   384
                printNameableList(modules);
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   385
            }
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   386
        }
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   387
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   388
        private void printNameableList(List<? extends QualifiedNameable> nameables) {
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   389
                writer.print(nameables.stream().
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   390
                             map(QualifiedNameable::getQualifiedName).
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   391
                             collect(Collectors.joining(", ")));
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   392
        }
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   393
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
        public void flush() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
            writer.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
        private void printDocComment(Element e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
            String docComment = elementUtils.getDocComment(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
            if (docComment != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
                // Break comment into lines
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
                java.util.StringTokenizer st = new StringTokenizer(docComment,
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
                                                                  "\n\r");
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
                indent();
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
                writer.println("/**");
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
06bc494ca11e Initial load
duke
parents:
diff changeset
   408
                while(st.hasMoreTokens()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
                    indent();
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
                    writer.print(" *");
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
                    writer.println(st.nextToken());
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
                indent();
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
                writer.println(" */");
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
        private void printModifiers(Element e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
            ElementKind kind = e.getKind();
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
            if (kind == PARAMETER) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
                printAnnotationsInline(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
                printAnnotations(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
                indent();
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
06bc494ca11e Initial load
duke
parents:
diff changeset
   428
            if (kind == ENUM_CONSTANT)
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
                return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22153
diff changeset
   431
            Set<Modifier> modifiers = new LinkedHashSet<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
            modifiers.addAll(e.getModifiers());
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
            switch (kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
            case ANNOTATION_TYPE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
            case INTERFACE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   437
                modifiers.remove(Modifier.ABSTRACT);
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
            case ENUM:
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
                modifiers.remove(Modifier.FINAL);
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
                modifiers.remove(Modifier.ABSTRACT);
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
            case METHOD:
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
            case FIELD:
06bc494ca11e Initial load
duke
parents:
diff changeset
   447
                Element enclosingElement = e.getEnclosingElement();
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
                if (enclosingElement != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
                    enclosingElement.getKind().isInterface()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
                    modifiers.remove(Modifier.PUBLIC);
06bc494ca11e Initial load
duke
parents:
diff changeset
   451
                    modifiers.remove(Modifier.ABSTRACT); // only for methods
06bc494ca11e Initial load
duke
parents:
diff changeset
   452
                    modifiers.remove(Modifier.STATIC);   // only for fields
06bc494ca11e Initial load
duke
parents:
diff changeset
   453
                    modifiers.remove(Modifier.FINAL);    // only for fields
06bc494ca11e Initial load
duke
parents:
diff changeset
   454
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   455
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   456
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   458
06bc494ca11e Initial load
duke
parents:
diff changeset
   459
            for(Modifier m: modifiers) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   460
                writer.print(m.toString() + " ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   461
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
2088
6e2c8594b2e5 6460529: Provide mixin interfaces for getQualifiedName and getTypeParameters
darcy
parents: 1264
diff changeset
   464
        private void printFormalTypeParameters(Parameterizable e,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
                                               boolean pad) {
2088
6e2c8594b2e5 6460529: Provide mixin interfaces for getQualifiedName and getTypeParameters
darcy
parents: 1264
diff changeset
   466
            List<? extends TypeParameterElement> typeParams = e.getTypeParameters();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
            if (typeParams.size() > 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   468
                writer.print("<");
06bc494ca11e Initial load
duke
parents:
diff changeset
   469
06bc494ca11e Initial load
duke
parents:
diff changeset
   470
                boolean first = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   471
                for(TypeParameterElement tpe: typeParams) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   472
                    if (!first)
06bc494ca11e Initial load
duke
parents:
diff changeset
   473
                        writer.print(", ");
3763
be9e8bdd7ead 6872011: Update printing processor to support JSR 308
darcy
parents: 2212
diff changeset
   474
                    printAnnotationsInline(tpe);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   475
                    writer.print(tpe.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   476
                    first = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   477
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   478
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
                writer.print(">");
06bc494ca11e Initial load
duke
parents:
diff changeset
   480
                if (pad)
06bc494ca11e Initial load
duke
parents:
diff changeset
   481
                    writer.print(" ");
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
        private void printAnnotationsInline(Element e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   486
            List<? extends AnnotationMirror> annots = e.getAnnotationMirrors();
06bc494ca11e Initial load
duke
parents:
diff changeset
   487
            for(AnnotationMirror annotationMirror : annots) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   488
                writer.print(annotationMirror);
06bc494ca11e Initial load
duke
parents:
diff changeset
   489
                writer.print(" ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   490
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   491
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   492
06bc494ca11e Initial load
duke
parents:
diff changeset
   493
        private void printAnnotations(Element e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   494
            List<? extends AnnotationMirror> annots = e.getAnnotationMirrors();
06bc494ca11e Initial load
duke
parents:
diff changeset
   495
            for(AnnotationMirror annotationMirror : annots) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   496
                indent();
06bc494ca11e Initial load
duke
parents:
diff changeset
   497
                writer.println(annotationMirror);
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   499
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   500
06bc494ca11e Initial load
duke
parents:
diff changeset
   501
        // TODO: Refactor
06bc494ca11e Initial load
duke
parents:
diff changeset
   502
        private void printParameters(ExecutableElement e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   503
            List<? extends VariableElement> parameters = e.getParameters();
06bc494ca11e Initial load
duke
parents:
diff changeset
   504
            int size = parameters.size();
06bc494ca11e Initial load
duke
parents:
diff changeset
   505
06bc494ca11e Initial load
duke
parents:
diff changeset
   506
            switch (size) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   507
            case 0:
06bc494ca11e Initial load
duke
parents:
diff changeset
   508
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   509
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
            case 1:
06bc494ca11e Initial load
duke
parents:
diff changeset
   511
                for(VariableElement parameter: parameters) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   512
                    printModifiers(parameter);
06bc494ca11e Initial load
duke
parents:
diff changeset
   513
06bc494ca11e Initial load
duke
parents:
diff changeset
   514
                    if (e.isVarArgs() ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   515
                        TypeMirror tm = parameter.asType();
06bc494ca11e Initial load
duke
parents:
diff changeset
   516
                        if (tm.getKind() != TypeKind.ARRAY)
06bc494ca11e Initial load
duke
parents:
diff changeset
   517
                            throw new AssertionError("Var-args parameter is not an array type: " + tm);
06bc494ca11e Initial load
duke
parents:
diff changeset
   518
                        writer.print((ArrayType.class.cast(tm)).getComponentType() );
06bc494ca11e Initial load
duke
parents:
diff changeset
   519
                        writer.print("...");
06bc494ca11e Initial load
duke
parents:
diff changeset
   520
                    } else
06bc494ca11e Initial load
duke
parents:
diff changeset
   521
                        writer.print(parameter.asType());
06bc494ca11e Initial load
duke
parents:
diff changeset
   522
                    writer.print(" " + parameter.getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   523
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   524
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   525
06bc494ca11e Initial load
duke
parents:
diff changeset
   526
            default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   527
                {
06bc494ca11e Initial load
duke
parents:
diff changeset
   528
                    int i = 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   529
                    for(VariableElement parameter: parameters) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   530
                        if (i == 2)
06bc494ca11e Initial load
duke
parents:
diff changeset
   531
                            indentation++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   532
06bc494ca11e Initial load
duke
parents:
diff changeset
   533
                        if (i > 1)
06bc494ca11e Initial load
duke
parents:
diff changeset
   534
                            indent();
06bc494ca11e Initial load
duke
parents:
diff changeset
   535
06bc494ca11e Initial load
duke
parents:
diff changeset
   536
                        printModifiers(parameter);
06bc494ca11e Initial load
duke
parents:
diff changeset
   537
06bc494ca11e Initial load
duke
parents:
diff changeset
   538
                        if (i == size && e.isVarArgs() ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   539
                            TypeMirror tm = parameter.asType();
06bc494ca11e Initial load
duke
parents:
diff changeset
   540
                            if (tm.getKind() != TypeKind.ARRAY)
06bc494ca11e Initial load
duke
parents:
diff changeset
   541
                                throw new AssertionError("Var-args parameter is not an array type: " + tm);
06bc494ca11e Initial load
duke
parents:
diff changeset
   542
                                    writer.print((ArrayType.class.cast(tm)).getComponentType() );
06bc494ca11e Initial load
duke
parents:
diff changeset
   543
06bc494ca11e Initial load
duke
parents:
diff changeset
   544
                            writer.print("...");
06bc494ca11e Initial load
duke
parents:
diff changeset
   545
                        } else
06bc494ca11e Initial load
duke
parents:
diff changeset
   546
                            writer.print(parameter.asType());
06bc494ca11e Initial load
duke
parents:
diff changeset
   547
                        writer.print(" " + parameter.getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   548
06bc494ca11e Initial load
duke
parents:
diff changeset
   549
                        if (i < size)
06bc494ca11e Initial load
duke
parents:
diff changeset
   550
                            writer.println(",");
06bc494ca11e Initial load
duke
parents:
diff changeset
   551
06bc494ca11e Initial load
duke
parents:
diff changeset
   552
                        i++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   553
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   554
06bc494ca11e Initial load
duke
parents:
diff changeset
   555
                    if (parameters.size() >= 2)
06bc494ca11e Initial load
duke
parents:
diff changeset
   556
                        indentation--;
06bc494ca11e Initial load
duke
parents:
diff changeset
   557
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   558
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   559
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   560
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   561
06bc494ca11e Initial load
duke
parents:
diff changeset
   562
        private void printInterfaces(TypeElement e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   563
            ElementKind kind = e.getKind();
06bc494ca11e Initial load
duke
parents:
diff changeset
   564
06bc494ca11e Initial load
duke
parents:
diff changeset
   565
            if(kind != ANNOTATION_TYPE) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   566
                List<? extends TypeMirror> interfaces = e.getInterfaces();
06bc494ca11e Initial load
duke
parents:
diff changeset
   567
                if (interfaces.size() > 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   568
                    writer.print((kind.isClass() ? " implements" : " extends"));
06bc494ca11e Initial load
duke
parents:
diff changeset
   569
06bc494ca11e Initial load
duke
parents:
diff changeset
   570
                    boolean first = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   571
                    for(TypeMirror interf: interfaces) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   572
                        if (!first)
06bc494ca11e Initial load
duke
parents:
diff changeset
   573
                            writer.print(",");
06bc494ca11e Initial load
duke
parents:
diff changeset
   574
                        writer.print(" ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   575
                        writer.print(interf.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   576
                        first = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   577
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   578
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   579
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   580
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   581
06bc494ca11e Initial load
duke
parents:
diff changeset
   582
        private void printThrows(ExecutableElement e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   583
            List<? extends TypeMirror> thrownTypes = e.getThrownTypes();
06bc494ca11e Initial load
duke
parents:
diff changeset
   584
            final int size = thrownTypes.size();
06bc494ca11e Initial load
duke
parents:
diff changeset
   585
            if (size != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   586
                writer.print(" throws");
06bc494ca11e Initial load
duke
parents:
diff changeset
   587
06bc494ca11e Initial load
duke
parents:
diff changeset
   588
                int i = 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   589
                for(TypeMirror thrownType: thrownTypes) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   590
                    if (i == 1)
06bc494ca11e Initial load
duke
parents:
diff changeset
   591
                        writer.print(" ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   592
06bc494ca11e Initial load
duke
parents:
diff changeset
   593
                    if (i == 2)
06bc494ca11e Initial load
duke
parents:
diff changeset
   594
                        indentation++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   595
06bc494ca11e Initial load
duke
parents:
diff changeset
   596
                    if (i >= 2)
06bc494ca11e Initial load
duke
parents:
diff changeset
   597
                        indent();
06bc494ca11e Initial load
duke
parents:
diff changeset
   598
06bc494ca11e Initial load
duke
parents:
diff changeset
   599
                    writer.print(thrownType);
06bc494ca11e Initial load
duke
parents:
diff changeset
   600
06bc494ca11e Initial load
duke
parents:
diff changeset
   601
                    if (i != size)
06bc494ca11e Initial load
duke
parents:
diff changeset
   602
                        writer.println(", ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   603
06bc494ca11e Initial load
duke
parents:
diff changeset
   604
                    i++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   605
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   606
06bc494ca11e Initial load
duke
parents:
diff changeset
   607
                if (size >= 2)
06bc494ca11e Initial load
duke
parents:
diff changeset
   608
                    indentation--;
06bc494ca11e Initial load
duke
parents:
diff changeset
   609
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   610
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   611
06bc494ca11e Initial load
duke
parents:
diff changeset
   612
        private static final String [] spaces = {
06bc494ca11e Initial load
duke
parents:
diff changeset
   613
            "",
06bc494ca11e Initial load
duke
parents:
diff changeset
   614
            "  ",
06bc494ca11e Initial load
duke
parents:
diff changeset
   615
            "    ",
06bc494ca11e Initial load
duke
parents:
diff changeset
   616
            "      ",
06bc494ca11e Initial load
duke
parents:
diff changeset
   617
            "        ",
06bc494ca11e Initial load
duke
parents:
diff changeset
   618
            "          ",
06bc494ca11e Initial load
duke
parents:
diff changeset
   619
            "            ",
06bc494ca11e Initial load
duke
parents:
diff changeset
   620
            "              ",
06bc494ca11e Initial load
duke
parents:
diff changeset
   621
            "                ",
06bc494ca11e Initial load
duke
parents:
diff changeset
   622
            "                  ",
06bc494ca11e Initial load
duke
parents:
diff changeset
   623
            "                    "
06bc494ca11e Initial load
duke
parents:
diff changeset
   624
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
   625
06bc494ca11e Initial load
duke
parents:
diff changeset
   626
        private void indent() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   627
            int indentation = this.indentation;
06bc494ca11e Initial load
duke
parents:
diff changeset
   628
            if (indentation < 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   629
                return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   630
            final int maxIndex = spaces.length - 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   631
06bc494ca11e Initial load
duke
parents:
diff changeset
   632
            while (indentation > maxIndex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   633
                writer.print(spaces[maxIndex]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   634
                indentation -= maxIndex;
06bc494ca11e Initial load
duke
parents:
diff changeset
   635
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   636
            writer.print(spaces[indentation]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   637
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   638
06bc494ca11e Initial load
duke
parents:
diff changeset
   639
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   640
}