langtools/src/share/classes/com/sun/tools/javac/code/Printer.java
author jjg
Wed, 21 Aug 2013 16:13:50 -0700
changeset 19651 b1aa46cc2198
parent 18646 e628560a86d1
child 20249 93f8eae31092
permissions -rw-r--r--
8023515: import type-annotations updates Reviewed-by: jjg Contributed-by: wdietl@gmail.com
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
/*
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14359
diff changeset
     2
 * Copyright (c) 2009, 2013, 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;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14359
diff changeset
    31
import com.sun.tools.javac.code.Type.AnnotatedType;
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
    32
import com.sun.tools.javac.code.Type.ArrayType;
14258
8d2148961366 8000663: clean up langtools imports
jjg
parents: 14058
diff changeset
    33
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
    34
import com.sun.tools.javac.code.Type.*;
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    35
import com.sun.tools.javac.util.List;
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    36
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
    37
2221
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.BoundKind.*;
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
    39
import static com.sun.tools.javac.code.Flags.*;
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) {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   106
        ListBuffer<String> sbuf = ListBuffer.lb();
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) {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   121
        ListBuffer<String> sbuf = ListBuffer.lb();
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))
e15ff3a34054 6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents: 2221
diff changeset
   153
            return localize(locale, "compiler.misc.type.captureof.1",
e15ff3a34054 6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents: 2221
diff changeset
   154
                capturedVarId(t, locale));
e15ff3a34054 6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents: 2221
diff changeset
   155
        else {
e15ff3a34054 6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents: 2221
diff changeset
   156
            try {
e15ff3a34054 6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents: 2221
diff changeset
   157
                seenCaptured = seenCaptured.prepend(t);
e15ff3a34054 6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents: 2221
diff changeset
   158
                return localize(locale, "compiler.misc.type.captureof",
e15ff3a34054 6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents: 2221
diff changeset
   159
                    capturedVarId(t, locale),
e15ff3a34054 6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents: 2221
diff changeset
   160
                    visit(t.wildcard, locale));
e15ff3a34054 6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents: 2221
diff changeset
   161
            }
e15ff3a34054 6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents: 2221
diff changeset
   162
            finally {
e15ff3a34054 6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents: 2221
diff changeset
   163
                seenCaptured = seenCaptured.tail;
e15ff3a34054 6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents: 2221
diff changeset
   164
            }
e15ff3a34054 6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents: 2221
diff changeset
   165
        }
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   166
    }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   167
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   168
    @Override
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   169
    public String visitForAll(ForAll t, Locale locale) {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   170
        return "<" + visitTypes(t.tvars, locale) + ">" + visit(t.qtype, locale);
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   171
    }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   172
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   173
    @Override
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   174
    public String visitUndetVar(UndetVar t, Locale locale) {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   175
        if (t.inst != null) {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   176
            return visit(t.inst, locale);
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   177
        } else {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   178
            return visit(t.qtype, locale) + "?";
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   179
        }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   180
    }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   181
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   182
    @Override
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   183
    public String visitArrayType(ArrayType t, Locale locale) {
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   184
        StringBuilder res = new StringBuilder();
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   185
        printBaseElementType(t, res, locale);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   186
        printBrackets(t, res, locale);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   187
        return res.toString();
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   188
    }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   189
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   190
    void printBaseElementType(Type t, StringBuilder sb, Locale locale) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   191
        Type arrel = t;
19651
b1aa46cc2198 8023515: import type-annotations updates
jjg
parents: 18646
diff changeset
   192
        while (arrel.hasTag(TypeTag.ARRAY)) {
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   193
            arrel = arrel.unannotatedType();
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   194
            arrel = ((ArrayType) arrel).elemtype;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   195
        }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   196
        sb.append(visit(arrel, locale));
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   197
    }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   198
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   199
    void printBrackets(Type t, StringBuilder sb, Locale locale) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   200
        Type arrel = t;
19651
b1aa46cc2198 8023515: import type-annotations updates
jjg
parents: 18646
diff changeset
   201
        while (arrel.hasTag(TypeTag.ARRAY)) {
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   202
            if (arrel.isAnnotated()) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   203
                sb.append(' ');
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   204
                sb.append(arrel.getAnnotationMirrors());
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   205
                sb.append(' ');
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   206
            }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   207
            sb.append("[]");
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   208
            arrel = arrel.unannotatedType();
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   209
            arrel = ((ArrayType) arrel).elemtype;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   210
        }
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   211
    }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   212
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   213
    @Override
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   214
    public String visitClassType(ClassType t, Locale locale) {
14263
473b1eaede64 8000310: Clean up use of StringBuffer in langtools
jjg
parents: 14259
diff changeset
   215
        StringBuilder buf = new StringBuilder();
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 17578
diff changeset
   216
        if (t.getEnclosingType().hasTag(CLASS) && t.tsym.owner.kind == Kinds.TYP) {
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   217
            buf.append(visit(t.getEnclosingType(), locale));
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14359
diff changeset
   218
            buf.append('.');
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   219
            buf.append(className(t, false, locale));
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   220
        } else {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   221
            buf.append(className(t, true, locale));
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   222
        }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   223
        if (t.getTypeArguments().nonEmpty()) {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   224
            buf.append('<');
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   225
            buf.append(visitTypes(t.getTypeArguments(), locale));
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14359
diff changeset
   226
            buf.append('>');
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   227
        }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   228
        return buf.toString();
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   229
    }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   230
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   231
    @Override
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   232
    public String visitMethodType(MethodType t, Locale locale) {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   233
        return "(" + printMethodArgs(t.argtypes, false, locale) + ")" + visit(t.restype, locale);
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   234
    }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   235
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   236
    @Override
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   237
    public String visitPackageType(PackageType t, Locale locale) {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   238
        return t.tsym.getQualifiedName().toString();
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   239
    }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   240
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   241
    @Override
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   242
    public String visitWildcardType(WildcardType t, Locale locale) {
14263
473b1eaede64 8000310: Clean up use of StringBuffer in langtools
jjg
parents: 14259
diff changeset
   243
        StringBuilder s = new StringBuilder();
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   244
        s.append(t.kind);
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   245
        if (t.kind != UNBOUND) {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   246
            s.append(visit(t.type, locale));
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   247
        }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   248
        return s.toString();
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 visitErrorType(ErrorType t, Locale locale) {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   253
        return visitType(t, locale);
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 visitTypeVar(TypeVar t, Locale locale) {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   258
        return visitType(t, locale);
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   259
    }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   260
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14359
diff changeset
   261
    @Override
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14359
diff changeset
   262
    public String visitAnnotatedType(AnnotatedType t, Locale locale) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14359
diff changeset
   263
        if (t.typeAnnotations != null &&
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14359
diff changeset
   264
                t.typeAnnotations.nonEmpty()) {
19651
b1aa46cc2198 8023515: import type-annotations updates
jjg
parents: 18646
diff changeset
   265
            if (t.underlyingType.hasTag(TypeTag.ARRAY)) {
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   266
                StringBuilder res = new StringBuilder();
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   267
                printBaseElementType(t, res, locale);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   268
                printBrackets(t, res, locale);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   269
                return res.toString();
19651
b1aa46cc2198 8023515: import type-annotations updates
jjg
parents: 18646
diff changeset
   270
            } else if (t.underlyingType.hasTag(TypeTag.CLASS) &&
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   271
                    t.underlyingType.getEnclosingType() != Type.noType) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   272
                return visit(t.underlyingType.getEnclosingType(), locale) +
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   273
                        ". " +
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   274
                        t.typeAnnotations +
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   275
                        " " + className((ClassType)t.underlyingType, false, locale);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   276
            } else {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   277
                return t.typeAnnotations + " " + visit(t.underlyingType, locale);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   278
            }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14359
diff changeset
   279
        } else {
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   280
            return visit(t.underlyingType, locale);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14359
diff changeset
   281
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14359
diff changeset
   282
    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14359
diff changeset
   283
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   284
    public String visitType(Type t, Locale locale) {
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   285
        String s = (t.tsym == null || t.tsym.name == null)
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   286
                ? localize(locale, "compiler.misc.type.none")
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   287
                : t.tsym.name.toString();
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   288
        return s;
2221
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
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   291
    /**
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   292
     * Converts a class name into a (possibly localized) string. Anonymous
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16557
diff changeset
   293
     * 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
   294
     *
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   295
     * @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
   296
     * @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
   297
     * short name is chosen (w/o package)
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   298
     * @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
   299
     * @return localized string representation
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   300
     */
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   301
    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
   302
        Symbol sym = t.tsym;
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   303
        if (sym.name.length() == 0 && (sym.flags() & COMPOUND) != 0) {
14263
473b1eaede64 8000310: Clean up use of StringBuffer in langtools
jjg
parents: 14259
diff changeset
   304
            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
   305
            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
   306
                s.append('&');
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   307
                s.append(visit(is.head, locale));
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   308
            }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   309
            return s.toString();
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   310
        } else if (sym.name.length() == 0) {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   311
            String s;
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   312
            ClassType norm = (ClassType) t.tsym.type;
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   313
            if (norm == null) {
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", (Object) null);
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10626
diff changeset
   315
            } 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
   316
                s = localize(locale, "compiler.misc.anonymous.class",
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   317
                        visit(norm.interfaces_field.head, locale));
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   318
            } else {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   319
                s = localize(locale, "compiler.misc.anonymous.class",
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   320
                        visit(norm.supertype_field, locale));
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   321
            }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   322
            return s;
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   323
        } else if (longform) {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   324
            return sym.getQualifiedName().toString();
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   325
        } else {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   326
            return sym.name.toString();
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
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   330
    /**
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   331
     * 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
   332
     * localized string representation.
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   333
     *
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   334
     * @param args arguments to be rendered
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   335
     * @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
   336
     * @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
   337
     * @return localized string representation
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   338
     */
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   339
    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
   340
        if (!varArgs) {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   341
            return visitTypes(args, locale);
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   342
        } else {
14263
473b1eaede64 8000310: Clean up use of StringBuffer in langtools
jjg
parents: 14259
diff changeset
   343
            StringBuilder buf = new StringBuilder();
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   344
            while (args.tail.nonEmpty()) {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   345
                buf.append(visit(args.head, locale));
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   346
                args = args.tail;
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   347
                buf.append(',');
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   348
            }
19651
b1aa46cc2198 8023515: import type-annotations updates
jjg
parents: 18646
diff changeset
   349
            if (args.head.unannotatedType().hasTag(TypeTag.ARRAY)) {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14359
diff changeset
   350
                buf.append(visit(((ArrayType) args.head.unannotatedType()).elemtype, locale));
16557
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents: 15385
diff changeset
   351
                if (args.head.getAnnotationMirrors().nonEmpty()) {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14359
diff changeset
   352
                    buf.append(' ');
16557
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents: 15385
diff changeset
   353
                    buf.append(args.head.getAnnotationMirrors());
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14359
diff changeset
   354
                    buf.append(' ');
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14359
diff changeset
   355
                }
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   356
                buf.append("...");
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   357
            } else {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   358
                buf.append(visit(args.head, locale));
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
            return buf.toString();
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
    }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   363
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   364
    @Override
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   365
    public String visitClassSymbol(ClassSymbol sym, Locale locale) {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   366
        return sym.name.isEmpty()
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   367
                ? 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
   368
                : sym.fullname.toString();
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   369
    }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   370
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   371
    @Override
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   372
    public String visitMethodSymbol(MethodSymbol s, Locale locale) {
10626
83f0c2860f5b 7086595: Error message bug: name of initializer is 'null'
mcimadamore
parents: 8226
diff changeset
   373
        if (s.isStaticOrInstanceInit()) {
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   374
            return s.owner.name.toString();
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   375
        } else {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   376
            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
   377
                    ? s.owner.name.toString()
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   378
                    : s.name.toString();
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   379
            if (s.type != null) {
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 17578
diff changeset
   380
                if (s.type.hasTag(FORALL)) {
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   381
                    ms = "<" + visitTypes(s.type.getTypeArguments(), locale) + ">" + ms;
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   382
                }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   383
                ms += "(" + printMethodArgs(
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   384
                        s.type.getParameterTypes(),
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   385
                        (s.flags() & VARARGS) != 0,
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   386
                        locale) + ")";
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
            return ms;
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
    }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   391
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   392
    @Override
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   393
    public String visitOperatorSymbol(OperatorSymbol s, Locale locale) {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   394
        return visitMethodSymbol(s, locale);
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   395
    }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   396
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   397
    @Override
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   398
    public String visitPackageSymbol(PackageSymbol s, Locale locale) {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   399
        return s.isUnnamed()
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   400
                ? localize(locale, "compiler.misc.unnamed.package")
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   401
                : s.fullname.toString();
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   402
    }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   403
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   404
    @Override
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   405
    public String visitTypeSymbol(TypeSymbol s, Locale locale) {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   406
        return visitSymbol(s, locale);
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   407
    }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   408
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   409
    @Override
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   410
    public String visitVarSymbol(VarSymbol s, Locale locale) {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   411
        return visitSymbol(s, locale);
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   412
    }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   413
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   414
    @Override
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   415
    public String visitSymbol(Symbol s, Locale locale) {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   416
        return s.name.toString();
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   417
    }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
diff changeset
   418
}