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