langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Printer.java
author mcimadamore
Mon, 05 Dec 2016 19:00:56 +0000
changeset 42416 1cfad0990b99
parent 36526 3b41f1c69604
permissions -rw-r--r--
8170410: inference: javac doesn't implement 18.2.5 correctly Summary: javac does not generate constraints of the kind 'throws alpha' as described in the spec Reviewed-by: vromero, dlsmith
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
     1
/*
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33917
diff changeset
     2
 * Copyright (c) 2009, 2014, Oracle and/or its affiliates. All rights reserved.
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
     4
 *
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
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: 3380
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3380
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    10
 *
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    15
 * accompanied this code).
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    16
 *
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3380
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3380
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3380
diff changeset
    23
 * questions.
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    24
 */
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    25
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    26
package com.sun.tools.javac.code;
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    27
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    28
import java.util.Locale;
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    29
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    30
import com.sun.tools.javac.api.Messages;
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
    31
import com.sun.tools.javac.code.Type.ArrayType;
14258
8d2148961366 8000663: clean up langtools imports
jjg
parents: 14058
diff changeset
    32
import com.sun.tools.javac.code.Symbol.*;
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    33
import com.sun.tools.javac.code.Type.*;
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    34
import com.sun.tools.javac.util.List;
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    35
import com.sun.tools.javac.util.ListBuffer;
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14263
diff changeset
    36
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    37
import static com.sun.tools.javac.code.BoundKind.*;
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    38
import static com.sun.tools.javac.code.Flags.*;
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 25874
diff changeset
    39
import static com.sun.tools.javac.code.Kinds.Kind.*;
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14263
diff changeset
    40
import static com.sun.tools.javac.code.TypeTag.CLASS;
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14263
diff changeset
    41
