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

Tue, 25 Jun 2013 16:12:53 +0100

author
vromero
date
Tue, 25 Jun 2013 16:12:53 +0100
changeset 1853
831467c4c6a7
parent 1755
ddb4a2bfcd82
child 1969
7de231613e4a
permissions
-rw-r--r--

8017104: javac should have a class for primitive types that inherits from Type
Reviewed-by: jjg

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

mercurial