src/jdk.compiler/share/classes/com/sun/tools/javac/processing/PrintingProcessor.java
author darcy
Wed, 25 Sep 2019 13:37:42 -0700
changeset 58344 d29f0181ba42
parent 55382 30b1b7b4dd86
permissions -rw-r--r--
8231443: Add java.io.Serial to list of platform annotations for annotation processing Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
55382
30b1b7b4dd86 8214546: Start of release updates for JDK 14
darcy
parents: 54602
diff changeset
     2
 * Copyright (c) 2005, 2019, 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("*")
55382
30b1b7b4dd86 8214546: Start of release updates for JDK 14
darcy
parents: 54602
diff changeset
    58
@SupportedSourceVersion(SourceVersion.RELEASE_14)
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!
50964
225b61293064 8173606: Deprecate constructors of 7-era visitors
darcy
parents: 50892
diff changeset
   126
                    (new SimpleElementVisitor9<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()) {
43376
f165c71d9e03 8172810: ModuleElement should declare and provide appropriate modifiers
jjg
parents: 43375
diff changeset
   309
                if (e.isOpen()) {
f165c71d9e03 8172810: ModuleElement should declare and provide appropriate modifiers
jjg
parents: 43375
diff changeset
   310
                    writer.print("open ");
f165c71d9e03 8172810: ModuleElement should declare and provide appropriate modifiers
jjg
parents: 43375
diff changeset
   311
                }
43375
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();
44191
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   326
            (new PrintDirective(writer)).visit(directive);
43375
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   327
            writer.println(";");
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
44191
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   330
        private static class PrintDirective implements ModuleElement.DirectiveVisitor<Void, Void> {
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   331
            private final PrintWriter writer;
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   332
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   333
            PrintDirective(PrintWriter writer) {
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   334
                this.writer = writer;
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   335
            }
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   336
44193
11902232a629 8176482: Use of DirectiveVisitor needs @DefinedBy annotation for RunCodingRules.java
darcy
parents: 44191
diff changeset
   337
            @Override @DefinedBy(Api.LANGUAGE_MODEL)
44191
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   338
            public Void visitExports(ExportsDirective d, Void p) {
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   339
                // "exports package-name [to module-name-list]"
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   340
                writer.print("exports ");
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   341
                writer.print(d.getPackage().getQualifiedName());
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   342
                printModuleList(d.getTargetModules());
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   343
                return null;
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   344
            }
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   345
44193
11902232a629 8176482: Use of DirectiveVisitor needs @DefinedBy annotation for RunCodingRules.java
darcy
parents: 44191
diff changeset
   346
            @Override @DefinedBy(Api.LANGUAGE_MODEL)
44191
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   347
            public Void visitOpens(OpensDirective d, Void p) {
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   348
                // opens package-name [to module-name-list]
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   349
                writer.print("opens ");
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   350
                writer.print(d.getPackage().getQualifiedName());
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   351
                printModuleList(d.getTargetModules());
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   352
                return null;
43375
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   353
            }
44191
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   354
44193
11902232a629 8176482: Use of DirectiveVisitor needs @DefinedBy annotation for RunCodingRules.java
darcy
parents: 44191
diff changeset
   355
            @Override @DefinedBy(Api.LANGUAGE_MODEL)
44191
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   356
            public Void visitProvides(ProvidesDirective d, Void p) {
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   357
                // provides service-name with implementation-name
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   358
                writer.print("provides ");
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   359
                writer.print(d.getService().getQualifiedName());
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   360
                writer.print(" with ");
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   361
                printNameableList(d.getImplementations());
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   362
                return null;
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   363
            }
43375
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   364
44193
11902232a629 8176482: Use of DirectiveVisitor needs @DefinedBy annotation for RunCodingRules.java
darcy
parents: 44191
diff changeset
   365
            @Override @DefinedBy(Api.LANGUAGE_MODEL)
44191
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   366
            public Void visitRequires(RequiresDirective d, Void p) {
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   367
                // requires (static|transitive)* module-name
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   368
                writer.print("requires ");
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   369
                if (d.isStatic())
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   370
                    writer.print("static ");
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   371
                if (d.isTransitive())
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   372
                    writer.print("transitive ");
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   373
                writer.print(d.getDependency().getQualifiedName());
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   374
                return null;
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   375
            }
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   376
44193
11902232a629 8176482: Use of DirectiveVisitor needs @DefinedBy annotation for RunCodingRules.java
darcy
parents: 44191
diff changeset
   377
            @Override @DefinedBy(Api.LANGUAGE_MODEL)
44191
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   378
            public Void visitUses(UsesDirective d, Void p) {
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   379
                // uses service-name
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   380
                writer.print("uses ");
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   381
                writer.print(d.getService().getQualifiedName());
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   382
                return null;
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   383
            }
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   384
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   385
            private void printModuleList(List<? extends ModuleElement> modules) {
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   386
                if (modules != null) {
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   387
                    writer.print(" to ");
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   388
                    printNameableList(modules);
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   389
                }
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   390
            }
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   391
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   392
            private void printNameableList(List<? extends QualifiedNameable> nameables) {
43375
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   393
                writer.print(nameables.stream().
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   394
                             map(QualifiedNameable::getQualifiedName).
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   395
                             collect(Collectors.joining(", ")));
44191
adc8e954fe5b 8176477: Use DirectiveVisitor to print module information
darcy
parents: 43376
diff changeset
   396
            }
43375
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   397
        }
0945845b891f 8173609: Elements.printElements needs to support modules
darcy
parents: 26266
diff changeset
   398
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
        public void flush() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
            writer.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
        private void printDocComment(Element e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
            String docComment = elementUtils.getDocComment(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
            if (docComment != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
                // Break comment into lines
06bc494ca11e Initial load
duke
parents:
diff changeset
   408
                java.util.StringTokenizer st = new StringTokenizer(docComment,
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
                                                                  "\n\r");
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
                indent();
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
                writer.println("/**");
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
                while(st.hasMoreTokens()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
                    indent();
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
                    writer.print(" *");
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
                    writer.println(st.nextToken());
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
                indent();
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
                writer.println(" */");
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
        private void printModifiers(Element e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
            ElementKind kind = e.getKind();
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
            if (kind == PARAMETER) {
54602
848a2f381e2c 8222817: Refactor printing processor to use streams
darcy
parents: 53023
diff changeset
   427
                // Print annotation inline
848a2f381e2c 8222817: Refactor printing processor to use streams
darcy
parents: 53023
diff changeset
   428
                writer.print(annotationsToString(e));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
                printAnnotations(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   431
                indent();
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
            if (kind == ENUM_CONSTANT)
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
                return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22153
diff changeset
   437
            Set<Modifier> modifiers = new LinkedHashSet<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
            modifiers.addAll(e.getModifiers());
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
            switch (kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
            case ANNOTATION_TYPE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
            case INTERFACE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
                modifiers.remove(Modifier.ABSTRACT);
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
            case ENUM:
06bc494ca11e Initial load
duke
parents:
diff changeset
   447
                modifiers.remove(Modifier.FINAL);
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
                modifiers.remove(Modifier.ABSTRACT);
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
06bc494ca11e Initial load
duke
parents:
diff changeset
   451
            case METHOD:
06bc494ca11e Initial load
duke
parents:
diff changeset
   452
            case FIELD:
06bc494ca11e Initial load
duke
parents:
diff changeset
   453
                Element enclosingElement = e.getEnclosingElement();
06bc494ca11e Initial load
duke
parents:
diff changeset
   454
                if (enclosingElement != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   455
                    enclosingElement.getKind().isInterface()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   456
                    modifiers.remove(Modifier.PUBLIC);
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
                    modifiers.remove(Modifier.ABSTRACT); // only for methods
06bc494ca11e Initial load
duke
parents:
diff changeset
   458
                    modifiers.remove(Modifier.STATIC);   // only for fields
06bc494ca11e Initial load
duke
parents:
diff changeset
   459
                    modifiers.remove(Modifier.FINAL);    // only for fields
06bc494ca11e Initial load
duke
parents:
diff changeset
   460
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   461
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
            }
54602
848a2f381e2c 8222817: Refactor printing processor to use streams
darcy
parents: 53023
diff changeset
   464
            if (!modifiers.isEmpty()) {
848a2f381e2c 8222817: Refactor printing processor to use streams
darcy
parents: 53023
diff changeset
   465
                writer.print(modifiers.stream()
848a2f381e2c 8222817: Refactor printing processor to use streams
darcy
parents: 53023
diff changeset
   466
                             .map(Modifier::toString)
848a2f381e2c 8222817: Refactor printing processor to use streams
darcy
parents: 53023
diff changeset
   467
                             .collect(Collectors.joining(" ", "", " ")));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   468
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   469
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   470
2088
6e2c8594b2e5 6460529: Provide mixin interfaces for getQualifiedName and getTypeParameters
darcy
parents: 1264
diff changeset
   471
        private void printFormalTypeParameters(Parameterizable e,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   472
                                               boolean pad) {
2088
6e2c8594b2e5 6460529: Provide mixin interfaces for getQualifiedName and getTypeParameters
darcy
parents: 1264
diff changeset
   473
            List<? extends TypeParameterElement> typeParams = e.getTypeParameters();
54602
848a2f381e2c 8222817: Refactor printing processor to use streams
darcy
parents: 53023
diff changeset
   474
            if (!typeParams.isEmpty()) {
848a2f381e2c 8222817: Refactor printing processor to use streams
darcy
parents: 53023
diff changeset
   475
                writer.print(typeParams.stream()
848a2f381e2c 8222817: Refactor printing processor to use streams
darcy
parents: 53023
diff changeset
   476
                             .map(tpe -> annotationsToString(tpe) + tpe.toString())
848a2f381e2c 8222817: Refactor printing processor to use streams
darcy
parents: 53023
diff changeset
   477
                             .collect(Collectors.joining(", ", "<", ">")));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   478
                if (pad)
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
                    writer.print(" ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   480
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   481
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   482
54602
848a2f381e2c 8222817: Refactor printing processor to use streams
darcy
parents: 53023
diff changeset
   483
        private String annotationsToString(Element e) {
848a2f381e2c 8222817: Refactor printing processor to use streams
darcy
parents: 53023
diff changeset
   484
            List<? extends AnnotationMirror> annotations = e.getAnnotationMirrors();
848a2f381e2c 8222817: Refactor printing processor to use streams
darcy
parents: 53023
diff changeset
   485
            return annotations.isEmpty() ?
848a2f381e2c 8222817: Refactor printing processor to use streams
darcy
parents: 53023
diff changeset
   486
                "" :
848a2f381e2c 8222817: Refactor printing processor to use streams
darcy
parents: 53023
diff changeset
   487
                annotations.stream()
848a2f381e2c 8222817: Refactor printing processor to use streams
darcy
parents: 53023
diff changeset
   488
                .map(AnnotationMirror::toString)
848a2f381e2c 8222817: Refactor printing processor to use streams
darcy
parents: 53023
diff changeset
   489
                .collect(Collectors.joining(" ", "", " "));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   490
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   491
06bc494ca11e Initial load
duke
parents:
diff changeset
   492
        private void printAnnotations(Element e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   493
            List<? extends AnnotationMirror> annots = e.getAnnotationMirrors();
06bc494ca11e Initial load
duke
parents:
diff changeset
   494
            for(AnnotationMirror annotationMirror : annots) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   495
                indent();
06bc494ca11e Initial load
duke
parents:
diff changeset
   496
                writer.println(annotationMirror);
06bc494ca11e Initial load
duke
parents:
diff changeset
   497
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   499
06bc494ca11e Initial load
duke
parents:
diff changeset
   500
        // TODO: Refactor
06bc494ca11e Initial load
duke
parents:
diff changeset
   501
        private void printParameters(ExecutableElement e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   502
            List<? extends VariableElement> parameters = e.getParameters();
06bc494ca11e Initial load
duke
parents:
diff changeset
   503
            int size = parameters.size();
06bc494ca11e Initial load
duke
parents:
diff changeset
   504
06bc494ca11e Initial load
duke
parents:
diff changeset
   505
            switch (size) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   506
            case 0:
06bc494ca11e Initial load
duke
parents:
diff changeset
   507
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   508
06bc494ca11e Initial load
duke
parents:
diff changeset
   509
            case 1:
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
                for(VariableElement parameter: parameters) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   511
                    printModifiers(parameter);
06bc494ca11e Initial load
duke
parents:
diff changeset
   512
06bc494ca11e Initial load
duke
parents:
diff changeset
   513
                    if (e.isVarArgs() ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   514
                        TypeMirror tm = parameter.asType();
06bc494ca11e Initial load
duke
parents:
diff changeset
   515
                        if (tm.getKind() != TypeKind.ARRAY)
06bc494ca11e Initial load
duke
parents:
diff changeset
   516
                            throw new AssertionError("Var-args parameter is not an array type: " + tm);
06bc494ca11e Initial load
duke
parents:
diff changeset
   517
                        writer.print((ArrayType.class.cast(tm)).getComponentType() );
06bc494ca11e Initial load
duke
parents:
diff changeset
   518
                        writer.print("...");
06bc494ca11e Initial load
duke
parents:
diff changeset
   519
                    } else
06bc494ca11e Initial load
duke
parents:
diff changeset
   520
                        writer.print(parameter.asType());
06bc494ca11e Initial load
duke
parents:
diff changeset
   521
                    writer.print(" " + parameter.getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   522
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   523
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   524
06bc494ca11e Initial load
duke
parents:
diff changeset
   525
            default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   526
                {
06bc494ca11e Initial load
duke
parents:
diff changeset
   527
                    int i = 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   528
                    for(VariableElement parameter: parameters) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   529
                        if (i == 2)
06bc494ca11e Initial load
duke
parents:
diff changeset
   530
                            indentation++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   531
06bc494ca11e Initial load
duke
parents:
diff changeset
   532
                        if (i > 1)
06bc494ca11e Initial load
duke
parents:
diff changeset
   533
                            indent();
06bc494ca11e Initial load
duke
parents:
diff changeset
   534
06bc494ca11e Initial load
duke
parents:
diff changeset
   535
                        printModifiers(parameter);
06bc494ca11e Initial load
duke
parents:
diff changeset
   536
06bc494ca11e Initial load
duke
parents:
diff changeset
   537
                        if (i == size && e.isVarArgs() ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   538
                            TypeMirror tm = parameter.asType();
06bc494ca11e Initial load
duke
parents:
diff changeset
   539
                            if (tm.getKind() != TypeKind.ARRAY)
06bc494ca11e Initial load
duke
parents:
diff changeset
   540
                                throw new AssertionError("Var-args parameter is not an array type: " + tm);
06bc494ca11e Initial load
duke
parents:
diff changeset
   541
                                    writer.print((ArrayType.class.cast(tm)).getComponentType() );
06bc494ca11e Initial load
duke
parents:
diff changeset
   542
06bc494ca11e Initial load
duke
parents:
diff changeset
   543
                            writer.print("...");
06bc494ca11e Initial load
duke
parents:
diff changeset
   544
                        } else
06bc494ca11e Initial load
duke
parents:
diff changeset
   545
                            writer.print(parameter.asType());
06bc494ca11e Initial load
duke
parents:
diff changeset
   546
                        writer.print(" " + parameter.getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   547
06bc494ca11e Initial load
duke
parents:
diff changeset
   548
                        if (i < size)
06bc494ca11e Initial load
duke
parents:
diff changeset
   549
                            writer.println(",");
06bc494ca11e Initial load
duke
parents:
diff changeset
   550
06bc494ca11e Initial load
duke
parents:
diff changeset
   551
                        i++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   552
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   553
06bc494ca11e Initial load
duke
parents:
diff changeset
   554
                    if (parameters.size() >= 2)
06bc494ca11e Initial load
duke
parents:
diff changeset
   555
                        indentation--;
06bc494ca11e Initial load
duke
parents:
diff changeset
   556
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   557
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   558
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   559
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   560
06bc494ca11e Initial load
duke
parents:
diff changeset
   561
        private void printInterfaces(TypeElement e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   562
            ElementKind kind = e.getKind();
06bc494ca11e Initial load
duke
parents:
diff changeset
   563
06bc494ca11e Initial load
duke
parents:
diff changeset
   564
            if(kind != ANNOTATION_TYPE) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   565
                List<? extends TypeMirror> interfaces = e.getInterfaces();
54602
848a2f381e2c 8222817: Refactor printing processor to use streams
darcy
parents: 53023
diff changeset
   566
                if (!interfaces.isEmpty()) {
848a2f381e2c 8222817: Refactor printing processor to use streams
darcy
parents: 53023
diff changeset
   567
                    writer.print((kind.isClass() ? " implements " : " extends "));
848a2f381e2c 8222817: Refactor printing processor to use streams
darcy
parents: 53023
diff changeset
   568
                    writer.print(interfaces.stream()
848a2f381e2c 8222817: Refactor printing processor to use streams
darcy
parents: 53023
diff changeset
   569
                                 .map(TypeMirror::toString)
848a2f381e2c 8222817: Refactor printing processor to use streams
darcy
parents: 53023
diff changeset
   570
                                 .collect(Collectors.joining(", ")));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   571
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   572
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   573
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   574
06bc494ca11e Initial load
duke
parents:
diff changeset
   575
        private void printThrows(ExecutableElement e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   576
            List<? extends TypeMirror> thrownTypes = e.getThrownTypes();
06bc494ca11e Initial load
duke
parents:
diff changeset
   577
            final int size = thrownTypes.size();
06bc494ca11e Initial load
duke
parents:
diff changeset
   578
            if (size != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   579
                writer.print(" throws");
06bc494ca11e Initial load
duke
parents:
diff changeset
   580
06bc494ca11e Initial load
duke
parents:
diff changeset
   581
                int i = 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   582
                for(TypeMirror thrownType: thrownTypes) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   583
                    if (i == 1)
06bc494ca11e Initial load
duke
parents:
diff changeset
   584
                        writer.print(" ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   585
06bc494ca11e Initial load
duke
parents:
diff changeset
   586
                    if (i == 2)
06bc494ca11e Initial load
duke
parents:
diff changeset
   587
                        indentation++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   588
06bc494ca11e Initial load
duke
parents:
diff changeset
   589
                    if (i >= 2)
06bc494ca11e Initial load
duke
parents:
diff changeset
   590
                        indent();
06bc494ca11e Initial load
duke
parents:
diff changeset
   591
06bc494ca11e Initial load
duke
parents:
diff changeset
   592
                    writer.print(thrownType);
06bc494ca11e Initial load
duke
parents:
diff changeset
   593
06bc494ca11e Initial load
duke
parents:
diff changeset
   594
                    if (i != size)
06bc494ca11e Initial load
duke
parents:
diff changeset
   595
                        writer.println(", ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   596
06bc494ca11e Initial load
duke
parents:
diff changeset
   597
                    i++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   598
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   599
06bc494ca11e Initial load
duke
parents:
diff changeset
   600
                if (size >= 2)
06bc494ca11e Initial load
duke
parents:
diff changeset
   601
                    indentation--;
06bc494ca11e Initial load
duke
parents:
diff changeset
   602
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   603
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   604
06bc494ca11e Initial load
duke
parents:
diff changeset
   605
        private static final String [] spaces = {
06bc494ca11e Initial load
duke
parents:
diff changeset
   606
            "",
06bc494ca11e Initial load
duke
parents:
diff changeset
   607
            "  ",
06bc494ca11e Initial load
duke
parents:
diff changeset
   608
            "    ",
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
            "            ",
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
        private void indent() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   620
            int indentation = this.indentation;
06bc494ca11e Initial load
duke
parents:
diff changeset
   621
            if (indentation < 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   622
                return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   623
            final int maxIndex = spaces.length - 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   624
06bc494ca11e Initial load
duke
parents:
diff changeset
   625
            while (indentation > maxIndex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   626
                writer.print(spaces[maxIndex]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   627
                indentation -= maxIndex;
06bc494ca11e Initial load
duke
parents:
diff changeset
   628
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   629
            writer.print(spaces[indentation]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   630
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   631
06bc494ca11e Initial load
duke
parents:
diff changeset
   632
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   633
}