Tue, 09 Oct 2012 19:10:00 -0700
8000663: clean up langtools imports
Reviewed-by: darcy
1 /*
2 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
26 package com.sun.tools.javac.code;
28 import java.util.Locale;
30 import com.sun.tools.javac.api.Messages;
31 import com.sun.tools.javac.code.Symbol.*;
32 import com.sun.tools.javac.code.Type.*;
33 import com.sun.tools.javac.util.List;
34 import com.sun.tools.javac.util.ListBuffer;
35 import static com.sun.tools.javac.code.BoundKind.*;
36 import static com.sun.tools.javac.code.Flags.*;
37 import static com.sun.tools.javac.code.TypeTags.*;
39 /**
40 * A combined type/symbol visitor for generating non-trivial localized string
41 * representation of types and symbols.
42 *
43 * <p><b>This is NOT part of any supported API.
44 * If you write code that depends on this, you do so at your own risk.
45 * This code and its internal interfaces are subject to change or
46 * deletion without notice.</b>
47 */
48 public abstract class Printer implements Type.Visitor<String, Locale>, Symbol.Visitor<String, Locale> {
50 List<Type> seenCaptured = List.nil();
51 static final int PRIME = 997; // largest prime less than 1000
53 protected Printer() { }
55 /**
56 * This method should be overriden in order to provide proper i18n support.
57 *
58 * @param locale the locale in which the string is to be rendered
59 * @param key the key corresponding to the message to be displayed
60 * @param args a list of optional arguments
61 * @return localized string representation
62 */
63 protected abstract String localize(Locale locale, String key, Object... args);
65 /**
66 * Maps a captured type into an unique identifier.
67 *
68 * @param t the captured type for which an id is to be retrieved
69 * @param locale locale settings
70 * @return unique id representing this captured type
71 */
72 protected abstract String capturedVarId(CapturedType t, Locale locale);
74 /**
75 * Create a printer with default i18n support provided by Messages. By default,
76 * captured types ids are generated using hashcode.
77 *
78 * @param messages Messages class to be used for i18n
79 * @return printer visitor instance
80 */
81 public static Printer createStandardPrinter(final Messages messages) {
82 return new Printer() {
83 @Override
84 protected String localize(Locale locale, String key, Object... args) {
85 return messages.getLocalizedString(locale, key, args);
86 }
88 @Override
89 protected String capturedVarId(CapturedType t, Locale locale) {
90 return (t.hashCode() & 0xFFFFFFFFL) % PRIME + "";
91 }};
92 }
94 /**
95 * Get a localized string representation for all the types in the input list.
96 *
97 * @param ts types to be displayed
98 * @param locale the locale in which the string is to be rendered
99 * @return localized string representation
100 */
101 public String visitTypes(List<Type> ts, Locale locale) {
102 ListBuffer<String> sbuf = ListBuffer.lb();
103 for (Type t : ts) {
104 sbuf.append(visit(t, locale));
105 }
106 return sbuf.toList().toString();
107 }
109 /**
110 * * Get a localized string representation for all the symbols in the input list.
111 *
112 * @param ts symbols to be displayed
113 * @param locale the locale in which the string is to be rendered
114 * @return localized string representation
115 */
116 public String visitSymbols(List<Symbol> ts, Locale locale) {
117 ListBuffer<String> sbuf = ListBuffer.lb();
118 for (Symbol t : ts) {
119 sbuf.append(visit(t, locale));
120 }
121 return sbuf.toList().toString();
122 }
124 /**
125 * Get a localized string represenation for a given type.
126 *
127 * @param ts type to be displayed
128 * @param locale the locale in which the string is to be rendered
129 * @return localized string representation
130 */
131 public String visit(Type t, Locale locale) {
132 return t.accept(this, locale);
133 }
135 /**
136 * Get a localized string represenation for a given symbol.
137 *
138 * @param ts symbol to be displayed
139 * @param locale the locale in which the string is to be rendered
140 * @return localized string representation
141 */
142 public String visit(Symbol s, Locale locale) {
143 return s.accept(this, locale);
144 }
146 @Override
147 public String visitCapturedType(CapturedType t, Locale locale) {
148 if (seenCaptured.contains(t))
149 return localize(locale, "compiler.misc.type.captureof.1",
150 capturedVarId(t, locale));
151 else {
152 try {
153 seenCaptured = seenCaptured.prepend(t);
154 return localize(locale, "compiler.misc.type.captureof",
155 capturedVarId(t, locale),
156 visit(t.wildcard, locale));
157 }
158 finally {
159 seenCaptured = seenCaptured.tail;
160 }
161 }
162 }
164 @Override
165 public String visitForAll(ForAll t, Locale locale) {
166 return "<" + visitTypes(t.tvars, locale) + ">" + visit(t.qtype, locale);
167 }
169 @Override
170 public String visitUndetVar(UndetVar t, Locale locale) {
171 if (t.inst != null) {
172 return visit(t.inst, locale);
173 } else {
174 return visit(t.qtype, locale) + "?";
175 }
176 }
178 @Override
179 public String visitArrayType(ArrayType t, Locale locale) {
180 return visit(t.elemtype, locale) + "[]";
181 }
183 @Override
184 public String visitClassType(ClassType t, Locale locale) {
185 StringBuffer buf = new StringBuffer();
186 if (t.getEnclosingType().tag == CLASS && t.tsym.owner.kind == Kinds.TYP) {
187 buf.append(visit(t.getEnclosingType(), locale));
188 buf.append(".");
189 buf.append(className(t, false, locale));
190 } else {
191 buf.append(className(t, true, locale));
192 }
193 if (t.getTypeArguments().nonEmpty()) {
194 buf.append('<');
195 buf.append(visitTypes(t.getTypeArguments(), locale));
196 buf.append(">");
197 }
198 return buf.toString();
199 }
201 @Override
202 public String visitMethodType(MethodType t, Locale locale) {
203 return "(" + printMethodArgs(t.argtypes, false, locale) + ")" + visit(t.restype, locale);
204 }
206 @Override
207 public String visitPackageType(PackageType t, Locale locale) {
208 return t.tsym.getQualifiedName().toString();
209 }
211 @Override
212 public String visitWildcardType(WildcardType t, Locale locale) {
213 StringBuffer s = new StringBuffer();
214 s.append(t.kind);
215 if (t.kind != UNBOUND) {
216 s.append(visit(t.type, locale));
217 }
218 return s.toString();
219 }
221 @Override
222 public String visitErrorType(ErrorType t, Locale locale) {
223 return visitType(t, locale);
224 }
226 @Override
227 public String visitTypeVar(TypeVar t, Locale locale) {
228 return visitType(t, locale);
229 }
231 public String visitType(Type t, Locale locale) {
232 String s = (t.tsym == null || t.tsym.name == null)
233 ? localize(locale, "compiler.misc.type.none")
234 : t.tsym.name.toString();
235 return s;
236 }
238 /**
239 * Converts a class name into a (possibly localized) string. Anonymous
240 * inner classes gets converted into a localized string.
241 *
242 * @param t the type of the class whose name is to be rendered
243 * @param longform if set, the class' fullname is displayed - if unset the
244 * short name is chosen (w/o package)
245 * @param locale the locale in which the string is to be rendered
246 * @return localized string representation
247 */
248 protected String className(ClassType t, boolean longform, Locale locale) {
249 Symbol sym = t.tsym;
250 if (sym.name.length() == 0 && (sym.flags() & COMPOUND) != 0) {
251 StringBuffer s = new StringBuffer(visit(t.supertype_field, locale));
252 for (List<Type> is = t.interfaces_field; is.nonEmpty(); is = is.tail) {
253 s.append("&");
254 s.append(visit(is.head, locale));
255 }
256 return s.toString();
257 } else if (sym.name.length() == 0) {
258 String s;
259 ClassType norm = (ClassType) t.tsym.type;
260 if (norm == null) {
261 s = localize(locale, "compiler.misc.anonymous.class", (Object) null);
262 } else if (norm.interfaces_field != null && norm.interfaces_field.nonEmpty()) {
263 s = localize(locale, "compiler.misc.anonymous.class",
264 visit(norm.interfaces_field.head, locale));
265 } else {
266 s = localize(locale, "compiler.misc.anonymous.class",
267 visit(norm.supertype_field, locale));
268 }
269 return s;
270 } else if (longform) {
271 return sym.getQualifiedName().toString();
272 } else {
273 return sym.name.toString();
274 }
275 }
277 /**
278 * Converts a set of method argument types into their corresponding
279 * localized string representation.
280 *
281 * @param args arguments to be rendered
282 * @param varArgs if true, the last method argument is regarded as a vararg
283 * @param locale the locale in which the string is to be rendered
284 * @return localized string representation
285 */
286 protected String printMethodArgs(List<Type> args, boolean varArgs, Locale locale) {
287 if (!varArgs) {
288 return visitTypes(args, locale);
289 } else {
290 StringBuffer buf = new StringBuffer();
291 while (args.tail.nonEmpty()) {
292 buf.append(visit(args.head, locale));
293 args = args.tail;
294 buf.append(',');
295 }
296 if (args.head.tag == ARRAY) {
297 buf.append(visit(((ArrayType) args.head).elemtype, locale));
298 buf.append("...");
299 } else {
300 buf.append(visit(args.head, locale));
301 }
302 return buf.toString();
303 }
304 }
306 @Override
307 public String visitClassSymbol(ClassSymbol sym, Locale locale) {
308 return sym.name.isEmpty()
309 ? localize(locale, "compiler.misc.anonymous.class", sym.flatname)
310 : sym.fullname.toString();
311 }
313 @Override
314 public String visitMethodSymbol(MethodSymbol s, Locale locale) {
315 if (s.isStaticOrInstanceInit()) {
316 return s.owner.name.toString();
317 } else {
318 String ms = (s.name == s.name.table.names.init)
319 ? s.owner.name.toString()
320 : s.name.toString();
321 if (s.type != null) {
322 if (s.type.tag == FORALL) {
323 ms = "<" + visitTypes(s.type.getTypeArguments(), locale) + ">" + ms;
324 }
325 ms += "(" + printMethodArgs(
326 s.type.getParameterTypes(),
327 (s.flags() & VARARGS) != 0,
328 locale) + ")";
329 }
330 return ms;
331 }
332 }
334 @Override
335 public String visitOperatorSymbol(OperatorSymbol s, Locale locale) {
336 return visitMethodSymbol(s, locale);
337 }
339 @Override
340 public String visitPackageSymbol(PackageSymbol s, Locale locale) {
341 return s.isUnnamed()
342 ? localize(locale, "compiler.misc.unnamed.package")
343 : s.fullname.toString();
344 }
346 @Override
347 public String visitTypeSymbol(TypeSymbol s, Locale locale) {
348 return visitSymbol(s, locale);
349 }
351 @Override
352 public String visitVarSymbol(VarSymbol s, Locale locale) {
353 return visitSymbol(s, locale);
354 }
356 @Override
357 public String visitSymbol(Symbol s, Locale locale) {
358 return s.name.toString();
359 }
360 }