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

Sun, 17 Feb 2013 16:44:55 -0500

author
dholmes
date
Sun, 17 Feb 2013 16:44:55 -0500
changeset 1571
af8417e590f4
parent 1521
71f35e4b93a5
child 1645
97f6839673d6
permissions
-rw-r--r--

Merge

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

mercurial