Types.java revision 2673:bf8500822576
1/* 2 * Copyright (c) 2003, 2014, 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 */ 25 26package com.sun.tools.javac.code; 27 28import java.lang.ref.SoftReference; 29import java.util.HashSet; 30import java.util.HashMap; 31import java.util.Locale; 32import java.util.Map; 33import java.util.Set; 34import java.util.WeakHashMap; 35 36import javax.tools.JavaFileObject; 37 38import com.sun.tools.javac.code.Attribute.RetentionPolicy; 39import com.sun.tools.javac.code.Lint.LintCategory; 40import com.sun.tools.javac.code.Type.UndetVar.InferenceBound; 41import com.sun.tools.javac.comp.AttrContext; 42import com.sun.tools.javac.comp.Check; 43import com.sun.tools.javac.comp.Enter; 44import com.sun.tools.javac.comp.Env; 45import com.sun.tools.javac.util.*; 46 47import static com.sun.tools.javac.code.BoundKind.*; 48import static com.sun.tools.javac.code.Flags.*; 49import static com.sun.tools.javac.code.Kinds.Kind.*; 50import static com.sun.tools.javac.code.Scope.*; 51import static com.sun.tools.javac.code.Symbol.*; 52import static com.sun.tools.javac.code.Type.*; 53import static com.sun.tools.javac.code.TypeTag.*; 54import static com.sun.tools.javac.jvm.ClassFile.externalize; 55 56/** 57 * Utility class containing various operations on types. 58 * 59 * <p>Unless other names are more illustrative, the following naming 60 * conventions should be observed in this file: 61 * 62 * <dl> 63 * <dt>t</dt> 64 * <dd>If the first argument to an operation is a type, it should be named t.</dd> 65 * <dt>s</dt> 66 * <dd>Similarly, if the second argument to an operation is a type, it should be named s.</dd> 67 * <dt>ts</dt> 68 * <dd>If an operations takes a list of types, the first should be named ts.</dd> 69 * <dt>ss</dt> 70 * <dd>A second list of types should be named ss.</dd> 71 * </dl> 72 * 73 * <p><b>This is NOT part of any supported API. 74 * If you write code that depends on this, you do so at your own risk. 75 * This code and its internal interfaces are subject to change or 76 * deletion without notice.</b> 77 */ 78public class Types { 79 protected static final Context.Key<Types> typesKey = new Context.Key<>(); 80 81 final Symtab syms; 82 final JavacMessages messages; 83 final Names names; 84 final boolean allowObjectToPrimitiveCast; 85 final Check chk; 86 final Enter enter; 87 JCDiagnostic.Factory diags; 88 List<Warner> warnStack = List.nil(); 89 final Name capturedName; 90 private final FunctionDescriptorLookupError functionDescriptorLookupError; 91 92 public final Warner noWarnings; 93 94 // <editor-fold defaultstate="collapsed" desc="Instantiating"> 95 public static Types instance(Context context) { 96 Types instance = context.get(typesKey); 97 if (instance == null) 98 instance = new Types(context); 99 return instance; 100 } 101 102 protected Types(Context context) { 103 context.put(typesKey, this); 104 syms = Symtab.instance(context); 105 names = Names.instance(context); 106 Source source = Source.instance(context); 107 allowObjectToPrimitiveCast = source.allowObjectToPrimitiveCast(); 108 chk = Check.instance(context); 109 enter = Enter.instance(context); 110 capturedName = names.fromString("<captured wildcard>"); 111 messages = JavacMessages.instance(context); 112 diags = JCDiagnostic.Factory.instance(context); 113 functionDescriptorLookupError = new FunctionDescriptorLookupError(); 114 noWarnings = new Warner(null); 115 } 116 // </editor-fold> 117 118 // <editor-fold defaultstate="collapsed" desc="bounds"> 119 /** 120 * Get a wildcard's upper bound, returning non-wildcards unchanged. 121 * @param t a type argument, either a wildcard or a type 122 */ 123 public Type wildUpperBound(Type t) { 124 if (t.hasTag(WILDCARD)) { 125 WildcardType w = (WildcardType) t; 126 if (w.isSuperBound()) 127 return w.bound == null ? syms.objectType : w.bound.bound; 128 else 129 return wildUpperBound(w.type); 130 } 131 else return t; 132 } 133 134 /** 135 * Get a capture variable's upper bound, returning other types unchanged. 136 * @param t a type 137 */ 138 public Type cvarUpperBound(Type t) { 139 if (t.hasTag(TYPEVAR)) { 140 TypeVar v = (TypeVar) t; 141 return v.isCaptured() ? cvarUpperBound(v.bound) : v; 142 } 143 else return t; 144 } 145 146 /** 147 * Get a wildcard's lower bound, returning non-wildcards unchanged. 148 * @param t a type argument, either a wildcard or a type 149 */ 150 public Type wildLowerBound(Type t) { 151 if (t.hasTag(WILDCARD)) { 152 WildcardType w = (WildcardType) t; 153 return w.isExtendsBound() ? syms.botType : wildLowerBound(w.type); 154 } 155 else return t; 156 } 157 158 /** 159 * Get a capture variable's lower bound, returning other types unchanged. 160 * @param t a type 161 */ 162 public Type cvarLowerBound(Type t) { 163 if (t.hasTag(TYPEVAR) && ((TypeVar) t).isCaptured()) { 164 return cvarLowerBound(t.getLowerBound()); 165 } 166 else return t; 167 } 168 // </editor-fold> 169 170 // <editor-fold defaultstate="collapsed" desc="isUnbounded"> 171 /** 172 * Checks that all the arguments to a class are unbounded 173 * wildcards or something else that doesn't make any restrictions 174 * on the arguments. If a class isUnbounded, a raw super- or 175 * subclass can be cast to it without a warning. 176 * @param t a type 177 * @return true iff the given type is unbounded or raw 178 */ 179 public boolean isUnbounded(Type t) { 180 return isUnbounded.visit(t); 181 } 182 // where 183 private final UnaryVisitor<Boolean> isUnbounded = new UnaryVisitor<Boolean>() { 184 185 public Boolean visitType(Type t, Void ignored) { 186 return true; 187 } 188 189 @Override 190 public Boolean visitClassType(ClassType t, Void ignored) { 191 List<Type> parms = t.tsym.type.allparams(); 192 List<Type> args = t.allparams(); 193 while (parms.nonEmpty()) { 194 WildcardType unb = new WildcardType(syms.objectType, 195 BoundKind.UNBOUND, 196 syms.boundClass, 197 (TypeVar)parms.head); 198 if (!containsType(args.head, unb)) 199 return false; 200 parms = parms.tail; 201 args = args.tail; 202 } 203 return true; 204 } 205 }; 206 // </editor-fold> 207 208 // <editor-fold defaultstate="collapsed" desc="asSub"> 209 /** 210 * Return the least specific subtype of t that starts with symbol 211 * sym. If none exists, return null. The least specific subtype 212 * is determined as follows: 213 * 214 * <p>If there is exactly one parameterized instance of sym that is a 215 * subtype of t, that parameterized instance is returned.<br> 216 * Otherwise, if the plain type or raw type `sym' is a subtype of 217 * type t, the type `sym' itself is returned. Otherwise, null is 218 * returned. 219 */ 220 public Type asSub(Type t, Symbol sym) { 221 return asSub.visit(t, sym); 222 } 223 // where 224 private final SimpleVisitor<Type,Symbol> asSub = new SimpleVisitor<Type,Symbol>() { 225 226 public Type visitType(Type t, Symbol sym) { 227 return null; 228 } 229 230 @Override 231 public Type visitClassType(ClassType t, Symbol sym) { 232 if (t.tsym == sym) 233 return t; 234 Type base = asSuper(sym.type, t.tsym); 235 if (base == null) 236 return null; 237 ListBuffer<Type> from = new ListBuffer<>(); 238 ListBuffer<Type> to = new ListBuffer<>(); 239 try { 240 adapt(base, t, from, to); 241 } catch (AdaptFailure ex) { 242 return null; 243 } 244 Type res = subst(sym.type, from.toList(), to.toList()); 245 if (!isSubtype(res, t)) 246 return null; 247 ListBuffer<Type> openVars = new ListBuffer<>(); 248 for (List<Type> l = sym.type.allparams(); 249 l.nonEmpty(); l = l.tail) 250 if (res.contains(l.head) && !t.contains(l.head)) 251 openVars.append(l.head); 252 if (openVars.nonEmpty()) { 253 if (t.isRaw()) { 254 // The subtype of a raw type is raw 255 res = erasure(res); 256 } else { 257 // Unbound type arguments default to ? 258 List<Type> opens = openVars.toList(); 259 ListBuffer<Type> qs = new ListBuffer<>(); 260 for (List<Type> iter = opens; iter.nonEmpty(); iter = iter.tail) { 261 qs.append(new WildcardType(syms.objectType, BoundKind.UNBOUND, 262 syms.boundClass, (TypeVar) iter.head)); 263 } 264 res = subst(res, opens, qs.toList()); 265 } 266 } 267 return res; 268 } 269 270 @Override 271 public Type visitErrorType(ErrorType t, Symbol sym) { 272 return t; 273 } 274 }; 275 // </editor-fold> 276 277 // <editor-fold defaultstate="collapsed" desc="isConvertible"> 278 /** 279 * Is t a subtype of or convertible via boxing/unboxing 280 * conversion to s? 281 */ 282 public boolean isConvertible(Type t, Type s, Warner warn) { 283 if (t.hasTag(ERROR)) { 284 return true; 285 } 286 boolean tPrimitive = t.isPrimitive(); 287 boolean sPrimitive = s.isPrimitive(); 288 if (tPrimitive == sPrimitive) { 289 return isSubtypeUnchecked(t, s, warn); 290 } 291 return tPrimitive 292 ? isSubtype(boxedClass(t).type, s) 293 : isSubtype(unboxedType(t), s); 294 } 295 296 /** 297 * Is t a subtype of or convertible via boxing/unboxing 298 * conversions to s? 299 */ 300 public boolean isConvertible(Type t, Type s) { 301 return isConvertible(t, s, noWarnings); 302 } 303 // </editor-fold> 304 305 // <editor-fold defaultstate="collapsed" desc="findSam"> 306 307 /** 308 * Exception used to report a function descriptor lookup failure. The exception 309 * wraps a diagnostic that can be used to generate more details error 310 * messages. 311 */ 312 public static class FunctionDescriptorLookupError extends RuntimeException { 313 private static final long serialVersionUID = 0; 314 315 JCDiagnostic diagnostic; 316 317 FunctionDescriptorLookupError() { 318 this.diagnostic = null; 319 } 320 321 FunctionDescriptorLookupError setMessage(JCDiagnostic diag) { 322 this.diagnostic = diag; 323 return this; 324 } 325 326 public JCDiagnostic getDiagnostic() { 327 return diagnostic; 328 } 329 } 330 331 /** 332 * A cache that keeps track of function descriptors associated with given 333 * functional interfaces. 334 */ 335 class DescriptorCache { 336 337 private WeakHashMap<TypeSymbol, Entry> _map = new WeakHashMap<>(); 338 339 class FunctionDescriptor { 340 Symbol descSym; 341 342 FunctionDescriptor(Symbol descSym) { 343 this.descSym = descSym; 344 } 345 346 public Symbol getSymbol() { 347 return descSym; 348 } 349 350 public Type getType(Type site) { 351 site = removeWildcards(site); 352 if (!chk.checkValidGenericType(site)) { 353 //if the inferred functional interface type is not well-formed, 354 //or if it's not a subtype of the original target, issue an error 355 throw failure(diags.fragment("no.suitable.functional.intf.inst", site)); 356 } 357 return memberType(site, descSym); 358 } 359 } 360 361 class Entry { 362 final FunctionDescriptor cachedDescRes; 363 final int prevMark; 364 365 public Entry(FunctionDescriptor cachedDescRes, 366 int prevMark) { 367 this.cachedDescRes = cachedDescRes; 368 this.prevMark = prevMark; 369 } 370 371 boolean matches(int mark) { 372 return this.prevMark == mark; 373 } 374 } 375 376 FunctionDescriptor get(TypeSymbol origin) throws FunctionDescriptorLookupError { 377 Entry e = _map.get(origin); 378 CompoundScope members = membersClosure(origin.type, false); 379 if (e == null || 380 !e.matches(members.getMark())) { 381 FunctionDescriptor descRes = findDescriptorInternal(origin, members); 382 _map.put(origin, new Entry(descRes, members.getMark())); 383 return descRes; 384 } 385 else { 386 return e.cachedDescRes; 387 } 388 } 389 390 /** 391 * Compute the function descriptor associated with a given functional interface 392 */ 393 public FunctionDescriptor findDescriptorInternal(TypeSymbol origin, 394 CompoundScope membersCache) throws FunctionDescriptorLookupError { 395 if (!origin.isInterface() || (origin.flags() & ANNOTATION) != 0) { 396 //t must be an interface 397 throw failure("not.a.functional.intf", origin); 398 } 399 400 final ListBuffer<Symbol> abstracts = new ListBuffer<>(); 401 for (Symbol sym : membersCache.getSymbols(new DescriptorFilter(origin))) { 402 Type mtype = memberType(origin.type, sym); 403 if (abstracts.isEmpty() || 404 (sym.name == abstracts.first().name && 405 overrideEquivalent(mtype, memberType(origin.type, abstracts.first())))) { 406 abstracts.append(sym); 407 } else { 408 //the target method(s) should be the only abstract members of t 409 throw failure("not.a.functional.intf.1", origin, 410 diags.fragment("incompatible.abstracts", Kinds.kindName(origin), origin)); 411 } 412 } 413 if (abstracts.isEmpty()) { 414 //t must define a suitable non-generic method 415 throw failure("not.a.functional.intf.1", origin, 416 diags.fragment("no.abstracts", Kinds.kindName(origin), origin)); 417 } else if (abstracts.size() == 1) { 418 return new FunctionDescriptor(abstracts.first()); 419 } else { // size > 1 420 FunctionDescriptor descRes = mergeDescriptors(origin, abstracts.toList()); 421 if (descRes == null) { 422 //we can get here if the functional interface is ill-formed 423 ListBuffer<JCDiagnostic> descriptors = new ListBuffer<>(); 424 for (Symbol desc : abstracts) { 425 String key = desc.type.getThrownTypes().nonEmpty() ? 426 "descriptor.throws" : "descriptor"; 427 descriptors.append(diags.fragment(key, desc.name, 428 desc.type.getParameterTypes(), 429 desc.type.getReturnType(), 430 desc.type.getThrownTypes())); 431 } 432 JCDiagnostic.MultilineDiagnostic incompatibleDescriptors = 433 new JCDiagnostic.MultilineDiagnostic(diags.fragment("incompatible.descs.in.functional.intf", 434 Kinds.kindName(origin), origin), descriptors.toList()); 435 throw failure(incompatibleDescriptors); 436 } 437 return descRes; 438 } 439 } 440 441 /** 442 * Compute a synthetic type for the target descriptor given a list 443 * of override-equivalent methods in the functional interface type. 444 * The resulting method type is a method type that is override-equivalent 445 * and return-type substitutable with each method in the original list. 446 */ 447 private FunctionDescriptor mergeDescriptors(TypeSymbol origin, List<Symbol> methodSyms) { 448 //pick argument types - simply take the signature that is a 449 //subsignature of all other signatures in the list (as per JLS 8.4.2) 450 List<Symbol> mostSpecific = List.nil(); 451 outer: for (Symbol msym1 : methodSyms) { 452 Type mt1 = memberType(origin.type, msym1); 453 for (Symbol msym2 : methodSyms) { 454 Type mt2 = memberType(origin.type, msym2); 455 if (!isSubSignature(mt1, mt2)) { 456 continue outer; 457 } 458 } 459 mostSpecific = mostSpecific.prepend(msym1); 460 } 461 if (mostSpecific.isEmpty()) { 462 return null; 463 } 464 465 466 //pick return types - this is done in two phases: (i) first, the most 467 //specific return type is chosen using strict subtyping; if this fails, 468 //a second attempt is made using return type substitutability (see JLS 8.4.5) 469 boolean phase2 = false; 470 Symbol bestSoFar = null; 471 while (bestSoFar == null) { 472 outer: for (Symbol msym1 : mostSpecific) { 473 Type mt1 = memberType(origin.type, msym1); 474 for (Symbol msym2 : methodSyms) { 475 Type mt2 = memberType(origin.type, msym2); 476 if (phase2 ? 477 !returnTypeSubstitutable(mt1, mt2) : 478 !isSubtypeInternal(mt1.getReturnType(), mt2.getReturnType())) { 479 continue outer; 480 } 481 } 482 bestSoFar = msym1; 483 } 484 if (phase2) { 485 break; 486 } else { 487 phase2 = true; 488 } 489 } 490 if (bestSoFar == null) return null; 491 492 //merge thrown types - form the intersection of all the thrown types in 493 //all the signatures in the list 494 boolean toErase = !bestSoFar.type.hasTag(FORALL); 495 List<Type> thrown = null; 496 Type mt1 = memberType(origin.type, bestSoFar); 497 for (Symbol msym2 : methodSyms) { 498 Type mt2 = memberType(origin.type, msym2); 499 List<Type> thrown_mt2 = mt2.getThrownTypes(); 500 if (toErase) { 501 thrown_mt2 = erasure(thrown_mt2); 502 } else { 503 /* If bestSoFar is generic then all the methods are generic. 504 * The opposite is not true: a non generic method can override 505 * a generic method (raw override) so it's safe to cast mt1 and 506 * mt2 to ForAll. 507 */ 508 ForAll fa1 = (ForAll)mt1; 509 ForAll fa2 = (ForAll)mt2; 510 thrown_mt2 = subst(thrown_mt2, fa2.tvars, fa1.tvars); 511 } 512 thrown = (thrown == null) ? 513 thrown_mt2 : 514 chk.intersect(thrown_mt2, thrown); 515 } 516 517 final List<Type> thrown1 = thrown; 518 return new FunctionDescriptor(bestSoFar) { 519 @Override 520 public Type getType(Type origin) { 521 Type mt = memberType(origin, getSymbol()); 522 return createMethodTypeWithThrown(mt, thrown1); 523 } 524 }; 525 } 526 527 boolean isSubtypeInternal(Type s, Type t) { 528 return (s.isPrimitive() && t.isPrimitive()) ? 529 isSameType(t, s) : 530 isSubtype(s, t); 531 } 532 533 FunctionDescriptorLookupError failure(String msg, Object... args) { 534 return failure(diags.fragment(msg, args)); 535 } 536 537 FunctionDescriptorLookupError failure(JCDiagnostic diag) { 538 return functionDescriptorLookupError.setMessage(diag); 539 } 540 } 541 542 private DescriptorCache descCache = new DescriptorCache(); 543 544 /** 545 * Find the method descriptor associated to this class symbol - if the 546 * symbol 'origin' is not a functional interface, an exception is thrown. 547 */ 548 public Symbol findDescriptorSymbol(TypeSymbol origin) throws FunctionDescriptorLookupError { 549 return descCache.get(origin).getSymbol(); 550 } 551 552 /** 553 * Find the type of the method descriptor associated to this class symbol - 554 * if the symbol 'origin' is not a functional interface, an exception is thrown. 555 */ 556 public Type findDescriptorType(Type origin) throws FunctionDescriptorLookupError { 557 return descCache.get(origin.tsym).getType(origin); 558 } 559 560 /** 561 * Is given type a functional interface? 562 */ 563 public boolean isFunctionalInterface(TypeSymbol tsym) { 564 try { 565 findDescriptorSymbol(tsym); 566 return true; 567 } catch (FunctionDescriptorLookupError ex) { 568 return false; 569 } 570 } 571 572 public boolean isFunctionalInterface(Type site) { 573 try { 574 findDescriptorType(site); 575 return true; 576 } catch (FunctionDescriptorLookupError ex) { 577 return false; 578 } 579 } 580 581 public Type removeWildcards(Type site) { 582 Type capturedSite = capture(site); 583 if (capturedSite != site) { 584 Type formalInterface = site.tsym.type; 585 ListBuffer<Type> typeargs = new ListBuffer<>(); 586 List<Type> actualTypeargs = site.getTypeArguments(); 587 List<Type> capturedTypeargs = capturedSite.getTypeArguments(); 588 //simply replace the wildcards with its bound 589 for (Type t : formalInterface.getTypeArguments()) { 590 if (actualTypeargs.head.hasTag(WILDCARD)) { 591 WildcardType wt = (WildcardType)actualTypeargs.head; 592 Type bound; 593 switch (wt.kind) { 594 case EXTENDS: 595 case UNBOUND: 596 CapturedType capVar = (CapturedType)capturedTypeargs.head; 597 //use declared bound if it doesn't depend on formal type-args 598 bound = capVar.bound.containsAny(capturedSite.getTypeArguments()) ? 599 wt.type : capVar.bound; 600 break; 601 default: 602 bound = wt.type; 603 } 604 typeargs.append(bound); 605 } else { 606 typeargs.append(actualTypeargs.head); 607 } 608 actualTypeargs = actualTypeargs.tail; 609 capturedTypeargs = capturedTypeargs.tail; 610 } 611 return subst(formalInterface, formalInterface.getTypeArguments(), typeargs.toList()); 612 } else { 613 return site; 614 } 615 } 616 617 /** 618 * Create a symbol for a class that implements a given functional interface 619 * and overrides its functional descriptor. This routine is used for two 620 * main purposes: (i) checking well-formedness of a functional interface; 621 * (ii) perform functional interface bridge calculation. 622 */ 623 public ClassSymbol makeFunctionalInterfaceClass(Env<AttrContext> env, Name name, List<Type> targets, long cflags) { 624 if (targets.isEmpty()) { 625 return null; 626 } 627 Symbol descSym = findDescriptorSymbol(targets.head.tsym); 628 Type descType = findDescriptorType(targets.head); 629 ClassSymbol csym = new ClassSymbol(cflags, name, env.enclClass.sym.outermostClass()); 630 csym.completer = null; 631 csym.members_field = WriteableScope.create(csym); 632 MethodSymbol instDescSym = new MethodSymbol(descSym.flags(), descSym.name, descType, csym); 633 csym.members_field.enter(instDescSym); 634 Type.ClassType ctype = new Type.ClassType(Type.noType, List.<Type>nil(), csym); 635 ctype.supertype_field = syms.objectType; 636 ctype.interfaces_field = targets; 637 csym.type = ctype; 638 csym.sourcefile = ((ClassSymbol)csym.owner).sourcefile; 639 return csym; 640 } 641 642 /** 643 * Find the minimal set of methods that are overridden by the functional 644 * descriptor in 'origin'. All returned methods are assumed to have different 645 * erased signatures. 646 */ 647 public List<Symbol> functionalInterfaceBridges(TypeSymbol origin) { 648 Assert.check(isFunctionalInterface(origin)); 649 Symbol descSym = findDescriptorSymbol(origin); 650 CompoundScope members = membersClosure(origin.type, false); 651 ListBuffer<Symbol> overridden = new ListBuffer<>(); 652 outer: for (Symbol m2 : members.getSymbolsByName(descSym.name, bridgeFilter)) { 653 if (m2 == descSym) continue; 654 else if (descSym.overrides(m2, origin, Types.this, false)) { 655 for (Symbol m3 : overridden) { 656 if (isSameType(m3.erasure(Types.this), m2.erasure(Types.this)) || 657 (m3.overrides(m2, origin, Types.this, false) && 658 (pendingBridges((ClassSymbol)origin, m3.enclClass()) || 659 (((MethodSymbol)m2).binaryImplementation((ClassSymbol)m3.owner, Types.this) != null)))) { 660 continue outer; 661 } 662 } 663 overridden.add(m2); 664 } 665 } 666 return overridden.toList(); 667 } 668 //where 669 private Filter<Symbol> bridgeFilter = new Filter<Symbol>() { 670 public boolean accepts(Symbol t) { 671 return t.kind == MTH && 672 t.name != names.init && 673 t.name != names.clinit && 674 (t.flags() & SYNTHETIC) == 0; 675 } 676 }; 677 private boolean pendingBridges(ClassSymbol origin, TypeSymbol s) { 678 //a symbol will be completed from a classfile if (a) symbol has 679 //an associated file object with CLASS kind and (b) the symbol has 680 //not been entered 681 if (origin.classfile != null && 682 origin.classfile.getKind() == JavaFileObject.Kind.CLASS && 683 enter.getEnv(origin) == null) { 684 return false; 685 } 686 if (origin == s) { 687 return true; 688 } 689 for (Type t : interfaces(origin.type)) { 690 if (pendingBridges((ClassSymbol)t.tsym, s)) { 691 return true; 692 } 693 } 694 return false; 695 } 696 // </editor-fold> 697 698 /** 699 * Scope filter used to skip methods that should be ignored (such as methods 700 * overridden by j.l.Object) during function interface conversion interface check 701 */ 702 class DescriptorFilter implements Filter<Symbol> { 703 704 TypeSymbol origin; 705 706 DescriptorFilter(TypeSymbol origin) { 707 this.origin = origin; 708 } 709 710 @Override 711 public boolean accepts(Symbol sym) { 712 return sym.kind == MTH && 713 (sym.flags() & (ABSTRACT | DEFAULT)) == ABSTRACT && 714 !overridesObjectMethod(origin, sym) && 715 (interfaceCandidates(origin.type, (MethodSymbol)sym).head.flags() & DEFAULT) == 0; 716 } 717 } 718 719 // <editor-fold defaultstate="collapsed" desc="isSubtype"> 720 /** 721 * Is t an unchecked subtype of s? 722 */ 723 public boolean isSubtypeUnchecked(Type t, Type s) { 724 return isSubtypeUnchecked(t, s, noWarnings); 725 } 726 /** 727 * Is t an unchecked subtype of s? 728 */ 729 public boolean isSubtypeUnchecked(Type t, Type s, Warner warn) { 730 boolean result = isSubtypeUncheckedInternal(t, s, warn); 731 if (result) { 732 checkUnsafeVarargsConversion(t, s, warn); 733 } 734 return result; 735 } 736 //where 737 private boolean isSubtypeUncheckedInternal(Type t, Type s, Warner warn) { 738 if (t.hasTag(ARRAY) && s.hasTag(ARRAY)) { 739 if (((ArrayType)t).elemtype.isPrimitive()) { 740 return isSameType(elemtype(t), elemtype(s)); 741 } else { 742 return isSubtypeUnchecked(elemtype(t), elemtype(s), warn); 743 } 744 } else if (isSubtype(t, s)) { 745 return true; 746 } else if (t.hasTag(TYPEVAR)) { 747 return isSubtypeUnchecked(t.getUpperBound(), s, warn); 748 } else if (!s.isRaw()) { 749 Type t2 = asSuper(t, s.tsym); 750 if (t2 != null && t2.isRaw()) { 751 if (isReifiable(s)) { 752 warn.silentWarn(LintCategory.UNCHECKED); 753 } else { 754 warn.warn(LintCategory.UNCHECKED); 755 } 756 return true; 757 } 758 } 759 return false; 760 } 761 762 private void checkUnsafeVarargsConversion(Type t, Type s, Warner warn) { 763 if (!t.hasTag(ARRAY) || isReifiable(t)) { 764 return; 765 } 766 ArrayType from = (ArrayType)t; 767 boolean shouldWarn = false; 768 switch (s.getTag()) { 769 case ARRAY: 770 ArrayType to = (ArrayType)s; 771 shouldWarn = from.isVarargs() && 772 !to.isVarargs() && 773 !isReifiable(from); 774 break; 775 case CLASS: 776 shouldWarn = from.isVarargs(); 777 break; 778 } 779 if (shouldWarn) { 780 warn.warn(LintCategory.VARARGS); 781 } 782 } 783 784 /** 785 * Is t a subtype of s?<br> 786 * (not defined for Method and ForAll types) 787 */ 788 final public boolean isSubtype(Type t, Type s) { 789 return isSubtype(t, s, true); 790 } 791 final public boolean isSubtypeNoCapture(Type t, Type s) { 792 return isSubtype(t, s, false); 793 } 794 public boolean isSubtype(Type t, Type s, boolean capture) { 795 if (t == s) 796 return true; 797 if (s.isPartial()) 798 return isSuperType(s, t); 799 800 if (s.isCompound()) { 801 for (Type s2 : interfaces(s).prepend(supertype(s))) { 802 if (!isSubtype(t, s2, capture)) 803 return false; 804 } 805 return true; 806 } 807 808 // Generally, if 's' is a type variable, recur on lower bound; but 809 // for inference variables and intersections, we need to keep 's' 810 // (see JLS 4.10.2 for intersections and 18.2.3 for inference vars) 811 if (!t.hasTag(UNDETVAR) && !t.isCompound()) { 812 // TODO: JDK-8039198, bounds checking sometimes passes in a wildcard as s 813 Type lower = cvarLowerBound(wildLowerBound(s)); 814 if (s != lower) 815 return isSubtype(capture ? capture(t) : t, lower, false); 816 } 817 818 return isSubtype.visit(capture ? capture(t) : t, s); 819 } 820 // where 821 private TypeRelation isSubtype = new TypeRelation() 822 { 823 @Override 824 public Boolean visitType(Type t, Type s) { 825 switch (t.getTag()) { 826 case BYTE: 827 return (!s.hasTag(CHAR) && t.getTag().isSubRangeOf(s.getTag())); 828 case CHAR: 829 return (!s.hasTag(SHORT) && t.getTag().isSubRangeOf(s.getTag())); 830 case SHORT: case INT: case LONG: 831 case FLOAT: case DOUBLE: 832 return t.getTag().isSubRangeOf(s.getTag()); 833 case BOOLEAN: case VOID: 834 return t.hasTag(s.getTag()); 835 case TYPEVAR: 836 return isSubtypeNoCapture(t.getUpperBound(), s); 837 case BOT: 838 return 839 s.hasTag(BOT) || s.hasTag(CLASS) || 840 s.hasTag(ARRAY) || s.hasTag(TYPEVAR); 841 case WILDCARD: //we shouldn't be here - avoids crash (see 7034495) 842 case NONE: 843 return false; 844 default: 845 throw new AssertionError("isSubtype " + t.getTag()); 846 } 847 } 848 849 private Set<TypePair> cache = new HashSet<>(); 850 851 private boolean containsTypeRecursive(Type t, Type s) { 852 TypePair pair = new TypePair(t, s); 853 if (cache.add(pair)) { 854 try { 855 return containsType(t.getTypeArguments(), 856 s.getTypeArguments()); 857 } finally { 858 cache.remove(pair); 859 } 860 } else { 861 return containsType(t.getTypeArguments(), 862 rewriteSupers(s).getTypeArguments()); 863 } 864 } 865 866 private Type rewriteSupers(Type t) { 867 if (!t.isParameterized()) 868 return t; 869 ListBuffer<Type> from = new ListBuffer<>(); 870 ListBuffer<Type> to = new ListBuffer<>(); 871 adaptSelf(t, from, to); 872 if (from.isEmpty()) 873 return t; 874 ListBuffer<Type> rewrite = new ListBuffer<>(); 875 boolean changed = false; 876 for (Type orig : to.toList()) { 877 Type s = rewriteSupers(orig); 878 if (s.isSuperBound() && !s.isExtendsBound()) { 879 s = new WildcardType(syms.objectType, 880 BoundKind.UNBOUND, 881 syms.boundClass, 882 s.getMetadata()); 883 changed = true; 884 } else if (s != orig) { 885 s = new WildcardType(wildUpperBound(s), 886 BoundKind.EXTENDS, 887 syms.boundClass, 888 s.getMetadata()); 889 changed = true; 890 } 891 rewrite.append(s); 892 } 893 if (changed) 894 return subst(t.tsym.type, from.toList(), rewrite.toList()); 895 else 896 return t; 897 } 898 899 @Override 900 public Boolean visitClassType(ClassType t, Type s) { 901 Type sup = asSuper(t, s.tsym); 902 if (sup == null) return false; 903 // If t is an intersection, sup might not be a class type 904 if (!sup.hasTag(CLASS)) return isSubtypeNoCapture(sup, s); 905 return sup.tsym == s.tsym 906 // Check type variable containment 907 && (!s.isParameterized() || containsTypeRecursive(s, sup)) 908 && isSubtypeNoCapture(sup.getEnclosingType(), 909 s.getEnclosingType()); 910 } 911 912 @Override 913 public Boolean visitArrayType(ArrayType t, Type s) { 914 if (s.hasTag(ARRAY)) { 915 if (t.elemtype.isPrimitive()) 916 return isSameType(t.elemtype, elemtype(s)); 917 else 918 return isSubtypeNoCapture(t.elemtype, elemtype(s)); 919 } 920 921 if (s.hasTag(CLASS)) { 922 Name sname = s.tsym.getQualifiedName(); 923 return sname == names.java_lang_Object 924 || sname == names.java_lang_Cloneable 925 || sname == names.java_io_Serializable; 926 } 927 928 return false; 929 } 930 931 @Override 932 public Boolean visitUndetVar(UndetVar t, Type s) { 933 //todo: test against origin needed? or replace with substitution? 934 if (t == s || t.qtype == s || s.hasTag(ERROR) || s.hasTag(UNKNOWN)) { 935 return true; 936 } else if (s.hasTag(BOT)) { 937 //if 's' is 'null' there's no instantiated type U for which 938 //U <: s (but 'null' itself, which is not a valid type) 939 return false; 940 } 941 942 t.addBound(InferenceBound.UPPER, s, Types.this); 943 return true; 944 } 945 946 @Override 947 public Boolean visitErrorType(ErrorType t, Type s) { 948 return true; 949 } 950 }; 951 952 /** 953 * Is t a subtype of every type in given list `ts'?<br> 954 * (not defined for Method and ForAll types)<br> 955 * Allows unchecked conversions. 956 */ 957 public boolean isSubtypeUnchecked(Type t, List<Type> ts, Warner warn) { 958 for (List<Type> l = ts; l.nonEmpty(); l = l.tail) 959 if (!isSubtypeUnchecked(t, l.head, warn)) 960 return false; 961 return true; 962 } 963 964 /** 965 * Are corresponding elements of ts subtypes of ss? If lists are 966 * of different length, return false. 967 */ 968 public boolean isSubtypes(List<Type> ts, List<Type> ss) { 969 while (ts.tail != null && ss.tail != null 970 /*inlined: ts.nonEmpty() && ss.nonEmpty()*/ && 971 isSubtype(ts.head, ss.head)) { 972 ts = ts.tail; 973 ss = ss.tail; 974 } 975 return ts.tail == null && ss.tail == null; 976 /*inlined: ts.isEmpty() && ss.isEmpty();*/ 977 } 978 979 /** 980 * Are corresponding elements of ts subtypes of ss, allowing 981 * unchecked conversions? If lists are of different length, 982 * return false. 983 **/ 984 public boolean isSubtypesUnchecked(List<Type> ts, List<Type> ss, Warner warn) { 985 while (ts.tail != null && ss.tail != null 986 /*inlined: ts.nonEmpty() && ss.nonEmpty()*/ && 987 isSubtypeUnchecked(ts.head, ss.head, warn)) { 988 ts = ts.tail; 989 ss = ss.tail; 990 } 991 return ts.tail == null && ss.tail == null; 992 /*inlined: ts.isEmpty() && ss.isEmpty();*/ 993 } 994 // </editor-fold> 995 996 // <editor-fold defaultstate="collapsed" desc="isSuperType"> 997 /** 998 * Is t a supertype of s? 999 */ 1000 public boolean isSuperType(Type t, Type s) { 1001 switch (t.getTag()) { 1002 case ERROR: 1003 return true; 1004 case UNDETVAR: { 1005 UndetVar undet = (UndetVar)t; 1006 if (t == s || 1007 undet.qtype == s || 1008 s.hasTag(ERROR) || 1009 s.hasTag(BOT)) { 1010 return true; 1011 } 1012 undet.addBound(InferenceBound.LOWER, s, this); 1013 return true; 1014 } 1015 default: 1016 return isSubtype(s, t); 1017 } 1018 } 1019 // </editor-fold> 1020 1021 // <editor-fold defaultstate="collapsed" desc="isSameType"> 1022 /** 1023 * Are corresponding elements of the lists the same type? If 1024 * lists are of different length, return false. 1025 */ 1026 public boolean isSameTypes(List<Type> ts, List<Type> ss) { 1027 return isSameTypes(ts, ss, false); 1028 } 1029 public boolean isSameTypes(List<Type> ts, List<Type> ss, boolean strict) { 1030 while (ts.tail != null && ss.tail != null 1031 /*inlined: ts.nonEmpty() && ss.nonEmpty()*/ && 1032 isSameType(ts.head, ss.head, strict)) { 1033 ts = ts.tail; 1034 ss = ss.tail; 1035 } 1036 return ts.tail == null && ss.tail == null; 1037 /*inlined: ts.isEmpty() && ss.isEmpty();*/ 1038 } 1039 1040 /** 1041 * A polymorphic signature method (JLS SE 7, 8.4.1) is a method that 1042 * (i) is declared in the java.lang.invoke.MethodHandle class, (ii) takes 1043 * a single variable arity parameter (iii) whose declared type is Object[], 1044 * (iv) has a return type of Object and (v) is native. 1045 */ 1046 public boolean isSignaturePolymorphic(MethodSymbol msym) { 1047 List<Type> argtypes = msym.type.getParameterTypes(); 1048 return (msym.flags_field & NATIVE) != 0 && 1049 msym.owner == syms.methodHandleType.tsym && 1050 argtypes.tail.tail == null && 1051 argtypes.head.hasTag(TypeTag.ARRAY) && 1052 msym.type.getReturnType().tsym == syms.objectType.tsym && 1053 ((ArrayType)argtypes.head).elemtype.tsym == syms.objectType.tsym; 1054 } 1055 1056 /** 1057 * Is t the same type as s? 1058 */ 1059 public boolean isSameType(Type t, Type s) { 1060 return isSameType(t, s, false); 1061 } 1062 public boolean isSameType(Type t, Type s, boolean strict) { 1063 return strict ? 1064 isSameTypeStrict.visit(t, s) : 1065 isSameTypeLoose.visit(t, s); 1066 } 1067 public boolean isSameAnnotatedType(Type t, Type s) { 1068 return isSameAnnotatedType.visit(t, s); 1069 } 1070 // where 1071 abstract class SameTypeVisitor extends TypeRelation { 1072 1073 public Boolean visitType(Type t, Type s) { 1074 if (t == s) 1075 return true; 1076 1077 if (s.isPartial()) 1078 return visit(s, t); 1079 1080 switch (t.getTag()) { 1081 case BYTE: case CHAR: case SHORT: case INT: case LONG: case FLOAT: 1082 case DOUBLE: case BOOLEAN: case VOID: case BOT: case NONE: 1083 return t.hasTag(s.getTag()); 1084 case TYPEVAR: { 1085 if (s.hasTag(TYPEVAR)) { 1086 //type-substitution does not preserve type-var types 1087 //check that type var symbols and bounds are indeed the same 1088 return sameTypeVars((TypeVar)t, (TypeVar)s); 1089 } 1090 else { 1091 //special case for s == ? super X, where upper(s) = u 1092 //check that u == t, where u has been set by Type.withTypeVar 1093 return s.isSuperBound() && 1094 !s.isExtendsBound() && 1095 visit(t, wildUpperBound(s)); 1096 } 1097 } 1098 default: 1099 throw new AssertionError("isSameType " + t.getTag()); 1100 } 1101 } 1102 1103 abstract boolean sameTypeVars(TypeVar tv1, TypeVar tv2); 1104 1105 @Override 1106 public Boolean visitWildcardType(WildcardType t, Type s) { 1107 if (s.isPartial()) 1108 return visit(s, t); 1109 else 1110 return false; 1111 } 1112 1113 @Override 1114 public Boolean visitClassType(ClassType t, Type s) { 1115 if (t == s) 1116 return true; 1117 1118 if (s.isPartial()) 1119 return visit(s, t); 1120 1121 if (s.isSuperBound() && !s.isExtendsBound()) 1122 return visit(t, wildUpperBound(s)) && visit(t, wildLowerBound(s)); 1123 1124 if (t.isCompound() && s.isCompound()) { 1125 if (!visit(supertype(t), supertype(s))) 1126 return false; 1127 1128 HashSet<UniqueType> set = new HashSet<>(); 1129 for (Type x : interfaces(t)) 1130 set.add(new UniqueType(x, Types.this)); 1131 for (Type x : interfaces(s)) { 1132 if (!set.remove(new UniqueType(x, Types.this))) 1133 return false; 1134 } 1135 return (set.isEmpty()); 1136 } 1137 return t.tsym == s.tsym 1138 && visit(t.getEnclosingType(), s.getEnclosingType()) 1139 && containsTypes(t.getTypeArguments(), s.getTypeArguments()); 1140 } 1141 1142 abstract protected boolean containsTypes(List<Type> ts1, List<Type> ts2); 1143 1144 @Override 1145 public Boolean visitArrayType(ArrayType t, Type s) { 1146 if (t == s) 1147 return true; 1148 1149 if (s.isPartial()) 1150 return visit(s, t); 1151 1152 return s.hasTag(ARRAY) 1153 && containsTypeEquivalent(t.elemtype, elemtype(s)); 1154 } 1155 1156 @Override 1157 public Boolean visitMethodType(MethodType t, Type s) { 1158 // isSameType for methods does not take thrown 1159 // exceptions into account! 1160 return hasSameArgs(t, s) && visit(t.getReturnType(), s.getReturnType()); 1161 } 1162 1163 @Override 1164 public Boolean visitPackageType(PackageType t, Type s) { 1165 return t == s; 1166 } 1167 1168 @Override 1169 public Boolean visitForAll(ForAll t, Type s) { 1170 if (!s.hasTag(FORALL)) { 1171 return false; 1172 } 1173 1174 ForAll forAll = (ForAll)s; 1175 return hasSameBounds(t, forAll) 1176 && visit(t.qtype, subst(forAll.qtype, forAll.tvars, t.tvars)); 1177 } 1178 1179 @Override 1180 public Boolean visitUndetVar(UndetVar t, Type s) { 1181 if (s.hasTag(WILDCARD)) { 1182 // FIXME, this might be leftovers from before capture conversion 1183 return false; 1184 } 1185 1186 if (t == s || t.qtype == s || s.hasTag(ERROR) || s.hasTag(UNKNOWN)) { 1187 return true; 1188 } 1189 1190 t.addBound(InferenceBound.EQ, s, Types.this); 1191 1192 return true; 1193 } 1194 1195 @Override 1196 public Boolean visitErrorType(ErrorType t, Type s) { 1197 return true; 1198 } 1199 } 1200 1201 /** 1202 * Standard type-equality relation - type variables are considered 1203 * equals if they share the same type symbol. 1204 */ 1205 TypeRelation isSameTypeLoose = new LooseSameTypeVisitor(); 1206 1207 private class LooseSameTypeVisitor extends SameTypeVisitor { 1208 1209 /** cache of the type-variable pairs being (recursively) tested. */ 1210 private Set<TypePair> cache = new HashSet<>(); 1211 1212 @Override 1213 boolean sameTypeVars(TypeVar tv1, TypeVar tv2) { 1214 return tv1.tsym == tv2.tsym && checkSameBounds(tv1, tv2); 1215 } 1216 @Override 1217 protected boolean containsTypes(List<Type> ts1, List<Type> ts2) { 1218 return containsTypeEquivalent(ts1, ts2); 1219 } 1220 1221 /** 1222 * Since type-variable bounds can be recursive, we need to protect against 1223 * infinite loops - where the same bounds are checked over and over recursively. 1224 */ 1225 private boolean checkSameBounds(TypeVar tv1, TypeVar tv2) { 1226 TypePair p = new TypePair(tv1, tv2, true); 1227 if (cache.add(p)) { 1228 try { 1229 return visit(tv1.getUpperBound(), tv2.getUpperBound()); 1230 } finally { 1231 cache.remove(p); 1232 } 1233 } else { 1234 return false; 1235 } 1236 } 1237 }; 1238 1239 /** 1240 * Strict type-equality relation - type variables are considered 1241 * equals if they share the same object identity. 1242 */ 1243 TypeRelation isSameTypeStrict = new SameTypeVisitor() { 1244 @Override 1245 boolean sameTypeVars(TypeVar tv1, TypeVar tv2) { 1246 return tv1 == tv2; 1247 } 1248 @Override 1249 protected boolean containsTypes(List<Type> ts1, List<Type> ts2) { 1250 return isSameTypes(ts1, ts2, true); 1251 } 1252 1253 @Override 1254 public Boolean visitWildcardType(WildcardType t, Type s) { 1255 if (!s.hasTag(WILDCARD)) { 1256 return false; 1257 } else { 1258 WildcardType t2 = (WildcardType)s; 1259 return t.kind == t2.kind && 1260 isSameType(t.type, t2.type, true); 1261 } 1262 } 1263 }; 1264 1265 // </editor-fold> 1266 1267 TypeRelation isSameAnnotatedType = new LooseSameTypeVisitor() { 1268 private Boolean compareAnnotations(Type t1, Type t2) { 1269 List<Attribute.TypeCompound> annos1 = t1.getAnnotationMirrors(); 1270 List<Attribute.TypeCompound> annos2 = t2.getAnnotationMirrors(); 1271 return annos1.containsAll(annos2) && annos2.containsAll(annos1); 1272 } 1273 1274 @Override 1275 public Boolean visitType(Type t, Type s) { 1276 return compareAnnotations(t, s) && super.visitType(t, s); 1277 } 1278 1279 @Override 1280 public Boolean visitWildcardType(WildcardType t, Type s) { 1281 return compareAnnotations(t, s) && super.visitWildcardType(t, s); 1282 } 1283 1284 @Override 1285 public Boolean visitClassType(ClassType t, Type s) { 1286 return compareAnnotations(t, s) && super.visitClassType(t, s); 1287 } 1288 1289 @Override 1290 public Boolean visitArrayType(ArrayType t, Type s) { 1291 return compareAnnotations(t, s) && super.visitArrayType(t, s); 1292 } 1293 1294 @Override 1295 public Boolean visitForAll(ForAll t, Type s) { 1296 return compareAnnotations(t, s) && super.visitForAll(t, s); 1297 } 1298 }; 1299 1300 // <editor-fold defaultstate="collapsed" desc="Contains Type"> 1301 public boolean containedBy(Type t, Type s) { 1302 switch (t.getTag()) { 1303 case UNDETVAR: 1304 if (s.hasTag(WILDCARD)) { 1305 UndetVar undetvar = (UndetVar)t; 1306 WildcardType wt = (WildcardType)s; 1307 switch(wt.kind) { 1308 case UNBOUND: 1309 break; 1310 case EXTENDS: { 1311 Type bound = wildUpperBound(s); 1312 undetvar.addBound(InferenceBound.UPPER, bound, this); 1313 break; 1314 } 1315 case SUPER: { 1316 Type bound = wildLowerBound(s); 1317 undetvar.addBound(InferenceBound.LOWER, bound, this); 1318 break; 1319 } 1320 } 1321 return true; 1322 } else { 1323 return isSameType(t, s); 1324 } 1325 case ERROR: 1326 return true; 1327 default: 1328 return containsType(s, t); 1329 } 1330 } 1331 1332 boolean containsType(List<Type> ts, List<Type> ss) { 1333 while (ts.nonEmpty() && ss.nonEmpty() 1334 && containsType(ts.head, ss.head)) { 1335 ts = ts.tail; 1336 ss = ss.tail; 1337 } 1338 return ts.isEmpty() && ss.isEmpty(); 1339 } 1340 1341 /** 1342 * Check if t contains s. 1343 * 1344 * <p>T contains S if: 1345 * 1346 * <p>{@code L(T) <: L(S) && U(S) <: U(T)} 1347 * 1348 * <p>This relation is only used by ClassType.isSubtype(), that 1349 * is, 1350 * 1351 * <p>{@code C<S> <: C<T> if T contains S.} 1352 * 1353 * <p>Because of F-bounds, this relation can lead to infinite 1354 * recursion. Thus we must somehow break that recursion. Notice 1355 * that containsType() is only called from ClassType.isSubtype(). 1356 * Since the arguments have already been checked against their 1357 * bounds, we know: 1358 * 1359 * <p>{@code U(S) <: U(T) if T is "super" bound (U(T) *is* the bound)} 1360 * 1361 * <p>{@code L(T) <: L(S) if T is "extends" bound (L(T) is bottom)} 1362 * 1363 * @param t a type 1364 * @param s a type 1365 */ 1366 public boolean containsType(Type t, Type s) { 1367 return containsType.visit(t, s); 1368 } 1369 // where 1370 private TypeRelation containsType = new TypeRelation() { 1371 1372 public Boolean visitType(Type t, Type s) { 1373 if (s.isPartial()) 1374 return containedBy(s, t); 1375 else 1376 return isSameType(t, s); 1377 } 1378 1379// void debugContainsType(WildcardType t, Type s) { 1380// System.err.println(); 1381// System.err.format(" does %s contain %s?%n", t, s); 1382// System.err.format(" %s U(%s) <: U(%s) %s = %s%n", 1383// wildUpperBound(s), s, t, wildUpperBound(t), 1384// t.isSuperBound() 1385// || isSubtypeNoCapture(wildUpperBound(s), wildUpperBound(t))); 1386// System.err.format(" %s L(%s) <: L(%s) %s = %s%n", 1387// wildLowerBound(t), t, s, wildLowerBound(s), 1388// t.isExtendsBound() 1389// || isSubtypeNoCapture(wildLowerBound(t), wildLowerBound(s))); 1390// System.err.println(); 1391// } 1392 1393 @Override 1394 public Boolean visitWildcardType(WildcardType t, Type s) { 1395 if (s.isPartial()) 1396 return containedBy(s, t); 1397 else { 1398// debugContainsType(t, s); 1399 return isSameWildcard(t, s) 1400 || isCaptureOf(s, t) 1401 || ((t.isExtendsBound() || isSubtypeNoCapture(wildLowerBound(t), wildLowerBound(s))) && 1402 // TODO: JDK-8039214, cvarUpperBound call here is incorrect 1403 (t.isSuperBound() || isSubtypeNoCapture(cvarUpperBound(wildUpperBound(s)), wildUpperBound(t)))); 1404 } 1405 } 1406 1407 @Override 1408 public Boolean visitUndetVar(UndetVar t, Type s) { 1409 if (!s.hasTag(WILDCARD)) { 1410 return isSameType(t, s); 1411 } else { 1412 return false; 1413 } 1414 } 1415 1416 @Override 1417 public Boolean visitErrorType(ErrorType t, Type s) { 1418 return true; 1419 } 1420 }; 1421 1422 public boolean isCaptureOf(Type s, WildcardType t) { 1423 if (!s.hasTag(TYPEVAR) || !((TypeVar)s).isCaptured()) 1424 return false; 1425 return isSameWildcard(t, ((CapturedType)s).wildcard); 1426 } 1427 1428 public boolean isSameWildcard(WildcardType t, Type s) { 1429 if (!s.hasTag(WILDCARD)) 1430 return false; 1431 WildcardType w = (WildcardType)s; 1432 return w.kind == t.kind && w.type == t.type; 1433 } 1434 1435 public boolean containsTypeEquivalent(List<Type> ts, List<Type> ss) { 1436 while (ts.nonEmpty() && ss.nonEmpty() 1437 && containsTypeEquivalent(ts.head, ss.head)) { 1438 ts = ts.tail; 1439 ss = ss.tail; 1440 } 1441 return ts.isEmpty() && ss.isEmpty(); 1442 } 1443 // </editor-fold> 1444 1445 /** 1446 * Can t and s be compared for equality? Any primitive == 1447 * primitive or primitive == object comparisons here are an error. 1448 * Unboxing and correct primitive == primitive comparisons are 1449 * already dealt with in Attr.visitBinary. 1450 * 1451 */ 1452 public boolean isEqualityComparable(Type s, Type t, Warner warn) { 1453 if (t.isNumeric() && s.isNumeric()) 1454 return true; 1455 1456 boolean tPrimitive = t.isPrimitive(); 1457 boolean sPrimitive = s.isPrimitive(); 1458 if (!tPrimitive && !sPrimitive) { 1459 return isCastable(s, t, warn) || isCastable(t, s, warn); 1460 } else { 1461 return false; 1462 } 1463 } 1464 1465 // <editor-fold defaultstate="collapsed" desc="isCastable"> 1466 public boolean isCastable(Type t, Type s) { 1467 return isCastable(t, s, noWarnings); 1468 } 1469 1470 /** 1471 * Is t is castable to s?<br> 1472 * s is assumed to be an erased type.<br> 1473 * (not defined for Method and ForAll types). 1474 */ 1475 public boolean isCastable(Type t, Type s, Warner warn) { 1476 if (t == s) 1477 return true; 1478 1479 if (t.isPrimitive() != s.isPrimitive()) 1480 return (isConvertible(t, s, warn) 1481 || (allowObjectToPrimitiveCast && 1482 s.isPrimitive() && 1483 isSubtype(boxedClass(s).type, t))); 1484 if (warn != warnStack.head) { 1485 try { 1486 warnStack = warnStack.prepend(warn); 1487 checkUnsafeVarargsConversion(t, s, warn); 1488 return isCastable.visit(t,s); 1489 } finally { 1490 warnStack = warnStack.tail; 1491 } 1492 } else { 1493 return isCastable.visit(t,s); 1494 } 1495 } 1496 // where 1497 private TypeRelation isCastable = new TypeRelation() { 1498 1499 public Boolean visitType(Type t, Type s) { 1500 if (s.hasTag(ERROR)) 1501 return true; 1502 1503 switch (t.getTag()) { 1504 case BYTE: case CHAR: case SHORT: case INT: case LONG: case FLOAT: 1505 case DOUBLE: 1506 return s.isNumeric(); 1507 case BOOLEAN: 1508 return s.hasTag(BOOLEAN); 1509 case VOID: 1510 return false; 1511 case BOT: 1512 return isSubtype(t, s); 1513 default: 1514 throw new AssertionError(); 1515 } 1516 } 1517 1518 @Override 1519 public Boolean visitWildcardType(WildcardType t, Type s) { 1520 return isCastable(wildUpperBound(t), s, warnStack.head); 1521 } 1522 1523 @Override 1524 public Boolean visitClassType(ClassType t, Type s) { 1525 if (s.hasTag(ERROR) || s.hasTag(BOT)) 1526 return true; 1527 1528 if (s.hasTag(TYPEVAR)) { 1529 if (isCastable(t, s.getUpperBound(), noWarnings)) { 1530 warnStack.head.warn(LintCategory.UNCHECKED); 1531 return true; 1532 } else { 1533 return false; 1534 } 1535 } 1536 1537 if (t.isCompound() || s.isCompound()) { 1538 return !t.isCompound() ? 1539 visitIntersectionType((IntersectionClassType)s, t, true) : 1540 visitIntersectionType((IntersectionClassType)t, s, false); 1541 } 1542 1543 if (s.hasTag(CLASS) || s.hasTag(ARRAY)) { 1544 boolean upcast; 1545 if ((upcast = isSubtype(erasure(t), erasure(s))) 1546 || isSubtype(erasure(s), erasure(t))) { 1547 if (!upcast && s.hasTag(ARRAY)) { 1548 if (!isReifiable(s)) 1549 warnStack.head.warn(LintCategory.UNCHECKED); 1550 return true; 1551 } else if (s.isRaw()) { 1552 return true; 1553 } else if (t.isRaw()) { 1554 if (!isUnbounded(s)) 1555 warnStack.head.warn(LintCategory.UNCHECKED); 1556 return true; 1557 } 1558 // Assume |a| <: |b| 1559 final Type a = upcast ? t : s; 1560 final Type b = upcast ? s : t; 1561 final boolean HIGH = true; 1562 final boolean LOW = false; 1563 final boolean DONT_REWRITE_TYPEVARS = false; 1564 Type aHigh = rewriteQuantifiers(a, HIGH, DONT_REWRITE_TYPEVARS); 1565 Type aLow = rewriteQuantifiers(a, LOW, DONT_REWRITE_TYPEVARS); 1566 Type bHigh = rewriteQuantifiers(b, HIGH, DONT_REWRITE_TYPEVARS); 1567 Type bLow = rewriteQuantifiers(b, LOW, DONT_REWRITE_TYPEVARS); 1568 Type lowSub = asSub(bLow, aLow.tsym); 1569 Type highSub = (lowSub == null) ? null : asSub(bHigh, aHigh.tsym); 1570 if (highSub == null) { 1571 final boolean REWRITE_TYPEVARS = true; 1572 aHigh = rewriteQuantifiers(a, HIGH, REWRITE_TYPEVARS); 1573 aLow = rewriteQuantifiers(a, LOW, REWRITE_TYPEVARS); 1574 bHigh = rewriteQuantifiers(b, HIGH, REWRITE_TYPEVARS); 1575 bLow = rewriteQuantifiers(b, LOW, REWRITE_TYPEVARS); 1576 lowSub = asSub(bLow, aLow.tsym); 1577 highSub = (lowSub == null) ? null : asSub(bHigh, aHigh.tsym); 1578 } 1579 if (highSub != null) { 1580 if (!(a.tsym == highSub.tsym && a.tsym == lowSub.tsym)) { 1581 Assert.error(a.tsym + " != " + highSub.tsym + " != " + lowSub.tsym); 1582 } 1583 if (!disjointTypes(aHigh.allparams(), highSub.allparams()) 1584 && !disjointTypes(aHigh.allparams(), lowSub.allparams()) 1585 && !disjointTypes(aLow.allparams(), highSub.allparams()) 1586 && !disjointTypes(aLow.allparams(), lowSub.allparams())) { 1587 if (upcast ? giveWarning(a, b) : 1588 giveWarning(b, a)) 1589 warnStack.head.warn(LintCategory.UNCHECKED); 1590 return true; 1591 } 1592 } 1593 if (isReifiable(s)) 1594 return isSubtypeUnchecked(a, b); 1595 else 1596 return isSubtypeUnchecked(a, b, warnStack.head); 1597 } 1598 1599 // Sidecast 1600 if (s.hasTag(CLASS)) { 1601 if ((s.tsym.flags() & INTERFACE) != 0) { 1602 return ((t.tsym.flags() & FINAL) == 0) 1603 ? sideCast(t, s, warnStack.head) 1604 : sideCastFinal(t, s, warnStack.head); 1605 } else if ((t.tsym.flags() & INTERFACE) != 0) { 1606 return ((s.tsym.flags() & FINAL) == 0) 1607 ? sideCast(t, s, warnStack.head) 1608 : sideCastFinal(t, s, warnStack.head); 1609 } else { 1610 // unrelated class types 1611 return false; 1612 } 1613 } 1614 } 1615 return false; 1616 } 1617 1618 boolean visitIntersectionType(IntersectionClassType ict, Type s, boolean reverse) { 1619 Warner warn = noWarnings; 1620 for (Type c : ict.getComponents()) { 1621 warn.clear(); 1622 if (reverse ? !isCastable(s, c, warn) : !isCastable(c, s, warn)) 1623 return false; 1624 } 1625 if (warn.hasLint(LintCategory.UNCHECKED)) 1626 warnStack.head.warn(LintCategory.UNCHECKED); 1627 return true; 1628 } 1629 1630 @Override 1631 public Boolean visitArrayType(ArrayType t, Type s) { 1632 switch (s.getTag()) { 1633 case ERROR: 1634 case BOT: 1635 return true; 1636 case TYPEVAR: 1637 if (isCastable(s, t, noWarnings)) { 1638 warnStack.head.warn(LintCategory.UNCHECKED); 1639 return true; 1640 } else { 1641 return false; 1642 } 1643 case CLASS: 1644 return isSubtype(t, s); 1645 case ARRAY: 1646 if (elemtype(t).isPrimitive() || elemtype(s).isPrimitive()) { 1647 return elemtype(t).hasTag(elemtype(s).getTag()); 1648 } else { 1649 return visit(elemtype(t), elemtype(s)); 1650 } 1651 default: 1652 return false; 1653 } 1654 } 1655 1656 @Override 1657 public Boolean visitTypeVar(TypeVar t, Type s) { 1658 switch (s.getTag()) { 1659 case ERROR: 1660 case BOT: 1661 return true; 1662 case TYPEVAR: 1663 if (isSubtype(t, s)) { 1664 return true; 1665 } else if (isCastable(t.bound, s, noWarnings)) { 1666 warnStack.head.warn(LintCategory.UNCHECKED); 1667 return true; 1668 } else { 1669 return false; 1670 } 1671 default: 1672 return isCastable(t.bound, s, warnStack.head); 1673 } 1674 } 1675 1676 @Override 1677 public Boolean visitErrorType(ErrorType t, Type s) { 1678 return true; 1679 } 1680 }; 1681 // </editor-fold> 1682 1683 // <editor-fold defaultstate="collapsed" desc="disjointTypes"> 1684 public boolean disjointTypes(List<Type> ts, List<Type> ss) { 1685 while (ts.tail != null && ss.tail != null) { 1686 if (disjointType(ts.head, ss.head)) return true; 1687 ts = ts.tail; 1688 ss = ss.tail; 1689 } 1690 return false; 1691 } 1692 1693 /** 1694 * Two types or wildcards are considered disjoint if it can be 1695 * proven that no type can be contained in both. It is 1696 * conservative in that it is allowed to say that two types are 1697 * not disjoint, even though they actually are. 1698 * 1699 * The type {@code C<X>} is castable to {@code C<Y>} exactly if 1700 * {@code X} and {@code Y} are not disjoint. 1701 */ 1702 public boolean disjointType(Type t, Type s) { 1703 return disjointType.visit(t, s); 1704 } 1705 // where 1706 private TypeRelation disjointType = new TypeRelation() { 1707 1708 private Set<TypePair> cache = new HashSet<>(); 1709 1710 @Override 1711 public Boolean visitType(Type t, Type s) { 1712 if (s.hasTag(WILDCARD)) 1713 return visit(s, t); 1714 else 1715 return notSoftSubtypeRecursive(t, s) || notSoftSubtypeRecursive(s, t); 1716 } 1717 1718 private boolean isCastableRecursive(Type t, Type s) { 1719 TypePair pair = new TypePair(t, s); 1720 if (cache.add(pair)) { 1721 try { 1722 return Types.this.isCastable(t, s); 1723 } finally { 1724 cache.remove(pair); 1725 } 1726 } else { 1727 return true; 1728 } 1729 } 1730 1731 private boolean notSoftSubtypeRecursive(Type t, Type s) { 1732 TypePair pair = new TypePair(t, s); 1733 if (cache.add(pair)) { 1734 try { 1735 return Types.this.notSoftSubtype(t, s); 1736 } finally { 1737 cache.remove(pair); 1738 } 1739 } else { 1740 return false; 1741 } 1742 } 1743 1744 @Override 1745 public Boolean visitWildcardType(WildcardType t, Type s) { 1746 if (t.isUnbound()) 1747 return false; 1748 1749 if (!s.hasTag(WILDCARD)) { 1750 if (t.isExtendsBound()) 1751 return notSoftSubtypeRecursive(s, t.type); 1752 else 1753 return notSoftSubtypeRecursive(t.type, s); 1754 } 1755 1756 if (s.isUnbound()) 1757 return false; 1758 1759 if (t.isExtendsBound()) { 1760 if (s.isExtendsBound()) 1761 return !isCastableRecursive(t.type, wildUpperBound(s)); 1762 else if (s.isSuperBound()) 1763 return notSoftSubtypeRecursive(wildLowerBound(s), t.type); 1764 } else if (t.isSuperBound()) { 1765 if (s.isExtendsBound()) 1766 return notSoftSubtypeRecursive(t.type, wildUpperBound(s)); 1767 } 1768 return false; 1769 } 1770 }; 1771 // </editor-fold> 1772 1773 // <editor-fold defaultstate="collapsed" desc="cvarLowerBounds"> 1774 public List<Type> cvarLowerBounds(List<Type> ts) { 1775 return map(ts, cvarLowerBoundMapping); 1776 } 1777 private final Mapping cvarLowerBoundMapping = new Mapping("cvarLowerBound") { 1778 public Type apply(Type t) { 1779 return cvarLowerBound(t); 1780 } 1781 }; 1782 // </editor-fold> 1783 1784 // <editor-fold defaultstate="collapsed" desc="notSoftSubtype"> 1785 /** 1786 * This relation answers the question: is impossible that 1787 * something of type `t' can be a subtype of `s'? This is 1788 * different from the question "is `t' not a subtype of `s'?" 1789 * when type variables are involved: Integer is not a subtype of T 1790 * where {@code <T extends Number>} but it is not true that Integer cannot 1791 * possibly be a subtype of T. 1792 */ 1793 public boolean notSoftSubtype(Type t, Type s) { 1794 if (t == s) return false; 1795 if (t.hasTag(TYPEVAR)) { 1796 TypeVar tv = (TypeVar) t; 1797 return !isCastable(tv.bound, 1798 relaxBound(s), 1799 noWarnings); 1800 } 1801 if (!s.hasTag(WILDCARD)) 1802 s = cvarUpperBound(s); 1803 1804 return !isSubtype(t, relaxBound(s)); 1805 } 1806 1807 private Type relaxBound(Type t) { 1808 if (t.hasTag(TYPEVAR)) { 1809 while (t.hasTag(TYPEVAR)) 1810 t = t.getUpperBound(); 1811 t = rewriteQuantifiers(t, true, true); 1812 } 1813 return t; 1814 } 1815 // </editor-fold> 1816 1817 // <editor-fold defaultstate="collapsed" desc="isReifiable"> 1818 public boolean isReifiable(Type t) { 1819 return isReifiable.visit(t); 1820 } 1821 // where 1822 private UnaryVisitor<Boolean> isReifiable = new UnaryVisitor<Boolean>() { 1823 1824 public Boolean visitType(Type t, Void ignored) { 1825 return true; 1826 } 1827 1828 @Override 1829 public Boolean visitClassType(ClassType t, Void ignored) { 1830 if (t.isCompound()) 1831 return false; 1832 else { 1833 if (!t.isParameterized()) 1834 return true; 1835 1836 for (Type param : t.allparams()) { 1837 if (!param.isUnbound()) 1838 return false; 1839 } 1840 return true; 1841 } 1842 } 1843 1844 @Override 1845 public Boolean visitArrayType(ArrayType t, Void ignored) { 1846 return visit(t.elemtype); 1847 } 1848 1849 @Override 1850 public Boolean visitTypeVar(TypeVar t, Void ignored) { 1851 return false; 1852 } 1853 }; 1854 // </editor-fold> 1855 1856 // <editor-fold defaultstate="collapsed" desc="Array Utils"> 1857 public boolean isArray(Type t) { 1858 while (t.hasTag(WILDCARD)) 1859 t = wildUpperBound(t); 1860 return t.hasTag(ARRAY); 1861 } 1862 1863 /** 1864 * The element type of an array. 1865 */ 1866 public Type elemtype(Type t) { 1867 switch (t.getTag()) { 1868 case WILDCARD: 1869 return elemtype(wildUpperBound(t)); 1870 case ARRAY: 1871 return ((ArrayType)t).elemtype; 1872 case FORALL: 1873 return elemtype(((ForAll)t).qtype); 1874 case ERROR: 1875 return t; 1876 default: 1877 return null; 1878 } 1879 } 1880 1881 public Type elemtypeOrType(Type t) { 1882 Type elemtype = elemtype(t); 1883 return elemtype != null ? 1884 elemtype : 1885 t; 1886 } 1887 1888 /** 1889 * Mapping to take element type of an arraytype 1890 */ 1891 private Mapping elemTypeFun = new Mapping ("elemTypeFun") { 1892 public Type apply(Type t) { 1893 while (t.hasTag(TYPEVAR)) { 1894 t = t.getUpperBound(); 1895 } 1896 return elemtype(t); 1897 } 1898 }; 1899 1900 /** 1901 * The number of dimensions of an array type. 1902 */ 1903 public int dimensions(Type t) { 1904 int result = 0; 1905 while (t.hasTag(ARRAY)) { 1906 result++; 1907 t = elemtype(t); 1908 } 1909 return result; 1910 } 1911 1912 /** 1913 * Returns an ArrayType with the component type t 1914 * 1915 * @param t The component type of the ArrayType 1916 * @return the ArrayType for the given component 1917 */ 1918 public ArrayType makeArrayType(Type t) { 1919 if (t.hasTag(VOID) || t.hasTag(PACKAGE)) { 1920 Assert.error("Type t must not be a VOID or PACKAGE type, " + t.toString()); 1921 } 1922 return new ArrayType(t, syms.arrayClass); 1923 } 1924 // </editor-fold> 1925 1926 // <editor-fold defaultstate="collapsed" desc="asSuper"> 1927 /** 1928 * Return the (most specific) base type of t that starts with the 1929 * given symbol. If none exists, return null. 1930 * 1931 * @param t a type 1932 * @param sym a symbol 1933 */ 1934 public Type asSuper(Type t, Symbol sym) { 1935 /* Some examples: 1936 * 1937 * (Enum<E>, Comparable) => Comparable<E> 1938 * (c.s.s.d.AttributeTree.ValueKind, Enum) => Enum<c.s.s.d.AttributeTree.ValueKind> 1939 * (c.s.s.t.ExpressionTree, c.s.s.t.Tree) => c.s.s.t.Tree 1940 * (j.u.List<capture#160 of ? extends c.s.s.d.DocTree>, Iterable) => 1941 * Iterable<capture#160 of ? extends c.s.s.d.DocTree> 1942 */ 1943 if (sym.type == syms.objectType) { //optimization 1944 return syms.objectType; 1945 } 1946 return asSuper.visit(t, sym); 1947 } 1948 // where 1949 private SimpleVisitor<Type,Symbol> asSuper = new SimpleVisitor<Type,Symbol>() { 1950 1951 public Type visitType(Type t, Symbol sym) { 1952 return null; 1953 } 1954 1955 @Override 1956 public Type visitClassType(ClassType t, Symbol sym) { 1957 if (t.tsym == sym) 1958 return t; 1959 1960 Type st = supertype(t); 1961 if (st.hasTag(CLASS) || st.hasTag(TYPEVAR)) { 1962 Type x = asSuper(st, sym); 1963 if (x != null) 1964 return x; 1965 } 1966 if ((sym.flags() & INTERFACE) != 0) { 1967 for (List<Type> l = interfaces(t); l.nonEmpty(); l = l.tail) { 1968 if (!l.head.hasTag(ERROR)) { 1969 Type x = asSuper(l.head, sym); 1970 if (x != null) 1971 return x; 1972 } 1973 } 1974 } 1975 return null; 1976 } 1977 1978 @Override 1979 public Type visitArrayType(ArrayType t, Symbol sym) { 1980 return isSubtype(t, sym.type) ? sym.type : null; 1981 } 1982 1983 @Override 1984 public Type visitTypeVar(TypeVar t, Symbol sym) { 1985 if (t.tsym == sym) 1986 return t; 1987 else 1988 return asSuper(t.bound, sym); 1989 } 1990 1991 @Override 1992 public Type visitErrorType(ErrorType t, Symbol sym) { 1993 return t; 1994 } 1995 }; 1996 1997 /** 1998 * Return the base type of t or any of its outer types that starts 1999 * with the given symbol. If none exists, return null. 2000 * 2001 * @param t a type 2002 * @param sym a symbol 2003 */ 2004 public Type asOuterSuper(Type t, Symbol sym) { 2005 switch (t.getTag()) { 2006 case CLASS: 2007 do { 2008 Type s = asSuper(t, sym); 2009 if (s != null) return s; 2010 t = t.getEnclosingType(); 2011 } while (t.hasTag(CLASS)); 2012 return null; 2013 case ARRAY: 2014 return isSubtype(t, sym.type) ? sym.type : null; 2015 case TYPEVAR: 2016 return asSuper(t, sym); 2017 case ERROR: 2018 return t; 2019 default: 2020 return null; 2021 } 2022 } 2023 2024 /** 2025 * Return the base type of t or any of its enclosing types that 2026 * starts with the given symbol. If none exists, return null. 2027 * 2028 * @param t a type 2029 * @param sym a symbol 2030 */ 2031 public Type asEnclosingSuper(Type t, Symbol sym) { 2032 switch (t.getTag()) { 2033 case CLASS: 2034 do { 2035 Type s = asSuper(t, sym); 2036 if (s != null) return s; 2037 Type outer = t.getEnclosingType(); 2038 t = (outer.hasTag(CLASS)) ? outer : 2039 (t.tsym.owner.enclClass() != null) ? t.tsym.owner.enclClass().type : 2040 Type.noType; 2041 } while (t.hasTag(CLASS)); 2042 return null; 2043 case ARRAY: 2044 return isSubtype(t, sym.type) ? sym.type : null; 2045 case TYPEVAR: 2046 return asSuper(t, sym); 2047 case ERROR: 2048 return t; 2049 default: 2050 return null; 2051 } 2052 } 2053 // </editor-fold> 2054 2055 // <editor-fold defaultstate="collapsed" desc="memberType"> 2056 /** 2057 * The type of given symbol, seen as a member of t. 2058 * 2059 * @param t a type 2060 * @param sym a symbol 2061 */ 2062 public Type memberType(Type t, Symbol sym) { 2063 return (sym.flags() & STATIC) != 0 2064 ? sym.type 2065 : memberType.visit(t, sym); 2066 } 2067 // where 2068 private SimpleVisitor<Type,Symbol> memberType = new SimpleVisitor<Type,Symbol>() { 2069 2070 public Type visitType(Type t, Symbol sym) { 2071 return sym.type; 2072 } 2073 2074 @Override 2075 public Type visitWildcardType(WildcardType t, Symbol sym) { 2076 return memberType(wildUpperBound(t), sym); 2077 } 2078 2079 @Override 2080 public Type visitClassType(ClassType t, Symbol sym) { 2081 Symbol owner = sym.owner; 2082 long flags = sym.flags(); 2083 if (((flags & STATIC) == 0) && owner.type.isParameterized()) { 2084 Type base = asOuterSuper(t, owner); 2085 //if t is an intersection type T = CT & I1 & I2 ... & In 2086 //its supertypes CT, I1, ... In might contain wildcards 2087 //so we need to go through capture conversion 2088 base = t.isCompound() ? capture(base) : base; 2089 if (base != null) { 2090 List<Type> ownerParams = owner.type.allparams(); 2091 List<Type> baseParams = base.allparams(); 2092 if (ownerParams.nonEmpty()) { 2093 if (baseParams.isEmpty()) { 2094 // then base is a raw type 2095 return erasure(sym.type); 2096 } else { 2097 return subst(sym.type, ownerParams, baseParams); 2098 } 2099 } 2100 } 2101 } 2102 return sym.type; 2103 } 2104 2105 @Override 2106 public Type visitTypeVar(TypeVar t, Symbol sym) { 2107 return memberType(t.bound, sym); 2108 } 2109 2110 @Override 2111 public Type visitErrorType(ErrorType t, Symbol sym) { 2112 return t; 2113 } 2114 }; 2115 // </editor-fold> 2116 2117 // <editor-fold defaultstate="collapsed" desc="isAssignable"> 2118 public boolean isAssignable(Type t, Type s) { 2119 return isAssignable(t, s, noWarnings); 2120 } 2121 2122 /** 2123 * Is t assignable to s?<br> 2124 * Equivalent to subtype except for constant values and raw 2125 * types.<br> 2126 * (not defined for Method and ForAll types) 2127 */ 2128 public boolean isAssignable(Type t, Type s, Warner warn) { 2129 if (t.hasTag(ERROR)) 2130 return true; 2131 if (t.getTag().isSubRangeOf(INT) && t.constValue() != null) { 2132 int value = ((Number)t.constValue()).intValue(); 2133 switch (s.getTag()) { 2134 case BYTE: 2135 if (Byte.MIN_VALUE <= value && value <= Byte.MAX_VALUE) 2136 return true; 2137 break; 2138 case CHAR: 2139 if (Character.MIN_VALUE <= value && value <= Character.MAX_VALUE) 2140 return true; 2141 break; 2142 case SHORT: 2143 if (Short.MIN_VALUE <= value && value <= Short.MAX_VALUE) 2144 return true; 2145 break; 2146 case INT: 2147 return true; 2148 case CLASS: 2149 switch (unboxedType(s).getTag()) { 2150 case BYTE: 2151 case CHAR: 2152 case SHORT: 2153 return isAssignable(t, unboxedType(s), warn); 2154 } 2155 break; 2156 } 2157 } 2158 return isConvertible(t, s, warn); 2159 } 2160 // </editor-fold> 2161 2162 // <editor-fold defaultstate="collapsed" desc="erasure"> 2163 /** 2164 * The erasure of t {@code |t|} -- the type that results when all 2165 * type parameters in t are deleted. 2166 */ 2167 public Type erasure(Type t) { 2168 return eraseNotNeeded(t)? t : erasure(t, false); 2169 } 2170 //where 2171 private boolean eraseNotNeeded(Type t) { 2172 // We don't want to erase primitive types and String type as that 2173 // operation is idempotent. Also, erasing these could result in loss 2174 // of information such as constant values attached to such types. 2175 return (t.isPrimitive()) || (syms.stringType.tsym == t.tsym); 2176 } 2177 2178 private Type erasure(Type t, boolean recurse) { 2179 if (t.isPrimitive()) { 2180 return t; /* fast special case */ 2181 } else { 2182 Type out = erasure.visit(t, recurse); 2183 return out; 2184 } 2185 } 2186 // where 2187 private SimpleVisitor<Type, Boolean> erasure = new SimpleVisitor<Type, Boolean>() { 2188 private Type combineMetadata(final Type ty, 2189 final TypeMetadata md) { 2190 if (!md.isEmpty()) { 2191 switch (ty.getKind()) { 2192 default: return ty.clone(ty.metadata.combine(md)); 2193 case OTHER: 2194 case UNION: 2195 case INTERSECTION: 2196 case PACKAGE: 2197 case EXECUTABLE: 2198 case NONE: 2199 case VOID: 2200 case ERROR: 2201 return ty; 2202 } 2203 } else { 2204 return ty; 2205 } 2206 } 2207 2208 public Type visitType(Type t, Boolean recurse) { 2209 if (t.isPrimitive()) 2210 return t; /*fast special case*/ 2211 else { 2212 Type erased = t.map(recurse ? erasureRecFun : erasureFun); 2213 return combineMetadata(erased, t.getMetadata()); 2214 } 2215 } 2216 2217 @Override 2218 public Type visitClassType(ClassType t, Boolean recurse) { 2219 Type erased = t.tsym.erasure(Types.this); 2220 if (recurse) { 2221 erased = new ErasedClassType(erased.getEnclosingType(),erased.tsym, t.getMetadata()); 2222 return erased; 2223 } else { 2224 return combineMetadata(erased, t.getMetadata()); 2225 } 2226 } 2227 2228 @Override 2229 public Type visitTypeVar(TypeVar t, Boolean recurse) { 2230 Type erased = erasure(t.bound, recurse); 2231 return combineMetadata(erased, t.getMetadata()); 2232 } 2233 2234 @Override 2235 public Type visitErrorType(ErrorType t, Boolean recurse) { 2236 return t; 2237 } 2238 }; 2239 2240 private Mapping erasureFun = new Mapping ("erasure") { 2241 public Type apply(Type t) { return erasure(t); } 2242 }; 2243 2244 private Mapping erasureRecFun = new Mapping ("erasureRecursive") { 2245 public Type apply(Type t) { return erasureRecursive(t); } 2246 }; 2247 2248 public List<Type> erasure(List<Type> ts) { 2249 return Type.map(ts, erasureFun); 2250 } 2251 2252 public Type erasureRecursive(Type t) { 2253 return erasure(t, true); 2254 } 2255 2256 public List<Type> erasureRecursive(List<Type> ts) { 2257 return Type.map(ts, erasureRecFun); 2258 } 2259 // </editor-fold> 2260 2261 // <editor-fold defaultstate="collapsed" desc="makeCompoundType"> 2262 /** 2263 * Make a compound type from non-empty list of types. The list should be 2264 * ordered according to {@link Symbol#precedes(TypeSymbol,Types)}. 2265 * 2266 * @param bounds the types from which the compound type is formed 2267 * @param supertype is objectType if all bounds are interfaces, 2268 * null otherwise. 2269 */ 2270 public Type makeCompoundType(List<Type> bounds) { 2271 return makeCompoundType(bounds, bounds.head.tsym.isInterface()); 2272 } 2273 public Type makeCompoundType(List<Type> bounds, boolean allInterfaces) { 2274 Assert.check(bounds.nonEmpty()); 2275 Type firstExplicitBound = bounds.head; 2276 if (allInterfaces) { 2277 bounds = bounds.prepend(syms.objectType); 2278 } 2279 ClassSymbol bc = 2280 new ClassSymbol(ABSTRACT|PUBLIC|SYNTHETIC|COMPOUND|ACYCLIC, 2281 Type.moreInfo 2282 ? names.fromString(bounds.toString()) 2283 : names.empty, 2284 null, 2285 syms.noSymbol); 2286 bc.type = new IntersectionClassType(bounds, bc, allInterfaces); 2287 bc.erasure_field = (bounds.head.hasTag(TYPEVAR)) ? 2288 syms.objectType : // error condition, recover 2289 erasure(firstExplicitBound); 2290 bc.members_field = WriteableScope.create(bc); 2291 return bc.type; 2292 } 2293 2294 /** 2295 * A convenience wrapper for {@link #makeCompoundType(List)}; the 2296 * arguments are converted to a list and passed to the other 2297 * method. Note that this might cause a symbol completion. 2298 * Hence, this version of makeCompoundType may not be called 2299 * during a classfile read. 2300 */ 2301 public Type makeCompoundType(Type bound1, Type bound2) { 2302 return makeCompoundType(List.of(bound1, bound2)); 2303 } 2304 // </editor-fold> 2305 2306 // <editor-fold defaultstate="collapsed" desc="supertype"> 2307 public Type supertype(Type t) { 2308 return supertype.visit(t); 2309 } 2310 // where 2311 private UnaryVisitor<Type> supertype = new UnaryVisitor<Type>() { 2312 2313 public Type visitType(Type t, Void ignored) { 2314 // A note on wildcards: there is no good way to 2315 // determine a supertype for a super bounded wildcard. 2316 return Type.noType; 2317 } 2318 2319 @Override 2320 public Type visitClassType(ClassType t, Void ignored) { 2321 if (t.supertype_field == null) { 2322 Type supertype = ((ClassSymbol)t.tsym).getSuperclass(); 2323 // An interface has no superclass; its supertype is Object. 2324 if (t.isInterface()) 2325 supertype = ((ClassType)t.tsym.type).supertype_field; 2326 if (t.supertype_field == null) { 2327 List<Type> actuals = classBound(t).allparams(); 2328 List<Type> formals = t.tsym.type.allparams(); 2329 if (t.hasErasedSupertypes()) { 2330 t.supertype_field = erasureRecursive(supertype); 2331 } else if (formals.nonEmpty()) { 2332 t.supertype_field = subst(supertype, formals, actuals); 2333 } 2334 else { 2335 t.supertype_field = supertype; 2336 } 2337 } 2338 } 2339 return t.supertype_field; 2340 } 2341 2342 /** 2343 * The supertype is always a class type. If the type 2344 * variable's bounds start with a class type, this is also 2345 * the supertype. Otherwise, the supertype is 2346 * java.lang.Object. 2347 */ 2348 @Override 2349 public Type visitTypeVar(TypeVar t, Void ignored) { 2350 if (t.bound.hasTag(TYPEVAR) || 2351 (!t.bound.isCompound() && !t.bound.isInterface())) { 2352 return t.bound; 2353 } else { 2354 return supertype(t.bound); 2355 } 2356 } 2357 2358 @Override 2359 public Type visitArrayType(ArrayType t, Void ignored) { 2360 if (t.elemtype.isPrimitive() || isSameType(t.elemtype, syms.objectType)) 2361 return arraySuperType(); 2362 else 2363 return new ArrayType(supertype(t.elemtype), t.tsym); 2364 } 2365 2366 @Override 2367 public Type visitErrorType(ErrorType t, Void ignored) { 2368 return Type.noType; 2369 } 2370 }; 2371 // </editor-fold> 2372 2373 // <editor-fold defaultstate="collapsed" desc="interfaces"> 2374 /** 2375 * Return the interfaces implemented by this class. 2376 */ 2377 public List<Type> interfaces(Type t) { 2378 return interfaces.visit(t); 2379 } 2380 // where 2381 private UnaryVisitor<List<Type>> interfaces = new UnaryVisitor<List<Type>>() { 2382 2383 public List<Type> visitType(Type t, Void ignored) { 2384 return List.nil(); 2385 } 2386 2387 @Override 2388 public List<Type> visitClassType(ClassType t, Void ignored) { 2389 if (t.interfaces_field == null) { 2390 List<Type> interfaces = ((ClassSymbol)t.tsym).getInterfaces(); 2391 if (t.interfaces_field == null) { 2392 // If t.interfaces_field is null, then t must 2393 // be a parameterized type (not to be confused 2394 // with a generic type declaration). 2395 // Terminology: 2396 // Parameterized type: List<String> 2397 // Generic type declaration: class List<E> { ... } 2398 // So t corresponds to List<String> and 2399 // t.tsym.type corresponds to List<E>. 2400 // The reason t must be parameterized type is 2401 // that completion will happen as a side 2402 // effect of calling 2403 // ClassSymbol.getInterfaces. Since 2404 // t.interfaces_field is null after 2405 // completion, we can assume that t is not the 2406 // type of a class/interface declaration. 2407 Assert.check(t != t.tsym.type, t); 2408 List<Type> actuals = t.allparams(); 2409 List<Type> formals = t.tsym.type.allparams(); 2410 if (t.hasErasedSupertypes()) { 2411 t.interfaces_field = erasureRecursive(interfaces); 2412 } else if (formals.nonEmpty()) { 2413 t.interfaces_field = subst(interfaces, formals, actuals); 2414 } 2415 else { 2416 t.interfaces_field = interfaces; 2417 } 2418 } 2419 } 2420 return t.interfaces_field; 2421 } 2422 2423 @Override 2424 public List<Type> visitTypeVar(TypeVar t, Void ignored) { 2425 if (t.bound.isCompound()) 2426 return interfaces(t.bound); 2427 2428 if (t.bound.isInterface()) 2429 return List.of(t.bound); 2430 2431 return List.nil(); 2432 } 2433 }; 2434 2435 public List<Type> directSupertypes(Type t) { 2436 return directSupertypes.visit(t); 2437 } 2438 // where 2439 private final UnaryVisitor<List<Type>> directSupertypes = new UnaryVisitor<List<Type>>() { 2440 2441 public List<Type> visitType(final Type type, final Void ignored) { 2442 if (!type.isCompound()) { 2443 final Type sup = supertype(type); 2444 return (sup == Type.noType || sup == type || sup == null) 2445 ? interfaces(type) 2446 : interfaces(type).prepend(sup); 2447 } else { 2448 return visitIntersectionType((IntersectionClassType) type); 2449 } 2450 } 2451 2452 private List<Type> visitIntersectionType(final IntersectionClassType it) { 2453 return it.getExplicitComponents(); 2454 } 2455 2456 }; 2457 2458 public boolean isDirectSuperInterface(TypeSymbol isym, TypeSymbol origin) { 2459 for (Type i2 : interfaces(origin.type)) { 2460 if (isym == i2.tsym) return true; 2461 } 2462 return false; 2463 } 2464 // </editor-fold> 2465 2466 // <editor-fold defaultstate="collapsed" desc="isDerivedRaw"> 2467 Map<Type,Boolean> isDerivedRawCache = new HashMap<>(); 2468 2469 public boolean isDerivedRaw(Type t) { 2470 Boolean result = isDerivedRawCache.get(t); 2471 if (result == null) { 2472 result = isDerivedRawInternal(t); 2473 isDerivedRawCache.put(t, result); 2474 } 2475 return result; 2476 } 2477 2478 public boolean isDerivedRawInternal(Type t) { 2479 if (t.isErroneous()) 2480 return false; 2481 return 2482 t.isRaw() || 2483 supertype(t) != Type.noType && isDerivedRaw(supertype(t)) || 2484 isDerivedRaw(interfaces(t)); 2485 } 2486 2487 public boolean isDerivedRaw(List<Type> ts) { 2488 List<Type> l = ts; 2489 while (l.nonEmpty() && !isDerivedRaw(l.head)) l = l.tail; 2490 return l.nonEmpty(); 2491 } 2492 // </editor-fold> 2493 2494 // <editor-fold defaultstate="collapsed" desc="setBounds"> 2495 /** 2496 * Set the bounds field of the given type variable to reflect a 2497 * (possibly multiple) list of bounds. 2498 * @param t a type variable 2499 * @param bounds the bounds, must be nonempty 2500 * @param supertype is objectType if all bounds are interfaces, 2501 * null otherwise. 2502 */ 2503 public void setBounds(TypeVar t, List<Type> bounds) { 2504 setBounds(t, bounds, bounds.head.tsym.isInterface()); 2505 } 2506 2507 /** 2508 * Same as {@link #setBounds(Type.TypeVar,List,Type)}, except that 2509 * third parameter is computed directly, as follows: if all 2510 * all bounds are interface types, the computed supertype is Object, 2511 * otherwise the supertype is simply left null (in this case, the supertype 2512 * is assumed to be the head of the bound list passed as second argument). 2513 * Note that this check might cause a symbol completion. Hence, this version of 2514 * setBounds may not be called during a classfile read. 2515 */ 2516 public void setBounds(TypeVar t, List<Type> bounds, boolean allInterfaces) { 2517 t.bound = bounds.tail.isEmpty() ? 2518 bounds.head : 2519 makeCompoundType(bounds, allInterfaces); 2520 t.rank_field = -1; 2521 } 2522 // </editor-fold> 2523 2524 // <editor-fold defaultstate="collapsed" desc="getBounds"> 2525 /** 2526 * Return list of bounds of the given type variable. 2527 */ 2528 public List<Type> getBounds(TypeVar t) { 2529 if (t.bound.hasTag(NONE)) 2530 return List.nil(); 2531 else if (t.bound.isErroneous() || !t.bound.isCompound()) 2532 return List.of(t.bound); 2533 else if ((erasure(t).tsym.flags() & INTERFACE) == 0) 2534 return interfaces(t).prepend(supertype(t)); 2535 else 2536 // No superclass was given in bounds. 2537 // In this case, supertype is Object, erasure is first interface. 2538 return interfaces(t); 2539 } 2540 // </editor-fold> 2541 2542 // <editor-fold defaultstate="collapsed" desc="classBound"> 2543 /** 2544 * If the given type is a (possibly selected) type variable, 2545 * return the bounding class of this type, otherwise return the 2546 * type itself. 2547 */ 2548 public Type classBound(Type t) { 2549 return classBound.visit(t); 2550 } 2551 // where 2552 private UnaryVisitor<Type> classBound = new UnaryVisitor<Type>() { 2553 2554 public Type visitType(Type t, Void ignored) { 2555 return t; 2556 } 2557 2558 @Override 2559 public Type visitClassType(ClassType t, Void ignored) { 2560 Type outer1 = classBound(t.getEnclosingType()); 2561 if (outer1 != t.getEnclosingType()) 2562 return new ClassType(outer1, t.getTypeArguments(), t.tsym, 2563 t.getMetadata()); 2564 else 2565 return t; 2566 } 2567 2568 @Override 2569 public Type visitTypeVar(TypeVar t, Void ignored) { 2570 return classBound(supertype(t)); 2571 } 2572 2573 @Override 2574 public Type visitErrorType(ErrorType t, Void ignored) { 2575 return t; 2576 } 2577 }; 2578 // </editor-fold> 2579 2580 // <editor-fold defaultstate="collapsed" desc="sub signature / override equivalence"> 2581 /** 2582 * Returns true iff the first signature is a <em>sub 2583 * signature</em> of the other. This is <b>not</b> an equivalence 2584 * relation. 2585 * 2586 * @jls section 8.4.2. 2587 * @see #overrideEquivalent(Type t, Type s) 2588 * @param t first signature (possibly raw). 2589 * @param s second signature (could be subjected to erasure). 2590 * @return true if t is a sub signature of s. 2591 */ 2592 public boolean isSubSignature(Type t, Type s) { 2593 return isSubSignature(t, s, true); 2594 } 2595 2596 public boolean isSubSignature(Type t, Type s, boolean strict) { 2597 return hasSameArgs(t, s, strict) || hasSameArgs(t, erasure(s), strict); 2598 } 2599 2600 /** 2601 * Returns true iff these signatures are related by <em>override 2602 * equivalence</em>. This is the natural extension of 2603 * isSubSignature to an equivalence relation. 2604 * 2605 * @jls section 8.4.2. 2606 * @see #isSubSignature(Type t, Type s) 2607 * @param t a signature (possible raw, could be subjected to 2608 * erasure). 2609 * @param s a signature (possible raw, could be subjected to 2610 * erasure). 2611 * @return true if either argument is a sub signature of the other. 2612 */ 2613 public boolean overrideEquivalent(Type t, Type s) { 2614 return hasSameArgs(t, s) || 2615 hasSameArgs(t, erasure(s)) || hasSameArgs(erasure(t), s); 2616 } 2617 2618 public boolean overridesObjectMethod(TypeSymbol origin, Symbol msym) { 2619 for (Symbol sym : syms.objectType.tsym.members().getSymbolsByName(msym.name)) { 2620 if (msym.overrides(sym, origin, Types.this, true)) { 2621 return true; 2622 } 2623 } 2624 return false; 2625 } 2626 2627 // <editor-fold defaultstate="collapsed" desc="Determining method implementation in given site"> 2628 class ImplementationCache { 2629 2630 private WeakHashMap<MethodSymbol, SoftReference<Map<TypeSymbol, Entry>>> _map = new WeakHashMap<>(); 2631 2632 class Entry { 2633 final MethodSymbol cachedImpl; 2634 final Filter<Symbol> implFilter; 2635 final boolean checkResult; 2636 final int prevMark; 2637 2638 public Entry(MethodSymbol cachedImpl, 2639 Filter<Symbol> scopeFilter, 2640 boolean checkResult, 2641 int prevMark) { 2642 this.cachedImpl = cachedImpl; 2643 this.implFilter = scopeFilter; 2644 this.checkResult = checkResult; 2645 this.prevMark = prevMark; 2646 } 2647 2648 boolean matches(Filter<Symbol> scopeFilter, boolean checkResult, int mark) { 2649 return this.implFilter == scopeFilter && 2650 this.checkResult == checkResult && 2651 this.prevMark == mark; 2652 } 2653 } 2654 2655 MethodSymbol get(MethodSymbol ms, TypeSymbol origin, boolean checkResult, Filter<Symbol> implFilter) { 2656 SoftReference<Map<TypeSymbol, Entry>> ref_cache = _map.get(ms); 2657 Map<TypeSymbol, Entry> cache = ref_cache != null ? ref_cache.get() : null; 2658 if (cache == null) { 2659 cache = new HashMap<>(); 2660 _map.put(ms, new SoftReference<>(cache)); 2661 } 2662 Entry e = cache.get(origin); 2663 CompoundScope members = membersClosure(origin.type, true); 2664 if (e == null || 2665 !e.matches(implFilter, checkResult, members.getMark())) { 2666 MethodSymbol impl = implementationInternal(ms, origin, checkResult, implFilter); 2667 cache.put(origin, new Entry(impl, implFilter, checkResult, members.getMark())); 2668 return impl; 2669 } 2670 else { 2671 return e.cachedImpl; 2672 } 2673 } 2674 2675 private MethodSymbol implementationInternal(MethodSymbol ms, TypeSymbol origin, boolean checkResult, Filter<Symbol> implFilter) { 2676 for (Type t = origin.type; t.hasTag(CLASS) || t.hasTag(TYPEVAR); t = supertype(t)) { 2677 while (t.hasTag(TYPEVAR)) 2678 t = t.getUpperBound(); 2679 TypeSymbol c = t.tsym; 2680 for (Symbol sym : c.members().getSymbolsByName(ms.name, implFilter)) { 2681 if (sym != null && 2682 sym.overrides(ms, origin, Types.this, checkResult)) 2683 return (MethodSymbol)sym; 2684 } 2685 } 2686 return null; 2687 } 2688 } 2689 2690 private ImplementationCache implCache = new ImplementationCache(); 2691 2692 public MethodSymbol implementation(MethodSymbol ms, TypeSymbol origin, boolean checkResult, Filter<Symbol> implFilter) { 2693 return implCache.get(ms, origin, checkResult, implFilter); 2694 } 2695 // </editor-fold> 2696 2697 // <editor-fold defaultstate="collapsed" desc="compute transitive closure of all members in given site"> 2698 class MembersClosureCache extends SimpleVisitor<CompoundScope, Boolean> { 2699 2700 private WeakHashMap<TypeSymbol, Entry> _map = new WeakHashMap<>(); 2701 2702 class Entry { 2703 final boolean skipInterfaces; 2704 final CompoundScope compoundScope; 2705 2706 public Entry(boolean skipInterfaces, CompoundScope compoundScope) { 2707 this.skipInterfaces = skipInterfaces; 2708 this.compoundScope = compoundScope; 2709 } 2710 2711 boolean matches(boolean skipInterfaces) { 2712 return this.skipInterfaces == skipInterfaces; 2713 } 2714 } 2715 2716 List<TypeSymbol> seenTypes = List.nil(); 2717 2718 /** members closure visitor methods **/ 2719 2720 public CompoundScope visitType(Type t, Boolean skipInterface) { 2721 return null; 2722 } 2723 2724 @Override 2725 public CompoundScope visitClassType(ClassType t, Boolean skipInterface) { 2726 if (seenTypes.contains(t.tsym)) { 2727 //this is possible when an interface is implemented in multiple 2728 //superclasses, or when a classs hierarchy is circular - in such 2729 //cases we don't need to recurse (empty scope is returned) 2730 return new CompoundScope(t.tsym); 2731 } 2732 try { 2733 seenTypes = seenTypes.prepend(t.tsym); 2734 ClassSymbol csym = (ClassSymbol)t.tsym; 2735 Entry e = _map.get(csym); 2736 if (e == null || !e.matches(skipInterface)) { 2737 CompoundScope membersClosure = new CompoundScope(csym); 2738 if (!skipInterface) { 2739 for (Type i : interfaces(t)) { 2740 membersClosure.prependSubScope(visit(i, skipInterface)); 2741 } 2742 } 2743 membersClosure.prependSubScope(visit(supertype(t), skipInterface)); 2744 membersClosure.prependSubScope(csym.members()); 2745 e = new Entry(skipInterface, membersClosure); 2746 _map.put(csym, e); 2747 } 2748 return e.compoundScope; 2749 } 2750 finally { 2751 seenTypes = seenTypes.tail; 2752 } 2753 } 2754 2755 @Override 2756 public CompoundScope visitTypeVar(TypeVar t, Boolean skipInterface) { 2757 return visit(t.getUpperBound(), skipInterface); 2758 } 2759 } 2760 2761 private MembersClosureCache membersCache = new MembersClosureCache(); 2762 2763 public CompoundScope membersClosure(Type site, boolean skipInterface) { 2764 return membersCache.visit(site, skipInterface); 2765 } 2766 // </editor-fold> 2767 2768 2769 //where 2770 public List<MethodSymbol> interfaceCandidates(Type site, MethodSymbol ms) { 2771 Filter<Symbol> filter = new MethodFilter(ms, site); 2772 List<MethodSymbol> candidates = List.nil(); 2773 for (Symbol s : membersClosure(site, false).getSymbols(filter)) { 2774 if (!site.tsym.isInterface() && !s.owner.isInterface()) { 2775 return List.of((MethodSymbol)s); 2776 } else if (!candidates.contains(s)) { 2777 candidates = candidates.prepend((MethodSymbol)s); 2778 } 2779 } 2780 return prune(candidates); 2781 } 2782 2783 public List<MethodSymbol> prune(List<MethodSymbol> methods) { 2784 ListBuffer<MethodSymbol> methodsMin = new ListBuffer<>(); 2785 for (MethodSymbol m1 : methods) { 2786 boolean isMin_m1 = true; 2787 for (MethodSymbol m2 : methods) { 2788 if (m1 == m2) continue; 2789 if (m2.owner != m1.owner && 2790 asSuper(m2.owner.type, m1.owner) != null) { 2791 isMin_m1 = false; 2792 break; 2793 } 2794 } 2795 if (isMin_m1) 2796 methodsMin.append(m1); 2797 } 2798 return methodsMin.toList(); 2799 } 2800 // where 2801 private class MethodFilter implements Filter<Symbol> { 2802 2803 Symbol msym; 2804 Type site; 2805 2806 MethodFilter(Symbol msym, Type site) { 2807 this.msym = msym; 2808 this.site = site; 2809 } 2810 2811 public boolean accepts(Symbol s) { 2812 return s.kind == MTH && 2813 s.name == msym.name && 2814 (s.flags() & SYNTHETIC) == 0 && 2815 s.isInheritedIn(site.tsym, Types.this) && 2816 overrideEquivalent(memberType(site, s), memberType(site, msym)); 2817 } 2818 } 2819 // </editor-fold> 2820 2821 /** 2822 * Does t have the same arguments as s? It is assumed that both 2823 * types are (possibly polymorphic) method types. Monomorphic 2824 * method types "have the same arguments", if their argument lists 2825 * are equal. Polymorphic method types "have the same arguments", 2826 * if they have the same arguments after renaming all type 2827 * variables of one to corresponding type variables in the other, 2828 * where correspondence is by position in the type parameter list. 2829 */ 2830 public boolean hasSameArgs(Type t, Type s) { 2831 return hasSameArgs(t, s, true); 2832 } 2833 2834 public boolean hasSameArgs(Type t, Type s, boolean strict) { 2835 return hasSameArgs(t, s, strict ? hasSameArgs_strict : hasSameArgs_nonstrict); 2836 } 2837 2838 private boolean hasSameArgs(Type t, Type s, TypeRelation hasSameArgs) { 2839 return hasSameArgs.visit(t, s); 2840 } 2841 // where 2842 private class HasSameArgs extends TypeRelation { 2843 2844 boolean strict; 2845 2846 public HasSameArgs(boolean strict) { 2847 this.strict = strict; 2848 } 2849 2850 public Boolean visitType(Type t, Type s) { 2851 throw new AssertionError(); 2852 } 2853 2854 @Override 2855 public Boolean visitMethodType(MethodType t, Type s) { 2856 return s.hasTag(METHOD) 2857 && containsTypeEquivalent(t.argtypes, s.getParameterTypes()); 2858 } 2859 2860 @Override 2861 public Boolean visitForAll(ForAll t, Type s) { 2862 if (!s.hasTag(FORALL)) 2863 return strict ? false : visitMethodType(t.asMethodType(), s); 2864 2865 ForAll forAll = (ForAll)s; 2866 return hasSameBounds(t, forAll) 2867 && visit(t.qtype, subst(forAll.qtype, forAll.tvars, t.tvars)); 2868 } 2869 2870 @Override 2871 public Boolean visitErrorType(ErrorType t, Type s) { 2872 return false; 2873 } 2874 } 2875 2876 TypeRelation hasSameArgs_strict = new HasSameArgs(true); 2877 TypeRelation hasSameArgs_nonstrict = new HasSameArgs(false); 2878 2879 // </editor-fold> 2880 2881 // <editor-fold defaultstate="collapsed" desc="subst"> 2882 public List<Type> subst(List<Type> ts, 2883 List<Type> from, 2884 List<Type> to) { 2885 return new Subst(from, to).subst(ts); 2886 } 2887 2888 /** 2889 * Substitute all occurrences of a type in `from' with the 2890 * corresponding type in `to' in 't'. Match lists `from' and `to' 2891 * from the right: If lists have different length, discard leading 2892 * elements of the longer list. 2893 */ 2894 public Type subst(Type t, List<Type> from, List<Type> to) { 2895 return new Subst(from, to).subst(t); 2896 } 2897 2898 private class Subst extends UnaryVisitor<Type> { 2899 List<Type> from; 2900 List<Type> to; 2901 2902 public Subst(List<Type> from, List<Type> to) { 2903 int fromLength = from.length(); 2904 int toLength = to.length(); 2905 while (fromLength > toLength) { 2906 fromLength--; 2907 from = from.tail; 2908 } 2909 while (fromLength < toLength) { 2910 toLength--; 2911 to = to.tail; 2912 } 2913 this.from = from; 2914 this.to = to; 2915 } 2916 2917 Type subst(Type t) { 2918 if (from.tail == null) 2919 return t; 2920 else 2921 return visit(t); 2922 } 2923 2924 List<Type> subst(List<Type> ts) { 2925 if (from.tail == null) 2926 return ts; 2927 boolean wild = false; 2928 if (ts.nonEmpty() && from.nonEmpty()) { 2929 Type head1 = subst(ts.head); 2930 List<Type> tail1 = subst(ts.tail); 2931 if (head1 != ts.head || tail1 != ts.tail) 2932 return tail1.prepend(head1); 2933 } 2934 return ts; 2935 } 2936 2937 public Type visitType(Type t, Void ignored) { 2938 return t; 2939 } 2940 2941 @Override 2942 public Type visitMethodType(MethodType t, Void ignored) { 2943 List<Type> argtypes = subst(t.argtypes); 2944 Type restype = subst(t.restype); 2945 List<Type> thrown = subst(t.thrown); 2946 if (argtypes == t.argtypes && 2947 restype == t.restype && 2948 thrown == t.thrown) 2949 return t; 2950 else 2951 return new MethodType(argtypes, restype, thrown, t.tsym); 2952 } 2953 2954 @Override 2955 public Type visitTypeVar(TypeVar t, Void ignored) { 2956 for (List<Type> from = this.from, to = this.to; 2957 from.nonEmpty(); 2958 from = from.tail, to = to.tail) { 2959 if (t == from.head) { 2960 return to.head.withTypeVar(t); 2961 } 2962 } 2963 return t; 2964 } 2965 2966 @Override 2967 public Type visitUndetVar(UndetVar t, Void ignored) { 2968 //do nothing - we should not replace inside undet variables 2969 return t; 2970 } 2971 2972 @Override 2973 public Type visitClassType(ClassType t, Void ignored) { 2974 if (!t.isCompound()) { 2975 List<Type> typarams = t.getTypeArguments(); 2976 List<Type> typarams1 = subst(typarams); 2977 Type outer = t.getEnclosingType(); 2978 Type outer1 = subst(outer); 2979 if (typarams1 == typarams && outer1 == outer) 2980 return t; 2981 else 2982 return new ClassType(outer1, typarams1, t.tsym, 2983 t.getMetadata()); 2984 } else { 2985 Type st = subst(supertype(t)); 2986 List<Type> is = subst(interfaces(t)); 2987 if (st == supertype(t) && is == interfaces(t)) 2988 return t; 2989 else 2990 return makeCompoundType(is.prepend(st)); 2991 } 2992 } 2993 2994 @Override 2995 public Type visitWildcardType(WildcardType t, Void ignored) { 2996 Type bound = t.type; 2997 if (t.kind != BoundKind.UNBOUND) 2998 bound = subst(bound); 2999 if (bound == t.type) { 3000 return t; 3001 } else { 3002 if (t.isExtendsBound() && bound.isExtendsBound()) 3003 bound = wildUpperBound(bound); 3004 return new WildcardType(bound, t.kind, syms.boundClass, 3005 t.bound, t.getMetadata()); 3006 } 3007 } 3008 3009 @Override 3010 public Type visitArrayType(ArrayType t, Void ignored) { 3011 Type elemtype = subst(t.elemtype); 3012 if (elemtype == t.elemtype) 3013 return t; 3014 else 3015 return new ArrayType(elemtype, t.tsym, t.getMetadata()); 3016 } 3017 3018 @Override 3019 public Type visitForAll(ForAll t, Void ignored) { 3020 if (Type.containsAny(to, t.tvars)) { 3021 //perform alpha-renaming of free-variables in 't' 3022 //if 'to' types contain variables that are free in 't' 3023 List<Type> freevars = newInstances(t.tvars); 3024 t = new ForAll(freevars, 3025 Types.this.subst(t.qtype, t.tvars, freevars)); 3026 } 3027 List<Type> tvars1 = substBounds(t.tvars, from, to); 3028 Type qtype1 = subst(t.qtype); 3029 if (tvars1 == t.tvars && qtype1 == t.qtype) { 3030 return t; 3031 } else if (tvars1 == t.tvars) { 3032 return new ForAll(tvars1, qtype1); 3033 } else { 3034 return new ForAll(tvars1, 3035 Types.this.subst(qtype1, t.tvars, tvars1)); 3036 } 3037 } 3038 3039 @Override 3040 public Type visitErrorType(ErrorType t, Void ignored) { 3041 return t; 3042 } 3043 } 3044 3045 public List<Type> substBounds(List<Type> tvars, 3046 List<Type> from, 3047 List<Type> to) { 3048 if (tvars.isEmpty()) 3049 return tvars; 3050 ListBuffer<Type> newBoundsBuf = new ListBuffer<>(); 3051 boolean changed = false; 3052 // calculate new bounds 3053 for (Type t : tvars) { 3054 TypeVar tv = (TypeVar) t; 3055 Type bound = subst(tv.bound, from, to); 3056 if (bound != tv.bound) 3057 changed = true; 3058 newBoundsBuf.append(bound); 3059 } 3060 if (!changed) 3061 return tvars; 3062 ListBuffer<Type> newTvars = new ListBuffer<>(); 3063 // create new type variables without bounds 3064 for (Type t : tvars) { 3065 newTvars.append(new TypeVar(t.tsym, null, syms.botType, 3066 t.getMetadata())); 3067 } 3068 // the new bounds should use the new type variables in place 3069 // of the old 3070 List<Type> newBounds = newBoundsBuf.toList(); 3071 from = tvars; 3072 to = newTvars.toList(); 3073 for (; !newBounds.isEmpty(); newBounds = newBounds.tail) { 3074 newBounds.head = subst(newBounds.head, from, to); 3075 } 3076 newBounds = newBoundsBuf.toList(); 3077 // set the bounds of new type variables to the new bounds 3078 for (Type t : newTvars.toList()) { 3079 TypeVar tv = (TypeVar) t; 3080 tv.bound = newBounds.head; 3081 newBounds = newBounds.tail; 3082 } 3083 return newTvars.toList(); 3084 } 3085 3086 public TypeVar substBound(TypeVar t, List<Type> from, List<Type> to) { 3087 Type bound1 = subst(t.bound, from, to); 3088 if (bound1 == t.bound) 3089 return t; 3090 else { 3091 // create new type variable without bounds 3092 TypeVar tv = new TypeVar(t.tsym, null, syms.botType, 3093 t.getMetadata()); 3094 // the new bound should use the new type variable in place 3095 // of the old 3096 tv.bound = subst(bound1, List.<Type>of(t), List.<Type>of(tv)); 3097 return tv; 3098 } 3099 } 3100 // </editor-fold> 3101 3102 // <editor-fold defaultstate="collapsed" desc="hasSameBounds"> 3103 /** 3104 * Does t have the same bounds for quantified variables as s? 3105 */ 3106 public boolean hasSameBounds(ForAll t, ForAll s) { 3107 List<Type> l1 = t.tvars; 3108 List<Type> l2 = s.tvars; 3109 while (l1.nonEmpty() && l2.nonEmpty() && 3110 isSameType(l1.head.getUpperBound(), 3111 subst(l2.head.getUpperBound(), 3112 s.tvars, 3113 t.tvars))) { 3114 l1 = l1.tail; 3115 l2 = l2.tail; 3116 } 3117 return l1.isEmpty() && l2.isEmpty(); 3118 } 3119 // </editor-fold> 3120 3121 // <editor-fold defaultstate="collapsed" desc="newInstances"> 3122 /** Create new vector of type variables from list of variables 3123 * changing all recursive bounds from old to new list. 3124 */ 3125 public List<Type> newInstances(List<Type> tvars) { 3126 List<Type> tvars1 = Type.map(tvars, newInstanceFun); 3127 for (List<Type> l = tvars1; l.nonEmpty(); l = l.tail) { 3128 TypeVar tv = (TypeVar) l.head; 3129 tv.bound = subst(tv.bound, tvars, tvars1); 3130 } 3131 return tvars1; 3132 } 3133 private static final Mapping newInstanceFun = new Mapping("newInstanceFun") { 3134 public Type apply(Type t) { return new TypeVar(t.tsym, t.getUpperBound(), t.getLowerBound(), t.getMetadata()); } 3135 }; 3136 // </editor-fold> 3137 3138 public Type createMethodTypeWithParameters(Type original, List<Type> newParams) { 3139 return original.accept(methodWithParameters, newParams); 3140 } 3141 // where 3142 private final MapVisitor<List<Type>> methodWithParameters = new MapVisitor<List<Type>>() { 3143 public Type visitType(Type t, List<Type> newParams) { 3144 throw new IllegalArgumentException("Not a method type: " + t); 3145 } 3146 public Type visitMethodType(MethodType t, List<Type> newParams) { 3147 return new MethodType(newParams, t.restype, t.thrown, t.tsym); 3148 } 3149 public Type visitForAll(ForAll t, List<Type> newParams) { 3150 return new ForAll(t.tvars, t.qtype.accept(this, newParams)); 3151 } 3152 }; 3153 3154 public Type createMethodTypeWithThrown(Type original, List<Type> newThrown) { 3155 return original.accept(methodWithThrown, newThrown); 3156 } 3157 // where 3158 private final MapVisitor<List<Type>> methodWithThrown = new MapVisitor<List<Type>>() { 3159 public Type visitType(Type t, List<Type> newThrown) { 3160 throw new IllegalArgumentException("Not a method type: " + t); 3161 } 3162 public Type visitMethodType(MethodType t, List<Type> newThrown) { 3163 return new MethodType(t.argtypes, t.restype, newThrown, t.tsym); 3164 } 3165 public Type visitForAll(ForAll t, List<Type> newThrown) { 3166 return new ForAll(t.tvars, t.qtype.accept(this, newThrown)); 3167 } 3168 }; 3169 3170 public Type createMethodTypeWithReturn(Type original, Type newReturn) { 3171 return original.accept(methodWithReturn, newReturn); 3172 } 3173 // where 3174 private final MapVisitor<Type> methodWithReturn = new MapVisitor<Type>() { 3175 public Type visitType(Type t, Type newReturn) { 3176 throw new IllegalArgumentException("Not a method type: " + t); 3177 } 3178 public Type visitMethodType(MethodType t, Type newReturn) { 3179 return new MethodType(t.argtypes, newReturn, t.thrown, t.tsym); 3180 } 3181 public Type visitForAll(ForAll t, Type newReturn) { 3182 return new ForAll(t.tvars, t.qtype.accept(this, newReturn)); 3183 } 3184 }; 3185 3186 // <editor-fold defaultstate="collapsed" desc="createErrorType"> 3187 public Type createErrorType(Type originalType) { 3188 return new ErrorType(originalType, syms.errSymbol); 3189 } 3190 3191 public Type createErrorType(ClassSymbol c, Type originalType) { 3192 return new ErrorType(c, originalType); 3193 } 3194 3195 public Type createErrorType(Name name, TypeSymbol container, Type originalType) { 3196 return new ErrorType(name, container, originalType); 3197 } 3198 // </editor-fold> 3199 3200 // <editor-fold defaultstate="collapsed" desc="rank"> 3201 /** 3202 * The rank of a class is the length of the longest path between 3203 * the class and java.lang.Object in the class inheritance 3204 * graph. Undefined for all but reference types. 3205 */ 3206 public int rank(Type t) { 3207 switch(t.getTag()) { 3208 case CLASS: { 3209 ClassType cls = (ClassType)t; 3210 if (cls.rank_field < 0) { 3211 Name fullname = cls.tsym.getQualifiedName(); 3212 if (fullname == names.java_lang_Object) 3213 cls.rank_field = 0; 3214 else { 3215 int r = rank(supertype(cls)); 3216 for (List<Type> l = interfaces(cls); 3217 l.nonEmpty(); 3218 l = l.tail) { 3219 if (rank(l.head) > r) 3220 r = rank(l.head); 3221 } 3222 cls.rank_field = r + 1; 3223 } 3224 } 3225 return cls.rank_field; 3226 } 3227 case TYPEVAR: { 3228 TypeVar tvar = (TypeVar)t; 3229 if (tvar.rank_field < 0) { 3230 int r = rank(supertype(tvar)); 3231 for (List<Type> l = interfaces(tvar); 3232 l.nonEmpty(); 3233 l = l.tail) { 3234 if (rank(l.head) > r) r = rank(l.head); 3235 } 3236 tvar.rank_field = r + 1; 3237 } 3238 return tvar.rank_field; 3239 } 3240 case ERROR: 3241 case NONE: 3242 return 0; 3243 default: 3244 throw new AssertionError(); 3245 } 3246 } 3247 // </editor-fold> 3248 3249 /** 3250 * Helper method for generating a string representation of a given type 3251 * accordingly to a given locale 3252 */ 3253 public String toString(Type t, Locale locale) { 3254 return Printer.createStandardPrinter(messages).visit(t, locale); 3255 } 3256 3257 /** 3258 * Helper method for generating a string representation of a given type 3259 * accordingly to a given locale 3260 */ 3261 public String toString(Symbol t, Locale locale) { 3262 return Printer.createStandardPrinter(messages).visit(t, locale); 3263 } 3264 3265 // <editor-fold defaultstate="collapsed" desc="toString"> 3266 /** 3267 * This toString is slightly more descriptive than the one on Type. 3268 * 3269 * @deprecated Types.toString(Type t, Locale l) provides better support 3270 * for localization 3271 */ 3272 @Deprecated 3273 public String toString(Type t) { 3274 if (t.hasTag(FORALL)) { 3275 ForAll forAll = (ForAll)t; 3276 return typaramsString(forAll.tvars) + forAll.qtype; 3277 } 3278 return "" + t; 3279 } 3280 // where 3281 private String typaramsString(List<Type> tvars) { 3282 StringBuilder s = new StringBuilder(); 3283 s.append('<'); 3284 boolean first = true; 3285 for (Type t : tvars) { 3286 if (!first) s.append(", "); 3287 first = false; 3288 appendTyparamString(((TypeVar)t), s); 3289 } 3290 s.append('>'); 3291 return s.toString(); 3292 } 3293 private void appendTyparamString(TypeVar t, StringBuilder buf) { 3294 buf.append(t); 3295 if (t.bound == null || 3296 t.bound.tsym.getQualifiedName() == names.java_lang_Object) 3297 return; 3298 buf.append(" extends "); // Java syntax; no need for i18n 3299 Type bound = t.bound; 3300 if (!bound.isCompound()) { 3301 buf.append(bound); 3302 } else if ((erasure(t).tsym.flags() & INTERFACE) == 0) { 3303 buf.append(supertype(t)); 3304 for (Type intf : interfaces(t)) { 3305 buf.append('&'); 3306 buf.append(intf); 3307 } 3308 } else { 3309 // No superclass was given in bounds. 3310 // In this case, supertype is Object, erasure is first interface. 3311 boolean first = true; 3312 for (Type intf : interfaces(t)) { 3313 if (!first) buf.append('&'); 3314 first = false; 3315 buf.append(intf); 3316 } 3317 } 3318 } 3319 // </editor-fold> 3320 3321 // <editor-fold defaultstate="collapsed" desc="Determining least upper bounds of types"> 3322 /** 3323 * A cache for closures. 3324 * 3325 * <p>A closure is a list of all the supertypes and interfaces of 3326 * a class or interface type, ordered by ClassSymbol.precedes 3327 * (that is, subclasses come first, arbitrary but fixed 3328 * otherwise). 3329 */ 3330 private Map<Type,List<Type>> closureCache = new HashMap<>(); 3331 3332 /** 3333 * Returns the closure of a class or interface type. 3334 */ 3335 public List<Type> closure(Type t) { 3336 List<Type> cl = closureCache.get(t); 3337 if (cl == null) { 3338 Type st = supertype(t); 3339 if (!t.isCompound()) { 3340 if (st.hasTag(CLASS)) { 3341 cl = insert(closure(st), t); 3342 } else if (st.hasTag(TYPEVAR)) { 3343 cl = closure(st).prepend(t); 3344 } else { 3345 cl = List.of(t); 3346 } 3347 } else { 3348 cl = closure(supertype(t)); 3349 } 3350 for (List<Type> l = interfaces(t); l.nonEmpty(); l = l.tail) 3351 cl = union(cl, closure(l.head)); 3352 closureCache.put(t, cl); 3353 } 3354 return cl; 3355 } 3356 3357 /** 3358 * Insert a type in a closure 3359 */ 3360 public List<Type> insert(List<Type> cl, Type t) { 3361 if (cl.isEmpty()) { 3362 return cl.prepend(t); 3363 } else if (t.tsym == cl.head.tsym) { 3364 return cl; 3365 } else if (t.tsym.precedes(cl.head.tsym, this)) { 3366 return cl.prepend(t); 3367 } else { 3368 // t comes after head, or the two are unrelated 3369 return insert(cl.tail, t).prepend(cl.head); 3370 } 3371 } 3372 3373 /** 3374 * Form the union of two closures 3375 */ 3376 public List<Type> union(List<Type> cl1, List<Type> cl2) { 3377 if (cl1.isEmpty()) { 3378 return cl2; 3379 } else if (cl2.isEmpty()) { 3380 return cl1; 3381 } else if (cl1.head.tsym == cl2.head.tsym) { 3382 return union(cl1.tail, cl2.tail).prepend(cl1.head); 3383 } else if (cl1.head.tsym.precedes(cl2.head.tsym, this)) { 3384 return union(cl1.tail, cl2).prepend(cl1.head); 3385 } else if (cl2.head.tsym.precedes(cl1.head.tsym, this)) { 3386 return union(cl1, cl2.tail).prepend(cl2.head); 3387 } else { 3388 // unrelated types 3389 return union(cl1.tail, cl2).prepend(cl1.head); 3390 } 3391 } 3392 3393 /** 3394 * Intersect two closures 3395 */ 3396 public List<Type> intersect(List<Type> cl1, List<Type> cl2) { 3397 if (cl1 == cl2) 3398 return cl1; 3399 if (cl1.isEmpty() || cl2.isEmpty()) 3400 return List.nil(); 3401 if (cl1.head.tsym.precedes(cl2.head.tsym, this)) 3402 return intersect(cl1.tail, cl2); 3403 if (cl2.head.tsym.precedes(cl1.head.tsym, this)) 3404 return intersect(cl1, cl2.tail); 3405 if (isSameType(cl1.head, cl2.head)) 3406 return intersect(cl1.tail, cl2.tail).prepend(cl1.head); 3407 if (cl1.head.tsym == cl2.head.tsym && 3408 cl1.head.hasTag(CLASS) && cl2.head.hasTag(CLASS)) { 3409 if (cl1.head.isParameterized() && cl2.head.isParameterized()) { 3410 Type merge = merge(cl1.head,cl2.head); 3411 return intersect(cl1.tail, cl2.tail).prepend(merge); 3412 } 3413 if (cl1.head.isRaw() || cl2.head.isRaw()) 3414 return intersect(cl1.tail, cl2.tail).prepend(erasure(cl1.head)); 3415 } 3416 return intersect(cl1.tail, cl2.tail); 3417 } 3418 // where 3419 class TypePair { 3420 final Type t1; 3421 final Type t2; 3422 boolean strict; 3423 3424 TypePair(Type t1, Type t2) { 3425 this(t1, t2, false); 3426 } 3427 3428 TypePair(Type t1, Type t2, boolean strict) { 3429 this.t1 = t1; 3430 this.t2 = t2; 3431 this.strict = strict; 3432 } 3433 @Override 3434 public int hashCode() { 3435 return 127 * Types.this.hashCode(t1) + Types.this.hashCode(t2); 3436 } 3437 @Override 3438 public boolean equals(Object obj) { 3439 if (!(obj instanceof TypePair)) 3440 return false; 3441 TypePair typePair = (TypePair)obj; 3442 return isSameType(t1, typePair.t1, strict) 3443 && isSameType(t2, typePair.t2, strict); 3444 } 3445 } 3446 Set<TypePair> mergeCache = new HashSet<>(); 3447 private Type merge(Type c1, Type c2) { 3448 ClassType class1 = (ClassType) c1; 3449 List<Type> act1 = class1.getTypeArguments(); 3450 ClassType class2 = (ClassType) c2; 3451 List<Type> act2 = class2.getTypeArguments(); 3452 ListBuffer<Type> merged = new ListBuffer<>(); 3453 List<Type> typarams = class1.tsym.type.getTypeArguments(); 3454 3455 while (act1.nonEmpty() && act2.nonEmpty() && typarams.nonEmpty()) { 3456 if (containsType(act1.head, act2.head)) { 3457 merged.append(act1.head); 3458 } else if (containsType(act2.head, act1.head)) { 3459 merged.append(act2.head); 3460 } else { 3461 TypePair pair = new TypePair(c1, c2); 3462 Type m; 3463 if (mergeCache.add(pair)) { 3464 m = new WildcardType(lub(wildUpperBound(act1.head), 3465 wildUpperBound(act2.head)), 3466 BoundKind.EXTENDS, 3467 syms.boundClass); 3468 mergeCache.remove(pair); 3469 } else { 3470 m = new WildcardType(syms.objectType, 3471 BoundKind.UNBOUND, 3472 syms.boundClass); 3473 } 3474 merged.append(m.withTypeVar(typarams.head)); 3475 } 3476 act1 = act1.tail; 3477 act2 = act2.tail; 3478 typarams = typarams.tail; 3479 } 3480 Assert.check(act1.isEmpty() && act2.isEmpty() && typarams.isEmpty()); 3481 // There is no spec detailing how type annotations are to 3482 // be inherited. So set it to noAnnotations for now 3483 return new ClassType(class1.getEnclosingType(), merged.toList(), 3484 class1.tsym); 3485 } 3486 3487 /** 3488 * Return the minimum type of a closure, a compound type if no 3489 * unique minimum exists. 3490 */ 3491 private Type compoundMin(List<Type> cl) { 3492 if (cl.isEmpty()) return syms.objectType; 3493 List<Type> compound = closureMin(cl); 3494 if (compound.isEmpty()) 3495 return null; 3496 else if (compound.tail.isEmpty()) 3497 return compound.head; 3498 else 3499 return makeCompoundType(compound); 3500 } 3501 3502 /** 3503 * Return the minimum types of a closure, suitable for computing 3504 * compoundMin or glb. 3505 */ 3506 private List<Type> closureMin(List<Type> cl) { 3507 ListBuffer<Type> classes = new ListBuffer<>(); 3508 ListBuffer<Type> interfaces = new ListBuffer<>(); 3509 Set<Type> toSkip = new HashSet<>(); 3510 while (!cl.isEmpty()) { 3511 Type current = cl.head; 3512 boolean keep = !toSkip.contains(current); 3513 if (keep && current.hasTag(TYPEVAR)) { 3514 // skip lower-bounded variables with a subtype in cl.tail 3515 for (Type t : cl.tail) { 3516 if (isSubtypeNoCapture(t, current)) { 3517 keep = false; 3518 break; 3519 } 3520 } 3521 } 3522 if (keep) { 3523 if (current.isInterface()) 3524 interfaces.append(current); 3525 else 3526 classes.append(current); 3527 for (Type t : cl.tail) { 3528 // skip supertypes of 'current' in cl.tail 3529 if (isSubtypeNoCapture(current, t)) 3530 toSkip.add(t); 3531 } 3532 } 3533 cl = cl.tail; 3534 } 3535 return classes.appendList(interfaces).toList(); 3536 } 3537 3538 /** 3539 * Return the least upper bound of list of types. if the lub does 3540 * not exist return null. 3541 */ 3542 public Type lub(List<Type> ts) { 3543 return lub(ts.toArray(new Type[ts.length()])); 3544 } 3545 3546 /** 3547 * Return the least upper bound (lub) of set of types. If the lub 3548 * does not exist return the type of null (bottom). 3549 */ 3550 public Type lub(Type... ts) { 3551 final int UNKNOWN_BOUND = 0; 3552 final int ARRAY_BOUND = 1; 3553 final int CLASS_BOUND = 2; 3554 3555 int[] kinds = new int[ts.length]; 3556 3557 int boundkind = UNKNOWN_BOUND; 3558 for (int i = 0 ; i < ts.length ; i++) { 3559 Type t = ts[i]; 3560 switch (t.getTag()) { 3561 case CLASS: 3562 boundkind |= kinds[i] = CLASS_BOUND; 3563 break; 3564 case ARRAY: 3565 boundkind |= kinds[i] = ARRAY_BOUND; 3566 break; 3567 case TYPEVAR: 3568 do { 3569 t = t.getUpperBound(); 3570 } while (t.hasTag(TYPEVAR)); 3571 if (t.hasTag(ARRAY)) { 3572 boundkind |= kinds[i] = ARRAY_BOUND; 3573 } else { 3574 boundkind |= kinds[i] = CLASS_BOUND; 3575 } 3576 break; 3577 default: 3578 kinds[i] = UNKNOWN_BOUND; 3579 if (t.isPrimitive()) 3580 return syms.errType; 3581 } 3582 } 3583 switch (boundkind) { 3584 case 0: 3585 return syms.botType; 3586 3587 case ARRAY_BOUND: 3588 // calculate lub(A[], B[]) 3589 Type[] elements = new Type[ts.length]; 3590 for (int i = 0 ; i < ts.length ; i++) { 3591 Type elem = elements[i] = elemTypeFun.apply(ts[i]); 3592 if (elem.isPrimitive()) { 3593 // if a primitive type is found, then return 3594 // arraySuperType unless all the types are the 3595 // same 3596 Type first = ts[0]; 3597 for (int j = 1 ; j < ts.length ; j++) { 3598 if (!isSameType(first, ts[j])) { 3599 // lub(int[], B[]) is Cloneable & Serializable 3600 return arraySuperType(); 3601 } 3602 } 3603 // all the array types are the same, return one 3604 // lub(int[], int[]) is int[] 3605 return first; 3606 } 3607 } 3608 // lub(A[], B[]) is lub(A, B)[] 3609 return new ArrayType(lub(elements), syms.arrayClass); 3610 3611 case CLASS_BOUND: 3612 // calculate lub(A, B) 3613 int startIdx = 0; 3614 for (int i = 0; i < ts.length ; i++) { 3615 Type t = ts[i]; 3616 if (t.hasTag(CLASS) || t.hasTag(TYPEVAR)) { 3617 break; 3618 } else { 3619 startIdx++; 3620 } 3621 } 3622 Assert.check(startIdx < ts.length); 3623 //step 1 - compute erased candidate set (EC) 3624 List<Type> cl = erasedSupertypes(ts[startIdx]); 3625 for (int i = startIdx + 1 ; i < ts.length ; i++) { 3626 Type t = ts[i]; 3627 if (t.hasTag(CLASS) || t.hasTag(TYPEVAR)) 3628 cl = intersect(cl, erasedSupertypes(t)); 3629 } 3630 //step 2 - compute minimal erased candidate set (MEC) 3631 List<Type> mec = closureMin(cl); 3632 //step 3 - for each element G in MEC, compute lci(Inv(G)) 3633 List<Type> candidates = List.nil(); 3634 for (Type erasedSupertype : mec) { 3635 List<Type> lci = List.of(asSuper(ts[startIdx], erasedSupertype.tsym)); 3636 for (int i = startIdx + 1 ; i < ts.length ; i++) { 3637 lci = intersect(lci, List.of(asSuper(ts[i], erasedSupertype.tsym))); 3638 } 3639 candidates = candidates.appendList(lci); 3640 } 3641 //step 4 - let MEC be { G1, G2 ... Gn }, then we have that 3642 //lub = lci(Inv(G1)) & lci(Inv(G2)) & ... & lci(Inv(Gn)) 3643 return compoundMin(candidates); 3644 3645 default: 3646 // calculate lub(A, B[]) 3647 List<Type> classes = List.of(arraySuperType()); 3648 for (int i = 0 ; i < ts.length ; i++) { 3649 if (kinds[i] != ARRAY_BOUND) // Filter out any arrays 3650 classes = classes.prepend(ts[i]); 3651 } 3652 // lub(A, B[]) is lub(A, arraySuperType) 3653 return lub(classes); 3654 } 3655 } 3656 // where 3657 List<Type> erasedSupertypes(Type t) { 3658 ListBuffer<Type> buf = new ListBuffer<>(); 3659 for (Type sup : closure(t)) { 3660 if (sup.hasTag(TYPEVAR)) { 3661 buf.append(sup); 3662 } else { 3663 buf.append(erasure(sup)); 3664 } 3665 } 3666 return buf.toList(); 3667 } 3668 3669 private Type arraySuperType = null; 3670 private Type arraySuperType() { 3671 // initialized lazily to avoid problems during compiler startup 3672 if (arraySuperType == null) { 3673 synchronized (this) { 3674 if (arraySuperType == null) { 3675 // JLS 10.8: all arrays implement Cloneable and Serializable. 3676 arraySuperType = makeCompoundType(List.of(syms.serializableType, 3677 syms.cloneableType), true); 3678 } 3679 } 3680 } 3681 return arraySuperType; 3682 } 3683 // </editor-fold> 3684 3685 // <editor-fold defaultstate="collapsed" desc="Greatest lower bound"> 3686 public Type glb(List<Type> ts) { 3687 Type t1 = ts.head; 3688 for (Type t2 : ts.tail) { 3689 if (t1.isErroneous()) 3690 return t1; 3691 t1 = glb(t1, t2); 3692 } 3693 return t1; 3694 } 3695 //where 3696 public Type glb(Type t, Type s) { 3697 if (s == null) 3698 return t; 3699 else if (t.isPrimitive() || s.isPrimitive()) 3700 return syms.errType; 3701 else if (isSubtypeNoCapture(t, s)) 3702 return t; 3703 else if (isSubtypeNoCapture(s, t)) 3704 return s; 3705 3706 List<Type> closure = union(closure(t), closure(s)); 3707 return glbFlattened(closure, t); 3708 } 3709 //where 3710 /** 3711 * Perform glb for a list of non-primitive, non-error, non-compound types; 3712 * redundant elements are removed. Bounds should be ordered according to 3713 * {@link Symbol#precedes(TypeSymbol,Types)}. 3714 * 3715 * @param flatBounds List of type to glb 3716 * @param errT Original type to use if the result is an error type 3717 */ 3718 private Type glbFlattened(List<Type> flatBounds, Type errT) { 3719 List<Type> bounds = closureMin(flatBounds); 3720 3721 if (bounds.isEmpty()) { // length == 0 3722 return syms.objectType; 3723 } else if (bounds.tail.isEmpty()) { // length == 1 3724 return bounds.head; 3725 } else { // length > 1 3726 int classCount = 0; 3727 List<Type> lowers = List.nil(); 3728 for (Type bound : bounds) { 3729 if (!bound.isInterface()) { 3730 classCount++; 3731 Type lower = cvarLowerBound(bound); 3732 if (bound != lower && !lower.hasTag(BOT)) 3733 lowers = insert(lowers, lower); 3734 } 3735 } 3736 if (classCount > 1) { 3737 if (lowers.isEmpty()) 3738 return createErrorType(errT); 3739 else 3740 return glbFlattened(union(bounds, lowers), errT); 3741 } 3742 } 3743 return makeCompoundType(bounds); 3744 } 3745 // </editor-fold> 3746 3747 // <editor-fold defaultstate="collapsed" desc="hashCode"> 3748 /** 3749 * Compute a hash code on a type. 3750 */ 3751 public int hashCode(Type t) { 3752 return hashCode.visit(t); 3753 } 3754 // where 3755 private static final UnaryVisitor<Integer> hashCode = new UnaryVisitor<Integer>() { 3756 3757 public Integer visitType(Type t, Void ignored) { 3758 return t.getTag().ordinal(); 3759 } 3760 3761 @Override 3762 public Integer visitClassType(ClassType t, Void ignored) { 3763 int result = visit(t.getEnclosingType()); 3764 result *= 127; 3765 result += t.tsym.flatName().hashCode(); 3766 for (Type s : t.getTypeArguments()) { 3767 result *= 127; 3768 result += visit(s); 3769 } 3770 return result; 3771 } 3772 3773 @Override 3774 public Integer visitMethodType(MethodType t, Void ignored) { 3775 int h = METHOD.ordinal(); 3776 for (List<Type> thisargs = t.argtypes; 3777 thisargs.tail != null; 3778 thisargs = thisargs.tail) 3779 h = (h << 5) + visit(thisargs.head); 3780 return (h << 5) + visit(t.restype); 3781 } 3782 3783 @Override 3784 public Integer visitWildcardType(WildcardType t, Void ignored) { 3785 int result = t.kind.hashCode(); 3786 if (t.type != null) { 3787 result *= 127; 3788 result += visit(t.type); 3789 } 3790 return result; 3791 } 3792 3793 @Override 3794 public Integer visitArrayType(ArrayType t, Void ignored) { 3795 return visit(t.elemtype) + 12; 3796 } 3797 3798 @Override 3799 public Integer visitTypeVar(TypeVar t, Void ignored) { 3800 return System.identityHashCode(t.tsym); 3801 } 3802 3803 @Override 3804 public Integer visitUndetVar(UndetVar t, Void ignored) { 3805 return System.identityHashCode(t); 3806 } 3807 3808 @Override 3809 public Integer visitErrorType(ErrorType t, Void ignored) { 3810 return 0; 3811 } 3812 }; 3813 // </editor-fold> 3814 3815 // <editor-fold defaultstate="collapsed" desc="Return-Type-Substitutable"> 3816 /** 3817 * Does t have a result that is a subtype of the result type of s, 3818 * suitable for covariant returns? It is assumed that both types 3819 * are (possibly polymorphic) method types. Monomorphic method 3820 * types are handled in the obvious way. Polymorphic method types 3821 * require renaming all type variables of one to corresponding 3822 * type variables in the other, where correspondence is by 3823 * position in the type parameter list. */ 3824 public boolean resultSubtype(Type t, Type s, Warner warner) { 3825 List<Type> tvars = t.getTypeArguments(); 3826 List<Type> svars = s.getTypeArguments(); 3827 Type tres = t.getReturnType(); 3828 Type sres = subst(s.getReturnType(), svars, tvars); 3829 return covariantReturnType(tres, sres, warner); 3830 } 3831 3832 /** 3833 * Return-Type-Substitutable. 3834 * @jls section 8.4.5 3835 */ 3836 public boolean returnTypeSubstitutable(Type r1, Type r2) { 3837 if (hasSameArgs(r1, r2)) 3838 return resultSubtype(r1, r2, noWarnings); 3839 else 3840 return covariantReturnType(r1.getReturnType(), 3841 erasure(r2.getReturnType()), 3842 noWarnings); 3843 } 3844 3845 public boolean returnTypeSubstitutable(Type r1, 3846 Type r2, Type r2res, 3847 Warner warner) { 3848 if (isSameType(r1.getReturnType(), r2res)) 3849 return true; 3850 if (r1.getReturnType().isPrimitive() || r2res.isPrimitive()) 3851 return false; 3852 3853 if (hasSameArgs(r1, r2)) 3854 return covariantReturnType(r1.getReturnType(), r2res, warner); 3855 if (isSubtypeUnchecked(r1.getReturnType(), r2res, warner)) 3856 return true; 3857 if (!isSubtype(r1.getReturnType(), erasure(r2res))) 3858 return false; 3859 warner.warn(LintCategory.UNCHECKED); 3860 return true; 3861 } 3862 3863 /** 3864 * Is t an appropriate return type in an overrider for a 3865 * method that returns s? 3866 */ 3867 public boolean covariantReturnType(Type t, Type s, Warner warner) { 3868 return 3869 isSameType(t, s) || 3870 !t.isPrimitive() && 3871 !s.isPrimitive() && 3872 isAssignable(t, s, warner); 3873 } 3874 // </editor-fold> 3875 3876 // <editor-fold defaultstate="collapsed" desc="Box/unbox support"> 3877 /** 3878 * Return the class that boxes the given primitive. 3879 */ 3880 public ClassSymbol boxedClass(Type t) { 3881 return syms.enterClass(syms.boxedName[t.getTag().ordinal()]); 3882 } 3883 3884 /** 3885 * Return the boxed type if 't' is primitive, otherwise return 't' itself. 3886 */ 3887 public Type boxedTypeOrType(Type t) { 3888 return t.isPrimitive() ? 3889 boxedClass(t).type : 3890 t; 3891 } 3892 3893 /** 3894 * Return the primitive type corresponding to a boxed type. 3895 */ 3896 public Type unboxedType(Type t) { 3897 for (int i=0; i<syms.boxedName.length; i++) { 3898 Name box = syms.boxedName[i]; 3899 if (box != null && 3900 asSuper(t, syms.enterClass(box)) != null) 3901 return syms.typeOfTag[i]; 3902 } 3903 return Type.noType; 3904 } 3905 3906 /** 3907 * Return the unboxed type if 't' is a boxed class, otherwise return 't' itself. 3908 */ 3909 public Type unboxedTypeOrType(Type t) { 3910 Type unboxedType = unboxedType(t); 3911 return unboxedType.hasTag(NONE) ? t : unboxedType; 3912 } 3913 // </editor-fold> 3914 3915 // <editor-fold defaultstate="collapsed" desc="Capture conversion"> 3916 /* 3917 * JLS 5.1.10 Capture Conversion: 3918 * 3919 * Let G name a generic type declaration with n formal type 3920 * parameters A1 ... An with corresponding bounds U1 ... Un. There 3921 * exists a capture conversion from G<T1 ... Tn> to G<S1 ... Sn>, 3922 * where, for 1 <= i <= n: 3923 * 3924 * + If Ti is a wildcard type argument (4.5.1) of the form ? then 3925 * Si is a fresh type variable whose upper bound is 3926 * Ui[A1 := S1, ..., An := Sn] and whose lower bound is the null 3927 * type. 3928 * 3929 * + If Ti is a wildcard type argument of the form ? extends Bi, 3930 * then Si is a fresh type variable whose upper bound is 3931 * glb(Bi, Ui[A1 := S1, ..., An := Sn]) and whose lower bound is 3932 * the null type, where glb(V1,... ,Vm) is V1 & ... & Vm. It is 3933 * a compile-time error if for any two classes (not interfaces) 3934 * Vi and Vj,Vi is not a subclass of Vj or vice versa. 3935 * 3936 * + If Ti is a wildcard type argument of the form ? super Bi, 3937 * then Si is a fresh type variable whose upper bound is 3938 * Ui[A1 := S1, ..., An := Sn] and whose lower bound is Bi. 3939 * 3940 * + Otherwise, Si = Ti. 3941 * 3942 * Capture conversion on any type other than a parameterized type 3943 * (4.5) acts as an identity conversion (5.1.1). Capture 3944 * conversions never require a special action at run time and 3945 * therefore never throw an exception at run time. 3946 * 3947 * Capture conversion is not applied recursively. 3948 */ 3949 /** 3950 * Capture conversion as specified by the JLS. 3951 */ 3952 3953 public List<Type> capture(List<Type> ts) { 3954 List<Type> buf = List.nil(); 3955 for (Type t : ts) { 3956 buf = buf.prepend(capture(t)); 3957 } 3958 return buf.reverse(); 3959 } 3960 3961 public Type capture(Type t) { 3962 if (!t.hasTag(CLASS)) { 3963 return t; 3964 } 3965 if (t.getEnclosingType() != Type.noType) { 3966 Type capturedEncl = capture(t.getEnclosingType()); 3967 if (capturedEncl != t.getEnclosingType()) { 3968 Type type1 = memberType(capturedEncl, t.tsym); 3969 t = subst(type1, t.tsym.type.getTypeArguments(), t.getTypeArguments()); 3970 } 3971 } 3972 ClassType cls = (ClassType)t; 3973 if (cls.isRaw() || !cls.isParameterized()) 3974 return cls; 3975 3976 ClassType G = (ClassType)cls.asElement().asType(); 3977 List<Type> A = G.getTypeArguments(); 3978 List<Type> T = cls.getTypeArguments(); 3979 List<Type> S = freshTypeVariables(T); 3980 3981 List<Type> currentA = A; 3982 List<Type> currentT = T; 3983 List<Type> currentS = S; 3984 boolean captured = false; 3985 while (!currentA.isEmpty() && 3986 !currentT.isEmpty() && 3987 !currentS.isEmpty()) { 3988 if (currentS.head != currentT.head) { 3989 captured = true; 3990 WildcardType Ti = (WildcardType)currentT.head; 3991 Type Ui = currentA.head.getUpperBound(); 3992 CapturedType Si = (CapturedType)currentS.head; 3993 if (Ui == null) 3994 Ui = syms.objectType; 3995 switch (Ti.kind) { 3996 case UNBOUND: 3997 Si.bound = subst(Ui, A, S); 3998 Si.lower = syms.botType; 3999 break; 4000 case EXTENDS: 4001 Si.bound = glb(Ti.getExtendsBound(), subst(Ui, A, S)); 4002 Si.lower = syms.botType; 4003 break; 4004 case SUPER: 4005 Si.bound = subst(Ui, A, S); 4006 Si.lower = Ti.getSuperBound(); 4007 break; 4008 } 4009 Type tmpBound = Si.bound.hasTag(UNDETVAR) ? ((UndetVar)Si.bound).qtype : Si.bound; 4010 Type tmpLower = Si.lower.hasTag(UNDETVAR) ? ((UndetVar)Si.lower).qtype : Si.lower; 4011 if (!Si.bound.hasTag(ERROR) && 4012 !Si.lower.hasTag(ERROR) && 4013 isSameType(tmpBound, tmpLower, false)) { 4014 currentS.head = Si.bound; 4015 } 4016 } 4017 currentA = currentA.tail; 4018 currentT = currentT.tail; 4019 currentS = currentS.tail; 4020 } 4021 if (!currentA.isEmpty() || !currentT.isEmpty() || !currentS.isEmpty()) 4022 return erasure(t); // some "rare" type involved 4023 4024 if (captured) 4025 return new ClassType(cls.getEnclosingType(), S, cls.tsym, 4026 cls.getMetadata()); 4027 else 4028 return t; 4029 } 4030 // where 4031 public List<Type> freshTypeVariables(List<Type> types) { 4032 ListBuffer<Type> result = new ListBuffer<>(); 4033 for (Type t : types) { 4034 if (t.hasTag(WILDCARD)) { 4035 Type bound = ((WildcardType)t).getExtendsBound(); 4036 if (bound == null) 4037 bound = syms.objectType; 4038 result.append(new CapturedType(capturedName, 4039 syms.noSymbol, 4040 bound, 4041 syms.botType, 4042 (WildcardType)t)); 4043 } else { 4044 result.append(t); 4045 } 4046 } 4047 return result.toList(); 4048 } 4049 // </editor-fold> 4050 4051 // <editor-fold defaultstate="collapsed" desc="Internal utility methods"> 4052 private boolean sideCast(Type from, Type to, Warner warn) { 4053 // We are casting from type $from$ to type $to$, which are 4054 // non-final unrelated types. This method 4055 // tries to reject a cast by transferring type parameters 4056 // from $to$ to $from$ by common superinterfaces. 4057 boolean reverse = false; 4058 Type target = to; 4059 if ((to.tsym.flags() & INTERFACE) == 0) { 4060 Assert.check((from.tsym.flags() & INTERFACE) != 0); 4061 reverse = true; 4062 to = from; 4063 from = target; 4064 } 4065 List<Type> commonSupers = superClosure(to, erasure(from)); 4066 boolean giveWarning = commonSupers.isEmpty(); 4067 // The arguments to the supers could be unified here to 4068 // get a more accurate analysis 4069 while (commonSupers.nonEmpty()) { 4070 Type t1 = asSuper(from, commonSupers.head.tsym); 4071 Type t2 = commonSupers.head; // same as asSuper(to, commonSupers.head.tsym); 4072 if (disjointTypes(t1.getTypeArguments(), t2.getTypeArguments())) 4073 return false; 4074 giveWarning = giveWarning || (reverse ? giveWarning(t2, t1) : giveWarning(t1, t2)); 4075 commonSupers = commonSupers.tail; 4076 } 4077 if (giveWarning && !isReifiable(reverse ? from : to)) 4078 warn.warn(LintCategory.UNCHECKED); 4079 return true; 4080 } 4081 4082 private boolean sideCastFinal(Type from, Type to, Warner warn) { 4083 // We are casting from type $from$ to type $to$, which are 4084 // unrelated types one of which is final and the other of 4085 // which is an interface. This method 4086 // tries to reject a cast by transferring type parameters 4087 // from the final class to the interface. 4088 boolean reverse = false; 4089 Type target = to; 4090 if ((to.tsym.flags() & INTERFACE) == 0) { 4091 Assert.check((from.tsym.flags() & INTERFACE) != 0); 4092 reverse = true; 4093 to = from; 4094 from = target; 4095 } 4096 Assert.check((from.tsym.flags() & FINAL) != 0); 4097 Type t1 = asSuper(from, to.tsym); 4098 if (t1 == null) return false; 4099 Type t2 = to; 4100 if (disjointTypes(t1.getTypeArguments(), t2.getTypeArguments())) 4101 return false; 4102 if (!isReifiable(target) && 4103 (reverse ? giveWarning(t2, t1) : giveWarning(t1, t2))) 4104 warn.warn(LintCategory.UNCHECKED); 4105 return true; 4106 } 4107 4108 private boolean giveWarning(Type from, Type to) { 4109 List<Type> bounds = to.isCompound() ? 4110 ((IntersectionClassType)to).getComponents() : List.of(to); 4111 for (Type b : bounds) { 4112 Type subFrom = asSub(from, b.tsym); 4113 if (b.isParameterized() && 4114 (!(isUnbounded(b) || 4115 isSubtype(from, b) || 4116 ((subFrom != null) && containsType(b.allparams(), subFrom.allparams()))))) { 4117 return true; 4118 } 4119 } 4120 return false; 4121 } 4122 4123 private List<Type> superClosure(Type t, Type s) { 4124 List<Type> cl = List.nil(); 4125 for (List<Type> l = interfaces(t); l.nonEmpty(); l = l.tail) { 4126 if (isSubtype(s, erasure(l.head))) { 4127 cl = insert(cl, l.head); 4128 } else { 4129 cl = union(cl, superClosure(l.head, s)); 4130 } 4131 } 4132 return cl; 4133 } 4134 4135 private boolean containsTypeEquivalent(Type t, Type s) { 4136 return 4137 isSameType(t, s) || // shortcut 4138 containsType(t, s) && containsType(s, t); 4139 } 4140 4141 // <editor-fold defaultstate="collapsed" desc="adapt"> 4142 /** 4143 * Adapt a type by computing a substitution which maps a source 4144 * type to a target type. 4145 * 4146 * @param source the source type 4147 * @param target the target type 4148 * @param from the type variables of the computed substitution 4149 * @param to the types of the computed substitution. 4150 */ 4151 public void adapt(Type source, 4152 Type target, 4153 ListBuffer<Type> from, 4154 ListBuffer<Type> to) throws AdaptFailure { 4155 new Adapter(from, to).adapt(source, target); 4156 } 4157 4158 class Adapter extends SimpleVisitor<Void, Type> { 4159 4160 ListBuffer<Type> from; 4161 ListBuffer<Type> to; 4162 Map<Symbol,Type> mapping; 4163 4164 Adapter(ListBuffer<Type> from, ListBuffer<Type> to) { 4165 this.from = from; 4166 this.to = to; 4167 mapping = new HashMap<>(); 4168 } 4169 4170 public void adapt(Type source, Type target) throws AdaptFailure { 4171 visit(source, target); 4172 List<Type> fromList = from.toList(); 4173 List<Type> toList = to.toList(); 4174 while (!fromList.isEmpty()) { 4175 Type val = mapping.get(fromList.head.tsym); 4176 if (toList.head != val) 4177 toList.head = val; 4178 fromList = fromList.tail; 4179 toList = toList.tail; 4180 } 4181 } 4182 4183 @Override 4184 public Void visitClassType(ClassType source, Type target) throws AdaptFailure { 4185 if (target.hasTag(CLASS)) 4186 adaptRecursive(source.allparams(), target.allparams()); 4187 return null; 4188 } 4189 4190 @Override 4191 public Void visitArrayType(ArrayType source, Type target) throws AdaptFailure { 4192 if (target.hasTag(ARRAY)) 4193 adaptRecursive(elemtype(source), elemtype(target)); 4194 return null; 4195 } 4196 4197 @Override 4198 public Void visitWildcardType(WildcardType source, Type target) throws AdaptFailure { 4199 if (source.isExtendsBound()) 4200 adaptRecursive(wildUpperBound(source), wildUpperBound(target)); 4201 else if (source.isSuperBound()) 4202 adaptRecursive(wildLowerBound(source), wildLowerBound(target)); 4203 return null; 4204 } 4205 4206 @Override 4207 public Void visitTypeVar(TypeVar source, Type target) throws AdaptFailure { 4208 // Check to see if there is 4209 // already a mapping for $source$, in which case 4210 // the old mapping will be merged with the new 4211 Type val = mapping.get(source.tsym); 4212 if (val != null) { 4213 if (val.isSuperBound() && target.isSuperBound()) { 4214 val = isSubtype(wildLowerBound(val), wildLowerBound(target)) 4215 ? target : val; 4216 } else if (val.isExtendsBound() && target.isExtendsBound()) { 4217 val = isSubtype(wildUpperBound(val), wildUpperBound(target)) 4218 ? val : target; 4219 } else if (!isSameType(val, target)) { 4220 throw new AdaptFailure(); 4221 } 4222 } else { 4223 val = target; 4224 from.append(source); 4225 to.append(target); 4226 } 4227 mapping.put(source.tsym, val); 4228 return null; 4229 } 4230 4231 @Override 4232 public Void visitType(Type source, Type target) { 4233 return null; 4234 } 4235 4236 private Set<TypePair> cache = new HashSet<>(); 4237 4238 private void adaptRecursive(Type source, Type target) { 4239 TypePair pair = new TypePair(source, target); 4240 if (cache.add(pair)) { 4241 try { 4242 visit(source, target); 4243 } finally { 4244 cache.remove(pair); 4245 } 4246 } 4247 } 4248 4249 private void adaptRecursive(List<Type> source, List<Type> target) { 4250 if (source.length() == target.length()) { 4251 while (source.nonEmpty()) { 4252 adaptRecursive(source.head, target.head); 4253 source = source.tail; 4254 target = target.tail; 4255 } 4256 } 4257 } 4258 } 4259 4260 public static class AdaptFailure extends RuntimeException { 4261 static final long serialVersionUID = -7490231548272701566L; 4262 } 4263 4264 private void adaptSelf(Type t, 4265 ListBuffer<Type> from, 4266 ListBuffer<Type> to) { 4267 try { 4268 //if (t.tsym.type != t) 4269 adapt(t.tsym.type, t, from, to); 4270 } catch (AdaptFailure ex) { 4271 // Adapt should never fail calculating a mapping from 4272 // t.tsym.type to t as there can be no merge problem. 4273 throw new AssertionError(ex); 4274 } 4275 } 4276 // </editor-fold> 4277 4278 /** 4279 * Rewrite all type variables (universal quantifiers) in the given 4280 * type to wildcards (existential quantifiers). This is used to 4281 * determine if a cast is allowed. For example, if high is true 4282 * and {@code T <: Number}, then {@code List<T>} is rewritten to 4283 * {@code List<? extends Number>}. Since {@code List<Integer> <: 4284 * List<? extends Number>} a {@code List<T>} can be cast to {@code 4285 * List<Integer>} with a warning. 4286 * @param t a type 4287 * @param high if true return an upper bound; otherwise a lower 4288 * bound 4289 * @param rewriteTypeVars only rewrite captured wildcards if false; 4290 * otherwise rewrite all type variables 4291 * @return the type rewritten with wildcards (existential 4292 * quantifiers) only 4293 */ 4294 private Type rewriteQuantifiers(Type t, boolean high, boolean rewriteTypeVars) { 4295 return new Rewriter(high, rewriteTypeVars).visit(t); 4296 } 4297 4298 class Rewriter extends UnaryVisitor<Type> { 4299 4300 boolean high; 4301 boolean rewriteTypeVars; 4302 4303 Rewriter(boolean high, boolean rewriteTypeVars) { 4304 this.high = high; 4305 this.rewriteTypeVars = rewriteTypeVars; 4306 } 4307 4308 @Override 4309 public Type visitClassType(ClassType t, Void s) { 4310 ListBuffer<Type> rewritten = new ListBuffer<>(); 4311 boolean changed = false; 4312 for (Type arg : t.allparams()) { 4313 Type bound = visit(arg); 4314 if (arg != bound) { 4315 changed = true; 4316 } 4317 rewritten.append(bound); 4318 } 4319 if (changed) 4320 return subst(t.tsym.type, 4321 t.tsym.type.allparams(), 4322 rewritten.toList()); 4323 else 4324 return t; 4325 } 4326 4327 public Type visitType(Type t, Void s) { 4328 return t; 4329 } 4330 4331 @Override 4332 public Type visitCapturedType(CapturedType t, Void s) { 4333 Type w_bound = t.wildcard.type; 4334 Type bound = w_bound.contains(t) ? 4335 erasure(w_bound) : 4336 visit(w_bound); 4337 return rewriteAsWildcardType(visit(bound), t.wildcard.bound, t.wildcard.kind); 4338 } 4339 4340 @Override 4341 public Type visitTypeVar(TypeVar t, Void s) { 4342 if (rewriteTypeVars) { 4343 Type bound = t.bound.contains(t) ? 4344 erasure(t.bound) : 4345 visit(t.bound); 4346 return rewriteAsWildcardType(bound, t, EXTENDS); 4347 } else { 4348 return t; 4349 } 4350 } 4351 4352 @Override 4353 public Type visitWildcardType(WildcardType t, Void s) { 4354 Type bound2 = visit(t.type); 4355 return t.type == bound2 ? t : rewriteAsWildcardType(bound2, t.bound, t.kind); 4356 } 4357 4358 private Type rewriteAsWildcardType(Type bound, TypeVar formal, BoundKind bk) { 4359 switch (bk) { 4360 case EXTENDS: return high ? 4361 makeExtendsWildcard(B(bound), formal) : 4362 makeExtendsWildcard(syms.objectType, formal); 4363 case SUPER: return high ? 4364 makeSuperWildcard(syms.botType, formal) : 4365 makeSuperWildcard(B(bound), formal); 4366 case UNBOUND: return makeExtendsWildcard(syms.objectType, formal); 4367 default: 4368 Assert.error("Invalid bound kind " + bk); 4369 return null; 4370 } 4371 } 4372 4373 Type B(Type t) { 4374 while (t.hasTag(WILDCARD)) { 4375 WildcardType w = (WildcardType)t; 4376 t = high ? 4377 w.getExtendsBound() : 4378 w.getSuperBound(); 4379 if (t == null) { 4380 t = high ? syms.objectType : syms.botType; 4381 } 4382 } 4383 return t; 4384 } 4385 } 4386 4387 4388 /** 4389 * Create a wildcard with the given upper (extends) bound; create 4390 * an unbounded wildcard if bound is Object. 4391 * 4392 * @param bound the upper bound 4393 * @param formal the formal type parameter that will be 4394 * substituted by the wildcard 4395 */ 4396 private WildcardType makeExtendsWildcard(Type bound, TypeVar formal) { 4397 if (bound == syms.objectType) { 4398 return new WildcardType(syms.objectType, 4399 BoundKind.UNBOUND, 4400 syms.boundClass, 4401 formal); 4402 } else { 4403 return new WildcardType(bound, 4404 BoundKind.EXTENDS, 4405 syms.boundClass, 4406 formal); 4407 } 4408 } 4409 4410 /** 4411 * Create a wildcard with the given lower (super) bound; create an 4412 * unbounded wildcard if bound is bottom (type of {@code null}). 4413 * 4414 * @param bound the lower bound 4415 * @param formal the formal type parameter that will be 4416 * substituted by the wildcard 4417 */ 4418 private WildcardType makeSuperWildcard(Type bound, TypeVar formal) { 4419 if (bound.hasTag(BOT)) { 4420 return new WildcardType(syms.objectType, 4421 BoundKind.UNBOUND, 4422 syms.boundClass, 4423 formal); 4424 } else { 4425 return new WildcardType(bound, 4426 BoundKind.SUPER, 4427 syms.boundClass, 4428 formal); 4429 } 4430 } 4431 4432 /** 4433 * A wrapper for a type that allows use in sets. 4434 */ 4435 public static class UniqueType { 4436 public final Type type; 4437 final Types types; 4438 4439 public UniqueType(Type type, Types types) { 4440 this.type = type; 4441 this.types = types; 4442 } 4443 4444 public int hashCode() { 4445 return types.hashCode(type); 4446 } 4447 4448 public boolean equals(Object obj) { 4449 return (obj instanceof UniqueType) && 4450 types.isSameType(type, ((UniqueType)obj).type); 4451 } 4452 4453 public String toString() { 4454 return type.toString(); 4455 } 4456 4457 } 4458 // </editor-fold> 4459 4460 // <editor-fold defaultstate="collapsed" desc="Visitors"> 4461 /** 4462 * A default visitor for types. All visitor methods except 4463 * visitType are implemented by delegating to visitType. Concrete 4464 * subclasses must provide an implementation of visitType and can 4465 * override other methods as needed. 4466 * 4467 * @param <R> the return type of the operation implemented by this 4468 * visitor; use Void if no return type is needed. 4469 * @param <S> the type of the second argument (the first being the 4470 * type itself) of the operation implemented by this visitor; use 4471 * Void if a second argument is not needed. 4472 */ 4473 public static abstract class DefaultTypeVisitor<R,S> implements Type.Visitor<R,S> { 4474 final public R visit(Type t, S s) { return t.accept(this, s); } 4475 public R visitClassType(ClassType t, S s) { return visitType(t, s); } 4476 public R visitWildcardType(WildcardType t, S s) { return visitType(t, s); } 4477 public R visitArrayType(ArrayType t, S s) { return visitType(t, s); } 4478 public R visitMethodType(MethodType t, S s) { return visitType(t, s); } 4479 public R visitPackageType(PackageType t, S s) { return visitType(t, s); } 4480 public R visitTypeVar(TypeVar t, S s) { return visitType(t, s); } 4481 public R visitCapturedType(CapturedType t, S s) { return visitType(t, s); } 4482 public R visitForAll(ForAll t, S s) { return visitType(t, s); } 4483 public R visitUndetVar(UndetVar t, S s) { return visitType(t, s); } 4484 public R visitErrorType(ErrorType t, S s) { return visitType(t, s); } 4485 } 4486 4487 /** 4488 * A default visitor for symbols. All visitor methods except 4489 * visitSymbol are implemented by delegating to visitSymbol. Concrete 4490 * subclasses must provide an implementation of visitSymbol and can 4491 * override other methods as needed. 4492 * 4493 * @param <R> the return type of the operation implemented by this 4494 * visitor; use Void if no return type is needed. 4495 * @param <S> the type of the second argument (the first being the 4496 * symbol itself) of the operation implemented by this visitor; use 4497 * Void if a second argument is not needed. 4498 */ 4499 public static abstract class DefaultSymbolVisitor<R,S> implements Symbol.Visitor<R,S> { 4500 final public R visit(Symbol s, S arg) { return s.accept(this, arg); } 4501 public R visitClassSymbol(ClassSymbol s, S arg) { return visitSymbol(s, arg); } 4502 public R visitMethodSymbol(MethodSymbol s, S arg) { return visitSymbol(s, arg); } 4503 public R visitOperatorSymbol(OperatorSymbol s, S arg) { return visitSymbol(s, arg); } 4504 public R visitPackageSymbol(PackageSymbol s, S arg) { return visitSymbol(s, arg); } 4505 public R visitTypeSymbol(TypeSymbol s, S arg) { return visitSymbol(s, arg); } 4506 public R visitVarSymbol(VarSymbol s, S arg) { return visitSymbol(s, arg); } 4507 } 4508 4509 /** 4510 * A <em>simple</em> visitor for types. This visitor is simple as 4511 * captured wildcards, for-all types (generic methods), and 4512 * undetermined type variables (part of inference) are hidden. 4513 * Captured wildcards are hidden by treating them as type 4514 * variables and the rest are hidden by visiting their qtypes. 4515 * 4516 * @param <R> the return type of the operation implemented by this 4517 * visitor; use Void if no return type is needed. 4518 * @param <S> the type of the second argument (the first being the 4519 * type itself) of the operation implemented by this visitor; use 4520 * Void if a second argument is not needed. 4521 */ 4522 public static abstract class SimpleVisitor<R,S> extends DefaultTypeVisitor<R,S> { 4523 @Override 4524 public R visitCapturedType(CapturedType t, S s) { 4525 return visitTypeVar(t, s); 4526 } 4527 @Override 4528 public R visitForAll(ForAll t, S s) { 4529 return visit(t.qtype, s); 4530 } 4531 @Override 4532 public R visitUndetVar(UndetVar t, S s) { 4533 return visit(t.qtype, s); 4534 } 4535 } 4536 4537 /** 4538 * A plain relation on types. That is a 2-ary function on the 4539 * form Type × Type → Boolean. 4540 * <!-- In plain text: Type x Type -> Boolean --> 4541 */ 4542 public static abstract class TypeRelation extends SimpleVisitor<Boolean,Type> {} 4543 4544 /** 4545 * A convenience visitor for implementing operations that only 4546 * require one argument (the type itself), that is, unary 4547 * operations. 4548 * 4549 * @param <R> the return type of the operation implemented by this 4550 * visitor; use Void if no return type is needed. 4551 */ 4552 public static abstract class UnaryVisitor<R> extends SimpleVisitor<R,Void> { 4553 final public R visit(Type t) { return t.accept(this, null); } 4554 } 4555 4556 /** 4557 * A visitor for implementing a mapping from types to types. The 4558 * default behavior of this class is to implement the identity 4559 * mapping (mapping a type to itself). This can be overridden in 4560 * subclasses. 4561 * 4562 * @param <S> the type of the second argument (the first being the 4563 * type itself) of this mapping; use Void if a second argument is 4564 * not needed. 4565 */ 4566 public static class MapVisitor<S> extends DefaultTypeVisitor<Type,S> { 4567 final public Type visit(Type t) { return t.accept(this, null); } 4568 public Type visitType(Type t, S s) { return t; } 4569 } 4570 // </editor-fold> 4571 4572 4573 // <editor-fold defaultstate="collapsed" desc="Annotation support"> 4574 4575 public RetentionPolicy getRetention(Attribute.Compound a) { 4576 return getRetention(a.type.tsym); 4577 } 4578 4579 public RetentionPolicy getRetention(Symbol sym) { 4580 RetentionPolicy vis = RetentionPolicy.CLASS; // the default 4581 Attribute.Compound c = sym.attribute(syms.retentionType.tsym); 4582 if (c != null) { 4583 Attribute value = c.member(names.value); 4584 if (value != null && value instanceof Attribute.Enum) { 4585 Name levelName = ((Attribute.Enum)value).value.name; 4586 if (levelName == names.SOURCE) vis = RetentionPolicy.SOURCE; 4587 else if (levelName == names.CLASS) vis = RetentionPolicy.CLASS; 4588 else if (levelName == names.RUNTIME) vis = RetentionPolicy.RUNTIME; 4589 else ;// /* fail soft */ throw new AssertionError(levelName); 4590 } 4591 } 4592 return vis; 4593 } 4594 // </editor-fold> 4595 4596 // <editor-fold defaultstate="collapsed" desc="Signature Generation"> 4597 4598 public static abstract class SignatureGenerator { 4599 4600 private final Types types; 4601 4602 protected abstract void append(char ch); 4603 protected abstract void append(byte[] ba); 4604 protected abstract void append(Name name); 4605 protected void classReference(ClassSymbol c) { /* by default: no-op */ } 4606 4607 protected SignatureGenerator(Types types) { 4608 this.types = types; 4609 } 4610 4611 /** 4612 * Assemble signature of given type in string buffer. 4613 */ 4614 public void assembleSig(Type type) { 4615 switch (type.getTag()) { 4616 case BYTE: 4617 append('B'); 4618 break; 4619 case SHORT: 4620 append('S'); 4621 break; 4622 case CHAR: 4623 append('C'); 4624 break; 4625 case INT: 4626 append('I'); 4627 break; 4628 case LONG: 4629 append('J'); 4630 break; 4631 case FLOAT: 4632 append('F'); 4633 break; 4634 case DOUBLE: 4635 append('D'); 4636 break; 4637 case BOOLEAN: 4638 append('Z'); 4639 break; 4640 case VOID: 4641 append('V'); 4642 break; 4643 case CLASS: 4644 append('L'); 4645 assembleClassSig(type); 4646 append(';'); 4647 break; 4648 case ARRAY: 4649 ArrayType at = (ArrayType) type; 4650 append('['); 4651 assembleSig(at.elemtype); 4652 break; 4653 case METHOD: 4654 MethodType mt = (MethodType) type; 4655 append('('); 4656 assembleSig(mt.argtypes); 4657 append(')'); 4658 assembleSig(mt.restype); 4659 if (hasTypeVar(mt.thrown)) { 4660 for (List<Type> l = mt.thrown; l.nonEmpty(); l = l.tail) { 4661 append('^'); 4662 assembleSig(l.head); 4663 } 4664 } 4665 break; 4666 case WILDCARD: { 4667 Type.WildcardType ta = (Type.WildcardType) type; 4668 switch (ta.kind) { 4669 case SUPER: 4670 append('-'); 4671 assembleSig(ta.type); 4672 break; 4673 case EXTENDS: 4674 append('+'); 4675 assembleSig(ta.type); 4676 break; 4677 case UNBOUND: 4678 append('*'); 4679 break; 4680 default: 4681 throw new AssertionError(ta.kind); 4682 } 4683 break; 4684 } 4685 case TYPEVAR: 4686 append('T'); 4687 append(type.tsym.name); 4688 append(';'); 4689 break; 4690 case FORALL: 4691 Type.ForAll ft = (Type.ForAll) type; 4692 assembleParamsSig(ft.tvars); 4693 assembleSig(ft.qtype); 4694 break; 4695 default: 4696 throw new AssertionError("typeSig " + type.getTag()); 4697 } 4698 } 4699 4700 public boolean hasTypeVar(List<Type> l) { 4701 while (l.nonEmpty()) { 4702 if (l.head.hasTag(TypeTag.TYPEVAR)) { 4703 return true; 4704 } 4705 l = l.tail; 4706 } 4707 return false; 4708 } 4709 4710 public void assembleClassSig(Type type) { 4711 ClassType ct = (ClassType) type; 4712 ClassSymbol c = (ClassSymbol) ct.tsym; 4713 classReference(c); 4714 Type outer = ct.getEnclosingType(); 4715 if (outer.allparams().nonEmpty()) { 4716 boolean rawOuter = 4717 c.owner.kind == MTH || // either a local class 4718 c.name == types.names.empty; // or anonymous 4719 assembleClassSig(rawOuter 4720 ? types.erasure(outer) 4721 : outer); 4722 append(rawOuter ? '$' : '.'); 4723 Assert.check(c.flatname.startsWith(c.owner.enclClass().flatname)); 4724 append(rawOuter 4725 ? c.flatname.subName(c.owner.enclClass().flatname.getByteLength() + 1, c.flatname.getByteLength()) 4726 : c.name); 4727 } else { 4728 append(externalize(c.flatname)); 4729 } 4730 if (ct.getTypeArguments().nonEmpty()) { 4731 append('<'); 4732 assembleSig(ct.getTypeArguments()); 4733 append('>'); 4734 } 4735 } 4736 4737 public void assembleParamsSig(List<Type> typarams) { 4738 append('<'); 4739 for (List<Type> ts = typarams; ts.nonEmpty(); ts = ts.tail) { 4740 Type.TypeVar tvar = (Type.TypeVar) ts.head; 4741 append(tvar.tsym.name); 4742 List<Type> bounds = types.getBounds(tvar); 4743 if ((bounds.head.tsym.flags() & INTERFACE) != 0) { 4744 append(':'); 4745 } 4746 for (List<Type> l = bounds; l.nonEmpty(); l = l.tail) { 4747 append(':'); 4748 assembleSig(l.head); 4749 } 4750 } 4751 append('>'); 4752 } 4753 4754 private void assembleSig(List<Type> types) { 4755 for (List<Type> ts = types; ts.nonEmpty(); ts = ts.tail) { 4756 assembleSig(ts.head); 4757 } 4758 } 4759 } 4760 // </editor-fold> 4761 4762 public void newRound() { 4763 descCache._map.clear(); 4764 isDerivedRawCache.clear(); 4765 implCache._map.clear(); 4766 membersCache._map.clear(); 4767 closureCache.clear(); 4768 } 4769} 4770