src/share/classes/com/sun/tools/javac/code/Printer.java

Tue, 09 Oct 2012 19:10:00 -0700

author
jjg
date
Tue, 09 Oct 2012 19:10:00 -0700
changeset 1357
c75be5bc5283
parent 1348
573ceb23beeb
child 1358
fc123bdeddb8
permissions
-rw-r--r--

8000663: clean up langtools imports
Reviewed-by: darcy

mcimadamore@238 1 /*
jjg@1357 2 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
mcimadamore@238 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
mcimadamore@238 4 *
mcimadamore@238 5 * This code is free software; you can redistribute it and/or modify it
mcimadamore@238 6 * under the terms of the GNU General Public License version 2 only, as
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
mcimadamore@238 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle in the LICENSE file that accompanied this code.
mcimadamore@238 10 *
mcimadamore@238 11 * This code is distributed in the hope that it will be useful, but WITHOUT
mcimadamore@238 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
mcimadamore@238 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
mcimadamore@238 14 * version 2 for more details (a copy is included in the LICENSE file that
mcimadamore@238 15 * accompanied this code).
mcimadamore@238 16 *
mcimadamore@238 17 * You should have received a copy of the GNU General Public License version
mcimadamore@238 18 * 2 along with this work; if not, write to the Free Software Foundation,
mcimadamore@238 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
mcimadamore@238 20 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
mcimadamore@238 24 */
mcimadamore@238 25
mcimadamore@238 26 package com.sun.tools.javac.code;
mcimadamore@238 27
mcimadamore@238 28 import java.util.Locale;
mcimadamore@238 29
mcimadamore@238 30 import com.sun.tools.javac.api.Messages;
jjg@1357 31 import com.sun.tools.javac.code.Symbol.*;
mcimadamore@238 32 import com.sun.tools.javac.code.Type.*;
mcimadamore@238 33 import com.sun.tools.javac.util.List;
mcimadamore@238 34 import com.sun.tools.javac.util.ListBuffer;
mcimadamore@238 35 import static com.sun.tools.javac.code.BoundKind.*;
mcimadamore@238 36 import static com.sun.tools.javac.code.Flags.*;
jjg@1357 37 import static com.sun.tools.javac.code.TypeTags.*;
mcimadamore@238 38
mcimadamore@238 39 /**
mcimadamore@238 40 * A combined type/symbol visitor for generating non-trivial localized string
mcimadamore@238 41 * representation of types and symbols.
jjg@333 42 *
jjg@581 43 * <p><b>This is NOT part of any supported API.
jjg@333 44 * If you write code that depends on this, you do so at your own risk.
jjg@333 45 * This code and its internal interfaces are subject to change or
jjg@333 46 * deletion without notice.</b>
mcimadamore@238 47 */
mcimadamore@238 48 public abstract class Printer implements Type.Visitor<String, Locale>, Symbol.Visitor<String, Locale> {
mcimadamore@238 49
mcimadamore@288 50 List<Type> seenCaptured = List.nil();
mcimadamore@288 51 static final int PRIME = 997; // largest prime less than 1000
mcimadamore@1347 52
mcimadamore@1348 53 protected Printer() { }
mcimadamore@288 54
mcimadamore@238 55 /**
mcimadamore@238 56 * This method should be overriden in order to provide proper i18n support.
mcimadamore@238 57 *
mcimadamore@238 58 * @param locale the locale in which the string is to be rendered
mcimadamore@238 59 * @param key the key corresponding to the message to be displayed
mcimadamore@238 60 * @param args a list of optional arguments
mcimadamore@238 61 * @return localized string representation
mcimadamore@238 62 */
mcimadamore@238 63 protected abstract String localize(Locale locale, String key, Object... args);
mcimadamore@238 64
mcimadamore@238 65 /**
mcimadamore@288 66 * Maps a captured type into an unique identifier.
mcimadamore@288 67 *
mcimadamore@288 68 * @param t the captured type for which an id is to be retrieved
mcimadamore@288 69 * @param locale locale settings
mcimadamore@288 70 * @return unique id representing this captured type
mcimadamore@288 71 */
mcimadamore@288 72 protected abstract String capturedVarId(CapturedType t, Locale locale);
mcimadamore@288 73
mcimadamore@288 74 /**
mcimadamore@288 75 * Create a printer with default i18n support provided by Messages. By default,
mcimadamore@288 76 * captured types ids are generated using hashcode.
mcimadamore@288 77 *
mcimadamore@238 78 * @param messages Messages class to be used for i18n
mcimadamore@238 79 * @return printer visitor instance
mcimadamore@238 80 */
mcimadamore@238 81 public static Printer createStandardPrinter(final Messages messages) {
mcimadamore@1348 82 return new Printer() {
mcimadamore@238 83 @Override
mcimadamore@238 84 protected String localize(Locale locale, String key, Object... args) {
mcimadamore@238 85 return messages.getLocalizedString(locale, key, args);
mcimadamore@288 86 }
mcimadamore@288 87
mcimadamore@288 88 @Override
mcimadamore@288 89 protected String capturedVarId(CapturedType t, Locale locale) {
mcimadamore@288 90 return (t.hashCode() & 0xFFFFFFFFL) % PRIME + "";
mcimadamore@238 91 }};
mcimadamore@238 92 }
mcimadamore@238 93
mcimadamore@238 94 /**
mcimadamore@238 95 * Get a localized string representation for all the types in the input list.
mcimadamore@238 96 *
mcimadamore@238 97 * @param ts types to be displayed
mcimadamore@238 98 * @param locale the locale in which the string is to be rendered
mcimadamore@238 99 * @return localized string representation
mcimadamore@238 100 */
mcimadamore@238 101 public String visitTypes(List<Type> ts, Locale locale) {
mcimadamore@238 102 ListBuffer<String> sbuf = ListBuffer.lb();
mcimadamore@238 103 for (Type t : ts) {
mcimadamore@238 104 sbuf.append(visit(t, locale));
mcimadamore@238 105 }
mcimadamore@238 106 return sbuf.toList().toString();
mcimadamore@238 107 }
mcimadamore@238 108
mcimadamore@238 109 /**
jjg@842 110 * * Get a localized string representation for all the symbols in the input list.
mcimadamore@238 111 *
mcimadamore@238 112 * @param ts symbols to be displayed
mcimadamore@238 113 * @param locale the locale in which the string is to be rendered
mcimadamore@238 114 * @return localized string representation
mcimadamore@238 115 */
mcimadamore@238 116 public String visitSymbols(List<Symbol> ts, Locale locale) {
mcimadamore@238 117 ListBuffer<String> sbuf = ListBuffer.lb();
mcimadamore@238 118 for (Symbol t : ts) {
mcimadamore@238 119 sbuf.append(visit(t, locale));
mcimadamore@238 120 }
mcimadamore@238 121 return sbuf.toList().toString();
mcimadamore@238 122 }
mcimadamore@238 123
mcimadamore@238 124 /**
mcimadamore@238 125 * Get a localized string represenation for a given type.
mcimadamore@238 126 *
mcimadamore@238 127 * @param ts type to be displayed
mcimadamore@238 128 * @param locale the locale in which the string is to be rendered
mcimadamore@238 129 * @return localized string representation
mcimadamore@238 130 */
mcimadamore@238 131 public String visit(Type t, Locale locale) {
mcimadamore@238 132 return t.accept(this, locale);
mcimadamore@238 133 }
mcimadamore@238 134
mcimadamore@238 135 /**
mcimadamore@238 136 * Get a localized string represenation for a given symbol.
mcimadamore@238 137 *
mcimadamore@238 138 * @param ts symbol to be displayed
mcimadamore@238 139 * @param locale the locale in which the string is to be rendered
mcimadamore@238 140 * @return localized string representation
mcimadamore@238 141 */
mcimadamore@238 142 public String visit(Symbol s, Locale locale) {
mcimadamore@238 143 return s.accept(this, locale);
mcimadamore@238 144 }
mcimadamore@238 145
mcimadamore@238 146 @Override
mcimadamore@238 147 public String visitCapturedType(CapturedType t, Locale locale) {
mcimadamore@288 148 if (seenCaptured.contains(t))
mcimadamore@288 149 return localize(locale, "compiler.misc.type.captureof.1",
mcimadamore@288 150 capturedVarId(t, locale));
mcimadamore@288 151 else {
mcimadamore@288 152 try {
mcimadamore@288 153 seenCaptured = seenCaptured.prepend(t);
mcimadamore@288 154 return localize(locale, "compiler.misc.type.captureof",
mcimadamore@288 155 capturedVarId(t, locale),
mcimadamore@288 156 visit(t.wildcard, locale));
mcimadamore@288 157 }
mcimadamore@288 158 finally {
mcimadamore@288 159 seenCaptured = seenCaptured.tail;
mcimadamore@288 160 }
mcimadamore@288 161 }
mcimadamore@238 162 }
mcimadamore@238 163
mcimadamore@238 164 @Override
mcimadamore@238 165 public String visitForAll(ForAll t, Locale locale) {
mcimadamore@238 166 return "<" + visitTypes(t.tvars, locale) + ">" + visit(t.qtype, locale);
mcimadamore@238 167 }
mcimadamore@238 168
mcimadamore@238 169 @Override
mcimadamore@238 170 public String visitUndetVar(UndetVar t, Locale locale) {
mcimadamore@238 171 if (t.inst != null) {
mcimadamore@238 172 return visit(t.inst, locale);
mcimadamore@238 173 } else {
mcimadamore@238 174 return visit(t.qtype, locale) + "?";
mcimadamore@238 175 }
mcimadamore@238 176 }
mcimadamore@238 177
mcimadamore@238 178 @Override
mcimadamore@238 179 public String visitArrayType(ArrayType t, Locale locale) {
mcimadamore@238 180 return visit(t.elemtype, locale) + "[]";
mcimadamore@238 181 }
mcimadamore@238 182
mcimadamore@238 183 @Override
mcimadamore@238 184 public String visitClassType(ClassType t, Locale locale) {
mcimadamore@238 185 StringBuffer buf = new StringBuffer();
mcimadamore@238 186 if (t.getEnclosingType().tag == CLASS && t.tsym.owner.kind == Kinds.TYP) {
mcimadamore@238 187 buf.append(visit(t.getEnclosingType(), locale));
mcimadamore@238 188 buf.append(".");
mcimadamore@238 189 buf.append(className(t, false, locale));
mcimadamore@238 190 } else {
mcimadamore@238 191 buf.append(className(t, true, locale));
mcimadamore@238 192 }
mcimadamore@238 193 if (t.getTypeArguments().nonEmpty()) {
mcimadamore@238 194 buf.append('<');
mcimadamore@238 195 buf.append(visitTypes(t.getTypeArguments(), locale));
mcimadamore@238 196 buf.append(">");
mcimadamore@238 197 }
mcimadamore@238 198 return buf.toString();
mcimadamore@238 199 }
mcimadamore@238 200
mcimadamore@238 201 @Override
mcimadamore@238 202 public String visitMethodType(MethodType t, Locale locale) {
mcimadamore@238 203 return "(" + printMethodArgs(t.argtypes, false, locale) + ")" + visit(t.restype, locale);
mcimadamore@238 204 }
mcimadamore@238 205
mcimadamore@238 206 @Override
mcimadamore@238 207 public String visitPackageType(PackageType t, Locale locale) {
mcimadamore@238 208 return t.tsym.getQualifiedName().toString();
mcimadamore@238 209 }
mcimadamore@238 210
mcimadamore@238 211 @Override
mcimadamore@238 212 public String visitWildcardType(WildcardType t, Locale locale) {
mcimadamore@238 213 StringBuffer s = new StringBuffer();
mcimadamore@238 214 s.append(t.kind);
mcimadamore@238 215 if (t.kind != UNBOUND) {
mcimadamore@238 216 s.append(visit(t.type, locale));
mcimadamore@238 217 }
mcimadamore@238 218 return s.toString();
mcimadamore@238 219 }
mcimadamore@238 220
mcimadamore@238 221 @Override
mcimadamore@238 222 public String visitErrorType(ErrorType t, Locale locale) {
mcimadamore@238 223 return visitType(t, locale);
mcimadamore@238 224 }
mcimadamore@238 225
mcimadamore@238 226 @Override
mcimadamore@238 227 public String visitTypeVar(TypeVar t, Locale locale) {
mcimadamore@238 228 return visitType(t, locale);
mcimadamore@238 229 }
mcimadamore@238 230
mcimadamore@238 231 public String visitType(Type t, Locale locale) {
mcimadamore@1348 232 String s = (t.tsym == null || t.tsym.name == null)
mcimadamore@1348 233 ? localize(locale, "compiler.misc.type.none")
mcimadamore@1348 234 : t.tsym.name.toString();
mcimadamore@1348 235 return s;
mcimadamore@238 236 }
mcimadamore@238 237
mcimadamore@238 238 /**
mcimadamore@238 239 * Converts a class name into a (possibly localized) string. Anonymous
mcimadamore@238 240 * inner classes gets converted into a localized string.
mcimadamore@238 241 *
mcimadamore@238 242 * @param t the type of the class whose name is to be rendered
mcimadamore@238 243 * @param longform if set, the class' fullname is displayed - if unset the
mcimadamore@238 244 * short name is chosen (w/o package)
mcimadamore@238 245 * @param locale the locale in which the string is to be rendered
mcimadamore@238 246 * @return localized string representation
mcimadamore@238 247 */
mcimadamore@238 248 protected String className(ClassType t, boolean longform, Locale locale) {
mcimadamore@238 249 Symbol sym = t.tsym;
mcimadamore@238 250 if (sym.name.length() == 0 && (sym.flags() & COMPOUND) != 0) {
mcimadamore@238 251 StringBuffer s = new StringBuffer(visit(t.supertype_field, locale));
mcimadamore@238 252 for (List<Type> is = t.interfaces_field; is.nonEmpty(); is = is.tail) {
mcimadamore@238 253 s.append("&");
mcimadamore@238 254 s.append(visit(is.head, locale));
mcimadamore@238 255 }
mcimadamore@238 256 return s.toString();
mcimadamore@238 257 } else if (sym.name.length() == 0) {
mcimadamore@238 258 String s;
mcimadamore@238 259 ClassType norm = (ClassType) t.tsym.type;
mcimadamore@238 260 if (norm == null) {
mcimadamore@238 261 s = localize(locale, "compiler.misc.anonymous.class", (Object) null);
mcimadamore@1114 262 } else if (norm.interfaces_field != null && norm.interfaces_field.nonEmpty()) {
mcimadamore@238 263 s = localize(locale, "compiler.misc.anonymous.class",
mcimadamore@238 264 visit(norm.interfaces_field.head, locale));
mcimadamore@238 265 } else {
mcimadamore@238 266 s = localize(locale, "compiler.misc.anonymous.class",
mcimadamore@238 267 visit(norm.supertype_field, locale));
mcimadamore@238 268 }
mcimadamore@238 269 return s;
mcimadamore@238 270 } else if (longform) {
mcimadamore@238 271 return sym.getQualifiedName().toString();
mcimadamore@238 272 } else {
mcimadamore@238 273 return sym.name.toString();
mcimadamore@238 274 }
mcimadamore@238 275 }
mcimadamore@238 276
mcimadamore@238 277 /**
mcimadamore@238 278 * Converts a set of method argument types into their corresponding
mcimadamore@238 279 * localized string representation.
mcimadamore@238 280 *
mcimadamore@238 281 * @param args arguments to be rendered
mcimadamore@238 282 * @param varArgs if true, the last method argument is regarded as a vararg
mcimadamore@238 283 * @param locale the locale in which the string is to be rendered
mcimadamore@238 284 * @return localized string representation
mcimadamore@238 285 */
mcimadamore@238 286 protected String printMethodArgs(List<Type> args, boolean varArgs, Locale locale) {
mcimadamore@238 287 if (!varArgs) {
mcimadamore@238 288 return visitTypes(args, locale);
mcimadamore@238 289 } else {
mcimadamore@238 290 StringBuffer buf = new StringBuffer();
mcimadamore@238 291 while (args.tail.nonEmpty()) {
mcimadamore@238 292 buf.append(visit(args.head, locale));
mcimadamore@238 293 args = args.tail;
mcimadamore@238 294 buf.append(',');
mcimadamore@238 295 }
mcimadamore@238 296 if (args.head.tag == ARRAY) {
mcimadamore@238 297 buf.append(visit(((ArrayType) args.head).elemtype, locale));
mcimadamore@238 298 buf.append("...");
mcimadamore@238 299 } else {
mcimadamore@238 300 buf.append(visit(args.head, locale));
mcimadamore@238 301 }
mcimadamore@238 302 return buf.toString();
mcimadamore@238 303 }
mcimadamore@238 304 }
mcimadamore@238 305
mcimadamore@238 306 @Override
mcimadamore@238 307 public String visitClassSymbol(ClassSymbol sym, Locale locale) {
mcimadamore@238 308 return sym.name.isEmpty()
mcimadamore@238 309 ? localize(locale, "compiler.misc.anonymous.class", sym.flatname)
mcimadamore@238 310 : sym.fullname.toString();
mcimadamore@238 311 }
mcimadamore@238 312
mcimadamore@238 313 @Override
mcimadamore@238 314 public String visitMethodSymbol(MethodSymbol s, Locale locale) {
mcimadamore@1085 315 if (s.isStaticOrInstanceInit()) {
mcimadamore@238 316 return s.owner.name.toString();
mcimadamore@238 317 } else {
mcimadamore@238 318 String ms = (s.name == s.name.table.names.init)
mcimadamore@238 319 ? s.owner.name.toString()
mcimadamore@238 320 : s.name.toString();
mcimadamore@238 321 if (s.type != null) {
mcimadamore@238 322 if (s.type.tag == FORALL) {
mcimadamore@238 323 ms = "<" + visitTypes(s.type.getTypeArguments(), locale) + ">" + ms;
mcimadamore@238 324 }
mcimadamore@238 325 ms += "(" + printMethodArgs(
mcimadamore@238 326 s.type.getParameterTypes(),
mcimadamore@238 327 (s.flags() & VARARGS) != 0,
mcimadamore@238 328 locale) + ")";
mcimadamore@238 329 }
mcimadamore@238 330 return ms;
mcimadamore@238 331 }
mcimadamore@238 332 }
mcimadamore@238 333
mcimadamore@238 334 @Override
mcimadamore@238 335 public String visitOperatorSymbol(OperatorSymbol s, Locale locale) {
mcimadamore@238 336 return visitMethodSymbol(s, locale);
mcimadamore@238 337 }
mcimadamore@238 338
mcimadamore@238 339 @Override
mcimadamore@238 340 public String visitPackageSymbol(PackageSymbol s, Locale locale) {
mcimadamore@238 341 return s.isUnnamed()
mcimadamore@238 342 ? localize(locale, "compiler.misc.unnamed.package")
mcimadamore@238 343 : s.fullname.toString();
mcimadamore@238 344 }
mcimadamore@238 345
mcimadamore@238 346 @Override
mcimadamore@238 347 public String visitTypeSymbol(TypeSymbol s, Locale locale) {
mcimadamore@238 348 return visitSymbol(s, locale);
mcimadamore@238 349 }
mcimadamore@238 350
mcimadamore@238 351 @Override
mcimadamore@238 352 public String visitVarSymbol(VarSymbol s, Locale locale) {
mcimadamore@238 353 return visitSymbol(s, locale);
mcimadamore@238 354 }
mcimadamore@238 355
mcimadamore@238 356 @Override
mcimadamore@238 357 public String visitSymbol(Symbol s, Locale locale) {
mcimadamore@238 358 return s.name.toString();
mcimadamore@238 359 }
mcimadamore@238 360 }

mercurial