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