1.1 --- a/src/share/classes/com/sun/tools/javac/code/Types.java Mon Mar 09 13:34:19 2009 -0700 1.2 +++ b/src/share/classes/com/sun/tools/javac/code/Types.java Mon Mar 09 23:53:41 2009 -0700 1.3 @@ -27,6 +27,8 @@ 1.4 1.5 import java.util.*; 1.6 1.7 +import com.sun.tools.javac.api.Messages; 1.8 + 1.9 import com.sun.tools.javac.util.*; 1.10 import com.sun.tools.javac.util.List; 1.11 1.12 @@ -1010,8 +1012,8 @@ 1.13 && !disjointTypes(aHigh.allparams(), lowSub.allparams()) 1.14 && !disjointTypes(aLow.allparams(), highSub.allparams()) 1.15 && !disjointTypes(aLow.allparams(), lowSub.allparams())) { 1.16 - if (upcast ? giveWarning(a, highSub) || giveWarning(a, lowSub) 1.17 - : giveWarning(highSub, a) || giveWarning(lowSub, a)) 1.18 + if (upcast ? giveWarning(a, b) : 1.19 + giveWarning(b, a)) 1.20 warnStack.head.warnUnchecked(); 1.21 return true; 1.22 } 1.23 @@ -2019,7 +2021,7 @@ 1.24 return t; 1.25 else 1.26 return visit(t); 1.27 - } 1.28 + } 1.29 1.30 List<Type> subst(List<Type> ts) { 1.31 if (from.tail == null) 1.32 @@ -2279,225 +2281,21 @@ 1.33 } 1.34 // </editor-fold> 1.35 1.36 - // <editor-fold defaultstate="collapsed" desc="printType"> 1.37 /** 1.38 - * Visitor for generating a string representation of a given type 1.39 + * Helper method for generating a string representation of a given type 1.40 * accordingly to a given locale 1.41 */ 1.42 public String toString(Type t, Locale locale) { 1.43 - return typePrinter.visit(t, locale); 1.44 + return Printer.createStandardPrinter(messages).visit(t, locale); 1.45 } 1.46 - // where 1.47 - private TypePrinter typePrinter = new TypePrinter(); 1.48 1.49 - public class TypePrinter extends DefaultTypeVisitor<String, Locale> { 1.50 - 1.51 - public String visit(List<Type> ts, Locale locale) { 1.52 - ListBuffer<String> sbuf = lb(); 1.53 - for (Type t : ts) { 1.54 - sbuf.append(visit(t, locale)); 1.55 - } 1.56 - return sbuf.toList().toString(); 1.57 - } 1.58 - 1.59 - @Override 1.60 - public String visitCapturedType(CapturedType t, Locale locale) { 1.61 - return messages.getLocalizedString("compiler.misc.type.captureof", 1.62 - (t.hashCode() & 0xFFFFFFFFL) % Type.CapturedType.PRIME, 1.63 - visit(t.wildcard, locale)); 1.64 - } 1.65 - 1.66 - @Override 1.67 - public String visitForAll(ForAll t, Locale locale) { 1.68 - return "<" + visit(t.tvars, locale) + ">" + visit(t.qtype, locale); 1.69 - } 1.70 - 1.71 - @Override 1.72 - public String visitUndetVar(UndetVar t, Locale locale) { 1.73 - if (t.inst != null) { 1.74 - return visit(t.inst, locale); 1.75 - } else { 1.76 - return visit(t.qtype, locale) + "?"; 1.77 - } 1.78 - } 1.79 - 1.80 - @Override 1.81 - public String visitArrayType(ArrayType t, Locale locale) { 1.82 - return visit(t.elemtype, locale) + "[]"; 1.83 - } 1.84 - 1.85 - @Override 1.86 - public String visitClassType(ClassType t, Locale locale) { 1.87 - StringBuffer buf = new StringBuffer(); 1.88 - if (t.getEnclosingType().tag == CLASS && t.tsym.owner.kind == Kinds.TYP) { 1.89 - buf.append(visit(t.getEnclosingType(), locale)); 1.90 - buf.append("."); 1.91 - buf.append(className(t, false, locale)); 1.92 - } else { 1.93 - buf.append(className(t, true, locale)); 1.94 - } 1.95 - if (t.getTypeArguments().nonEmpty()) { 1.96 - buf.append('<'); 1.97 - buf.append(visit(t.getTypeArguments(), locale)); 1.98 - buf.append(">"); 1.99 - } 1.100 - return buf.toString(); 1.101 - } 1.102 - 1.103 - @Override 1.104 - public String visitMethodType(MethodType t, Locale locale) { 1.105 - return "(" + printMethodArgs(t.argtypes, false, locale) + ")" + visit(t.restype, locale); 1.106 - } 1.107 - 1.108 - @Override 1.109 - public String visitPackageType(PackageType t, Locale locale) { 1.110 - return t.tsym.getQualifiedName().toString(); 1.111 - } 1.112 - 1.113 - @Override 1.114 - public String visitWildcardType(WildcardType t, Locale locale) { 1.115 - StringBuffer s = new StringBuffer(); 1.116 - s.append(t.kind); 1.117 - if (t.kind != UNBOUND) { 1.118 - s.append(visit(t.type, locale)); 1.119 - } 1.120 - return s.toString(); 1.121 - } 1.122 - 1.123 - 1.124 - public String visitType(Type t, Locale locale) { 1.125 - String s = (t.tsym == null || t.tsym.name == null) 1.126 - ? messages.getLocalizedString("compiler.misc.type.none") 1.127 - : t.tsym.name.toString(); 1.128 - return s; 1.129 - } 1.130 - 1.131 - protected String className(ClassType t, boolean longform, Locale locale) { 1.132 - Symbol sym = t.tsym; 1.133 - if (sym.name.length() == 0 && (sym.flags() & COMPOUND) != 0) { 1.134 - StringBuffer s = new StringBuffer(visit(supertype(t), locale)); 1.135 - for (List<Type> is = interfaces(t); is.nonEmpty(); is = is.tail) { 1.136 - s.append("&"); 1.137 - s.append(visit(is.head, locale)); 1.138 - } 1.139 - return s.toString(); 1.140 - } else if (sym.name.length() == 0) { 1.141 - String s; 1.142 - ClassType norm = (ClassType) t.tsym.type; 1.143 - if (norm == null) { 1.144 - s = getLocalizedString(locale, "compiler.misc.anonymous.class", (Object) null); 1.145 - } else if (interfaces(norm).nonEmpty()) { 1.146 - s = getLocalizedString(locale, "compiler.misc.anonymous.class", 1.147 - visit(interfaces(norm).head, locale)); 1.148 - } else { 1.149 - s = getLocalizedString(locale, "compiler.misc.anonymous.class", 1.150 - visit(supertype(norm), locale)); 1.151 - } 1.152 - return s; 1.153 - } else if (longform) { 1.154 - return sym.getQualifiedName().toString(); 1.155 - } else { 1.156 - return sym.name.toString(); 1.157 - } 1.158 - } 1.159 - 1.160 - protected String printMethodArgs(List<Type> args, boolean varArgs, Locale locale) { 1.161 - if (!varArgs) { 1.162 - return visit(args, locale); 1.163 - } else { 1.164 - StringBuffer buf = new StringBuffer(); 1.165 - while (args.tail.nonEmpty()) { 1.166 - buf.append(visit(args.head, locale)); 1.167 - args = args.tail; 1.168 - buf.append(','); 1.169 - } 1.170 - if (args.head.tag == ARRAY) { 1.171 - buf.append(visit(((ArrayType) args.head).elemtype, locale)); 1.172 - buf.append("..."); 1.173 - } else { 1.174 - buf.append(visit(args.head, locale)); 1.175 - } 1.176 - return buf.toString(); 1.177 - } 1.178 - } 1.179 - 1.180 - protected String getLocalizedString(Locale locale, String key, Object... args) { 1.181 - return messages.getLocalizedString(key, args); 1.182 - } 1.183 - }; 1.184 - // </editor-fold> 1.185 - 1.186 - // <editor-fold defaultstate="collapsed" desc="printSymbol"> 1.187 /** 1.188 - * Visitor for generating a string representation of a given symbol 1.189 + * Helper method for generating a string representation of a given type 1.190 * accordingly to a given locale 1.191 */ 1.192 public String toString(Symbol t, Locale locale) { 1.193 - return symbolPrinter.visit(t, locale); 1.194 + return Printer.createStandardPrinter(messages).visit(t, locale); 1.195 } 1.196 - // where 1.197 - private SymbolPrinter symbolPrinter = new SymbolPrinter(); 1.198 - 1.199 - public class SymbolPrinter extends DefaultSymbolVisitor<String, Locale> { 1.200 - 1.201 - @Override 1.202 - public String visitClassSymbol(ClassSymbol sym, Locale locale) { 1.203 - return sym.name.isEmpty() 1.204 - ? getLocalizedString(locale, "compiler.misc.anonymous.class", sym.flatname) 1.205 - : sym.fullname.toString(); 1.206 - } 1.207 - 1.208 - @Override 1.209 - public String visitMethodSymbol(MethodSymbol s, Locale locale) { 1.210 - if ((s.flags() & BLOCK) != 0) { 1.211 - return s.owner.name.toString(); 1.212 - } else { 1.213 - String ms = (s.name == names.init) 1.214 - ? s.owner.name.toString() 1.215 - : s.name.toString(); 1.216 - if (s.type != null) { 1.217 - if (s.type.tag == FORALL) { 1.218 - ms = "<" + typePrinter.visit(s.type.getTypeArguments(), locale) + ">" + ms; 1.219 - } 1.220 - ms += "(" + typePrinter.printMethodArgs( 1.221 - s.type.getParameterTypes(), 1.222 - (s.flags() & VARARGS) != 0, 1.223 - locale) + ")"; 1.224 - } 1.225 - return ms; 1.226 - } 1.227 - } 1.228 - 1.229 - @Override 1.230 - public String visitOperatorSymbol(OperatorSymbol s, Locale locale) { 1.231 - return visitMethodSymbol(s, locale); 1.232 - } 1.233 - 1.234 - @Override 1.235 - public String visitPackageSymbol(PackageSymbol s, Locale locale) { 1.236 - return s.name.isEmpty() 1.237 - ? getLocalizedString(locale, "compiler.misc.unnamed.package") 1.238 - : s.fullname.toString(); 1.239 - } 1.240 - 1.241 - @Override 1.242 - public String visitSymbol(Symbol s, Locale locale) { 1.243 - return s.name.toString(); 1.244 - } 1.245 - 1.246 - public String visit(List<Symbol> ts, Locale locale) { 1.247 - ListBuffer<String> sbuf = lb(); 1.248 - for (Symbol t : ts) { 1.249 - sbuf.append(visit(t, locale)); 1.250 - } 1.251 - return sbuf.toList().toString(); 1.252 - } 1.253 - 1.254 - protected String getLocalizedString(Locale locale, String key, Object... args) { 1.255 - return messages.getLocalizedString(key, args); 1.256 - } 1.257 - }; 1.258 - // </editor-fold> 1.259 1.260 // <editor-fold defaultstate="collapsed" desc="toString"> 1.261 /** 1.262 @@ -3128,7 +2926,7 @@ 1.263 return t; 1.264 } 1.265 // where 1.266 - private List<Type> freshTypeVariables(List<Type> types) { 1.267 + public List<Type> freshTypeVariables(List<Type> types) { 1.268 ListBuffer<Type> result = lb(); 1.269 for (Type t : types) { 1.270 if (t.tag == WILDCARD) { 1.271 @@ -3224,9 +3022,11 @@ 1.272 } 1.273 1.274 private boolean giveWarning(Type from, Type to) { 1.275 - // To and from are (possibly different) parameterizations 1.276 - // of the same class or interface 1.277 - return to.isParameterized() && !containsType(to.allparams(), from.allparams()); 1.278 + Type subFrom = asSub(from, to.tsym); 1.279 + return to.isParameterized() && 1.280 + (!(isUnbounded(to) || 1.281 + isSubtype(from, to) || 1.282 + ((subFrom != null) && isSameType(subFrom, to)))); 1.283 } 1.284 1.285 private List<Type> superClosure(Type t, Type s) {