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