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