1 /* 2 * Copyright (c) 1999, 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 26 package com.sun.tools.javac.code; 27 28 import java.lang.annotation.Annotation; 29 import java.util.Collections; 30 import java.util.EnumMap; 31 import java.util.EnumSet; 32 import java.util.Map; 33 import java.util.Set; 34 35 import javax.lang.model.type.*; 36 37 import com.sun.tools.javac.code.Symbol.*; 38 import com.sun.tools.javac.util.*; 39 import static com.sun.tools.javac.code.BoundKind.*; 40 import static com.sun.tools.javac.code.Flags.*; 41 import static com.sun.tools.javac.code.Kinds.*; 42 import static com.sun.tools.javac.code.TypeTag.*; 43 44 /** This class represents Java types. The class itself defines the behavior of 45 * the following types: 46 * <pre> 47 * base types (tags: BYTE, CHAR, SHORT, INT, LONG, FLOAT, DOUBLE, BOOLEAN), 48 * type `void' (tag: VOID), 49 * the bottom type (tag: BOT), 50 * the missing type (tag: NONE). 51 * </pre> 52 * <p>The behavior of the following types is defined in subclasses, which are 53 * all static inner classes of this class: 54 * <pre> 55 * class types (tag: CLASS, class: ClassType), 56 * array types (tag: ARRAY, class: ArrayType), 57 * method types (tag: METHOD, class: MethodType), 58 * package types (tag: PACKAGE, class: PackageType), 59 * type variables (tag: TYPEVAR, class: TypeVar), 60 * type arguments (tag: WILDCARD, class: WildcardType), 61 * generic method types (tag: FORALL, class: ForAll), 62 * the error type (tag: ERROR, class: ErrorType). 63 * </pre> 64 * 65 * <p><b>This is NOT part of any supported API. 66 * If you write code that depends on this, you do so at your own risk. 67 * This code and its internal interfaces are subject to change or 68 * deletion without notice.</b> 69 * 70 * @see TypeTag 71 */ 72 public abstract class Type extends AnnoConstruct implements TypeMirror { 73 74 /** Constant type: no type at all. */ 75 public static final JCNoType noType = new JCNoType() { 76 @Override 77 public String toString() { 78 return "none"; 79 } 80 }; 81 82 /** Constant type: special type to be used during recovery of deferred expressions. */ 83 public static final JCNoType recoveryType = new JCNoType(){ 84 @Override 85 public String toString() { 86 return "recovery"; 87 } 88 }; 89 90 /** Constant type: special type to be used for marking stuck trees. */ 91 public static final JCNoType stuckType = new JCNoType() { 92 @Override 93 public String toString() { 94 return "stuck"; 95 } 96 }; 97 98 /** If this switch is turned on, the names of type variables 99 * and anonymous classes are printed with hashcodes appended. 100 */ 101 public static boolean moreInfo = false; 102 103 /** The defining class / interface / package / type variable. 104 */ 105 public TypeSymbol tsym; 106 107 /** 108 * Checks if the current type tag is equal to the given tag. 109 * @return true if tag is equal to the current type tag. 110 */ hasTag(TypeTag tag)111 public boolean hasTag(TypeTag tag) { 112 return tag == getTag(); 113 } 114 115 /** 116 * Returns the current type tag. 117 * @return the value of the current type tag. 118 */ getTag()119 public abstract TypeTag getTag(); 120 isNumeric()121 public boolean isNumeric() { 122 return false; 123 } 124 isPrimitive()125 public boolean isPrimitive() { 126 return false; 127 } 128 isPrimitiveOrVoid()129 public boolean isPrimitiveOrVoid() { 130 return false; 131 } 132 isReference()133 public boolean isReference() { 134 return false; 135 } 136 isNullOrReference()137 public boolean isNullOrReference() { 138 return false; 139 } 140 isPartial()141 public boolean isPartial() { 142 return false; 143 } 144 145 /** 146 * The constant value of this type, null if this type does not 147 * have a constant value attribute. Only primitive types and 148 * strings (ClassType) can have a constant value attribute. 149 * @return the constant value attribute of this type 150 */ constValue()151 public Object constValue() { 152 return null; 153 } 154 155 /** Is this a constant type whose value is false? 156 */ isFalse()157 public boolean isFalse() { 158 return false; 159 } 160 161 /** Is this a constant type whose value is true? 162 */ isTrue()163 public boolean isTrue() { 164 return false; 165 } 166 167 /** 168 * Get the representation of this type used for modelling purposes. 169 * By default, this is itself. For ErrorType, a different value 170 * may be provided. 171 */ getModelType()172 public Type getModelType() { 173 return this; 174 } 175 getModelTypes(List<Type> ts)176 public static List<Type> getModelTypes(List<Type> ts) { 177 ListBuffer<Type> lb = new ListBuffer<>(); 178 for (Type t: ts) 179 lb.append(t.getModelType()); 180 return lb.toList(); 181 } 182 183 /**For ErrorType, returns the original type, otherwise returns the type itself. 184 */ getOriginalType()185 public Type getOriginalType() { 186 return this; 187 } 188 accept(Type.Visitor<R,S> v, S s)189 public <R,S> R accept(Type.Visitor<R,S> v, S s) { return v.visitType(this, s); } 190 191 /** Define a type given its tag and type symbol 192 */ Type(TypeSymbol tsym)193 public Type(TypeSymbol tsym) { 194 this.tsym = tsym; 195 } 196 197 /** An abstract class for mappings from types to types 198 */ 199 public static abstract class Mapping { 200 private String name; Mapping(String name)201 public Mapping(String name) { 202 this.name = name; 203 } apply(Type t)204 public abstract Type apply(Type t); toString()205 public String toString() { 206 return name; 207 } 208 } 209 210 /** map a type function over all immediate descendants of this type 211 */ map(Mapping f)212 public Type map(Mapping f) { 213 return this; 214 } 215 216 /** map a type function over a list of types 217 */ map(List<Type> ts, Mapping f)218 public static List<Type> map(List<Type> ts, Mapping f) { 219 if (ts.nonEmpty()) { 220 List<Type> tail1 = map(ts.tail, f); 221 Type t = f.apply(ts.head); 222 if (tail1 != ts.tail || t != ts.head) 223 return tail1.prepend(t); 224 } 225 return ts; 226 } 227 228 /** Define a constant type, of the same kind as this type 229 * and with given constant value 230 */ constType(Object constValue)231 public Type constType(Object constValue) { 232 throw new AssertionError(); 233 } 234 235 /** 236 * If this is a constant type, return its underlying type. 237 * Otherwise, return the type itself. 238 */ baseType()239 public Type baseType() { 240 return this; 241 } 242 annotatedType(List<Attribute.TypeCompound> annos)243 public Type annotatedType(List<Attribute.TypeCompound> annos) { 244 return new AnnotatedType(annos, this); 245 } 246 isAnnotated()247 public boolean isAnnotated() { 248 return false; 249 } 250 251 /** 252 * If this is an annotated type, return the underlying type. 253 * Otherwise, return the type itself. 254 */ unannotatedType()255 public Type unannotatedType() { 256 return this; 257 } 258 259 @Override getAnnotationMirrors()260 public List<Attribute.TypeCompound> getAnnotationMirrors() { 261 return List.nil(); 262 } 263 264 265 @Override getAnnotation(Class<A> annotationType)266 public <A extends Annotation> A getAnnotation(Class<A> annotationType) { 267 return null; 268 } 269 270 271 @Override getAnnotationsByType(Class<A> annotationType)272 public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationType) { 273 @SuppressWarnings("unchecked") 274 A[] tmp = (A[]) java.lang.reflect.Array.newInstance(annotationType, 0); 275 return tmp; 276 } 277 278 /** Return the base types of a list of types. 279 */ baseTypes(List<Type> ts)280 public static List<Type> baseTypes(List<Type> ts) { 281 if (ts.nonEmpty()) { 282 Type t = ts.head.baseType(); 283 List<Type> baseTypes = baseTypes(ts.tail); 284 if (t != ts.head || baseTypes != ts.tail) 285 return baseTypes.prepend(t); 286 } 287 return ts; 288 } 289 290 /** The Java source which this type represents. 291 */ toString()292 public String toString() { 293 String s = (tsym == null || tsym.name == null) 294 ? "<none>" 295 : tsym.name.toString(); 296 if (moreInfo && hasTag(TYPEVAR)) { 297 s = s + hashCode(); 298 } 299 return s; 300 } 301 302 /** 303 * The Java source which this type list represents. A List is 304 * represented as a comma-spearated listing of the elements in 305 * that list. 306 */ toString(List<Type> ts)307 public static String toString(List<Type> ts) { 308 if (ts.isEmpty()) { 309 return ""; 310 } else { 311 StringBuilder buf = new StringBuilder(); 312 buf.append(ts.head.toString()); 313 for (List<Type> l = ts.tail; l.nonEmpty(); l = l.tail) 314 buf.append(",").append(l.head.toString()); 315 return buf.toString(); 316 } 317 } 318 319 /** 320 * The constant value of this type, converted to String 321 */ stringValue()322 public String stringValue() { 323 Object cv = Assert.checkNonNull(constValue()); 324 return cv.toString(); 325 } 326 327 /** 328 * This method is analogous to isSameType, but weaker, since we 329 * never complete classes. Where isSameType would complete a 330 * class, equals assumes that the two types are different. 331 */ 332 @Override equals(Object t)333 public boolean equals(Object t) { 334 return super.equals(t); 335 } 336 337 @Override hashCode()338 public int hashCode() { 339 return super.hashCode(); 340 } 341 argtypes(boolean varargs)342 public String argtypes(boolean varargs) { 343 List<Type> args = getParameterTypes(); 344 if (!varargs) return args.toString(); 345 StringBuilder buf = new StringBuilder(); 346 while (args.tail.nonEmpty()) { 347 buf.append(args.head); 348 args = args.tail; 349 buf.append(','); 350 } 351 if (args.head.unannotatedType().hasTag(ARRAY)) { 352 buf.append(((ArrayType)args.head.unannotatedType()).elemtype); 353 if (args.head.getAnnotationMirrors().nonEmpty()) { 354 buf.append(args.head.getAnnotationMirrors()); 355 } 356 buf.append("..."); 357 } else { 358 buf.append(args.head); 359 } 360 return buf.toString(); 361 } 362 363 /** Access methods. 364 */ getTypeArguments()365 public List<Type> getTypeArguments() { return List.nil(); } getEnclosingType()366 public Type getEnclosingType() { return null; } getParameterTypes()367 public List<Type> getParameterTypes() { return List.nil(); } getReturnType()368 public Type getReturnType() { return null; } getReceiverType()369 public Type getReceiverType() { return null; } getThrownTypes()370 public List<Type> getThrownTypes() { return List.nil(); } getUpperBound()371 public Type getUpperBound() { return null; } getLowerBound()372 public Type getLowerBound() { return null; } 373 374 /** Navigation methods, these will work for classes, type variables, 375 * foralls, but will return null for arrays and methods. 376 */ 377 378 /** Return all parameters of this type and all its outer types in order 379 * outer (first) to inner (last). 380 */ allparams()381 public List<Type> allparams() { return List.nil(); } 382 383 /** Does this type contain "error" elements? 384 */ isErroneous()385 public boolean isErroneous() { 386 return false; 387 } 388 isErroneous(List<Type> ts)389 public static boolean isErroneous(List<Type> ts) { 390 for (List<Type> l = ts; l.nonEmpty(); l = l.tail) 391 if (l.head.isErroneous()) return true; 392 return false; 393 } 394 395 /** Is this type parameterized? 396 * A class type is parameterized if it has some parameters. 397 * An array type is parameterized if its element type is parameterized. 398 * All other types are not parameterized. 399 */ isParameterized()400 public boolean isParameterized() { 401 return false; 402 } 403 404 /** Is this type a raw type? 405 * A class type is a raw type if it misses some of its parameters. 406 * An array type is a raw type if its element type is raw. 407 * All other types are not raw. 408 * Type validation will ensure that the only raw types 409 * in a program are types that miss all their type variables. 410 */ isRaw()411 public boolean isRaw() { 412 return false; 413 } 414 isCompound()415 public boolean isCompound() { 416 return tsym.completer == null 417 // Compound types can't have a completer. Calling 418 // flags() will complete the symbol causing the 419 // compiler to load classes unnecessarily. This led 420 // to regression 6180021. 421 && (tsym.flags() & COMPOUND) != 0; 422 } 423 isIntersection()424 public boolean isIntersection() { 425 return false; 426 } 427 isUnion()428 public boolean isUnion() { 429 return false; 430 } 431 isInterface()432 public boolean isInterface() { 433 return (tsym.flags() & INTERFACE) != 0; 434 } 435 isFinal()436 public boolean isFinal() { 437 return (tsym.flags() & FINAL) != 0; 438 } 439 440 /** 441 * Does this type contain occurrences of type t? 442 */ contains(Type t)443 public boolean contains(Type t) { 444 return t == this; 445 } 446 contains(List<Type> ts, Type t)447 public static boolean contains(List<Type> ts, Type t) { 448 for (List<Type> l = ts; 449 l.tail != null /*inlined: l.nonEmpty()*/; 450 l = l.tail) 451 if (l.head.contains(t)) return true; 452 return false; 453 } 454 455 /** Does this type contain an occurrence of some type in 'ts'? 456 */ containsAny(List<Type> ts)457 public boolean containsAny(List<Type> ts) { 458 for (Type t : ts) 459 if (this.contains(t)) return true; 460 return false; 461 } 462 containsAny(List<Type> ts1, List<Type> ts2)463 public static boolean containsAny(List<Type> ts1, List<Type> ts2) { 464 for (Type t : ts1) 465 if (t.containsAny(ts2)) return true; 466 return false; 467 } 468 filter(List<Type> ts, Filter<Type> tf)469 public static List<Type> filter(List<Type> ts, Filter<Type> tf) { 470 ListBuffer<Type> buf = new ListBuffer<>(); 471 for (Type t : ts) { 472 if (tf.accepts(t)) { 473 buf.append(t); 474 } 475 } 476 return buf.toList(); 477 } 478 isSuperBound()479 public boolean isSuperBound() { return false; } isExtendsBound()480 public boolean isExtendsBound() { return false; } isUnbound()481 public boolean isUnbound() { return false; } withTypeVar(Type t)482 public Type withTypeVar(Type t) { return this; } 483 484 /** The underlying method type of this type. 485 */ asMethodType()486 public MethodType asMethodType() { throw new AssertionError(); } 487 488 /** Complete loading all classes in this type. 489 */ complete()490 public void complete() {} 491 asElement()492 public TypeSymbol asElement() { 493 return tsym; 494 } 495 496 @Override getKind()497 public TypeKind getKind() { 498 return TypeKind.OTHER; 499 } 500 501 @Override accept(TypeVisitor<R, P> v, P p)502 public <R, P> R accept(TypeVisitor<R, P> v, P p) { 503 throw new AssertionError(); 504 } 505 506 public static class JCPrimitiveType extends Type 507 implements javax.lang.model.type.PrimitiveType { 508 509 TypeTag tag; 510 JCPrimitiveType(TypeTag tag, TypeSymbol tsym)511 public JCPrimitiveType(TypeTag tag, TypeSymbol tsym) { 512 super(tsym); 513 this.tag = tag; 514 Assert.check(tag.isPrimitive); 515 } 516 517 @Override isNumeric()518 public boolean isNumeric() { 519 return tag != BOOLEAN; 520 } 521 522 @Override isPrimitive()523 public boolean isPrimitive() { 524 return true; 525 } 526 527 @Override getTag()528 public TypeTag getTag() { 529 return tag; 530 } 531 532 @Override isPrimitiveOrVoid()533 public boolean isPrimitiveOrVoid() { 534 return true; 535 } 536 537 /** Define a constant type, of the same kind as this type 538 * and with given constant value 539 */ 540 @Override constType(Object constValue)541 public Type constType(Object constValue) { 542 final Object value = constValue; 543 return new JCPrimitiveType(tag, tsym) { 544 @Override 545 public Object constValue() { 546 return value; 547 } 548 @Override 549 public Type baseType() { 550 return tsym.type; 551 } 552 }; 553 } 554 555 /** 556 * The constant value of this type, converted to String 557 */ 558 @Override stringValue()559 public String stringValue() { 560 Object cv = Assert.checkNonNull(constValue()); 561 if (tag == BOOLEAN) { 562 return ((Integer) cv).intValue() == 0 ? "false" : "true"; 563 } 564 else if (tag == CHAR) { 565 return String.valueOf((char) ((Integer) cv).intValue()); 566 } 567 else { 568 return cv.toString(); 569 } 570 } 571 572 /** Is this a constant type whose value is false? 573 */ 574 @Override isFalse()575 public boolean isFalse() { 576 return 577 tag == BOOLEAN && 578 constValue() != null && 579 ((Integer)constValue()).intValue() == 0; 580 } 581 582 /** Is this a constant type whose value is true? 583 */ 584 @Override isTrue()585 public boolean isTrue() { 586 return 587 tag == BOOLEAN && 588 constValue() != null && 589 ((Integer)constValue()).intValue() != 0; 590 } 591 592 @Override accept(TypeVisitor<R, P> v, P p)593 public <R, P> R accept(TypeVisitor<R, P> v, P p) { 594 return v.visitPrimitive(this, p); 595 } 596 597 @Override getKind()598 public TypeKind getKind() { 599 switch (tag) { 600 case BYTE: return TypeKind.BYTE; 601 case CHAR: return TypeKind.CHAR; 602 case SHORT: return TypeKind.SHORT; 603 case INT: return TypeKind.INT; 604 case LONG: return TypeKind.LONG; 605 case FLOAT: return TypeKind.FLOAT; 606 case DOUBLE: return TypeKind.DOUBLE; 607 case BOOLEAN: return TypeKind.BOOLEAN; 608 } 609 throw new AssertionError(); 610 } 611 612 } 613 614 public static class WildcardType extends Type 615 implements javax.lang.model.type.WildcardType { 616 617 public Type type; 618 public BoundKind kind; 619 public TypeVar bound; 620 621 @Override 622 public <R,S> R accept(Type.Visitor<R,S> v, S s) { 623 return v.visitWildcardType(this, s); 624 } 625 626 public WildcardType(Type type, BoundKind kind, TypeSymbol tsym) { 627 super(tsym); 628 this.type = Assert.checkNonNull(type); 629 this.kind = kind; 630 } 631 public WildcardType(WildcardType t, TypeVar bound) { 632 this(t.type, t.kind, t.tsym, bound); 633 } 634 635 public WildcardType(Type type, BoundKind kind, TypeSymbol tsym, TypeVar bound) { 636 this(type, kind, tsym); 637 this.bound = bound; 638 } 639 640 @Override 641 public TypeTag getTag() { 642 return WILDCARD; 643 } 644 645 @Override 646 public boolean contains(Type t) { 647 return kind != UNBOUND && type.contains(t); 648 } 649 650 public boolean isSuperBound() { 651 return kind == SUPER || 652 kind == UNBOUND; 653 } 654 public boolean isExtendsBound() { 655 return kind == EXTENDS || 656 kind == UNBOUND; 657 } 658 public boolean isUnbound() { 659 return kind == UNBOUND; 660 } 661 662 @Override 663 public boolean isReference() { 664 return true; 665 } 666 667 @Override 668 public boolean isNullOrReference() { 669 return true; 670 } 671 672 @Override 673 public Type withTypeVar(Type t) { 674 //-System.err.println(this+".withTypeVar("+t+");");//DEBUG 675 if (bound == t) 676 return this; 677 bound = (TypeVar)t; 678 return this; 679 } 680 681 boolean isPrintingBound = false; 682 public String toString() { 683 StringBuilder s = new StringBuilder(); 684 s.append(kind.toString()); 685 if (kind != UNBOUND) 686 s.append(type); 687 if (moreInfo && bound != null && !isPrintingBound) 688 try { 689 isPrintingBound = true; 690 s.append("{:").append(bound.bound).append(":}"); 691 } finally { 692 isPrintingBound = false; 693 } 694 return s.toString(); 695 } 696 697 public Type map(Mapping f) { 698 //- System.err.println(" (" + this + ").map(" + f + ")");//DEBUG 699 Type t = type; 700 if (t != null) 701 t = f.apply(t); 702 if (t == type) 703 return this; 704 else 705 return new WildcardType(t, kind, tsym, bound); 706 } 707 708 public Type getExtendsBound() { 709 if (kind == EXTENDS) 710 return type; 711 else 712 return null; 713 } 714 715 public Type getSuperBound() { 716 if (kind == SUPER) 717 return type; 718 else 719 return null; 720 } 721 722 public TypeKind getKind() { 723 return TypeKind.WILDCARD; 724 } 725 726 public <R, P> R accept(TypeVisitor<R, P> v, P p) { 727 return v.visitWildcard(this, p); 728 } 729 } 730 731 public static class ClassType extends Type implements DeclaredType { 732 733 /** The enclosing type of this type. If this is the type of an inner 734 * class, outer_field refers to the type of its enclosing 735 * instance class, in all other cases it refers to noType. 736 */ 737 private Type outer_field; 738 739 /** The type parameters of this type (to be set once class is loaded). 740 */ 741 public List<Type> typarams_field; 742 743 /** A cache variable for the type parameters of this type, 744 * appended to all parameters of its enclosing class. 745 * @see #allparams 746 */ 747 public List<Type> allparams_field; 748 749 /** The supertype of this class (to be set once class is loaded). 750 */ 751 public Type supertype_field; 752 753 /** The interfaces of this class (to be set once class is loaded). 754 */ 755 public List<Type> interfaces_field; 756 757 /** All the interfaces of this class, including missing ones. 758 */ 759 public List<Type> all_interfaces_field; 760 761 public ClassType(Type outer, List<Type> typarams, TypeSymbol tsym) { 762 super(tsym); 763 this.outer_field = outer; 764 this.typarams_field = typarams; 765 this.allparams_field = null; 766 this.supertype_field = null; 767 this.interfaces_field = null; 768 /* 769 // this can happen during error recovery 770 assert 771 outer.isParameterized() ? 772 typarams.length() == tsym.type.typarams().length() : 773 outer.isRaw() ? 774 typarams.length() == 0 : 775 true; 776 */ 777 } 778 779 @Override 780 public TypeTag getTag() { 781 return CLASS; 782 } 783 784 @Override 785 public <R,S> R accept(Type.Visitor<R,S> v, S s) { 786 return v.visitClassType(this, s); 787 } 788 789 public Type constType(Object constValue) { 790 final Object value = constValue; 791 return new ClassType(getEnclosingType(), typarams_field, tsym) { 792 @Override 793 public Object constValue() { 794 return value; 795 } 796 @Override 797 public Type baseType() { 798 return tsym.type; 799 } 800 }; 801 } 802 803 /** The Java source which this type represents. 804 */ 805 public String toString() { 806 StringBuilder buf = new StringBuilder(); 807 if (getEnclosingType().hasTag(CLASS) && tsym.owner.kind == TYP) { 808 buf.append(getEnclosingType().toString()); 809 buf.append("."); 810 buf.append(className(tsym, false)); 811 } else { 812 buf.append(className(tsym, true)); 813 } 814 if (getTypeArguments().nonEmpty()) { 815 buf.append('<'); 816 buf.append(getTypeArguments().toString()); 817 buf.append(">"); 818 } 819 return buf.toString(); 820 } 821 //where 822 private String className(Symbol sym, boolean longform) { 823 if (sym.name.isEmpty() && (sym.flags() & COMPOUND) != 0) { 824 StringBuilder s = new StringBuilder(supertype_field.toString()); 825 for (List<Type> is=interfaces_field; is.nonEmpty(); is = is.tail) { 826 s.append("&"); 827 s.append(is.head.toString()); 828 } 829 return s.toString(); 830 } else if (sym.name.isEmpty()) { 831 String s; 832 ClassType norm = (ClassType) tsym.type.unannotatedType(); 833 if (norm == null) { 834 s = Log.getLocalizedString("anonymous.class", (Object)null); 835 } else if (norm.interfaces_field != null && norm.interfaces_field.nonEmpty()) { 836 s = Log.getLocalizedString("anonymous.class", 837 norm.interfaces_field.head); 838 } else { 839 s = Log.getLocalizedString("anonymous.class", 840 norm.supertype_field); 841 } 842 if (moreInfo) 843 s += String.valueOf(sym.hashCode()); 844 return s; 845 } else if (longform) { 846 return sym.getQualifiedName().toString(); 847 } else { 848 return sym.name.toString(); 849 } 850 } 851 852 public List<Type> getTypeArguments() { 853 if (typarams_field == null) { 854 complete(); 855 if (typarams_field == null) 856 typarams_field = List.nil(); 857 } 858 return typarams_field; 859 } 860 861 public boolean hasErasedSupertypes() { 862 return isRaw(); 863 } 864 865 public Type getEnclosingType() { 866 return outer_field; 867 } 868 869 public void setEnclosingType(Type outer) { 870 outer_field = outer; 871 } 872 873 public List<Type> allparams() { 874 if (allparams_field == null) { 875 allparams_field = getTypeArguments().prependList(getEnclosingType().allparams()); 876 } 877 return allparams_field; 878 } 879 880 public boolean isErroneous() { 881 return 882 getEnclosingType().isErroneous() || 883 isErroneous(getTypeArguments()) || 884 this != tsym.type.unannotatedType() && tsym.type.isErroneous(); 885 } 886 887 public boolean isParameterized() { 888 return allparams().tail != null; 889 // optimization, was: allparams().nonEmpty(); 890 } 891 892 @Override 893 public boolean isReference() { 894 return true; 895 } 896 897 @Override 898 public boolean isNullOrReference() { 899 return true; 900 } 901 902 /** A cache for the rank. */ 903 int rank_field = -1; 904 905 /** A class type is raw if it misses some 906 * of its type parameter sections. 907 * After validation, this is equivalent to: 908 * {@code allparams.isEmpty() && tsym.type.allparams.nonEmpty(); } 909 */ 910 public boolean isRaw() { 911 return 912 this != tsym.type && // necessary, but not sufficient condition 913 tsym.type.allparams().nonEmpty() && 914 allparams().isEmpty(); 915 } 916 917 public Type map(Mapping f) { 918 Type outer = getEnclosingType(); 919 Type outer1 = f.apply(outer); 920 List<Type> typarams = getTypeArguments(); 921 List<Type> typarams1 = map(typarams, f); 922 if (outer1 == outer && typarams1 == typarams) return this; 923 else return new ClassType(outer1, typarams1, tsym); 924 } 925 926 public boolean contains(Type elem) { 927 return 928 elem == this 929 || (isParameterized() 930 && (getEnclosingType().contains(elem) || contains(getTypeArguments(), elem))) 931 || (isCompound() 932 && (supertype_field.contains(elem) || contains(interfaces_field, elem))); 933 } 934 935 public void complete() { 936 if (tsym.completer != null) tsym.complete(); 937 } 938 939 public TypeKind getKind() { 940 return TypeKind.DECLARED; 941 } 942 943 public <R, P> R accept(TypeVisitor<R, P> v, P p) { 944 return v.visitDeclared(this, p); 945 } 946 } 947 948 public static class ErasedClassType extends ClassType { 949 public ErasedClassType(Type outer, TypeSymbol tsym) { 950 super(outer, List.<Type>nil(), tsym); 951 } 952 953 @Override 954 public boolean hasErasedSupertypes() { 955 return true; 956 } 957 } 958 959 // a clone of a ClassType that knows about the alternatives of a union type. 960 public static class UnionClassType extends ClassType implements UnionType { 961 final List<? extends Type> alternatives_field; 962 963 public UnionClassType(ClassType ct, List<? extends Type> alternatives) { 964 super(ct.outer_field, ct.typarams_field, ct.tsym); 965 allparams_field = ct.allparams_field; 966 supertype_field = ct.supertype_field; 967 interfaces_field = ct.interfaces_field; 968 all_interfaces_field = ct.interfaces_field; 969 alternatives_field = alternatives; 970 } 971 972 public Type getLub() { 973 return tsym.type; 974 } 975 976 public java.util.List<? extends TypeMirror> getAlternatives() { 977 return Collections.unmodifiableList(alternatives_field); 978 } 979 980 @Override 981 public boolean isUnion() { 982 return true; 983 } 984 985 @Override 986 public TypeKind getKind() { 987 return TypeKind.UNION; 988 } 989 990 @Override 991 public <R, P> R accept(TypeVisitor<R, P> v, P p) { 992 return v.visitUnion(this, p); 993 } 994 } 995 996 // a clone of a ClassType that knows about the bounds of an intersection type. 997 public static class IntersectionClassType extends ClassType implements IntersectionType { 998 999 public boolean allInterfaces; 1000 1001 public IntersectionClassType(List<Type> bounds, ClassSymbol csym, boolean allInterfaces) { 1002 super(Type.noType, List.<Type>nil(), csym); 1003 this.allInterfaces = allInterfaces; 1004 Assert.check((csym.flags() & COMPOUND) != 0); 1005 supertype_field = bounds.head; 1006 interfaces_field = bounds.tail; 1007 Assert.check(supertype_field.tsym.completer != null || 1008 !supertype_field.isInterface(), supertype_field); 1009 } 1010 1011 public java.util.List<? extends TypeMirror> getBounds() { 1012 return Collections.unmodifiableList(getExplicitComponents()); 1013 } 1014 1015 public List<Type> getComponents() { 1016 return interfaces_field.prepend(supertype_field); 1017 } 1018 1019 @Override 1020 public boolean isIntersection() { 1021 return true; 1022 } 1023 1024 public List<Type> getExplicitComponents() { 1025 return allInterfaces ? 1026 interfaces_field : 1027 getComponents(); 1028 } 1029 1030 @Override 1031 public TypeKind getKind() { 1032 return TypeKind.INTERSECTION; 1033 } 1034 1035 @Override 1036 public <R, P> R accept(TypeVisitor<R, P> v, P p) { 1037 return v.visitIntersection(this, p); 1038 } 1039 } 1040 1041 public static class ArrayType extends Type 1042 implements javax.lang.model.type.ArrayType { 1043 1044 public Type elemtype; 1045 1046 public ArrayType(Type elemtype, TypeSymbol arrayClass) { 1047 super(arrayClass); 1048 this.elemtype = elemtype; 1049 } 1050 1051 @Override 1052 public TypeTag getTag() { 1053 return ARRAY; 1054 } 1055 1056 public <R,S> R accept(Type.Visitor<R,S> v, S s) { 1057 return v.visitArrayType(this, s); 1058 } 1059 1060 public String toString() { 1061 return elemtype + "[]"; 1062 } 1063 1064 public boolean equals(Object obj) { 1065 return 1066 this == obj || 1067 (obj instanceof ArrayType && 1068 this.elemtype.equals(((ArrayType)obj).elemtype)); 1069 } 1070 1071 public int hashCode() { 1072 return (ARRAY.ordinal() << 5) + elemtype.hashCode(); 1073 } 1074 1075 public boolean isVarargs() { 1076 return false; 1077 } 1078 1079 public List<Type> allparams() { return elemtype.allparams(); } 1080 1081 public boolean isErroneous() { 1082 return elemtype.isErroneous(); 1083 } 1084 1085 public boolean isParameterized() { 1086 return elemtype.isParameterized(); 1087 } 1088 1089 @Override 1090 public boolean isReference() { 1091 return true; 1092 } 1093 1094 @Override 1095 public boolean isNullOrReference() { 1096 return true; 1097 } 1098 1099 public boolean isRaw() { 1100 return elemtype.isRaw(); 1101 } 1102 1103 public ArrayType makeVarargs() { 1104 return new ArrayType(elemtype, tsym) { 1105 @Override 1106 public boolean isVarargs() { 1107 return true; 1108 } 1109 }; 1110 } 1111 1112 public Type map(Mapping f) { 1113 Type elemtype1 = f.apply(elemtype); 1114 if (elemtype1 == elemtype) return this; 1115 else return new ArrayType(elemtype1, tsym); 1116 } 1117 1118 public boolean contains(Type elem) { 1119 return elem == this || elemtype.contains(elem); 1120 } 1121 1122 public void complete() { 1123 elemtype.complete(); 1124 } 1125 1126 public Type getComponentType() { 1127 return elemtype; 1128 } 1129 1130 public TypeKind getKind() { 1131 return TypeKind.ARRAY; 1132 } 1133 1134 public <R, P> R accept(TypeVisitor<R, P> v, P p) { 1135 return v.visitArray(this, p); 1136 } 1137 } 1138 1139 public static class MethodType extends Type implements ExecutableType { 1140 1141 public List<Type> argtypes; 1142 public Type restype; 1143 public List<Type> thrown; 1144 1145 /** The type annotations on the method receiver. 1146 */ 1147 public Type recvtype; 1148 1149 public MethodType(List<Type> argtypes, 1150 Type restype, 1151 List<Type> thrown, 1152 TypeSymbol methodClass) { 1153 super(methodClass); 1154 this.argtypes = argtypes; 1155 this.restype = restype; 1156 this.thrown = thrown; 1157 } 1158 1159 @Override 1160 public TypeTag getTag() { 1161 return METHOD; 1162 } 1163 1164 public <R,S> R accept(Type.Visitor<R,S> v, S s) { 1165 return v.visitMethodType(this, s); 1166 } 1167 1168 /** The Java source which this type represents. 1169 * 1170 * XXX 06/09/99 iris This isn't correct Java syntax, but it probably 1171 * should be. 1172 */ 1173 public String toString() { 1174 return "(" + argtypes + ")" + restype; 1175 } 1176 1177 public List<Type> getParameterTypes() { return argtypes; } 1178 public Type getReturnType() { return restype; } 1179 public Type getReceiverType() { return recvtype; } 1180 public List<Type> getThrownTypes() { return thrown; } 1181 1182 public boolean isErroneous() { 1183 return 1184 isErroneous(argtypes) || 1185 restype != null && restype.isErroneous(); 1186 } 1187 1188 public Type map(Mapping f) { 1189 List<Type> argtypes1 = map(argtypes, f); 1190 Type restype1 = f.apply(restype); 1191 List<Type> thrown1 = map(thrown, f); 1192 if (argtypes1 == argtypes && 1193 restype1 == restype && 1194 thrown1 == thrown) return this; 1195 else return new MethodType(argtypes1, restype1, thrown1, tsym); 1196 } 1197 1198 public boolean contains(Type elem) { 1199 return elem == this || contains(argtypes, elem) || restype.contains(elem) || contains(thrown, elem); 1200 } 1201 1202 public MethodType asMethodType() { return this; } 1203 1204 public void complete() { 1205 for (List<Type> l = argtypes; l.nonEmpty(); l = l.tail) 1206 l.head.complete(); 1207 restype.complete(); 1208 recvtype.complete(); 1209 for (List<Type> l = thrown; l.nonEmpty(); l = l.tail) 1210 l.head.complete(); 1211 } 1212 1213 public List<TypeVar> getTypeVariables() { 1214 return List.nil(); 1215 } 1216 1217 public TypeSymbol asElement() { 1218 return null; 1219 } 1220 1221 public TypeKind getKind() { 1222 return TypeKind.EXECUTABLE; 1223 } 1224 1225 public <R, P> R accept(TypeVisitor<R, P> v, P p) { 1226 return v.visitExecutable(this, p); 1227 } 1228 } 1229 1230 public static class PackageType extends Type implements NoType { 1231 1232 PackageType(TypeSymbol tsym) { 1233 super(tsym); 1234 } 1235 1236 @Override 1237 public TypeTag getTag() { 1238 return PACKAGE; 1239 } 1240 1241 @Override 1242 public <R,S> R accept(Type.Visitor<R,S> v, S s) { 1243 return v.visitPackageType(this, s); 1244 } 1245 1246 public String toString() { 1247 return tsym.getQualifiedName().toString(); 1248 } 1249 1250 public TypeKind getKind() { 1251 return TypeKind.PACKAGE; 1252 } 1253 1254 public <R, P> R accept(TypeVisitor<R, P> v, P p) { 1255 return v.visitNoType(this, p); 1256 } 1257 } 1258 1259 public static class TypeVar extends Type implements TypeVariable { 1260 1261 /** The upper bound of this type variable; set from outside. 1262 * Must be nonempty once it is set. 1263 * For a bound, `bound' is the bound type itself. 1264 * Multiple bounds are expressed as a single class type which has the 1265 * individual bounds as superclass, respectively interfaces. 1266 * The class type then has as `tsym' a compiler generated class `c', 1267 * which has a flag COMPOUND and whose owner is the type variable 1268 * itself. Furthermore, the erasure_field of the class 1269 * points to the first class or interface bound. 1270 */ 1271 public Type bound = null; 1272 1273 /** The lower bound of this type variable. 1274 * TypeVars don't normally have a lower bound, so it is normally set 1275 * to syms.botType. 1276 * Subtypes, such as CapturedType, may provide a different value. 1277 */ 1278 public Type lower; 1279 1280 public TypeVar(Name name, Symbol owner, Type lower) { 1281 super(null); 1282 tsym = new TypeVariableSymbol(0, name, this, owner); 1283 this.lower = lower; 1284 } 1285 1286 public TypeVar(TypeSymbol tsym, Type bound, Type lower) { 1287 super(tsym); 1288 this.bound = bound; 1289 this.lower = lower; 1290 } 1291 1292 @Override 1293 public TypeTag getTag() { 1294 return TYPEVAR; 1295 } 1296 1297 @Override 1298 public <R,S> R accept(Type.Visitor<R,S> v, S s) { 1299 return v.visitTypeVar(this, s); 1300 } 1301 1302 @Override 1303 public Type getUpperBound() { 1304 if ((bound == null || bound.hasTag(NONE)) && this != tsym.type) { 1305 bound = tsym.type.getUpperBound(); 1306 } 1307 return bound; 1308 } 1309 1310 int rank_field = -1; 1311 1312 @Override 1313 public Type getLowerBound() { 1314 return lower; 1315 } 1316 1317 public TypeKind getKind() { 1318 return TypeKind.TYPEVAR; 1319 } 1320 1321 public boolean isCaptured() { 1322 return false; 1323 } 1324 1325 @Override 1326 public boolean isReference() { 1327 return true; 1328 } 1329 1330 @Override 1331 public boolean isNullOrReference() { 1332 return true; 1333 } 1334 1335 @Override 1336 public <R, P> R accept(TypeVisitor<R, P> v, P p) { 1337 return v.visitTypeVariable(this, p); 1338 } 1339 } 1340 1341 /** A captured type variable comes from wildcards which can have 1342 * both upper and lower bound. CapturedType extends TypeVar with 1343 * a lower bound. 1344 */ 1345 public static class CapturedType extends TypeVar { 1346 1347 public WildcardType wildcard; 1348 1349 public CapturedType(Name name, 1350 Symbol owner, 1351 Type upper, 1352 Type lower, 1353 WildcardType wildcard) { 1354 super(name, owner, lower); 1355 this.lower = Assert.checkNonNull(lower); 1356 this.bound = upper; 1357 this.wildcard = wildcard; 1358 } 1359 1360 @Override 1361 public <R,S> R accept(Type.Visitor<R,S> v, S s) { 1362 return v.visitCapturedType(this, s); 1363 } 1364 1365 @Override 1366 public boolean isCaptured() { 1367 return true; 1368 } 1369 1370 @Override 1371 public String toString() { 1372 return "capture#" 1373 + (hashCode() & 0xFFFFFFFFL) % Printer.PRIME 1374 + " of " 1375 + wildcard; 1376 } 1377 } 1378 1379 public static abstract class DelegatedType extends Type { 1380 public Type qtype; 1381 public TypeTag tag; 1382 public DelegatedType(TypeTag tag, Type qtype) { 1383 super(qtype.tsym); 1384 this.tag = tag; 1385 this.qtype = qtype; 1386 } 1387 public TypeTag getTag() { return tag; } 1388 public String toString() { return qtype.toString(); } 1389 public List<Type> getTypeArguments() { return qtype.getTypeArguments(); } 1390 public Type getEnclosingType() { return qtype.getEnclosingType(); } 1391 public List<Type> getParameterTypes() { return qtype.getParameterTypes(); } 1392 public Type getReturnType() { return qtype.getReturnType(); } 1393 public Type getReceiverType() { return qtype.getReceiverType(); } 1394 public List<Type> getThrownTypes() { return qtype.getThrownTypes(); } 1395 public List<Type> allparams() { return qtype.allparams(); } 1396 public Type getUpperBound() { return qtype.getUpperBound(); } 1397 public boolean isErroneous() { return qtype.isErroneous(); } 1398 } 1399 1400 /** 1401 * The type of a generic method type. It consists of a method type and 1402 * a list of method type-parameters that are used within the method 1403 * type. 1404 */ 1405 public static class ForAll extends DelegatedType implements ExecutableType { 1406 public List<Type> tvars; 1407 1408 public ForAll(List<Type> tvars, Type qtype) { 1409 super(FORALL, (MethodType)qtype); 1410 this.tvars = tvars; 1411 } 1412 1413 @Override 1414 public <R,S> R accept(Type.Visitor<R,S> v, S s) { 1415 return v.visitForAll(this, s); 1416 } 1417 1418 public String toString() { 1419 return "<" + tvars + ">" + qtype; 1420 } 1421 1422 public List<Type> getTypeArguments() { return tvars; } 1423 1424 public boolean isErroneous() { 1425 return qtype.isErroneous(); 1426 } 1427 1428 public Type map(Mapping f) { 1429 return f.apply(qtype); 1430 } 1431 1432 public boolean contains(Type elem) { 1433 return qtype.contains(elem); 1434 } 1435 1436 public MethodType asMethodType() { 1437 return (MethodType)qtype; 1438 } 1439 1440 public void complete() { 1441 for (List<Type> l = tvars; l.nonEmpty(); l = l.tail) { 1442 ((TypeVar)l.head).bound.complete(); 1443 } 1444 qtype.complete(); 1445 } 1446 1447 public List<TypeVar> getTypeVariables() { 1448 return List.convert(TypeVar.class, getTypeArguments()); 1449 } 1450 1451 public TypeKind getKind() { 1452 return TypeKind.EXECUTABLE; 1453 } 1454 1455 public <R, P> R accept(TypeVisitor<R, P> v, P p) { 1456 return v.visitExecutable(this, p); 1457 } 1458 } 1459 1460 /** A class for inference variables, for use during method/diamond type 1461 * inference. An inference variable has upper/lower bounds and a set 1462 * of equality constraints. Such bounds are set during subtyping, type-containment, 1463 * type-equality checks, when the types being tested contain inference variables. 1464 * A change listener can be attached to an inference variable, to receive notifications 1465 * whenever the bounds of an inference variable change. 1466 */ 1467 public static class UndetVar extends DelegatedType { 1468 1469 /** Inference variable change listener. The listener method is called 1470 * whenever a change to the inference variable's bounds occurs 1471 */ 1472 public interface UndetVarListener { 1473 /** called when some inference variable bounds (of given kinds ibs) change */ 1474 void varChanged(UndetVar uv, Set<InferenceBound> ibs); 1475 } 1476 1477 /** 1478 * Inference variable bound kinds 1479 */ 1480 public enum InferenceBound { 1481 UPPER { 1482 public InferenceBound complement() { return LOWER; } 1483 }, 1484 /** lower bounds */ 1485 LOWER { 1486 public InferenceBound complement() { return UPPER; } 1487 }, 1488 /** equality constraints */ 1489 EQ { 1490 public InferenceBound complement() { return EQ; } 1491 }; 1492 1493 public abstract InferenceBound complement(); 1494 } 1495 1496 /** inference variable bounds */ 1497 protected Map<InferenceBound, List<Type>> bounds; 1498 1499 /** inference variable's inferred type (set from Infer.java) */ 1500 public Type inst = null; 1501 1502 /** number of declared (upper) bounds */ 1503 public int declaredCount; 1504 1505 /** inference variable's change listener */ 1506 public UndetVarListener listener = null; 1507 1508 @Override 1509 public <R,S> R accept(Type.Visitor<R,S> v, S s) { 1510 return v.visitUndetVar(this, s); 1511 } 1512 1513 public UndetVar(TypeVar origin, Types types) { 1514 super(UNDETVAR, origin); 1515 bounds = new EnumMap<InferenceBound, List<Type>>(InferenceBound.class); 1516 List<Type> declaredBounds = types.getBounds(origin); 1517 declaredCount = declaredBounds.length(); 1518 bounds.put(InferenceBound.UPPER, declaredBounds); 1519 bounds.put(InferenceBound.LOWER, List.<Type>nil()); 1520 bounds.put(InferenceBound.EQ, List.<Type>nil()); 1521 } 1522 1523 public String toString() { 1524 return (inst == null) ? qtype + "?" : inst.toString(); 1525 } 1526 1527 public String debugString() { 1528 String result = "inference var = " + qtype + "\n"; 1529 if (inst != null) { 1530 result += "inst = " + inst + '\n'; 1531 } 1532 for (InferenceBound bound: InferenceBound.values()) { 1533 List<Type> aboundList = bounds.get(bound); 1534 if (aboundList.size() > 0) { 1535 result += bound + " = " + aboundList + '\n'; 1536 } 1537 } 1538 return result; 1539 } 1540 1541 @Override 1542 public boolean isPartial() { 1543 return true; 1544 } 1545 1546 @Override 1547 public Type baseType() { 1548 return (inst == null) ? this : inst.baseType(); 1549 } 1550 1551 /** get all bounds of a given kind */ 1552 public List<Type> getBounds(InferenceBound... ibs) { 1553 ListBuffer<Type> buf = new ListBuffer<>(); 1554 for (InferenceBound ib : ibs) { 1555 buf.appendList(bounds.get(ib)); 1556 } 1557 return buf.toList(); 1558 } 1559 1560 /** get the list of declared (upper) bounds */ 1561 public List<Type> getDeclaredBounds() { 1562 ListBuffer<Type> buf = new ListBuffer<>(); 1563 int count = 0; 1564 for (Type b : getBounds(InferenceBound.UPPER)) { 1565 if (count++ == declaredCount) break; 1566 buf.append(b); 1567 } 1568 return buf.toList(); 1569 } 1570 1571 /** internal method used to override an undetvar bounds */ 1572 public void setBounds(InferenceBound ib, List<Type> newBounds) { 1573 bounds.put(ib, newBounds); 1574 } 1575 1576 /** add a bound of a given kind - this might trigger listener notification */ 1577 public final void addBound(InferenceBound ib, Type bound, Types types) { 1578 addBound(ib, bound, types, false); 1579 } 1580 1581 protected void addBound(InferenceBound ib, Type bound, Types types, boolean update) { 1582 Type bound2 = toTypeVarMap.apply(bound).baseType(); 1583 List<Type> prevBounds = bounds.get(ib); 1584 for (Type b : prevBounds) { 1585 //check for redundancy - use strict version of isSameType on tvars 1586 //(as the standard version will lead to false positives w.r.t. clones ivars) 1587 if (types.isSameType(b, bound2, true) || bound == qtype) return; 1588 } 1589 bounds.put(ib, prevBounds.prepend(bound2)); 1590 notifyChange(EnumSet.of(ib)); 1591 } 1592 //where 1593 Type.Mapping toTypeVarMap = new Mapping("toTypeVarMap") { 1594 @Override 1595 public Type apply(Type t) { 1596 if (t.hasTag(UNDETVAR)) { 1597 UndetVar uv = (UndetVar)t; 1598 return uv.inst != null ? uv.inst : uv.qtype; 1599 } else { 1600 return t.map(this); 1601 } 1602 } 1603 }; 1604 1605 /** replace types in all bounds - this might trigger listener notification */ 1606 public void substBounds(List<Type> from, List<Type> to, Types types) { 1607 List<Type> instVars = from.diff(to); 1608 //if set of instantiated ivars is empty, there's nothing to do! 1609 if (instVars.isEmpty()) return; 1610 final EnumSet<InferenceBound> boundsChanged = EnumSet.noneOf(InferenceBound.class); 1611 UndetVarListener prevListener = listener; 1612 try { 1613 //setup new listener for keeping track of changed bounds 1614 listener = new UndetVarListener() { 1615 public void varChanged(UndetVar uv, Set<InferenceBound> ibs) { 1616 boundsChanged.addAll(ibs); 1617 } 1618 }; 1619 for (Map.Entry<InferenceBound, List<Type>> _entry : bounds.entrySet()) { 1620 InferenceBound ib = _entry.getKey(); 1621 List<Type> prevBounds = _entry.getValue(); 1622 ListBuffer<Type> newBounds = new ListBuffer<>(); 1623 ListBuffer<Type> deps = new ListBuffer<>(); 1624 //step 1 - re-add bounds that are not dependent on ivars 1625 for (Type t : prevBounds) { 1626 if (!t.containsAny(instVars)) { 1627 newBounds.append(t); 1628 } else { 1629 deps.append(t); 1630 } 1631 } 1632 //step 2 - replace bounds 1633 bounds.put(ib, newBounds.toList()); 1634 //step 3 - for each dependency, add new replaced bound 1635 for (Type dep : deps) { 1636 addBound(ib, types.subst(dep, from, to), types, true); 1637 } 1638 } 1639 } finally { 1640 listener = prevListener; 1641 if (!boundsChanged.isEmpty()) { 1642 notifyChange(boundsChanged); 1643 } 1644 } 1645 } 1646 1647 private void notifyChange(EnumSet<InferenceBound> ibs) { 1648 if (listener != null) { 1649 listener.varChanged(this, ibs); 1650 } 1651 } 1652 1653 public boolean isCaptured() { 1654 return false; 1655 } 1656 } 1657 1658 /** 1659 * This class is used to represent synthetic captured inference variables 1660 * that can be generated during nested generic method calls. The only difference 1661 * between these inference variables and ordinary ones is that captured inference 1662 * variables cannot get new bounds through incorporation. 1663 */ 1664 public static class CapturedUndetVar extends UndetVar { 1665 1666 public CapturedUndetVar(CapturedType origin, Types types) { 1667 super(origin, types); 1668 if (!origin.lower.hasTag(BOT)) { 1669 bounds.put(InferenceBound.LOWER, List.of(origin.lower)); 1670 } 1671 } 1672 1673 @Override 1674 public void addBound(InferenceBound ib, Type bound, Types types, boolean update) { 1675 if (update) { 1676 //only change bounds if request comes from substBounds 1677 super.addBound(ib, bound, types, update); 1678 } 1679 } 1680 1681 @Override 1682 public boolean isCaptured() { 1683 return true; 1684 } 1685 } 1686 1687 /** Represents NONE. 1688 */ 1689 public static class JCNoType extends Type implements NoType { 1690 public JCNoType() { 1691 super(null); 1692 } 1693 1694 @Override 1695 public TypeTag getTag() { 1696 return NONE; 1697 } 1698 1699 @Override 1700 public TypeKind getKind() { 1701 return TypeKind.NONE; 1702 } 1703 1704 @Override 1705 public <R, P> R accept(TypeVisitor<R, P> v, P p) { 1706 return v.visitNoType(this, p); 1707 } 1708 1709 @Override 1710 public boolean isCompound() { return false; } 1711 } 1712 1713 /** Represents VOID. 1714 */ 1715 public static class JCVoidType extends Type implements NoType { 1716 1717 public JCVoidType() { 1718 super(null); 1719 } 1720 1721 @Override 1722 public TypeTag getTag() { 1723 return VOID; 1724 } 1725 1726 @Override 1727 public TypeKind getKind() { 1728 return TypeKind.VOID; 1729 } 1730 1731 @Override 1732 public boolean isCompound() { return false; } 1733 1734 @Override 1735 public <R, P> R accept(TypeVisitor<R, P> v, P p) { 1736 return v.visitNoType(this, p); 1737 } 1738 1739 @Override 1740 public boolean isPrimitiveOrVoid() { 1741 return true; 1742 } 1743 } 1744 1745 static class BottomType extends Type implements NullType { 1746 public BottomType() { 1747 super(null); 1748 } 1749 1750 @Override 1751 public TypeTag getTag() { 1752 return BOT; 1753 } 1754 1755 @Override 1756 public TypeKind getKind() { 1757 return TypeKind.NULL; 1758 } 1759 1760 @Override 1761 public boolean isCompound() { return false; } 1762 1763 @Override 1764 public <R, P> R accept(TypeVisitor<R, P> v, P p) { 1765 return v.visitNull(this, p); 1766 } 1767 1768 @Override 1769 public Type constType(Object value) { 1770 return this; 1771 } 1772 1773 @Override 1774 public String stringValue() { 1775 return "null"; 1776 } 1777 1778 @Override 1779 public boolean isNullOrReference() { 1780 return true; 1781 } 1782 1783 } 1784 1785 public static class ErrorType extends ClassType 1786 implements javax.lang.model.type.ErrorType { 1787 1788 private Type originalType = null; 1789 1790 public ErrorType(Type originalType, TypeSymbol tsym) { 1791 super(noType, List.<Type>nil(), null); 1792 this.tsym = tsym; 1793 this.originalType = (originalType == null ? noType : originalType); 1794 } 1795 1796 public ErrorType(ClassSymbol c, Type originalType) { 1797 this(originalType, c); 1798 c.type = this; 1799 c.kind = ERR; 1800 c.members_field = new Scope.ErrorScope(c); 1801 } 1802 1803 @Override 1804 public TypeTag getTag() { 1805 return ERROR; 1806 } 1807 1808 @Override 1809 public boolean isPartial() { 1810 return true; 1811 } 1812 1813 @Override 1814 public boolean isReference() { 1815 return true; 1816 } 1817 1818 @Override 1819 public boolean isNullOrReference() { 1820 return true; 1821 } 1822 1823 public ErrorType(Name name, TypeSymbol container, Type originalType) { 1824 this(new ClassSymbol(PUBLIC|STATIC|ACYCLIC, name, null, container), originalType); 1825 } 1826 1827 @Override 1828 public <R,S> R accept(Type.Visitor<R,S> v, S s) { 1829 return v.visitErrorType(this, s); 1830 } 1831 1832 public Type constType(Object constValue) { return this; } 1833 public Type getEnclosingType() { return this; } 1834 public Type getReturnType() { return this; } 1835 public Type asSub(Symbol sym) { return this; } 1836 public Type map(Mapping f) { return this; } 1837 1838 public boolean isGenType(Type t) { return true; } 1839 public boolean isErroneous() { return true; } 1840 public boolean isCompound() { return false; } 1841 public boolean isInterface() { return false; } 1842 1843 public List<Type> allparams() { return List.nil(); } 1844 public List<Type> getTypeArguments() { return List.nil(); } 1845 1846 public TypeKind getKind() { 1847 return TypeKind.ERROR; 1848 } 1849 1850 public Type getOriginalType() { 1851 return originalType; 1852 } 1853 1854 public <R, P> R accept(TypeVisitor<R, P> v, P p) { 1855 return v.visitError(this, p); 1856 } 1857 } 1858 1859 public static class AnnotatedType extends Type 1860 implements 1861 javax.lang.model.type.ArrayType, 1862 javax.lang.model.type.DeclaredType, 1863 javax.lang.model.type.PrimitiveType, 1864 javax.lang.model.type.TypeVariable, 1865 javax.lang.model.type.WildcardType { 1866 /** The type annotations on this type. 1867 */ 1868 private List<Attribute.TypeCompound> typeAnnotations; 1869 1870 /** The underlying type that is annotated. 1871 */ 1872 private Type underlyingType; 1873 1874 protected AnnotatedType(List<Attribute.TypeCompound> typeAnnotations, 1875 Type underlyingType) { 1876 super(underlyingType.tsym); 1877 this.typeAnnotations = typeAnnotations; 1878 this.underlyingType = underlyingType; 1879 Assert.check(typeAnnotations != null && typeAnnotations.nonEmpty(), 1880 "Can't create AnnotatedType without annotations: " + underlyingType); 1881 Assert.check(!underlyingType.isAnnotated(), 1882 "Can't annotate already annotated type: " + underlyingType + 1883 "; adding: " + typeAnnotations); 1884 } 1885 1886 @Override 1887 public TypeTag getTag() { 1888 return underlyingType.getTag(); 1889 } 1890 1891 @Override 1892 public boolean isAnnotated() { 1893 return true; 1894 } 1895 1896 @Override 1897 public List<Attribute.TypeCompound> getAnnotationMirrors() { 1898 return typeAnnotations; 1899 } 1900 1901 1902 @Override 1903 public TypeKind getKind() { 1904 return underlyingType.getKind(); 1905 } 1906 1907 @Override 1908 public Type unannotatedType() { 1909 return underlyingType; 1910 } 1911 1912 @Override 1913 public <R,S> R accept(Type.Visitor<R,S> v, S s) { 1914 return v.visitAnnotatedType(this, s); 1915 } 1916 1917 @Override 1918 public <R, P> R accept(TypeVisitor<R, P> v, P p) { 1919 return underlyingType.accept(v, p); 1920 } 1921 1922 @Override 1923 public Type map(Mapping f) { 1924 underlyingType.map(f); 1925 return this; 1926 } 1927 1928 @Override 1929 public Type constType(Object constValue) { return underlyingType.constType(constValue); } 1930 @Override 1931 public Type getEnclosingType() { return underlyingType.getEnclosingType(); } 1932 1933 @Override 1934 public Type getReturnType() { return underlyingType.getReturnType(); } 1935 @Override 1936 public List<Type> getTypeArguments() { return underlyingType.getTypeArguments(); } 1937 @Override 1938 public List<Type> getParameterTypes() { return underlyingType.getParameterTypes(); } 1939 @Override 1940 public Type getReceiverType() { return underlyingType.getReceiverType(); } 1941 @Override 1942 public List<Type> getThrownTypes() { return underlyingType.getThrownTypes(); } 1943 @Override 1944 public Type getUpperBound() { return underlyingType.getUpperBound(); } 1945 @Override 1946 public Type getLowerBound() { return underlyingType.getLowerBound(); } 1947 1948 @Override 1949 public boolean isErroneous() { return underlyingType.isErroneous(); } 1950 @Override 1951 public boolean isCompound() { return underlyingType.isCompound(); } 1952 @Override 1953 public boolean isInterface() { return underlyingType.isInterface(); } 1954 @Override 1955 public List<Type> allparams() { return underlyingType.allparams(); } 1956 @Override 1957 public boolean isPrimitive() { return underlyingType.isPrimitive(); } 1958 @Override 1959 public boolean isPrimitiveOrVoid() { return underlyingType.isPrimitiveOrVoid(); } 1960 @Override 1961 public boolean isNumeric() { return underlyingType.isNumeric(); } 1962 @Override 1963 public boolean isReference() { return underlyingType.isReference(); } 1964 @Override 1965 public boolean isNullOrReference() { return underlyingType.isNullOrReference(); } 1966 @Override 1967 public boolean isPartial() { return underlyingType.isPartial(); } 1968 @Override 1969 public boolean isParameterized() { return underlyingType.isParameterized(); } 1970 @Override 1971 public boolean isRaw() { return underlyingType.isRaw(); } 1972 @Override 1973 public boolean isFinal() { return underlyingType.isFinal(); } 1974 @Override 1975 public boolean isSuperBound() { return underlyingType.isSuperBound(); } 1976 @Override 1977 public boolean isExtendsBound() { return underlyingType.isExtendsBound(); } 1978 @Override 1979 public boolean isUnbound() { return underlyingType.isUnbound(); } 1980 1981 @Override 1982 public String toString() { 1983 // This method is only used for internal debugging output. 1984 // See 1985 // com.sun.tools.javac.code.Printer.visitAnnotatedType(AnnotatedType, Locale) 1986 // for the user-visible logic. 1987 if (typeAnnotations != null && 1988 !typeAnnotations.isEmpty()) { 1989 return "(" + typeAnnotations.toString() + " :: " + underlyingType.toString() + ")"; 1990 } else { 1991 return "({} :: " + underlyingType.toString() +")"; 1992 } 1993 } 1994 1995 @Override 1996 public boolean contains(Type t) { return underlyingType.contains(t); } 1997 1998 @Override 1999 public Type withTypeVar(Type t) { 2000 // Don't create a new AnnotatedType, as 'this' will 2001 // get its annotations set later. 2002 underlyingType = underlyingType.withTypeVar(t); 2003 return this; 2004 } 2005 2006 // TODO: attach annotations? 2007 @Override 2008 public TypeSymbol asElement() { return underlyingType.asElement(); } 2009 2010 // TODO: attach annotations? 2011 @Override 2012 public MethodType asMethodType() { return underlyingType.asMethodType(); } 2013 2014 @Override 2015 public void complete() { underlyingType.complete(); } 2016 2017 @Override 2018 public TypeMirror getComponentType() { return ((ArrayType)underlyingType).getComponentType(); } 2019 2020 // The result is an ArrayType, but only in the model sense, not the Type sense. 2021 public Type makeVarargs() { 2022 return ((ArrayType) underlyingType).makeVarargs().annotatedType(typeAnnotations); 2023 } 2024 2025 @Override 2026 public TypeMirror getExtendsBound() { return ((WildcardType)underlyingType).getExtendsBound(); } 2027 @Override 2028 public TypeMirror getSuperBound() { return ((WildcardType)underlyingType).getSuperBound(); } 2029 } 2030 2031 public static class UnknownType extends Type { 2032 2033 public UnknownType() { 2034 super(null); 2035 } 2036 2037 @Override 2038 public TypeTag getTag() { 2039 return UNKNOWN; 2040 } 2041 2042 @Override 2043 public <R, P> R accept(TypeVisitor<R, P> v, P p) { 2044 return v.visitUnknown(this, p); 2045 } 2046 2047 @Override 2048 public boolean isPartial() { 2049 return true; 2050 } 2051 } 2052 2053 /** 2054 * A visitor for types. A visitor is used to implement operations 2055 * (or relations) on types. Most common operations on types are 2056 * binary relations and this interface is designed for binary 2057 * relations, that is, operations of the form 2058 * Type × S → R. 2059 * <!-- In plain text: Type x S -> R --> 2060 * 2061 * @param <R> the return type of the operation implemented by this 2062 * visitor; use Void if no return type is needed. 2063 * @param <S> the type of the second argument (the first being the 2064 * type itself) of the operation implemented by this visitor; use 2065 * Void if a second argument is not needed. 2066 */ 2067 public interface Visitor<R,S> { 2068 R visitClassType(ClassType t, S s); 2069 R visitWildcardType(WildcardType t, S s); 2070 R visitArrayType(ArrayType t, S s); 2071 R visitMethodType(MethodType t, S s); 2072 R visitPackageType(PackageType t, S s); 2073 R visitTypeVar(TypeVar t, S s); 2074 R visitCapturedType(CapturedType t, S s); 2075 R visitForAll(ForAll t, S s); 2076 R visitUndetVar(UndetVar t, S s); 2077 R visitErrorType(ErrorType t, S s); 2078 R visitAnnotatedType(AnnotatedType t, S s); 2079 R visitType(Type t, S s); 2080 } 2081 } 2082