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

changeset 238
86b60aa941c6
parent 235
850869f70213
child 240
8c55d5b0ed71
equal deleted inserted replaced
237:9711a6c2db7e 238:86b60aa941c6
1 /* 1 /*
2 * Copyright 2003-2008 Sun Microsystems, Inc. All Rights Reserved. 2 * Copyright 2003-2009 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * 4 *
5 * This code is free software; you can redistribute it and/or modify it 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 6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this 7 * published by the Free Software Foundation. Sun designates this
24 */ 24 */
25 25
26 package com.sun.tools.javac.code; 26 package com.sun.tools.javac.code;
27 27
28 import java.util.*; 28 import java.util.*;
29
30 import com.sun.tools.javac.api.Messages;
29 31
30 import com.sun.tools.javac.util.*; 32 import com.sun.tools.javac.util.*;
31 import com.sun.tools.javac.util.List; 33 import com.sun.tools.javac.util.List;
32 34
33 import com.sun.tools.javac.jvm.ClassReader; 35 import com.sun.tools.javac.jvm.ClassReader;
2017 Type subst(Type t) { 2019 Type subst(Type t) {
2018 if (from.tail == null) 2020 if (from.tail == null)
2019 return t; 2021 return t;
2020 else 2022 else
2021 return visit(t); 2023 return visit(t);
2022 } 2024 }
2023 2025
2024 List<Type> subst(List<Type> ts) { 2026 List<Type> subst(List<Type> ts) {
2025 if (from.tail == null) 2027 if (from.tail == null)
2026 return ts; 2028 return ts;
2027 boolean wild = false; 2029 boolean wild = false;
2277 throw new AssertionError(); 2279 throw new AssertionError();
2278 } 2280 }
2279 } 2281 }
2280 // </editor-fold> 2282 // </editor-fold>
2281 2283
2282 // <editor-fold defaultstate="collapsed" desc="printType"> 2284 /**
2283 /** 2285 * Helper method for generating a string representation of a given type
2284 * Visitor for generating a string representation of a given type
2285 * accordingly to a given locale 2286 * accordingly to a given locale
2286 */ 2287 */
2287 public String toString(Type t, Locale locale) { 2288 public String toString(Type t, Locale locale) {
2288 return typePrinter.visit(t, locale); 2289 return Printer.createStandardPrinter(messages).visit(t, locale);
2289 } 2290 }
2290 // where 2291
2291 private TypePrinter typePrinter = new TypePrinter(); 2292 /**
2292 2293 * Helper method for generating a string representation of a given type
2293 public class TypePrinter extends DefaultTypeVisitor<String, Locale> {
2294
2295 public String visit(List<Type> ts, Locale locale) {
2296 ListBuffer<String> sbuf = lb();
2297 for (Type t : ts) {
2298 sbuf.append(visit(t, locale));
2299 }
2300 return sbuf.toList().toString();
2301 }
2302
2303 @Override
2304 public String visitCapturedType(CapturedType t, Locale locale) {
2305 return messages.getLocalizedString("compiler.misc.type.captureof",
2306 (t.hashCode() & 0xFFFFFFFFL) % Type.CapturedType.PRIME,
2307 visit(t.wildcard, locale));
2308 }
2309
2310 @Override
2311 public String visitForAll(ForAll t, Locale locale) {
2312 return "<" + visit(t.tvars, locale) + ">" + visit(t.qtype, locale);
2313 }
2314
2315 @Override
2316 public String visitUndetVar(UndetVar t, Locale locale) {
2317 if (t.inst != null) {
2318 return visit(t.inst, locale);
2319 } else {
2320 return visit(t.qtype, locale) + "?";
2321 }
2322 }
2323
2324 @Override
2325 public String visitArrayType(ArrayType t, Locale locale) {
2326 return visit(t.elemtype, locale) + "[]";
2327 }
2328
2329 @Override
2330 public String visitClassType(ClassType t, Locale locale) {
2331 StringBuffer buf = new StringBuffer();
2332 if (t.getEnclosingType().tag == CLASS && t.tsym.owner.kind == Kinds.TYP) {
2333 buf.append(visit(t.getEnclosingType(), locale));
2334 buf.append(".");
2335 buf.append(className(t, false, locale));
2336 } else {
2337 buf.append(className(t, true, locale));
2338 }
2339 if (t.getTypeArguments().nonEmpty()) {
2340 buf.append('<');
2341 buf.append(visit(t.getTypeArguments(), locale));
2342 buf.append(">");
2343 }
2344 return buf.toString();
2345 }
2346
2347 @Override
2348 public String visitMethodType(MethodType t, Locale locale) {
2349 return "(" + printMethodArgs(t.argtypes, false, locale) + ")" + visit(t.restype, locale);
2350 }
2351
2352 @Override
2353 public String visitPackageType(PackageType t, Locale locale) {
2354 return t.tsym.getQualifiedName().toString();
2355 }
2356
2357 @Override
2358 public String visitWildcardType(WildcardType t, Locale locale) {
2359 StringBuffer s = new StringBuffer();
2360 s.append(t.kind);
2361 if (t.kind != UNBOUND) {
2362 s.append(visit(t.type, locale));
2363 }
2364 return s.toString();
2365 }
2366
2367
2368 public String visitType(Type t, Locale locale) {
2369 String s = (t.tsym == null || t.tsym.name == null)
2370 ? messages.getLocalizedString("compiler.misc.type.none")
2371 : t.tsym.name.toString();
2372 return s;
2373 }
2374
2375 protected String className(ClassType t, boolean longform, Locale locale) {
2376 Symbol sym = t.tsym;
2377 if (sym.name.length() == 0 && (sym.flags() & COMPOUND) != 0) {
2378 StringBuffer s = new StringBuffer(visit(supertype(t), locale));
2379 for (List<Type> is = interfaces(t); is.nonEmpty(); is = is.tail) {
2380 s.append("&");
2381 s.append(visit(is.head, locale));
2382 }
2383 return s.toString();
2384 } else if (sym.name.length() == 0) {
2385 String s;
2386 ClassType norm = (ClassType) t.tsym.type;
2387 if (norm == null) {
2388 s = getLocalizedString(locale, "compiler.misc.anonymous.class", (Object) null);
2389 } else if (interfaces(norm).nonEmpty()) {
2390 s = getLocalizedString(locale, "compiler.misc.anonymous.class",
2391 visit(interfaces(norm).head, locale));
2392 } else {
2393 s = getLocalizedString(locale, "compiler.misc.anonymous.class",
2394 visit(supertype(norm), locale));
2395 }
2396 return s;
2397 } else if (longform) {
2398 return sym.getQualifiedName().toString();
2399 } else {
2400 return sym.name.toString();
2401 }
2402 }
2403
2404 protected String printMethodArgs(List<Type> args, boolean varArgs, Locale locale) {
2405 if (!varArgs) {
2406 return visit(args, locale);
2407 } else {
2408 StringBuffer buf = new StringBuffer();
2409 while (args.tail.nonEmpty()) {
2410 buf.append(visit(args.head, locale));
2411 args = args.tail;
2412 buf.append(',');
2413 }
2414 if (args.head.tag == ARRAY) {
2415 buf.append(visit(((ArrayType) args.head).elemtype, locale));
2416 buf.append("...");
2417 } else {
2418 buf.append(visit(args.head, locale));
2419 }
2420 return buf.toString();
2421 }
2422 }
2423
2424 protected String getLocalizedString(Locale locale, String key, Object... args) {
2425 return messages.getLocalizedString(key, args);
2426 }
2427 };
2428 // </editor-fold>
2429
2430 // <editor-fold defaultstate="collapsed" desc="printSymbol">
2431 /**
2432 * Visitor for generating a string representation of a given symbol
2433 * accordingly to a given locale 2294 * accordingly to a given locale
2434 */ 2295 */
2435 public String toString(Symbol t, Locale locale) { 2296 public String toString(Symbol t, Locale locale) {
2436 return symbolPrinter.visit(t, locale); 2297 return Printer.createStandardPrinter(messages).visit(t, locale);
2437 } 2298 }
2438 // where
2439 private SymbolPrinter symbolPrinter = new SymbolPrinter();
2440
2441 public class SymbolPrinter extends DefaultSymbolVisitor<String, Locale> {
2442
2443 @Override
2444 public String visitClassSymbol(ClassSymbol sym, Locale locale) {
2445 return sym.name.isEmpty()
2446 ? getLocalizedString(locale, "compiler.misc.anonymous.class", sym.flatname)
2447 : sym.fullname.toString();
2448 }
2449
2450 @Override
2451 public String visitMethodSymbol(MethodSymbol s, Locale locale) {
2452 if ((s.flags() & BLOCK) != 0) {
2453 return s.owner.name.toString();
2454 } else {
2455 String ms = (s.name == names.init)
2456 ? s.owner.name.toString()
2457 : s.name.toString();
2458 if (s.type != null) {
2459 if (s.type.tag == FORALL) {
2460 ms = "<" + typePrinter.visit(s.type.getTypeArguments(), locale) + ">" + ms;
2461 }
2462 ms += "(" + typePrinter.printMethodArgs(
2463 s.type.getParameterTypes(),
2464 (s.flags() & VARARGS) != 0,
2465 locale) + ")";
2466 }
2467 return ms;
2468 }
2469 }
2470
2471 @Override
2472 public String visitOperatorSymbol(OperatorSymbol s, Locale locale) {
2473 return visitMethodSymbol(s, locale);
2474 }
2475
2476 @Override
2477 public String visitPackageSymbol(PackageSymbol s, Locale locale) {
2478 return s.name.isEmpty()
2479 ? getLocalizedString(locale, "compiler.misc.unnamed.package")
2480 : s.fullname.toString();
2481 }
2482
2483 @Override
2484 public String visitSymbol(Symbol s, Locale locale) {
2485 return s.name.toString();
2486 }
2487
2488 public String visit(List<Symbol> ts, Locale locale) {
2489 ListBuffer<String> sbuf = lb();
2490 for (Symbol t : ts) {
2491 sbuf.append(visit(t, locale));
2492 }
2493 return sbuf.toList().toString();
2494 }
2495
2496 protected String getLocalizedString(Locale locale, String key, Object... args) {
2497 return messages.getLocalizedString(key, args);
2498 }
2499 };
2500 // </editor-fold>
2501 2299
2502 // <editor-fold defaultstate="collapsed" desc="toString"> 2300 // <editor-fold defaultstate="collapsed" desc="toString">
2503 /** 2301 /**
2504 * This toString is slightly more descriptive than the one on Type. 2302 * This toString is slightly more descriptive than the one on Type.
2505 * 2303 *
3126 return new ClassType(cls.getEnclosingType(), S, cls.tsym); 2924 return new ClassType(cls.getEnclosingType(), S, cls.tsym);
3127 else 2925 else
3128 return t; 2926 return t;
3129 } 2927 }
3130 // where 2928 // where
3131 private List<Type> freshTypeVariables(List<Type> types) { 2929 public List<Type> freshTypeVariables(List<Type> types) {
3132 ListBuffer<Type> result = lb(); 2930 ListBuffer<Type> result = lb();
3133 for (Type t : types) { 2931 for (Type t : types) {
3134 if (t.tag == WILDCARD) { 2932 if (t.tag == WILDCARD) {
3135 Type bound = ((WildcardType)t).getExtendsBound(); 2933 Type bound = ((WildcardType)t).getExtendsBound();
3136 if (bound == null) 2934 if (bound == null)

mercurial