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

Tue, 15 Oct 2013 15:57:13 -0700

author
jjg
date
Tue, 15 Oct 2013 15:57:13 -0700
changeset 2134
b0c086cd4520
parent 2047
5f915a0c9615
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8026564: import changes from type-annotations forest
Reviewed-by: jjg
Contributed-by: wdietl@gmail.com, steve.sides@oracle.com

mcimadamore@238 1 /*
jjg@1521 2 * Copyright (c) 2009, 2013, 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@1521 31 import com.sun.tools.javac.code.Type.AnnotatedType;
jjg@1755 32 import com.sun.tools.javac.code.Type.ArrayType;
jjg@1357 33 import com.sun.tools.javac.code.Symbol.*;
mcimadamore@238 34 import com.sun.tools.javac.code.Type.*;
mcimadamore@238 35 import com.sun.tools.javac.util.List;
mcimadamore@238 36 import com.sun.tools.javac.util.ListBuffer;
jjg@1374 37
mcimadamore@238 38 import static com.sun.tools.javac.code.BoundKind.*;
mcimadamore@238 39 import static com.sun.tools.javac.code.Flags.*;
jjg@1374 40 import static com.sun.tools.javac.code.TypeTag.CLASS;
jjg@1374 41 import static com.sun.tools.javac.code.TypeTag.FORALL;
mcimadamore@238 42
mcimadamore@238 43 /**
mcimadamore@238 44 * A combined type/symbol visitor for generating non-trivial localized string
mcimadamore@238 45 * representation of types and symbols.
jjg@333 46 *
jjg@581 47 * <p><b>This is NOT part of any supported API.
jjg@333 48 * If you write code that depends on this, you do so at your own risk.
jjg@333 49 * This code and its internal interfaces are subject to change or
jjg@333 50 * deletion without notice.</b>
mcimadamore@238 51 */
mcimadamore@238 52 public abstract class Printer implements Type.Visitor<String, Locale>, Symbol.Visitor<String, Locale> {
mcimadamore@238 53
mcimadamore@288 54 List<Type> seenCaptured = List.nil();
mcimadamore@288 55 static final int PRIME = 997; // largest prime less than 1000
mcimadamore@1347 56
mcimadamore@1348 57 protected Printer() { }
mcimadamore@288 58
mcimadamore@238 59 /**
mcimadamore@238 60 * This method should be overriden in order to provide proper i18n support.
mcimadamore@238 61 *
mcimadamore@238 62 * @param locale the locale in which the string is to be rendered
mcimadamore@238 63 * @param key the key corresponding to the message to be displayed
mcimadamore@238 64 * @param args a list of optional arguments
mcimadamore@238 65 * @return localized string representation
mcimadamore@238 66 */
mcimadamore@238 67 protected abstract String localize(Locale locale, String key, Object... args);
mcimadamore@238 68
mcimadamore@238 69 /**
mcimadamore@288 70 * Maps a captured type into an unique identifier.
mcimadamore@288 71 *
mcimadamore@288 72 * @param t the captured type for which an id is to be retrieved
mcimadamore@288 73 * @param locale locale settings
mcimadamore@288 74 * @return unique id representing this captured type
mcimadamore@288 75 */
mcimadamore@288 76 protected abstract String capturedVarId(CapturedType t, Locale locale);
mcimadamore@288 77
mcimadamore@288 78 /**
mcimadamore@288 79 * Create a printer with default i18n support provided by Messages. By default,
mcimadamore@288 80 * captured types ids are generated using hashcode.
mcimadamore@288 81 *
mcimadamore@238 82 * @param messages Messages class to be used for i18n
mcimadamore@238 83 * @return printer visitor instance
mcimadamore@238 84 */
mcimadamore@238 85 public static Printer createStandardPrinter(final Messages messages) {
mcimadamore@1348 86 return new Printer() {
mcimadamore@238 87 @Override
mcimadamore@238 88 protected String localize(Locale locale, String key, Object... args) {
mcimadamore@238 89 return messages.getLocalizedString(locale, key, args);
mcimadamore@288 90 }
mcimadamore@288 91
mcimadamore@288 92 @Override
mcimadamore@288 93 protected String capturedVarId(CapturedType t, Locale locale) {
mcimadamore@288 94 return (t.hashCode() & 0xFFFFFFFFL) % PRIME + "";
mcimadamore@238 95 }};
mcimadamore@238 96 }
mcimadamore@238 97
mcimadamore@238 98 /**
mcimadamore@238 99 * Get a localized string representation for all the types in the input list.
mcimadamore@238 100 *
mcimadamore@238 101 * @param ts types to be displayed
mcimadamore@238 102 * @param locale the locale in which the string is to be rendered
mcimadamore@238 103 * @return localized string representation
mcimadamore@238 104 */
mcimadamore@238 105 public String visitTypes(List<Type> ts, Locale locale) {
alundblad@2047 106 ListBuffer<String> sbuf = new ListBuffer<>();
mcimadamore@238 107 for (Type t : ts) {
mcimadamore@238 108 sbuf.append(visit(t, locale));
mcimadamore@238 109 }
mcimadamore@238 110 return sbuf.toList().toString();
mcimadamore@238 111 }
mcimadamore@238 112
mcimadamore@238 113 /**
jjg@842 114 * * Get a localized string representation for all the symbols in the input list.
mcimadamore@238 115 *
mcimadamore@238 116 * @param ts symbols to be displayed
mcimadamore@238 117 * @param locale the locale in which the string is to be rendered
mcimadamore@238 118 * @return localized string representation
mcimadamore@238 119 */
mcimadamore@238 120 public String visitSymbols(List<Symbol> ts, Locale locale) {
alundblad@2047 121 ListBuffer<String> sbuf = new ListBuffer<>();
mcimadamore@238 122 for (Symbol t : ts) {
mcimadamore@238 123 sbuf.append(visit(t, locale));
mcimadamore@238 124 }
mcimadamore@238 125 return sbuf.toList().toString();
mcimadamore@238 126 }
mcimadamore@238 127
mcimadamore@238 128 /**
jjg@1755 129 * Get a localized string representation for a given type.
mcimadamore@238 130 *
jjg@1358 131 * @param t type to be displayed
mcimadamore@238 132 * @param locale the locale in which the string is to be rendered
mcimadamore@238 133 * @return localized string representation
mcimadamore@238 134 */
mcimadamore@238 135 public String visit(Type t, Locale locale) {
mcimadamore@238 136 return t.accept(this, locale);
mcimadamore@238 137 }
mcimadamore@238 138
mcimadamore@238 139 /**
jjg@1755 140 * Get a localized string representation for a given symbol.
mcimadamore@238 141 *
jjg@1358 142 * @param s symbol to be displayed
mcimadamore@238 143 * @param locale the locale in which the string is to be rendered
mcimadamore@238 144 * @return localized string representation
mcimadamore@238 145 */
mcimadamore@238 146 public String visit(Symbol s, Locale locale) {
mcimadamore@238 147 return s.accept(this, locale);
mcimadamore@238 148 }
mcimadamore@238 149
mcimadamore@238 150 @Override
mcimadamore@238 151 public String visitCapturedType(CapturedType t, Locale locale) {
mcimadamore@288 152 if (seenCaptured.contains(t))
mcimadamore@288 153 return localize(locale, "compiler.misc.type.captureof.1",
mcimadamore@288 154 capturedVarId(t, locale));
mcimadamore@288 155 else {
mcimadamore@288 156 try {
mcimadamore@288 157 seenCaptured = seenCaptured.prepend(t);
mcimadamore@288 158 return localize(locale, "compiler.misc.type.captureof",
mcimadamore@288 159 capturedVarId(t, locale),
mcimadamore@288 160 visit(t.wildcard, locale));
mcimadamore@288 161 }
mcimadamore@288 162 finally {
mcimadamore@288 163 seenCaptured = seenCaptured.tail;
mcimadamore@288 164 }
mcimadamore@288 165 }
mcimadamore@238 166 }
mcimadamore@238 167
mcimadamore@238 168 @Override
mcimadamore@238 169 public String visitForAll(ForAll t, Locale locale) {
mcimadamore@238 170 return "<" + visitTypes(t.tvars, locale) + ">" + visit(t.qtype, locale);
mcimadamore@238 171 }
mcimadamore@238 172
mcimadamore@238 173 @Override
mcimadamore@238 174 public String visitUndetVar(UndetVar t, Locale locale) {
mcimadamore@238 175 if (t.inst != null) {
mcimadamore@238 176 return visit(t.inst, locale);
mcimadamore@238 177 } else {
mcimadamore@238 178 return visit(t.qtype, locale) + "?";
mcimadamore@238 179 }
mcimadamore@238 180 }
mcimadamore@238 181
mcimadamore@238 182 @Override
mcimadamore@238 183 public String visitArrayType(ArrayType t, Locale locale) {
jjg@1755 184 StringBuilder res = new StringBuilder();
jjg@1755 185 printBaseElementType(t, res, locale);
jjg@1755 186 printBrackets(t, res, locale);
jjg@1755 187 return res.toString();
jjg@1755 188 }
jjg@1755 189
jjg@1755 190 void printBaseElementType(Type t, StringBuilder sb, Locale locale) {
jjg@1755 191 Type arrel = t;
jjg@1969 192 while (arrel.hasTag(TypeTag.ARRAY)) {
jjg@1755 193 arrel = arrel.unannotatedType();
jjg@1755 194 arrel = ((ArrayType) arrel).elemtype;
jjg@1755 195 }
jjg@1755 196 sb.append(visit(arrel, locale));
jjg@1755 197 }
jjg@1755 198
jjg@1755 199 void printBrackets(Type t, StringBuilder sb, Locale locale) {
jjg@1755 200 Type arrel = t;
jjg@1969 201 while (arrel.hasTag(TypeTag.ARRAY)) {
jjg@1755 202 if (arrel.isAnnotated()) {
jjg@1755 203 sb.append(' ');
jjg@1755 204 sb.append(arrel.getAnnotationMirrors());
jjg@1755 205 sb.append(' ');
jjg@1755 206 }
jjg@1755 207 sb.append("[]");
jjg@1755 208 arrel = arrel.unannotatedType();
jjg@1755 209 arrel = ((ArrayType) arrel).elemtype;
jjg@1755 210 }
mcimadamore@238 211 }
mcimadamore@238 212
mcimadamore@238 213 @Override
mcimadamore@238 214 public String visitClassType(ClassType t, Locale locale) {
jjg@1362 215 StringBuilder buf = new StringBuilder();
vromero@1853 216 if (t.getEnclosingType().hasTag(CLASS) && t.tsym.owner.kind == Kinds.TYP) {
mcimadamore@238 217 buf.append(visit(t.getEnclosingType(), locale));
jjg@1521 218 buf.append('.');
mcimadamore@238 219 buf.append(className(t, false, locale));
mcimadamore@238 220 } else {
mcimadamore@238 221 buf.append(className(t, true, locale));
mcimadamore@238 222 }
mcimadamore@238 223 if (t.getTypeArguments().nonEmpty()) {
mcimadamore@238 224 buf.append('<');
mcimadamore@238 225 buf.append(visitTypes(t.getTypeArguments(), locale));
jjg@1521 226 buf.append('>');
mcimadamore@238 227 }
mcimadamore@238 228 return buf.toString();
mcimadamore@238 229 }
mcimadamore@238 230
mcimadamore@238 231 @Override
mcimadamore@238 232 public String visitMethodType(MethodType t, Locale locale) {
mcimadamore@238 233 return "(" + printMethodArgs(t.argtypes, false, locale) + ")" + visit(t.restype, locale);
mcimadamore@238 234 }
mcimadamore@238 235
mcimadamore@238 236 @Override
mcimadamore@238 237 public String visitPackageType(PackageType t, Locale locale) {
mcimadamore@238 238 return t.tsym.getQualifiedName().toString();
mcimadamore@238 239 }
mcimadamore@238 240
mcimadamore@238 241 @Override
mcimadamore@238 242 public String visitWildcardType(WildcardType t, Locale locale) {
jjg@1362 243 StringBuilder s = new StringBuilder();
mcimadamore@238 244 s.append(t.kind);
mcimadamore@238 245 if (t.kind != UNBOUND) {
mcimadamore@238 246 s.append(visit(t.type, locale));
mcimadamore@238 247 }
mcimadamore@238 248 return s.toString();
mcimadamore@238 249 }
mcimadamore@238 250
mcimadamore@238 251 @Override
mcimadamore@238 252 public String visitErrorType(ErrorType t, Locale locale) {
mcimadamore@238 253 return visitType(t, locale);
mcimadamore@238 254 }
mcimadamore@238 255
mcimadamore@238 256 @Override
mcimadamore@238 257 public String visitTypeVar(TypeVar t, Locale locale) {
mcimadamore@238 258 return visitType(t, locale);
mcimadamore@238 259 }
mcimadamore@238 260
jjg@1521 261 @Override
jjg@1521 262 public String visitAnnotatedType(AnnotatedType t, Locale locale) {
jjg@2134 263 if (t.getAnnotationMirrors().nonEmpty()) {
jjg@2134 264 if (t.unannotatedType().hasTag(TypeTag.ARRAY)) {
jjg@1755 265 StringBuilder res = new StringBuilder();
jjg@1755 266 printBaseElementType(t, res, locale);
jjg@1755 267 printBrackets(t, res, locale);
jjg@1755 268 return res.toString();
jjg@2134 269 } else if (t.unannotatedType().hasTag(TypeTag.CLASS) &&
jjg@2134 270 t.unannotatedType().getEnclosingType() != Type.noType) {
jjg@2134 271 return visit(t.unannotatedType().getEnclosingType(), locale) +
jjg@1755 272 ". " +
jjg@2134 273 t.getAnnotationMirrors() +
jjg@2134 274 " " + className((ClassType)t.unannotatedType(), false, locale);
jjg@1755 275 } else {
jjg@2134 276 return t.getAnnotationMirrors() + " " + visit(t.unannotatedType(), locale);
jjg@1755 277 }
jjg@1521 278 } else {
jjg@2134 279 return visit(t.unannotatedType(), locale);
jjg@1521 280 }
jjg@1521 281 }
jjg@1521 282
mcimadamore@238 283 public String visitType(Type t, Locale locale) {
mcimadamore@1348 284 String s = (t.tsym == null || t.tsym.name == null)
mcimadamore@1348 285 ? localize(locale, "compiler.misc.type.none")
mcimadamore@1348 286 : t.tsym.name.toString();
mcimadamore@1348 287 return s;
mcimadamore@238 288 }
mcimadamore@238 289
mcimadamore@238 290 /**
mcimadamore@238 291 * Converts a class name into a (possibly localized) string. Anonymous
jjg@1755 292 * inner classes get converted into a localized string.
mcimadamore@238 293 *
mcimadamore@238 294 * @param t the type of the class whose name is to be rendered
mcimadamore@238 295 * @param longform if set, the class' fullname is displayed - if unset the
mcimadamore@238 296 * short name is chosen (w/o package)
mcimadamore@238 297 * @param locale the locale in which the string is to be rendered
mcimadamore@238 298 * @return localized string representation
mcimadamore@238 299 */
mcimadamore@238 300 protected String className(ClassType t, boolean longform, Locale locale) {
mcimadamore@238 301 Symbol sym = t.tsym;
mcimadamore@238 302 if (sym.name.length() == 0 && (sym.flags() & COMPOUND) != 0) {
jjg@1362 303 StringBuilder s = new StringBuilder(visit(t.supertype_field, locale));
mcimadamore@238 304 for (List<Type> is = t.interfaces_field; is.nonEmpty(); is = is.tail) {
jjg@1755 305 s.append('&');
mcimadamore@238 306 s.append(visit(is.head, locale));
mcimadamore@238 307 }
mcimadamore@238 308 return s.toString();
mcimadamore@238 309 } else if (sym.name.length() == 0) {
mcimadamore@238 310 String s;
mcimadamore@238 311 ClassType norm = (ClassType) t.tsym.type;
mcimadamore@238 312 if (norm == null) {
mcimadamore@238 313 s = localize(locale, "compiler.misc.anonymous.class", (Object) null);
mcimadamore@1114 314 } else if (norm.interfaces_field != null && norm.interfaces_field.nonEmpty()) {
mcimadamore@238 315 s = localize(locale, "compiler.misc.anonymous.class",
mcimadamore@238 316 visit(norm.interfaces_field.head, locale));
mcimadamore@238 317 } else {
mcimadamore@238 318 s = localize(locale, "compiler.misc.anonymous.class",
mcimadamore@238 319 visit(norm.supertype_field, locale));
mcimadamore@238 320 }
mcimadamore@238 321 return s;
mcimadamore@238 322 } else if (longform) {
mcimadamore@238 323 return sym.getQualifiedName().toString();
mcimadamore@238 324 } else {
mcimadamore@238 325 return sym.name.toString();
mcimadamore@238 326 }
mcimadamore@238 327 }
mcimadamore@238 328
mcimadamore@238 329 /**
mcimadamore@238 330 * Converts a set of method argument types into their corresponding
mcimadamore@238 331 * localized string representation.
mcimadamore@238 332 *
mcimadamore@238 333 * @param args arguments to be rendered
mcimadamore@238 334 * @param varArgs if true, the last method argument is regarded as a vararg
mcimadamore@238 335 * @param locale the locale in which the string is to be rendered
mcimadamore@238 336 * @return localized string representation
mcimadamore@238 337 */
mcimadamore@238 338 protected String printMethodArgs(List<Type> args, boolean varArgs, Locale locale) {
mcimadamore@238 339 if (!varArgs) {
mcimadamore@238 340 return visitTypes(args, locale);
mcimadamore@238 341 } else {
jjg@1362 342 StringBuilder buf = new StringBuilder();
mcimadamore@238 343 while (args.tail.nonEmpty()) {
mcimadamore@238 344 buf.append(visit(args.head, locale));
mcimadamore@238 345 args = args.tail;
mcimadamore@238 346 buf.append(',');
mcimadamore@238 347 }
jjg@1969 348 if (args.head.unannotatedType().hasTag(TypeTag.ARRAY)) {
jjg@1521 349 buf.append(visit(((ArrayType) args.head.unannotatedType()).elemtype, locale));
jjg@1645 350 if (args.head.getAnnotationMirrors().nonEmpty()) {
jjg@1521 351 buf.append(' ');
jjg@1645 352 buf.append(args.head.getAnnotationMirrors());
jjg@1521 353 buf.append(' ');
jjg@1521 354 }
mcimadamore@238 355 buf.append("...");
mcimadamore@238 356 } else {
mcimadamore@238 357 buf.append(visit(args.head, locale));
mcimadamore@238 358 }
mcimadamore@238 359 return buf.toString();
mcimadamore@238 360 }
mcimadamore@238 361 }
mcimadamore@238 362
mcimadamore@238 363 @Override
mcimadamore@238 364 public String visitClassSymbol(ClassSymbol sym, Locale locale) {
mcimadamore@238 365 return sym.name.isEmpty()
mcimadamore@238 366 ? localize(locale, "compiler.misc.anonymous.class", sym.flatname)
mcimadamore@238 367 : sym.fullname.toString();
mcimadamore@238 368 }
mcimadamore@238 369
mcimadamore@238 370 @Override
mcimadamore@238 371 public String visitMethodSymbol(MethodSymbol s, Locale locale) {
mcimadamore@1085 372 if (s.isStaticOrInstanceInit()) {
mcimadamore@238 373 return s.owner.name.toString();
mcimadamore@238 374 } else {
mcimadamore@238 375 String ms = (s.name == s.name.table.names.init)
mcimadamore@238 376 ? s.owner.name.toString()
mcimadamore@238 377 : s.name.toString();
mcimadamore@238 378 if (s.type != null) {
vromero@1853 379 if (s.type.hasTag(FORALL)) {
mcimadamore@238 380 ms = "<" + visitTypes(s.type.getTypeArguments(), locale) + ">" + ms;
mcimadamore@238 381 }
mcimadamore@238 382 ms += "(" + printMethodArgs(
mcimadamore@238 383 s.type.getParameterTypes(),
mcimadamore@238 384 (s.flags() & VARARGS) != 0,
mcimadamore@238 385 locale) + ")";
mcimadamore@238 386 }
mcimadamore@238 387 return ms;
mcimadamore@238 388 }
mcimadamore@238 389 }
mcimadamore@238 390
mcimadamore@238 391 @Override
mcimadamore@238 392 public String visitOperatorSymbol(OperatorSymbol s, Locale locale) {
mcimadamore@238 393 return visitMethodSymbol(s, locale);
mcimadamore@238 394 }
mcimadamore@238 395
mcimadamore@238 396 @Override
mcimadamore@238 397 public String visitPackageSymbol(PackageSymbol s, Locale locale) {
mcimadamore@238 398 return s.isUnnamed()
mcimadamore@238 399 ? localize(locale, "compiler.misc.unnamed.package")
mcimadamore@238 400 : s.fullname.toString();
mcimadamore@238 401 }
mcimadamore@238 402
mcimadamore@238 403 @Override
mcimadamore@238 404 public String visitTypeSymbol(TypeSymbol s, Locale locale) {
mcimadamore@238 405 return visitSymbol(s, locale);
mcimadamore@238 406 }
mcimadamore@238 407
mcimadamore@238 408 @Override
mcimadamore@238 409 public String visitVarSymbol(VarSymbol s, Locale locale) {
mcimadamore@238 410 return visitSymbol(s, locale);
mcimadamore@238 411 }
mcimadamore@238 412
mcimadamore@238 413 @Override
mcimadamore@238 414 public String visitSymbol(Symbol s, Locale locale) {
mcimadamore@238 415 return s.name.toString();
mcimadamore@238 416 }
mcimadamore@238 417 }

mercurial