import static com.sun.tools.javac.code.TypeTag.FORALL;
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    42
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    43
/**
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    44
 * A combined type/symbol visitor for generating non-trivial localized string
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    45
 * representation of types and symbols.
3380
a6c2bcab0fec 6865399: some javac files are missing Sun internal API comment
jjg
parents: 2984
diff changeset
    46
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    47
 * <p><b>This is NOT part of any supported API.
3380
a6c2bcab0fec 6865399: some javac files are missing Sun internal API comment
jjg
parents: 2984
diff changeset
    48
 * If you write code that depends on this, you do so at your own risk.
a6c2bcab0fec 6865399: some javac files are missing Sun internal API comment
jjg
parents: 2984
diff changeset
    49
 * This code and its internal interfaces are subject to change or
a6c2bcab0fec 6865399: some javac files are missing Sun internal API comment
jjg
parents: 2984
diff changeset
    50
 * deletion without notice.</b>
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    51
 */
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    52
public abstract class Printer implements Type.Visitor<String, Locale>, Symbol.Visitor<String, Locale> {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    53
2984
e15ff3a34054 6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents: 2221
diff changeset
    54
    List<Type> seenCaptured = List.nil();
e15ff3a34054 6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents: 2221
diff changeset
    55
    static final int PRIME = 997;  // largest prime less than 1000
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 10816
diff changeset
    56
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
    57
    protected Printer() { }
2984
e15ff3a34054 6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents: 2221
diff changeset
    58
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    59
    /**
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    60
     * This method should be overriden in order to provide proper i18n support.
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    61
     *
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    62
     * @param locale the locale in which the string is to be rendered
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    63
     * @param key the key corresponding to the message to be displayed
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    64
     * @param args a list of optional arguments
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    65
     * @return localized string representation
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    66
     */
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    67
    protected abstract String localize(Locale locale, String key, Object... args);
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    68
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    69
    /**
2984
e15ff3a34054 6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents: 2221
diff changeset
    70
     * Maps a captured type into an unique identifier.
e15ff3a34054 6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents: 2221
diff changeset
    71
     *
e15ff3a34054 6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents: 2221
diff changeset
    72
     * @param t the captured type for which an id is to be retrieved
e15ff3a34054 6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents: 2221
diff changeset
    73
     * @param locale locale settings
e15ff3a34054 6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents: 2221
diff changeset
    74
     * @return unique id representing this captured type
e15ff3a34054 6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents: 2221
diff changeset
    75
     */
e15ff3a34054 6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents: 2221
diff changeset
    76
    protected abstract String capturedVarId(CapturedType t, Locale locale);
e15ff3a34054 6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents: 2221
diff changeset
    77
e15ff3a34054 6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents: 2221
diff changeset
    78
    /**
e15ff3a34054 6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents: 2221
diff changeset
    79
     * Create a printer with default i18n support provided by Messages. By default,
e15ff3a34054 6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents: 2221
diff changeset
    80
     * captured types ids are generated using hashcode.
e15ff3a34054 6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents: 2221
diff changeset
    81
     *
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    82
     * @param messages Messages class to be used for i18n
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    83
     * @return printer visitor instance
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    84
     */
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    85
    public static Printer createStandardPrinter(final Messages messages) {
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
    86
        return new Printer() {
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    87
            @Override
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    88
            protected String localize(Locale locale, String key, Object... args) {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    89
                return messages.getLocalizedString(locale, key, args);
2984
e15ff3a34054 6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents: 2221
diff changeset
    90
            }
e15ff3a34054 6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents: 2221
diff changeset
    91
e15ff3a34054 6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents: 2221
diff changeset
    92
            @Override
e15ff3a34054 6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents: 2221
diff changeset
    93
            protected String capturedVarId(CapturedType t, Locale locale) {
e15ff3a34054 6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents: 2221
diff changeset
    94
                return (t.hashCode() & 0xFFFFFFFFL) % PRIME + "";
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    95
        }};
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    96
    }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    97
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    98
    /**
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    99
     * Get a localized string representation for all the types in the input list.
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   100
     *
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   101
     * @param ts types to be displayed
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   102
     * @param locale the locale in which the string is to be rendered
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   103
     * @return localized string representation
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   104
     */
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   105
    public String visitTypes(List<Type> ts, Locale locale) {
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19651
diff changeset
   106
        ListBuffer<String> sbuf = new ListBuffer<>();
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   107
        for (Type t : ts) {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   108
            sbuf.append(visit(t, locale));
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   109
        }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   110
        return sbuf.toList().toString();
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   111
    }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   112
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   113
    /**
8226
8c2fd7e7bcf3 7013272: Automatically generate info about how compiler resource keys are used
jjg
parents: 5847
diff changeset
   114
     * * Get a localized string representation for all the symbols in the input list.
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   115
     *
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   116
     * @param ts symbols to be displayed
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   117
     * @param locale the locale in which the string is to be rendered
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   118
     * @return localized string representation
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   119
     */
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   120
    public String visitSymbols(List<Symbol> ts, Locale locale) {
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19651
diff changeset
   121
        ListBuffer<String> sbuf = new ListBuffer<>();
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   122
        for (Symbol t : ts) {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   123
            sbuf.append(visit(t, locale));
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   124
        }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   125
        return sbuf.toList().toString();
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   126
    }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   127
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   128
    /**
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   129
     * Get a localized string representation for a given type.
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   130
     *
14259
fb94a1df0d53 8000208: fix langtools javadoc comment issues
jjg
parents: 14258
diff changeset
   131
     * @param t type to be displayed
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   132
     * @param locale the locale in which the string is to be rendered
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   133
     * @return localized string representation
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   134
     */
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   135
    public String visit(Type t, Locale locale) {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   136
        return t.accept(this, locale);
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   137
    }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   138
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   139
    /**
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   140
     * Get a localized string representation for a given symbol.
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   141
     *
14259
fb94a1df0d53 8000208: fix langtools javadoc comment issues
jjg
parents: 14258
diff changeset
   142
     * @param s symbol to be displayed
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   143
     * @param locale the locale in which the string is to be rendered
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   144
     * @return localized string representation
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   145
     */
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   146
    public String visit(Symbol s, Locale locale) {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   147
        return s.accept(this, locale);
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   148
    }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   149
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   150
    @Override
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   151
    public String visitCapturedType(CapturedType t, Locale locale) {
2984
e15ff3a34054 6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents: 2221
diff changeset
   152
        if (seenCaptured.contains(t))
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 21041
diff changeset
   153
            return printAnnotations(t) +
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 21041
diff changeset
   154
                localize(locale, "compiler.misc.type.captureof.1",
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33917
diff changeset
   155
                capturedVarId(t, locale));
2984
e15ff3a34054 6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents: 2221
diff changeset
   156
        else {
e15ff3a34054 6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents: 2221
diff changeset
   157
            try {
e15ff3a34054 6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents: 2221
diff changeset
   158
                seenCaptured = seenCaptured.prepend(t);
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 21041
diff changeset
   159
                return printAnnotations(t) +
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 21041
diff changeset
   160
                    localize(locale, "compiler.misc.type.captureof",
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33917
diff changeset
   161
                    capturedVarId(t, locale),
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33917
diff changeset
   162
                    visit(t.wildcard, locale));
2984
e15ff3a34054 6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents: 2221
diff changeset
   163
            }
e15ff3a34054 6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents: 2221
diff changeset
   164
            finally {
e15ff3a34054 6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents: 2221
diff changeset
   165
                seenCaptured = seenCaptured.tail;
e15ff3a34054 6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents: 2221
diff changeset
   166
            }
e15ff3a34054 6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents: 2221
diff changeset
   167
        }
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   168
    }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   169
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   170
    @Override
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   171
    public String visitForAll(ForAll t, Locale locale) {
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 21041
diff changeset
   172
        return printAnnotations(t) + "<" + visitTypes(t.tvars, locale) +
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 21041
diff changeset
   173
            ">" + visit(t.qtype, locale);
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   174
    }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   175
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   176
    @Override
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   177
    public String visitUndetVar(UndetVar t, Locale locale) {
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 27224
diff changeset
   178
        if (t.getInst() != null) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 27224
diff changeset
   179
            return printAnnotations(t) + visit(t.getInst(), locale);
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   180
        } else {
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 21041
diff changeset
   181
            return printAnnotations(t) + visit(t.qtype, locale) + "?";
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   182
        }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   183
    }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   184
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   185
    @Override
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   186
    public String visitArrayType(ArrayType t, Locale locale) {
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   187
        StringBuilder res = new StringBuilder();
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   188
        printBaseElementType(t, res, locale);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   189
        printBrackets(t, res, locale);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   190
        return res.toString();
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   191
    }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   192
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 21041
diff changeset
   193
    private String printAnnotations(Type t) {
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 21041
diff changeset
   194
        return printAnnotations(t, false);
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 21041
diff changeset
   195
    }
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 21041
diff changeset
   196
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 21041
diff changeset
   197
    private String printAnnotations(Type t, boolean prefix) {
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 21041
diff changeset
   198
        StringBuilder sb = new StringBuilder();
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 21041
diff changeset
   199
        List<Attribute.TypeCompound> annos = t.getAnnotationMirrors();
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 21041
diff changeset
   200
        if (!annos.isEmpty()) {
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 21041
diff changeset
   201
            if (prefix) sb.append(' ');
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 21041
diff changeset
   202
            sb.append(annos);
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 21041
diff changeset
   203
            sb.append(' ');
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 21041
diff changeset
   204
        }
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 21041
diff changeset
   205
        return sb.toString();
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 21041
diff changeset
   206
    }
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 21041
diff changeset
   207
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 21041
diff changeset
   208
    private void printBaseElementType(Type t, StringBuilder sb, Locale locale) {
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   209
        Type arrel = t;
19651
b1aa46cc2198 8023515: import type-annotations updates
jjg
parents: 18646
diff changeset
   210
        while (arrel.hasTag(TypeTag.ARRAY)) {
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   211
            arrel = ((ArrayType) arrel).elemtype;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   212
        }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   213
        sb.append(visit(arrel, locale));
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   214
    }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   215
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 21041
diff changeset
   216
    private void printBrackets(Type t, StringBuilder sb, Locale locale) {
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   217
        Type arrel = t;
19651
b1aa46cc2198 8023515: import type-annotations updates
jjg
parents: 18646
diff changeset
   218
        while (arrel.hasTag(TypeTag.ARRAY)) {
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 21041
diff changeset
   219
            sb.append(printAnnotations(arrel, true));
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   220
            sb.append("[]");
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   221
            arrel = ((ArrayType) arrel).elemtype;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   222
        }
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   223
    }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   224
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   225
    @Override
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   226
    public String visitClassType(ClassType t, Locale locale) {
14263
473b1eaede64 8000310: Clean up use of StringBuffer in langtools
jjg
parents: 14259
diff changeset
   227
        StringBuilder buf = new StringBuilder();
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 25874
diff changeset
   228
        if (t.getEnclosingType().hasTag(CLASS) && t.tsym.owner.kind == TYP) {
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   229
            buf.append(visit(t.getEnclosingType(), locale));
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14359
diff changeset
   230
            buf.append('.');
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 21041
diff changeset
   231
            buf.append(printAnnotations(t));
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   232
            buf.append(className(t, false, locale));
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   233
        } else {
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 21041
diff changeset
   234
            buf.append(printAnnotations(t));
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   235
            buf.append(className(t, true, locale));
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   236
        }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   237
        if (t.getTypeArguments().nonEmpty()) {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   238
            buf.append('<');
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   239
            buf.append(visitTypes(t.getTypeArguments(), locale));
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14359
diff changeset
   240
            buf.append('>');
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   241
        }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   242
        return buf.toString();
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   243
    }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   244
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   245
    @Override
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   246
    public String visitMethodType(MethodType t, Locale locale) {
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 21041
diff changeset
   247
        return "(" + printMethodArgs(t.argtypes, false, locale) + ")" +
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 21041
diff changeset
   248
            visit(t.restype, locale);
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   249
    }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   250
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   251
    @Override
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   252
    public String visitPackageType(PackageType t, Locale locale) {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   253
        return t.tsym.getQualifiedName().toString();
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   254
    }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   255
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   256
    @Override
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   257
    public String visitWildcardType(WildcardType t, Locale locale) {
14263
473b1eaede64 8000310: Clean up use of StringBuffer in langtools
jjg
parents: 14259
diff changeset
   258
        StringBuilder s = new StringBuilder();
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   259
        s.append(t.kind);
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   260
        if (t.kind != UNBOUND) {
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 21041
diff changeset
   261
            s.append(printAnnotations(t));
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   262
            s.append(visit(t.type, locale));
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   263
        }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   264
        return s.toString();
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   265
    }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   266
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   267
    @Override
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   268
    public String visitErrorType(ErrorType t, Locale locale) {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   269
        return visitType(t, locale);
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   270
    }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   271
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   272
    @Override
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   273
    public String visitTypeVar(TypeVar t, Locale locale) {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   274
        return visitType(t, locale);
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   275
    }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   276
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33917
diff changeset
   277
    @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33917
