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