mcimadamore@238: /* jjg@1521: * Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved. mcimadamore@238: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. mcimadamore@238: * mcimadamore@238: * This code is free software; you can redistribute it and/or modify it mcimadamore@238: * under the terms of the GNU General Public License version 2 only, as ohair@554: * published by the Free Software Foundation. Oracle designates this mcimadamore@238: * particular file as subject to the "Classpath" exception as provided ohair@554: * by Oracle in the LICENSE file that accompanied this code. mcimadamore@238: * mcimadamore@238: * This code is distributed in the hope that it will be useful, but WITHOUT mcimadamore@238: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or mcimadamore@238: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License mcimadamore@238: * version 2 for more details (a copy is included in the LICENSE file that mcimadamore@238: * accompanied this code). mcimadamore@238: * mcimadamore@238: * You should have received a copy of the GNU General Public License version mcimadamore@238: * 2 along with this work; if not, write to the Free Software Foundation, mcimadamore@238: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. mcimadamore@238: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. mcimadamore@238: */ mcimadamore@238: mcimadamore@238: package com.sun.tools.javac.code; mcimadamore@238: mcimadamore@238: import java.util.Locale; mcimadamore@238: jjg@1521: import javax.lang.model.type.TypeKind; jjg@1521: mcimadamore@238: import com.sun.tools.javac.api.Messages; jjg@1521: import com.sun.tools.javac.code.Type.AnnotatedType; jjg@1357: import com.sun.tools.javac.code.Symbol.*; mcimadamore@238: import com.sun.tools.javac.code.Type.*; mcimadamore@238: import com.sun.tools.javac.util.List; mcimadamore@238: import com.sun.tools.javac.util.ListBuffer; jjg@1374: mcimadamore@238: import static com.sun.tools.javac.code.BoundKind.*; mcimadamore@238: import static com.sun.tools.javac.code.Flags.*; jjg@1374: import static com.sun.tools.javac.code.TypeTag.CLASS; jjg@1374: import static com.sun.tools.javac.code.TypeTag.FORALL; mcimadamore@238: mcimadamore@238: /** mcimadamore@238: * A combined type/symbol visitor for generating non-trivial localized string mcimadamore@238: * representation of types and symbols. jjg@333: * jjg@581: *

This is NOT part of any supported API. jjg@333: * If you write code that depends on this, you do so at your own risk. jjg@333: * This code and its internal interfaces are subject to change or jjg@333: * deletion without notice. mcimadamore@238: */ mcimadamore@238: public abstract class Printer implements Type.Visitor, Symbol.Visitor { mcimadamore@238: mcimadamore@288: List seenCaptured = List.nil(); mcimadamore@288: static final int PRIME = 997; // largest prime less than 1000 mcimadamore@1347: mcimadamore@1348: protected Printer() { } mcimadamore@288: mcimadamore@238: /** mcimadamore@238: * This method should be overriden in order to provide proper i18n support. mcimadamore@238: * mcimadamore@238: * @param locale the locale in which the string is to be rendered mcimadamore@238: * @param key the key corresponding to the message to be displayed mcimadamore@238: * @param args a list of optional arguments mcimadamore@238: * @return localized string representation mcimadamore@238: */ mcimadamore@238: protected abstract String localize(Locale locale, String key, Object... args); mcimadamore@238: mcimadamore@238: /** mcimadamore@288: * Maps a captured type into an unique identifier. mcimadamore@288: * mcimadamore@288: * @param t the captured type for which an id is to be retrieved mcimadamore@288: * @param locale locale settings mcimadamore@288: * @return unique id representing this captured type mcimadamore@288: */ mcimadamore@288: protected abstract String capturedVarId(CapturedType t, Locale locale); mcimadamore@288: mcimadamore@288: /** mcimadamore@288: * Create a printer with default i18n support provided by Messages. By default, mcimadamore@288: * captured types ids are generated using hashcode. mcimadamore@288: * mcimadamore@238: * @param messages Messages class to be used for i18n mcimadamore@238: * @return printer visitor instance mcimadamore@238: */ mcimadamore@238: public static Printer createStandardPrinter(final Messages messages) { mcimadamore@1348: return new Printer() { mcimadamore@238: @Override mcimadamore@238: protected String localize(Locale locale, String key, Object... args) { mcimadamore@238: return messages.getLocalizedString(locale, key, args); mcimadamore@288: } mcimadamore@288: mcimadamore@288: @Override mcimadamore@288: protected String capturedVarId(CapturedType t, Locale locale) { mcimadamore@288: return (t.hashCode() & 0xFFFFFFFFL) % PRIME + ""; mcimadamore@238: }}; mcimadamore@238: } mcimadamore@238: mcimadamore@238: /** mcimadamore@238: * Get a localized string representation for all the types in the input list. mcimadamore@238: * mcimadamore@238: * @param ts types to be displayed mcimadamore@238: * @param locale the locale in which the string is to be rendered mcimadamore@238: * @return localized string representation mcimadamore@238: */ mcimadamore@238: public String visitTypes(List ts, Locale locale) { mcimadamore@238: ListBuffer sbuf = ListBuffer.lb(); mcimadamore@238: for (Type t : ts) { mcimadamore@238: sbuf.append(visit(t, locale)); mcimadamore@238: } mcimadamore@238: return sbuf.toList().toString(); mcimadamore@238: } mcimadamore@238: mcimadamore@238: /** jjg@842: * * Get a localized string representation for all the symbols in the input list. mcimadamore@238: * mcimadamore@238: * @param ts symbols to be displayed mcimadamore@238: * @param locale the locale in which the string is to be rendered mcimadamore@238: * @return localized string representation mcimadamore@238: */ mcimadamore@238: public String visitSymbols(List ts, Locale locale) { mcimadamore@238: ListBuffer sbuf = ListBuffer.lb(); mcimadamore@238: for (Symbol t : ts) { mcimadamore@238: sbuf.append(visit(t, locale)); mcimadamore@238: } mcimadamore@238: return sbuf.toList().toString(); mcimadamore@238: } mcimadamore@238: mcimadamore@238: /** mcimadamore@238: * Get a localized string represenation for a given type. mcimadamore@238: * jjg@1358: * @param t type to be displayed mcimadamore@238: * @param locale the locale in which the string is to be rendered mcimadamore@238: * @return localized string representation mcimadamore@238: */ mcimadamore@238: public String visit(Type t, Locale locale) { mcimadamore@238: return t.accept(this, locale); mcimadamore@238: } mcimadamore@238: mcimadamore@238: /** mcimadamore@238: * Get a localized string represenation for a given symbol. mcimadamore@238: * jjg@1358: * @param s symbol to be displayed mcimadamore@238: * @param locale the locale in which the string is to be rendered mcimadamore@238: * @return localized string representation mcimadamore@238: */ mcimadamore@238: public String visit(Symbol s, Locale locale) { mcimadamore@238: return s.accept(this, locale); mcimadamore@238: } mcimadamore@238: mcimadamore@238: @Override mcimadamore@238: public String visitCapturedType(CapturedType t, Locale locale) { mcimadamore@288: if (seenCaptured.contains(t)) mcimadamore@288: return localize(locale, "compiler.misc.type.captureof.1", mcimadamore@288: capturedVarId(t, locale)); mcimadamore@288: else { mcimadamore@288: try { mcimadamore@288: seenCaptured = seenCaptured.prepend(t); mcimadamore@288: return localize(locale, "compiler.misc.type.captureof", mcimadamore@288: capturedVarId(t, locale), mcimadamore@288: visit(t.wildcard, locale)); mcimadamore@288: } mcimadamore@288: finally { mcimadamore@288: seenCaptured = seenCaptured.tail; mcimadamore@288: } mcimadamore@288: } mcimadamore@238: } mcimadamore@238: mcimadamore@238: @Override mcimadamore@238: public String visitForAll(ForAll t, Locale locale) { mcimadamore@238: return "<" + visitTypes(t.tvars, locale) + ">" + visit(t.qtype, locale); mcimadamore@238: } mcimadamore@238: mcimadamore@238: @Override mcimadamore@238: public String visitUndetVar(UndetVar t, Locale locale) { mcimadamore@238: if (t.inst != null) { mcimadamore@238: return visit(t.inst, locale); mcimadamore@238: } else { mcimadamore@238: return visit(t.qtype, locale) + "?"; mcimadamore@238: } mcimadamore@238: } mcimadamore@238: mcimadamore@238: @Override mcimadamore@238: public String visitArrayType(ArrayType t, Locale locale) { mcimadamore@238: return visit(t.elemtype, locale) + "[]"; mcimadamore@238: } mcimadamore@238: mcimadamore@238: @Override mcimadamore@238: public String visitClassType(ClassType t, Locale locale) { jjg@1362: StringBuilder buf = new StringBuilder(); mcimadamore@238: if (t.getEnclosingType().tag == CLASS && t.tsym.owner.kind == Kinds.TYP) { mcimadamore@238: buf.append(visit(t.getEnclosingType(), locale)); jjg@1521: buf.append('.'); mcimadamore@238: buf.append(className(t, false, locale)); mcimadamore@238: } else { mcimadamore@238: buf.append(className(t, true, locale)); mcimadamore@238: } mcimadamore@238: if (t.getTypeArguments().nonEmpty()) { mcimadamore@238: buf.append('<'); mcimadamore@238: buf.append(visitTypes(t.getTypeArguments(), locale)); jjg@1521: buf.append('>'); mcimadamore@238: } mcimadamore@238: return buf.toString(); mcimadamore@238: } mcimadamore@238: mcimadamore@238: @Override mcimadamore@238: public String visitMethodType(MethodType t, Locale locale) { mcimadamore@238: return "(" + printMethodArgs(t.argtypes, false, locale) + ")" + visit(t.restype, locale); mcimadamore@238: } mcimadamore@238: mcimadamore@238: @Override mcimadamore@238: public String visitPackageType(PackageType t, Locale locale) { mcimadamore@238: return t.tsym.getQualifiedName().toString(); mcimadamore@238: } mcimadamore@238: mcimadamore@238: @Override mcimadamore@238: public String visitWildcardType(WildcardType t, Locale locale) { jjg@1362: StringBuilder s = new StringBuilder(); mcimadamore@238: s.append(t.kind); mcimadamore@238: if (t.kind != UNBOUND) { mcimadamore@238: s.append(visit(t.type, locale)); mcimadamore@238: } mcimadamore@238: return s.toString(); mcimadamore@238: } mcimadamore@238: mcimadamore@238: @Override mcimadamore@238: public String visitErrorType(ErrorType t, Locale locale) { mcimadamore@238: return visitType(t, locale); mcimadamore@238: } mcimadamore@238: mcimadamore@238: @Override mcimadamore@238: public String visitTypeVar(TypeVar t, Locale locale) { mcimadamore@238: return visitType(t, locale); mcimadamore@238: } mcimadamore@238: jjg@1521: @Override jjg@1521: public String visitAnnotatedType(AnnotatedType t, Locale locale) { jjg@1521: if (t.typeAnnotations != null && jjg@1521: t.typeAnnotations.nonEmpty()) { jjg@1521: // TODO: better logic for arrays, ... jjg@1521: return "(" + t.typeAnnotations + " :: " + visit(t.underlyingType, locale) + ")"; jjg@1521: } else { jjg@1521: return "({} :: " + visit(t.underlyingType, locale) + ")"; jjg@1521: } jjg@1521: } jjg@1521: mcimadamore@238: public String visitType(Type t, Locale locale) { mcimadamore@1348: String s = (t.tsym == null || t.tsym.name == null) mcimadamore@1348: ? localize(locale, "compiler.misc.type.none") mcimadamore@1348: : t.tsym.name.toString(); mcimadamore@1348: return s; mcimadamore@238: } mcimadamore@238: mcimadamore@238: /** mcimadamore@238: * Converts a class name into a (possibly localized) string. Anonymous mcimadamore@238: * inner classes gets converted into a localized string. mcimadamore@238: * mcimadamore@238: * @param t the type of the class whose name is to be rendered mcimadamore@238: * @param longform if set, the class' fullname is displayed - if unset the mcimadamore@238: * short name is chosen (w/o package) mcimadamore@238: * @param locale the locale in which the string is to be rendered mcimadamore@238: * @return localized string representation mcimadamore@238: */ mcimadamore@238: protected String className(ClassType t, boolean longform, Locale locale) { mcimadamore@238: Symbol sym = t.tsym; mcimadamore@238: if (sym.name.length() == 0 && (sym.flags() & COMPOUND) != 0) { jjg@1362: StringBuilder s = new StringBuilder(visit(t.supertype_field, locale)); mcimadamore@238: for (List is = t.interfaces_field; is.nonEmpty(); is = is.tail) { mcimadamore@238: s.append("&"); mcimadamore@238: s.append(visit(is.head, locale)); mcimadamore@238: } mcimadamore@238: return s.toString(); mcimadamore@238: } else if (sym.name.length() == 0) { mcimadamore@238: String s; mcimadamore@238: ClassType norm = (ClassType) t.tsym.type; mcimadamore@238: if (norm == null) { mcimadamore@238: s = localize(locale, "compiler.misc.anonymous.class", (Object) null); mcimadamore@1114: } else if (norm.interfaces_field != null && norm.interfaces_field.nonEmpty()) { mcimadamore@238: s = localize(locale, "compiler.misc.anonymous.class", mcimadamore@238: visit(norm.interfaces_field.head, locale)); mcimadamore@238: } else { mcimadamore@238: s = localize(locale, "compiler.misc.anonymous.class", mcimadamore@238: visit(norm.supertype_field, locale)); mcimadamore@238: } mcimadamore@238: return s; mcimadamore@238: } else if (longform) { mcimadamore@238: return sym.getQualifiedName().toString(); mcimadamore@238: } else { mcimadamore@238: return sym.name.toString(); mcimadamore@238: } mcimadamore@238: } mcimadamore@238: mcimadamore@238: /** mcimadamore@238: * Converts a set of method argument types into their corresponding mcimadamore@238: * localized string representation. mcimadamore@238: * mcimadamore@238: * @param args arguments to be rendered mcimadamore@238: * @param varArgs if true, the last method argument is regarded as a vararg mcimadamore@238: * @param locale the locale in which the string is to be rendered mcimadamore@238: * @return localized string representation mcimadamore@238: */ mcimadamore@238: protected String printMethodArgs(List args, boolean varArgs, Locale locale) { mcimadamore@238: if (!varArgs) { mcimadamore@238: return visitTypes(args, locale); mcimadamore@238: } else { jjg@1362: StringBuilder buf = new StringBuilder(); mcimadamore@238: while (args.tail.nonEmpty()) { mcimadamore@238: buf.append(visit(args.head, locale)); mcimadamore@238: args = args.tail; mcimadamore@238: buf.append(','); mcimadamore@238: } jjg@1521: if (args.head.unannotatedType().getKind() == TypeKind.ARRAY) { jjg@1521: buf.append(visit(((ArrayType) args.head.unannotatedType()).elemtype, locale)); jjg@1521: if (args.head.getAnnotations().nonEmpty()) { jjg@1521: buf.append(' '); jjg@1521: buf.append(args.head.getAnnotations()); jjg@1521: buf.append(' '); jjg@1521: } mcimadamore@238: buf.append("..."); mcimadamore@238: } else { mcimadamore@238: buf.append(visit(args.head, locale)); mcimadamore@238: } mcimadamore@238: return buf.toString(); mcimadamore@238: } mcimadamore@238: } mcimadamore@238: mcimadamore@238: @Override mcimadamore@238: public String visitClassSymbol(ClassSymbol sym, Locale locale) { mcimadamore@238: return sym.name.isEmpty() mcimadamore@238: ? localize(locale, "compiler.misc.anonymous.class", sym.flatname) mcimadamore@238: : sym.fullname.toString(); mcimadamore@238: } mcimadamore@238: mcimadamore@238: @Override mcimadamore@238: public String visitMethodSymbol(MethodSymbol s, Locale locale) { mcimadamore@1085: if (s.isStaticOrInstanceInit()) { mcimadamore@238: return s.owner.name.toString(); mcimadamore@238: } else { mcimadamore@238: String ms = (s.name == s.name.table.names.init) mcimadamore@238: ? s.owner.name.toString() mcimadamore@238: : s.name.toString(); mcimadamore@238: if (s.type != null) { mcimadamore@238: if (s.type.tag == FORALL) { mcimadamore@238: ms = "<" + visitTypes(s.type.getTypeArguments(), locale) + ">" + ms; mcimadamore@238: } mcimadamore@238: ms += "(" + printMethodArgs( mcimadamore@238: s.type.getParameterTypes(), mcimadamore@238: (s.flags() & VARARGS) != 0, mcimadamore@238: locale) + ")"; mcimadamore@238: } mcimadamore@238: return ms; mcimadamore@238: } mcimadamore@238: } mcimadamore@238: mcimadamore@238: @Override mcimadamore@238: public String visitOperatorSymbol(OperatorSymbol s, Locale locale) { mcimadamore@238: return visitMethodSymbol(s, locale); mcimadamore@238: } mcimadamore@238: mcimadamore@238: @Override mcimadamore@238: public String visitPackageSymbol(PackageSymbol s, Locale locale) { mcimadamore@238: return s.isUnnamed() mcimadamore@238: ? localize(locale, "compiler.misc.unnamed.package") mcimadamore@238: : s.fullname.toString(); mcimadamore@238: } mcimadamore@238: mcimadamore@238: @Override mcimadamore@238: public String visitTypeSymbol(TypeSymbol s, Locale locale) { mcimadamore@238: return visitSymbol(s, locale); mcimadamore@238: } mcimadamore@238: mcimadamore@238: @Override mcimadamore@238: public String visitVarSymbol(VarSymbol s, Locale locale) { mcimadamore@238: return visitSymbol(s, locale); mcimadamore@238: } mcimadamore@238: mcimadamore@238: @Override mcimadamore@238: public String visitSymbol(Symbol s, Locale locale) { mcimadamore@238: return s.name.toString(); mcimadamore@238: } mcimadamore@238: }