diff changeset
   278
    public String visitModuleType(ModuleType t, Locale locale) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33917
diff changeset
   279
        return visitType(t, locale);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33917
diff changeset
   280
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33917
diff changeset
   281
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   282
    public String visitType(Type t, Locale locale) {
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   283
        String s = (t.tsym == null || t.tsym.name == null)
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   284
                ? localize(locale, "compiler.misc.type.none")
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   285
                : t.tsym.name.toString();
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   286
        return s;
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   287
    }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   288
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   289
    /**
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   290
     * Converts a class name into a (possibly localized) string. Anonymous
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   291
     * inner classes get converted into a localized string.
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   292
     *
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   293
     * @param t the type of the class whose name is to be rendered
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   294
     * @param longform if set, the class' fullname is displayed - if unset the
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   295
     * short name is chosen (w/o package)
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   296
     * @param locale the locale in which the string is to be rendered
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   297
     * @return localized string representation
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   298
     */
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   299
    protected String className(ClassType t, boolean longform, Locale locale) {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   300
        Symbol sym = t.tsym;
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   301
        if (sym.name.length() == 0 && (sym.flags() & COMPOUND) != 0) {
14263
473b1eaede64 8000310: Clean up use of StringBuffer in langtools
jjg
parents: 14259
diff changeset
   302
            StringBuilder s = new StringBuilder(visit(t.supertype_field, locale));
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   303
            for (List<Type> is = t.interfaces_field; is.nonEmpty(); is = is.tail) {
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   304
                s.append('&');
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   305
                s.append(visit(is.head, locale));
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   306
            }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   307
            return s.toString();
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   308
        } else if (sym.name.length() == 0) {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   309
            String s;
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   310
            ClassType norm = (ClassType) t.tsym.type;
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   311
            if (norm == null) {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   312
                s = localize(locale, "compiler.misc.anonymous.class", (Object) null);
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10626
diff changeset
   313
            } else if (norm.interfaces_field != null && norm.interfaces_field.nonEmpty()) {
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   314
                s = localize(locale, "compiler.misc.anonymous.class",
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   315
                        visit(norm.interfaces_field.head, locale));
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   316
            } else {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   317
                s = localize(locale, "compiler.misc.anonymous.class",
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   318
                        visit(norm.supertype_field, locale));
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   319
            }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   320
            return s;
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   321
        } else if (longform) {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   322
            return sym.getQualifiedName().toString();
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   323
        } else {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   324
            return sym.name.toString();
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   325
        }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   326
    }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   327
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   328
    /**
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   329
     * Converts a set of method argument types into their corresponding
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   330
     * localized string representation.
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   331
     *
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   332
     * @param args arguments to be rendered
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   333
     * @param varArgs if true, the last method argument is regarded as a vararg
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   334
     * @param locale the locale in which the string is to be rendered
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   335
     * @return localized string representation
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   336
     */
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   337
    protected String printMethodArgs(List<Type> args, boolean varArgs, Locale locale) {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   338
        if (!varArgs) {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   339
            return visitTypes(args, locale);
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   340
        } else {
14263
473b1eaede64 8000310: Clean up use of StringBuffer in langtools
jjg
parents: 14259
diff changeset
   341
            StringBuilder buf = new StringBuilder();
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   342
            while (args.tail.nonEmpty()) {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   343
                buf.append(visit(args.head, locale));
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   344
                args = args.tail;
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   345
                buf.append(',');
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   346
            }
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 21041
diff changeset
   347
            if (args.head.hasTag(TypeTag.ARRAY)) {
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 21041
diff changeset
   348
                buf.append(visit(((ArrayType) args.head).elemtype, locale));
16557
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents: 15385
diff changeset
   349
                if (args.head.getAnnotationMirrors().nonEmpty()) {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14359
diff changeset
   350
                    buf.append(' ');
16557
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents: 15385
diff changeset
   351
                    buf.append(args.head.getAnnotationMirrors());
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14359
diff changeset
   352
                    buf.append(' ');
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14359
diff changeset
   353
                }
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   354
                buf.append("...");
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   355
            } else {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   356
                buf.append(visit(args.head, locale));
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   357
            }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   358
            return buf.toString();
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   359
        }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   360
    }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   361
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   362
    @Override
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   363
    public String visitClassSymbol(ClassSymbol sym, Locale locale) {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   364
        return sym.name.isEmpty()
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   365
                ? localize(locale, "compiler.misc.anonymous.class", sym.flatname)
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   366
                : sym.fullname.toString();
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   367
    }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   368
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   369
    @Override
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   370
    public String visitMethodSymbol(MethodSymbol s, Locale locale) {
10626
83f0c2860f5b 7086595: Error message bug: name of initializer is 'null'
mcimadamore
parents: 8226
diff changeset
   371
        if (s.isStaticOrInstanceInit()) {
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   372
            return s.owner.name.toString();
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   373
        } else {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   374
            String ms = (s.name == s.name.table.names.init)
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   375
                    ? s.owner.name.toString()
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   376
                    : s.name.toString();
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   377
            if (s.type != null) {
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 17578
diff changeset
   378
                if (s.type.hasTag(FORALL)) {
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   379
                    ms = "<" + visitTypes(s.type.getTypeArguments(), locale) + ">" + ms;
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   380
                }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   381
                ms += "(" + printMethodArgs(
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   382
                        s.type.getParameterTypes(),
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   383
                        (s.flags() & VARARGS) != 0,
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   384
                        locale) + ")";
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   385
            }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   386
            return ms;
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   387
        }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   388
    }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   389
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   390
    @Override
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   391
    public String visitOperatorSymbol(OperatorSymbol s, Locale locale) {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   392
        return visitMethodSymbol(s, locale);
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   393
    }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   394
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   395
    @Override
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   396
    public String visitPackageSymbol(PackageSymbol s, Locale locale) {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   397
        return s.isUnnamed()
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   398
                ? localize(locale, "compiler.misc.unnamed.package")
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   399
                : s.fullname.toString();
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   400
    }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   401
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   402
    @Override
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   403
    public String visitTypeSymbol(TypeSymbol s, Locale locale) {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   404
        return visitSymbol(s, locale);
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   405
    }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   406
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   407
    @Override
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   408
    public String visitVarSymbol(VarSymbol s, Locale locale) {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   409
        return visitSymbol(s, locale);
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   410
    }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   411
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   412
    @Override
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   413
    public String visitSymbol(Symbol s, Locale locale) {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   414
        return s.name.toString();
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   415
    }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   416
}