Type.java revision 3053:79e637c1e083
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 public boolean isCompound() { 587 // Compound types can't have a (non-terminal) completer. Calling 588 // flags() will complete the symbol causing the compiler to load 589 // classes unnecessarily. This led to regression 6180021. 590 return tsym.isCompleted() && (tsym.flags() & COMPOUND) != 0; 591 } 592 593 public boolean isIntersection() { 594 return false; 595 } 596 597 public boolean isUnion() { 598 return false; 599 } 600 601 public boolean isInterface() { 602 return (tsym.flags() & INTERFACE) != 0; 603 } 604 605 public boolean isFinal() { 606 return (tsym.flags() & FINAL) != 0; 607 } 608 609 /** 610 * Does this type contain occurrences of type t? 611 */ 612 public boolean contains(Type t) { 613 return t.equalsIgnoreMetadata(this); 614 } 615 616 public static boolean contains(List<Type> ts, Type t) { 617 for (List<Type> l = ts; 618 l.tail != null /*inlined: l.nonEmpty()*/; 619 l = l.tail) 620 if (l.head.contains(t)) return true; 621 return false; 622 } 623 624 /** Does this type contain an occurrence of some type in 'ts'? 625 */ 626 public boolean containsAny(List<Type> ts) { 627 for (Type t : ts) 628 if (this.contains(t)) return true; 629 return false; 630 } 631 632 public static boolean containsAny(List<Type> ts1, List<Type> ts2) { 633 for (Type t : ts1) 634 if (t.containsAny(ts2)) return true; 635 return false; 636 } 637 638 public static List<Type> filter(List<Type> ts, Filter<Type> tf) { 639 ListBuffer<Type> buf = new ListBuffer<>(); 640 for (Type t : ts) { 641 if (tf.accepts(t)) { 642 buf.append(t); 643 } 644 } 645 return buf.toList(); 646 } 647 648 public boolean isSuperBound() { return false; } 649 public boolean isExtendsBound() { return false; } 650 public boolean isUnbound() { return false; } 651 public Type withTypeVar(Type t) { return this; } 652 653 /** The underlying method type of this type. 654 */ 655 public MethodType asMethodType() { throw new AssertionError(); } 656 657 /** Complete loading all classes in this type. 658 */ 659 public void complete() {} 660 661 public TypeSymbol asElement() { 662 return tsym; 663 } 664 665 @Override @DefinedBy(Api.LANGUAGE_MODEL) 666 public TypeKind getKind() { 667 return TypeKind.OTHER; 668 } 669 670 @Override @DefinedBy(Api.LANGUAGE_MODEL) 671 public <R, P> R accept(TypeVisitor<R, P> v, P p) { 672 throw new AssertionError(); 673 } 674 675 public static class JCPrimitiveType extends Type 676 implements javax.lang.model.type.PrimitiveType { 677 678 TypeTag tag; 679 680 public JCPrimitiveType(TypeTag tag, TypeSymbol tsym) { 681 this(tag, tsym, TypeMetadata.EMPTY); 682 } 683 684 private JCPrimitiveType(TypeTag tag, TypeSymbol tsym, TypeMetadata metadata) { 685 super(tsym, metadata); 686 this.tag = tag; 687 Assert.check(tag.isPrimitive); 688 } 689 690 @Override 691 public JCPrimitiveType cloneWithMetadata(TypeMetadata md) { 692 return new JCPrimitiveType(tag, tsym, md) { 693 @Override 694 public Type baseType() { return JCPrimitiveType.this.baseType(); } 695 }; 696 } 697 698 @Override 699 public boolean isNumeric() { 700 return tag != BOOLEAN; 701 } 702 703 @Override 704 public boolean isIntegral() { 705 switch (tag) { 706 case CHAR: 707 case BYTE: 708 case SHORT: 709 case INT: 710 case LONG: 711 return true; 712 default: 713 return false; 714 } 715 } 716 717 @Override 718 public boolean isPrimitive() { 719 return true; 720 } 721 722 @Override 723 public TypeTag getTag() { 724 return tag; 725 } 726 727 @Override 728 public boolean isPrimitiveOrVoid() { 729 return true; 730 } 731 732 /** Define a constant type, of the same kind as this type 733 * and with given constant value 734 */ 735 @Override 736 public Type constType(Object constValue) { 737 final Object value = constValue; 738 return new JCPrimitiveType(tag, tsym, metadata) { 739 @Override 740 public Object constValue() { 741 return value; 742 } 743 @Override 744 public Type baseType() { 745 return tsym.type; 746 } 747 }; 748 } 749 750 /** 751 * The constant value of this type, converted to String 752 */ 753 @Override 754 public String stringValue() { 755 Object cv = Assert.checkNonNull(constValue()); 756 if (tag == BOOLEAN) { 757 return ((Integer) cv).intValue() == 0 ? "false" : "true"; 758 } 759 else if (tag == CHAR) { 760 return String.valueOf((char) ((Integer) cv).intValue()); 761 } 762 else { 763 return cv.toString(); 764 } 765 } 766 767 /** Is this a constant type whose value is false? 768 */ 769 @Override 770 public boolean isFalse() { 771 return 772 tag == BOOLEAN && 773 constValue() != null && 774 ((Integer)constValue()).intValue() == 0; 775 } 776 777 /** Is this a constant type whose value is true? 778 */ 779 @Override 780 public boolean isTrue() { 781 return 782 tag == BOOLEAN && 783 constValue() != null && 784 ((Integer)constValue()).intValue() != 0; 785 } 786 787 @Override @DefinedBy(Api.LANGUAGE_MODEL) 788 public <R, P> R accept(TypeVisitor<R, P> v, P p) { 789 return v.visitPrimitive(this, p); 790 } 791 792 @Override @DefinedBy(Api.LANGUAGE_MODEL) 793 public TypeKind getKind() { 794 switch (tag) { 795 case BYTE: return TypeKind.BYTE; 796 case CHAR: return TypeKind.CHAR; 797 case SHORT: return TypeKind.SHORT; 798 case INT: return TypeKind.INT; 799 case LONG: return TypeKind.LONG; 800 case FLOAT: return TypeKind.FLOAT; 801 case DOUBLE: return TypeKind.DOUBLE; 802 case BOOLEAN: return TypeKind.BOOLEAN; 803 } 804 throw new AssertionError(); 805 } 806 807 } 808 809 public static class WildcardType extends Type 810 implements javax.lang.model.type.WildcardType { 811 812 public Type type; 813 public BoundKind kind; 814 public TypeVar bound; 815 816 @Override 817 public <R,S> R accept(Type.Visitor<R,S> v, S s) { 818 return v.visitWildcardType(this, s); 819 } 820 821 public WildcardType(Type type, BoundKind kind, TypeSymbol tsym) { 822 this(type, kind, tsym, null, TypeMetadata.EMPTY); 823 } 824 825 public WildcardType(Type type, BoundKind kind, TypeSymbol tsym, 826 TypeMetadata metadata) { 827 this(type, kind, tsym, null, metadata); 828 } 829 830 public WildcardType(Type type, BoundKind kind, TypeSymbol tsym, 831 TypeVar bound) { 832 this(type, kind, tsym, bound, TypeMetadata.EMPTY); 833 } 834 835 public WildcardType(Type type, BoundKind kind, TypeSymbol tsym, 836 TypeVar bound, TypeMetadata metadata) { 837 super(tsym, metadata); 838 this.type = Assert.checkNonNull(type); 839 this.kind = kind; 840 this.bound = bound; 841 } 842 843 @Override 844 public WildcardType cloneWithMetadata(TypeMetadata md) { 845 return new WildcardType(type, kind, tsym, bound, md) { 846 @Override 847 public Type baseType() { return WildcardType.this.baseType(); } 848 }; 849 } 850 851 @Override 852 public TypeTag getTag() { 853 return WILDCARD; 854 } 855 856 @Override 857 public boolean contains(Type t) { 858 return kind != UNBOUND && type.contains(t); 859 } 860 861 public boolean isSuperBound() { 862 return kind == SUPER || 863 kind == UNBOUND; 864 } 865 public boolean isExtendsBound() { 866 return kind == EXTENDS || 867 kind == UNBOUND; 868 } 869 public boolean isUnbound() { 870 return kind == UNBOUND; 871 } 872 873 @Override 874 public boolean isReference() { 875 return true; 876 } 877 878 @Override 879 public boolean isNullOrReference() { 880 return true; 881 } 882 883 @Override 884 public Type withTypeVar(Type t) { 885 //-System.err.println(this+".withTypeVar("+t+");");//DEBUG 886 if (bound == t) 887 return this; 888 bound = (TypeVar)t; 889 return this; 890 } 891 892 boolean isPrintingBound = false; 893 @DefinedBy(Api.LANGUAGE_MODEL) 894 public String toString() { 895 StringBuilder s = new StringBuilder(); 896 appendAnnotationsString(s); 897 s.append(kind.toString()); 898 if (kind != UNBOUND) 899 s.append(type); 900 if (moreInfo && bound != null && !isPrintingBound) 901 try { 902 isPrintingBound = true; 903 s.append("{:").append(bound.bound).append(":}"); 904 } finally { 905 isPrintingBound = false; 906 } 907 return s.toString(); 908 } 909 910 @DefinedBy(Api.LANGUAGE_MODEL) 911 public Type getExtendsBound() { 912 if (kind == EXTENDS) 913 return type; 914 else 915 return null; 916 } 917 918 @DefinedBy(Api.LANGUAGE_MODEL) 919 public Type getSuperBound() { 920 if (kind == SUPER) 921 return type; 922 else 923 return null; 924 } 925 926 @DefinedBy(Api.LANGUAGE_MODEL) 927 public TypeKind getKind() { 928 return TypeKind.WILDCARD; 929 } 930 931 @DefinedBy(Api.LANGUAGE_MODEL) 932 public <R, P> R accept(TypeVisitor<R, P> v, P p) { 933 return v.visitWildcard(this, p); 934 } 935 } 936 937 public static class ClassType extends Type implements DeclaredType { 938 939 /** The enclosing type of this type. If this is the type of an inner 940 * class, outer_field refers to the type of its enclosing 941 * instance class, in all other cases it refers to noType. 942 */ 943 private Type outer_field; 944 945 /** The type parameters of this type (to be set once class is loaded). 946 */ 947 public List<Type> typarams_field; 948 949 /** A cache variable for the type parameters of this type, 950 * appended to all parameters of its enclosing class. 951 * @see #allparams 952 */ 953 public List<Type> allparams_field; 954 955 /** The supertype of this class (to be set once class is loaded). 956 */ 957 public Type supertype_field; 958 959 /** The interfaces of this class (to be set once class is loaded). 960 */ 961 public List<Type> interfaces_field; 962 963 /** All the interfaces of this class, including missing ones. 964 */ 965 public List<Type> all_interfaces_field; 966 967 public ClassType(Type outer, List<Type> typarams, TypeSymbol tsym) { 968 this(outer, typarams, tsym, TypeMetadata.EMPTY); 969 } 970 971 public ClassType(Type outer, List<Type> typarams, TypeSymbol tsym, 972 TypeMetadata metadata) { 973 super(tsym, metadata); 974 this.outer_field = outer; 975 this.typarams_field = typarams; 976 this.allparams_field = null; 977 this.supertype_field = null; 978 this.interfaces_field = null; 979 } 980 981 @Override 982 public ClassType cloneWithMetadata(TypeMetadata md) { 983 return new ClassType(outer_field, typarams_field, tsym, md) { 984 @Override 985 public Type baseType() { return ClassType.this.baseType(); } 986 }; 987 } 988 989 @Override 990 public TypeTag getTag() { 991 return CLASS; 992 } 993 994 @Override 995 public <R,S> R accept(Type.Visitor<R,S> v, S s) { 996 return v.visitClassType(this, s); 997 } 998 999 public Type constType(Object constValue) { 1000 final Object value = constValue; 1001 return new ClassType(getEnclosingType(), typarams_field, tsym, metadata) { 1002 @Override 1003 public Object constValue() { 1004 return value; 1005 } 1006 @Override 1007 public Type baseType() { 1008 return tsym.type; 1009 } 1010 }; 1011 } 1012 1013 /** The Java source which this type represents. 1014 */ 1015 @DefinedBy(Api.LANGUAGE_MODEL) 1016 public String toString() { 1017 StringBuilder buf = new StringBuilder(); 1018 if (getEnclosingType().hasTag(CLASS) && tsym.owner.kind == TYP) { 1019 buf.append(getEnclosingType().toString()); 1020 buf.append("."); 1021 appendAnnotationsString(buf); 1022 buf.append(className(tsym, false)); 1023 } else { 1024 appendAnnotationsString(buf); 1025 buf.append(className(tsym, true)); 1026 } 1027 1028 if (getTypeArguments().nonEmpty()) { 1029 buf.append('<'); 1030 buf.append(getTypeArguments().toString()); 1031 buf.append(">"); 1032 } 1033 return buf.toString(); 1034 } 1035//where 1036 private String className(Symbol sym, boolean longform) { 1037 if (sym.name.isEmpty() && (sym.flags() & COMPOUND) != 0) { 1038 StringBuilder s = new StringBuilder(supertype_field.toString()); 1039 for (List<Type> is=interfaces_field; is.nonEmpty(); is = is.tail) { 1040 s.append("&"); 1041 s.append(is.head.toString()); 1042 } 1043 return s.toString(); 1044 } else if (sym.name.isEmpty()) { 1045 String s; 1046 ClassType norm = (ClassType) tsym.type; 1047 if (norm == null) { 1048 s = Log.getLocalizedString("anonymous.class", (Object)null); 1049 } else if (norm.interfaces_field != null && norm.interfaces_field.nonEmpty()) { 1050 s = Log.getLocalizedString("anonymous.class", 1051 norm.interfaces_field.head); 1052 } else { 1053 s = Log.getLocalizedString("anonymous.class", 1054 norm.supertype_field); 1055 } 1056 if (moreInfo) 1057 s += String.valueOf(sym.hashCode()); 1058 return s; 1059 } else if (longform) { 1060 return sym.getQualifiedName().toString(); 1061 } else { 1062 return sym.name.toString(); 1063 } 1064 } 1065 1066 @DefinedBy(Api.LANGUAGE_MODEL) 1067 public List<Type> getTypeArguments() { 1068 if (typarams_field == null) { 1069 complete(); 1070 if (typarams_field == null) 1071 typarams_field = List.nil(); 1072 } 1073 return typarams_field; 1074 } 1075 1076 public boolean hasErasedSupertypes() { 1077 return isRaw(); 1078 } 1079 1080 @DefinedBy(Api.LANGUAGE_MODEL) 1081 public Type getEnclosingType() { 1082 return outer_field; 1083 } 1084 1085 public void setEnclosingType(Type outer) { 1086 outer_field = outer; 1087 } 1088 1089 public List<Type> allparams() { 1090 if (allparams_field == null) { 1091 allparams_field = getTypeArguments().prependList(getEnclosingType().allparams()); 1092 } 1093 return allparams_field; 1094 } 1095 1096 public boolean isErroneous() { 1097 return 1098 getEnclosingType().isErroneous() || 1099 isErroneous(getTypeArguments()) || 1100 this != tsym.type && tsym.type.isErroneous(); 1101 } 1102 1103 public boolean isParameterized() { 1104 return allparams().tail != null; 1105 // optimization, was: allparams().nonEmpty(); 1106 } 1107 1108 @Override 1109 public boolean isReference() { 1110 return true; 1111 } 1112 1113 @Override 1114 public boolean isNullOrReference() { 1115 return true; 1116 } 1117 1118 /** A cache for the rank. */ 1119 int rank_field = -1; 1120 1121 /** A class type is raw if it misses some 1122 * of its type parameter sections. 1123 * After validation, this is equivalent to: 1124 * {@code allparams.isEmpty() && tsym.type.allparams.nonEmpty(); } 1125 */ 1126 public boolean isRaw() { 1127 return 1128 this != tsym.type && // necessary, but not sufficient condition 1129 tsym.type.allparams().nonEmpty() && 1130 allparams().isEmpty(); 1131 } 1132 1133 public boolean contains(Type elem) { 1134 return 1135 elem.equalsIgnoreMetadata(this) 1136 || (isParameterized() 1137 && (getEnclosingType().contains(elem) || contains(getTypeArguments(), elem))) 1138 || (isCompound() 1139 && (supertype_field.contains(elem) || contains(interfaces_field, elem))); 1140 } 1141 1142 public void complete() { 1143 tsym.complete(); 1144 } 1145 1146 @DefinedBy(Api.LANGUAGE_MODEL) 1147 public TypeKind getKind() { 1148 return TypeKind.DECLARED; 1149 } 1150 1151 @DefinedBy(Api.LANGUAGE_MODEL) 1152 public <R, P> R accept(TypeVisitor<R, P> v, P p) { 1153 return v.visitDeclared(this, p); 1154 } 1155 } 1156 1157 public static class ErasedClassType extends ClassType { 1158 public ErasedClassType(Type outer, TypeSymbol tsym, 1159 TypeMetadata metadata) { 1160 super(outer, List.<Type>nil(), tsym, metadata); 1161 } 1162 1163 @Override 1164 public boolean hasErasedSupertypes() { 1165 return true; 1166 } 1167 } 1168 1169 // a clone of a ClassType that knows about the alternatives of a union type. 1170 public static class UnionClassType extends ClassType implements UnionType { 1171 final List<? extends Type> alternatives_field; 1172 1173 public UnionClassType(ClassType ct, List<? extends Type> alternatives) { 1174 // Presently no way to refer to this type directly, so we 1175 // cannot put annotations directly on it. 1176 super(ct.outer_field, ct.typarams_field, ct.tsym); 1177 allparams_field = ct.allparams_field; 1178 supertype_field = ct.supertype_field; 1179 interfaces_field = ct.interfaces_field; 1180 all_interfaces_field = ct.interfaces_field; 1181 alternatives_field = alternatives; 1182 } 1183 1184 @Override 1185 public UnionClassType cloneWithMetadata(TypeMetadata md) { 1186 throw new AssertionError("Cannot add metadata to a union type"); 1187 } 1188 1189 public Type getLub() { 1190 return tsym.type; 1191 } 1192 1193 @DefinedBy(Api.LANGUAGE_MODEL) 1194 public java.util.List<? extends TypeMirror> getAlternatives() { 1195 return Collections.unmodifiableList(alternatives_field); 1196 } 1197 1198 @Override 1199 public boolean isUnion() { 1200 return true; 1201 } 1202 1203 @Override @DefinedBy(Api.LANGUAGE_MODEL) 1204 public TypeKind getKind() { 1205 return TypeKind.UNION; 1206 } 1207 1208 @Override @DefinedBy(Api.LANGUAGE_MODEL) 1209 public <R, P> R accept(TypeVisitor<R, P> v, P p) { 1210 return v.visitUnion(this, p); 1211 } 1212 1213 public Iterable<? extends Type> getAlternativeTypes() { 1214 return alternatives_field; 1215 } 1216 } 1217 1218 // a clone of a ClassType that knows about the bounds of an intersection type. 1219 public static class IntersectionClassType extends ClassType implements IntersectionType { 1220 1221 public boolean allInterfaces; 1222 1223 public IntersectionClassType(List<Type> bounds, ClassSymbol csym, boolean allInterfaces) { 1224 // Presently no way to refer to this type directly, so we 1225 // cannot put annotations directly on it. 1226 super(Type.noType, List.<Type>nil(), csym); 1227 this.allInterfaces = allInterfaces; 1228 Assert.check((csym.flags() & COMPOUND) != 0); 1229 supertype_field = bounds.head; 1230 interfaces_field = bounds.tail; 1231 Assert.check(!supertype_field.tsym.isCompleted() || 1232 !supertype_field.isInterface(), supertype_field); 1233 } 1234 1235 @Override 1236 public IntersectionClassType cloneWithMetadata(TypeMetadata md) { 1237 throw new AssertionError("Cannot add metadata to an intersection type"); 1238 } 1239 1240 @DefinedBy(Api.LANGUAGE_MODEL) 1241 public java.util.List<? extends TypeMirror> getBounds() { 1242 return Collections.unmodifiableList(getExplicitComponents()); 1243 } 1244 1245 public List<Type> getComponents() { 1246 return interfaces_field.prepend(supertype_field); 1247 } 1248 1249 @Override 1250 public boolean isIntersection() { 1251 return true; 1252 } 1253 1254 public List<Type> getExplicitComponents() { 1255 return allInterfaces ? 1256 interfaces_field : 1257 getComponents(); 1258 } 1259 1260 @Override @DefinedBy(Api.LANGUAGE_MODEL) 1261 public TypeKind getKind() { 1262 return TypeKind.INTERSECTION; 1263 } 1264 1265 @Override @DefinedBy(Api.LANGUAGE_MODEL) 1266 public <R, P> R accept(TypeVisitor<R, P> v, P p) { 1267 return v.visitIntersection(this, p); 1268 } 1269 } 1270 1271 public static class ArrayType extends Type 1272 implements javax.lang.model.type.ArrayType { 1273 1274 public Type elemtype; 1275 1276 public ArrayType(Type elemtype, TypeSymbol arrayClass) { 1277 this(elemtype, arrayClass, TypeMetadata.EMPTY); 1278 } 1279 1280 public ArrayType(Type elemtype, TypeSymbol arrayClass, 1281 TypeMetadata metadata) { 1282 super(arrayClass, metadata); 1283 this.elemtype = elemtype; 1284 } 1285 1286 public ArrayType(ArrayType that) { 1287 //note: type metadata is deliberately shared here, as we want side-effects from annotation 1288 //processing to flow from original array to the cloned array. 1289 this(that.elemtype, that.tsym, that.getMetadata()); 1290 } 1291 1292 @Override 1293 public ArrayType cloneWithMetadata(TypeMetadata md) { 1294 return new ArrayType(elemtype, tsym, md) { 1295 @Override 1296 public Type baseType() { return ArrayType.this.baseType(); } 1297 }; 1298 } 1299 1300 @Override 1301 public TypeTag getTag() { 1302 return ARRAY; 1303 } 1304 1305 public <R,S> R accept(Type.Visitor<R,S> v, S s) { 1306 return v.visitArrayType(this, s); 1307 } 1308 1309 @DefinedBy(Api.LANGUAGE_MODEL) 1310 public String toString() { 1311 StringBuilder sb = new StringBuilder(); 1312 1313 // First append root component type 1314 Type t = elemtype; 1315 while (t.getKind() == TypeKind.ARRAY) 1316 t = ((ArrayType) t).getComponentType(); 1317 sb.append(t); 1318 1319 // then append @Anno[] @Anno[] ... @Anno[] 1320 t = this; 1321 do { 1322 t.appendAnnotationsString(sb, true); 1323 sb.append("[]"); 1324 t = ((ArrayType) t).getComponentType(); 1325 } while (t.getKind() == TypeKind.ARRAY); 1326 1327 return sb.toString(); 1328 } 1329 1330 @Override @DefinedBy(Api.LANGUAGE_MODEL) 1331 public boolean equals(Object obj) { 1332 if (obj instanceof ArrayType) { 1333 ArrayType that = (ArrayType)obj; 1334 return this == that || 1335 elemtype.equals(that.elemtype); 1336 } 1337 1338 return false; 1339 } 1340 1341 @DefinedBy(Api.LANGUAGE_MODEL) 1342 public int hashCode() { 1343 return (ARRAY.ordinal() << 5) + elemtype.hashCode(); 1344 } 1345 1346 public boolean isVarargs() { 1347 return false; 1348 } 1349 1350 public List<Type> allparams() { return elemtype.allparams(); } 1351 1352 public boolean isErroneous() { 1353 return elemtype.isErroneous(); 1354 } 1355 1356 public boolean isParameterized() { 1357 return elemtype.isParameterized(); 1358 } 1359 1360 @Override 1361 public boolean isReference() { 1362 return true; 1363 } 1364 1365 @Override 1366 public boolean isNullOrReference() { 1367 return true; 1368 } 1369 1370 public boolean isRaw() { 1371 return elemtype.isRaw(); 1372 } 1373 1374 public ArrayType makeVarargs() { 1375 return new ArrayType(elemtype, tsym, metadata) { 1376 @Override 1377 public boolean isVarargs() { 1378 return true; 1379 } 1380 }; 1381 } 1382 1383 public boolean contains(Type elem) { 1384 return elem.equalsIgnoreMetadata(this) || elemtype.contains(elem); 1385 } 1386 1387 public void complete() { 1388 elemtype.complete(); 1389 } 1390 1391 @DefinedBy(Api.LANGUAGE_MODEL) 1392 public Type getComponentType() { 1393 return elemtype; 1394 } 1395 1396 @DefinedBy(Api.LANGUAGE_MODEL) 1397 public TypeKind getKind() { 1398 return TypeKind.ARRAY; 1399 } 1400 1401 @DefinedBy(Api.LANGUAGE_MODEL) 1402 public <R, P> R accept(TypeVisitor<R, P> v, P p) { 1403 return v.visitArray(this, p); 1404 } 1405 } 1406 1407 public static class MethodType extends Type implements ExecutableType { 1408 1409 public List<Type> argtypes; 1410 public Type restype; 1411 public List<Type> thrown; 1412 1413 /** The type annotations on the method receiver. 1414 */ 1415 public Type recvtype; 1416 1417 public MethodType(List<Type> argtypes, 1418 Type restype, 1419 List<Type> thrown, 1420 TypeSymbol methodClass) { 1421 // Presently no way to refer to a method type directly, so 1422 // we cannot put type annotations on it. 1423 super(methodClass, TypeMetadata.EMPTY); 1424 this.argtypes = argtypes; 1425 this.restype = restype; 1426 this.thrown = thrown; 1427 } 1428 1429 @Override 1430 public MethodType cloneWithMetadata(TypeMetadata md) { 1431 throw new AssertionError("Cannot add metadata to a method type"); 1432 } 1433 1434 @Override 1435 public TypeTag getTag() { 1436 return METHOD; 1437 } 1438 1439 public <R,S> R accept(Type.Visitor<R,S> v, S s) { 1440 return v.visitMethodType(this, s); 1441 } 1442 1443 /** The Java source which this type represents. 1444 * 1445 * XXX 06/09/99 iris This isn't correct Java syntax, but it probably 1446 * should be. 1447 */ 1448 @DefinedBy(Api.LANGUAGE_MODEL) 1449 public String toString() { 1450 StringBuilder sb = new StringBuilder(); 1451 appendAnnotationsString(sb); 1452 sb.append('('); 1453 sb.append(argtypes); 1454 sb.append(')'); 1455 sb.append(restype); 1456 return sb.toString(); 1457 } 1458 1459 @DefinedBy(Api.LANGUAGE_MODEL) 1460 public List<Type> getParameterTypes() { return argtypes; } 1461 @DefinedBy(Api.LANGUAGE_MODEL) 1462 public Type getReturnType() { return restype; } 1463 @DefinedBy(Api.LANGUAGE_MODEL) 1464 public Type getReceiverType() { return recvtype; } 1465 @DefinedBy(Api.LANGUAGE_MODEL) 1466 public List<Type> getThrownTypes() { return thrown; } 1467 1468 public boolean isErroneous() { 1469 return 1470 isErroneous(argtypes) || 1471 restype != null && restype.isErroneous(); 1472 } 1473 1474 public boolean contains(Type elem) { 1475 return elem.equalsIgnoreMetadata(this) || contains(argtypes, elem) || restype.contains(elem) || contains(thrown, elem); 1476 } 1477 1478 public MethodType asMethodType() { return this; } 1479 1480 public void complete() { 1481 for (List<Type> l = argtypes; l.nonEmpty(); l = l.tail) 1482 l.head.complete(); 1483 restype.complete(); 1484 recvtype.complete(); 1485 for (List<Type> l = thrown; l.nonEmpty(); l = l.tail) 1486 l.head.complete(); 1487 } 1488 1489 @DefinedBy(Api.LANGUAGE_MODEL) 1490 public List<TypeVar> getTypeVariables() { 1491 return List.nil(); 1492 } 1493 1494 public TypeSymbol asElement() { 1495 return null; 1496 } 1497 1498 @DefinedBy(Api.LANGUAGE_MODEL) 1499 public TypeKind getKind() { 1500 return TypeKind.EXECUTABLE; 1501 } 1502 1503 @DefinedBy(Api.LANGUAGE_MODEL) 1504 public <R, P> R accept(TypeVisitor<R, P> v, P p) { 1505 return v.visitExecutable(this, p); 1506 } 1507 } 1508 1509 public static class PackageType extends Type implements NoType { 1510 1511 PackageType(TypeSymbol tsym) { 1512 // Package types cannot be annotated 1513 super(tsym, TypeMetadata.EMPTY); 1514 } 1515 1516 @Override 1517 public PackageType cloneWithMetadata(TypeMetadata md) { 1518 throw new AssertionError("Cannot add metadata to a package type"); 1519 } 1520 1521 @Override 1522 public TypeTag getTag() { 1523 return PACKAGE; 1524 } 1525 1526 @Override 1527 public <R,S> R accept(Type.Visitor<R,S> v, S s) { 1528 return v.visitPackageType(this, s); 1529 } 1530 1531 @DefinedBy(Api.LANGUAGE_MODEL) 1532 public String toString() { 1533 return tsym.getQualifiedName().toString(); 1534 } 1535 1536 @DefinedBy(Api.LANGUAGE_MODEL) 1537 public TypeKind getKind() { 1538 return TypeKind.PACKAGE; 1539 } 1540 1541 @DefinedBy(Api.LANGUAGE_MODEL) 1542 public <R, P> R accept(TypeVisitor<R, P> v, P p) { 1543 return v.visitNoType(this, p); 1544 } 1545 } 1546 1547 public static class TypeVar extends Type implements TypeVariable { 1548 1549 /** The upper bound of this type variable; set from outside. 1550 * Must be nonempty once it is set. 1551 * For a bound, `bound' is the bound type itself. 1552 * Multiple bounds are expressed as a single class type which has the 1553 * individual bounds as superclass, respectively interfaces. 1554 * The class type then has as `tsym' a compiler generated class `c', 1555 * which has a flag COMPOUND and whose owner is the type variable 1556 * itself. Furthermore, the erasure_field of the class 1557 * points to the first class or interface bound. 1558 */ 1559 public Type bound = null; 1560 1561 /** The lower bound of this type variable. 1562 * TypeVars don't normally have a lower bound, so it is normally set 1563 * to syms.botType. 1564 * Subtypes, such as CapturedType, may provide a different value. 1565 */ 1566 public Type lower; 1567 1568 public TypeVar(Name name, Symbol owner, Type lower) { 1569 super(null, TypeMetadata.EMPTY); 1570 tsym = new TypeVariableSymbol(0, name, this, owner); 1571 this.bound = null; 1572 this.lower = lower; 1573 } 1574 1575 public TypeVar(TypeSymbol tsym, Type bound, Type lower) { 1576 this(tsym, bound, lower, TypeMetadata.EMPTY); 1577 } 1578 1579 public TypeVar(TypeSymbol tsym, Type bound, Type lower, 1580 TypeMetadata metadata) { 1581 super(tsym, metadata); 1582 this.bound = bound; 1583 this.lower = lower; 1584 } 1585 1586 @Override 1587 public TypeVar cloneWithMetadata(TypeMetadata md) { 1588 return new TypeVar(tsym, bound, lower, md) { 1589 @Override 1590 public Type baseType() { return TypeVar.this.baseType(); } 1591 }; 1592 } 1593 1594 @Override 1595 public TypeTag getTag() { 1596 return TYPEVAR; 1597 } 1598 1599 @Override 1600 public <R,S> R accept(Type.Visitor<R,S> v, S s) { 1601 return v.visitTypeVar(this, s); 1602 } 1603 1604 @Override @DefinedBy(Api.LANGUAGE_MODEL) 1605 public Type getUpperBound() { 1606 if ((bound == null || bound.hasTag(NONE)) && this != tsym.type) { 1607 bound = tsym.type.getUpperBound(); 1608 } 1609 return bound; 1610 } 1611 1612 int rank_field = -1; 1613 1614 @Override @DefinedBy(Api.LANGUAGE_MODEL) 1615 public Type getLowerBound() { 1616 return lower; 1617 } 1618 1619 @DefinedBy(Api.LANGUAGE_MODEL) 1620 public TypeKind getKind() { 1621 return TypeKind.TYPEVAR; 1622 } 1623 1624 public boolean isCaptured() { 1625 return false; 1626 } 1627 1628 @Override 1629 public boolean isReference() { 1630 return true; 1631 } 1632 1633 @Override 1634 public boolean isNullOrReference() { 1635 return true; 1636 } 1637 1638 @Override @DefinedBy(Api.LANGUAGE_MODEL) 1639 public <R, P> R accept(TypeVisitor<R, P> v, P p) { 1640 return v.visitTypeVariable(this, p); 1641 } 1642 } 1643 1644 /** A captured type variable comes from wildcards which can have 1645 * both upper and lower bound. CapturedType extends TypeVar with 1646 * a lower bound. 1647 */ 1648 public static class CapturedType extends TypeVar { 1649 1650 public WildcardType wildcard; 1651 1652 public CapturedType(Name name, 1653 Symbol owner, 1654 Type upper, 1655 Type lower, 1656 WildcardType wildcard) { 1657 super(name, owner, lower); 1658 this.lower = Assert.checkNonNull(lower); 1659 this.bound = upper; 1660 this.wildcard = wildcard; 1661 } 1662 1663 public CapturedType(TypeSymbol tsym, 1664 Type bound, 1665 Type upper, 1666 Type lower, 1667 WildcardType wildcard, 1668 TypeMetadata metadata) { 1669 super(tsym, bound, lower, metadata); 1670 this.wildcard = wildcard; 1671 } 1672 1673 @Override 1674 public CapturedType cloneWithMetadata(TypeMetadata md) { 1675 return new CapturedType(tsym, bound, bound, lower, wildcard, md) { 1676 @Override 1677 public Type baseType() { return CapturedType.this.baseType(); } 1678 }; 1679 } 1680 1681 @Override 1682 public <R,S> R accept(Type.Visitor<R,S> v, S s) { 1683 return v.visitCapturedType(this, s); 1684 } 1685 1686 @Override 1687 public boolean isCaptured() { 1688 return true; 1689 } 1690 1691 @Override @DefinedBy(Api.LANGUAGE_MODEL) 1692 public String toString() { 1693 StringBuilder sb = new StringBuilder(); 1694 appendAnnotationsString(sb); 1695 sb.append("capture#"); 1696 sb.append((hashCode() & 0xFFFFFFFFL) % Printer.PRIME); 1697 sb.append(" of "); 1698 sb.append(wildcard); 1699 return sb.toString(); 1700 } 1701 } 1702 1703 public static abstract class DelegatedType extends Type { 1704 public Type qtype; 1705 public TypeTag tag; 1706 1707 public DelegatedType(TypeTag tag, Type qtype) { 1708 this(tag, qtype, TypeMetadata.EMPTY); 1709 } 1710 1711 public DelegatedType(TypeTag tag, Type qtype, 1712 TypeMetadata metadata) { 1713 super(qtype.tsym, metadata); 1714 this.tag = tag; 1715 this.qtype = qtype; 1716 } 1717 1718 public TypeTag getTag() { return tag; } 1719 @DefinedBy(Api.LANGUAGE_MODEL) 1720 public String toString() { return qtype.toString(); } 1721 public List<Type> getTypeArguments() { return qtype.getTypeArguments(); } 1722 public Type getEnclosingType() { return qtype.getEnclosingType(); } 1723 public List<Type> getParameterTypes() { return qtype.getParameterTypes(); } 1724 public Type getReturnType() { return qtype.getReturnType(); } 1725 public Type getReceiverType() { return qtype.getReceiverType(); } 1726 public List<Type> getThrownTypes() { return qtype.getThrownTypes(); } 1727 public List<Type> allparams() { return qtype.allparams(); } 1728 public Type getUpperBound() { return qtype.getUpperBound(); } 1729 public boolean isErroneous() { return qtype.isErroneous(); } 1730 } 1731 1732 /** 1733 * The type of a generic method type. It consists of a method type and 1734 * a list of method type-parameters that are used within the method 1735 * type. 1736 */ 1737 public static class ForAll extends DelegatedType implements ExecutableType { 1738 public List<Type> tvars; 1739 1740 public ForAll(List<Type> tvars, Type qtype) { 1741 super(FORALL, (MethodType)qtype); 1742 this.tvars = tvars; 1743 } 1744 1745 @Override 1746 public ForAll cloneWithMetadata(TypeMetadata md) { 1747 throw new AssertionError("Cannot add metadata to a forall type"); 1748 } 1749 1750 @Override 1751 public <R,S> R accept(Type.Visitor<R,S> v, S s) { 1752 return v.visitForAll(this, s); 1753 } 1754 1755 @DefinedBy(Api.LANGUAGE_MODEL) 1756 public String toString() { 1757 StringBuilder sb = new StringBuilder(); 1758 appendAnnotationsString(sb); 1759 sb.append('<'); 1760 sb.append(tvars); 1761 sb.append('>'); 1762 sb.append(qtype); 1763 return sb.toString(); 1764 } 1765 1766 public List<Type> getTypeArguments() { return tvars; } 1767 1768 public boolean isErroneous() { 1769 return qtype.isErroneous(); 1770 } 1771 1772 public boolean contains(Type elem) { 1773 return qtype.contains(elem); 1774 } 1775 1776 public MethodType asMethodType() { 1777 return (MethodType)qtype; 1778 } 1779 1780 public void complete() { 1781 for (List<Type> l = tvars; l.nonEmpty(); l = l.tail) { 1782 ((TypeVar)l.head).bound.complete(); 1783 } 1784 qtype.complete(); 1785 } 1786 1787 @DefinedBy(Api.LANGUAGE_MODEL) 1788 public List<TypeVar> getTypeVariables() { 1789 return List.convert(TypeVar.class, getTypeArguments()); 1790 } 1791 1792 @DefinedBy(Api.LANGUAGE_MODEL) 1793 public TypeKind getKind() { 1794 return TypeKind.EXECUTABLE; 1795 } 1796 1797 @DefinedBy(Api.LANGUAGE_MODEL) 1798 public <R, P> R accept(TypeVisitor<R, P> v, P p) { 1799 return v.visitExecutable(this, p); 1800 } 1801 } 1802 1803 /** A class for inference variables, for use during method/diamond type 1804 * inference. An inference variable has upper/lower bounds and a set 1805 * of equality constraints. Such bounds are set during subtyping, type-containment, 1806 * type-equality checks, when the types being tested contain inference variables. 1807 * A change listener can be attached to an inference variable, to receive notifications 1808 * whenever the bounds of an inference variable change. 1809 */ 1810 public static class UndetVar extends DelegatedType { 1811 1812 /** Inference variable change listener. The listener method is called 1813 * whenever a change to the inference variable's bounds occurs 1814 */ 1815 public interface UndetVarListener { 1816 /** called when some inference variable bounds (of given kinds ibs) change */ 1817 void varChanged(UndetVar uv, Set<InferenceBound> ibs); 1818 } 1819 1820 /** 1821 * Inference variable bound kinds 1822 */ 1823 public enum InferenceBound { 1824 /** upper bounds */ 1825 UPPER { 1826 public InferenceBound complement() { return LOWER; } 1827 }, 1828 /** lower bounds */ 1829 LOWER { 1830 public InferenceBound complement() { return UPPER; } 1831 }, 1832 /** equality constraints */ 1833 EQ { 1834 public InferenceBound complement() { return EQ; } 1835 }; 1836 1837 public abstract InferenceBound complement(); 1838 } 1839 1840 /** inference variable bounds */ 1841 protected Map<InferenceBound, List<Type>> bounds; 1842 1843 /** inference variable's inferred type (set from Infer.java) */ 1844 public Type inst = null; 1845 1846 /** number of declared (upper) bounds */ 1847 public int declaredCount; 1848 1849 /** inference variable's change listener */ 1850 public UndetVarListener listener = null; 1851 1852 @Override 1853 public <R,S> R accept(Type.Visitor<R,S> v, S s) { 1854 return v.visitUndetVar(this, s); 1855 } 1856 1857 public UndetVar(TypeVar origin, Types types) { 1858 // This is a synthesized internal type, so we cannot annotate it. 1859 super(UNDETVAR, origin); 1860 bounds = new EnumMap<>(InferenceBound.class); 1861 List<Type> declaredBounds = types.getBounds(origin); 1862 declaredCount = declaredBounds.length(); 1863 bounds.put(InferenceBound.UPPER, declaredBounds); 1864 bounds.put(InferenceBound.LOWER, List.<Type>nil()); 1865 bounds.put(InferenceBound.EQ, List.<Type>nil()); 1866 } 1867 1868 @DefinedBy(Api.LANGUAGE_MODEL) 1869 public String toString() { 1870 StringBuilder sb = new StringBuilder(); 1871 appendAnnotationsString(sb); 1872 if (inst == null) { 1873 sb.append(qtype); 1874 sb.append('?'); 1875 } else { 1876 sb.append(inst); 1877 } 1878 return sb.toString(); 1879 } 1880 1881 public String debugString() { 1882 String result = "inference var = " + qtype + "\n"; 1883 if (inst != null) { 1884 result += "inst = " + inst + '\n'; 1885 } 1886 for (InferenceBound bound: InferenceBound.values()) { 1887 List<Type> aboundList = bounds.get(bound); 1888 if (aboundList.size() > 0) { 1889 result += bound + " = " + aboundList + '\n'; 1890 } 1891 } 1892 return result; 1893 } 1894 1895 @Override 1896 public UndetVar cloneWithMetadata(TypeMetadata md) { 1897 throw new AssertionError("Cannot add metadata to an UndetVar type"); 1898 } 1899 1900 @Override 1901 public boolean isPartial() { 1902 return true; 1903 } 1904 1905 @Override 1906 public Type baseType() { 1907 return (inst == null) ? this : inst.baseType(); 1908 } 1909 1910 /** get all bounds of a given kind */ 1911 public List<Type> getBounds(InferenceBound... ibs) { 1912 ListBuffer<Type> buf = new ListBuffer<>(); 1913 for (InferenceBound ib : ibs) { 1914 buf.appendList(bounds.get(ib)); 1915 } 1916 return buf.toList(); 1917 } 1918 1919 /** get the list of declared (upper) bounds */ 1920 public List<Type> getDeclaredBounds() { 1921 ListBuffer<Type> buf = new ListBuffer<>(); 1922 int count = 0; 1923 for (Type b : getBounds(InferenceBound.UPPER)) { 1924 if (count++ == declaredCount) break; 1925 buf.append(b); 1926 } 1927 return buf.toList(); 1928 } 1929 1930 /** internal method used to override an undetvar bounds */ 1931 public void setBounds(InferenceBound ib, List<Type> newBounds) { 1932 bounds.put(ib, newBounds); 1933 } 1934 1935 /** add a bound of a given kind - this might trigger listener notification */ 1936 public final void addBound(InferenceBound ib, Type bound, Types types) { 1937 addBound(ib, bound, types, false); 1938 } 1939 1940 protected void addBound(InferenceBound ib, Type bound, Types types, boolean update) { 1941 Type bound2 = bound.map(toTypeVarMap).baseType(); 1942 List<Type> prevBounds = bounds.get(ib); 1943 for (Type b : prevBounds) { 1944 //check for redundancy - use strict version of isSameType on tvars 1945 //(as the standard version will lead to false positives w.r.t. clones ivars) 1946 if (types.isSameType(b, bound2, true) || bound == qtype) return; 1947 } 1948 bounds.put(ib, prevBounds.prepend(bound2)); 1949 notifyChange(EnumSet.of(ib)); 1950 } 1951 //where 1952 TypeMapping<Void> toTypeVarMap = new TypeMapping<Void>() { 1953 @Override 1954 public Type visitUndetVar(UndetVar uv, Void _unused) { 1955 return uv.inst != null ? uv.inst : uv.qtype; 1956 } 1957 }; 1958 1959 /** replace types in all bounds - this might trigger listener notification */ 1960 public void substBounds(List<Type> from, List<Type> to, Types types) { 1961 List<Type> instVars = from.diff(to); 1962 //if set of instantiated ivars is empty, there's nothing to do! 1963 if (instVars.isEmpty()) return; 1964 final EnumSet<InferenceBound> boundsChanged = EnumSet.noneOf(InferenceBound.class); 1965 UndetVarListener prevListener = listener; 1966 try { 1967 //setup new listener for keeping track of changed bounds 1968 listener = new UndetVarListener() { 1969 public void varChanged(UndetVar uv, Set<InferenceBound> ibs) { 1970 boundsChanged.addAll(ibs); 1971 } 1972 }; 1973 for (Map.Entry<InferenceBound, List<Type>> _entry : bounds.entrySet()) { 1974 InferenceBound ib = _entry.getKey(); 1975 List<Type> prevBounds = _entry.getValue(); 1976 ListBuffer<Type> newBounds = new ListBuffer<>(); 1977 ListBuffer<Type> deps = new ListBuffer<>(); 1978 //step 1 - re-add bounds that are not dependent on ivars 1979 for (Type t : prevBounds) { 1980 if (!t.containsAny(instVars)) { 1981 newBounds.append(t); 1982 } else { 1983 deps.append(t); 1984 } 1985 } 1986 //step 2 - replace bounds 1987 bounds.put(ib, newBounds.toList()); 1988 //step 3 - for each dependency, add new replaced bound 1989 for (Type dep : deps) { 1990 addBound(ib, types.subst(dep, from, to), types, true); 1991 } 1992 } 1993 } finally { 1994 listener = prevListener; 1995 if (!boundsChanged.isEmpty()) { 1996 notifyChange(boundsChanged); 1997 } 1998 } 1999 } 2000 2001 private void notifyChange(EnumSet<InferenceBound> ibs) { 2002 if (listener != null) { 2003 listener.varChanged(this, ibs); 2004 } 2005 } 2006 2007 public boolean isCaptured() { 2008 return false; 2009 } 2010 } 2011 2012 /** 2013 * This class is used to represent synthetic captured inference variables 2014 * that can be generated during nested generic method calls. The only difference 2015 * between these inference variables and ordinary ones is that captured inference 2016 * variables cannot get new bounds through incorporation. 2017 */ 2018 public static class CapturedUndetVar extends UndetVar { 2019 2020 public CapturedUndetVar(CapturedType origin, Types types) { 2021 super(origin, types); 2022 if (!origin.lower.hasTag(BOT)) { 2023 bounds.put(InferenceBound.LOWER, List.of(origin.lower)); 2024 } 2025 } 2026 2027 @Override 2028 public void addBound(InferenceBound ib, Type bound, Types types, boolean update) { 2029 if (update) { 2030 //only change bounds if request comes from substBounds 2031 super.addBound(ib, bound, types, update); 2032 } 2033 else if (bound.hasTag(UNDETVAR) && !((UndetVar) bound).isCaptured()) { 2034 ((UndetVar) bound).addBound(ib.complement(), this, types, false); 2035 } 2036 } 2037 2038 @Override 2039 public boolean isCaptured() { 2040 return true; 2041 } 2042 } 2043 2044 /** Represents NONE. 2045 */ 2046 public static class JCNoType extends Type implements NoType { 2047 public JCNoType() { 2048 // Need to use List.nil(), because JCNoType constructor 2049 // gets called in static initializers in Type, where 2050 // noAnnotations is also defined. 2051 super(null, TypeMetadata.EMPTY); 2052 } 2053 2054 @Override 2055 public JCNoType cloneWithMetadata(TypeMetadata md) { 2056 throw new AssertionError("Cannot add metadata to a JCNoType"); 2057 } 2058 2059 @Override 2060 public TypeTag getTag() { 2061 return NONE; 2062 } 2063 2064 @Override @DefinedBy(Api.LANGUAGE_MODEL) 2065 public TypeKind getKind() { 2066 return TypeKind.NONE; 2067 } 2068 2069 @Override @DefinedBy(Api.LANGUAGE_MODEL) 2070 public <R, P> R accept(TypeVisitor<R, P> v, P p) { 2071 return v.visitNoType(this, p); 2072 } 2073 2074 @Override 2075 public boolean isCompound() { return false; } 2076 } 2077 2078 /** Represents VOID. 2079 */ 2080 public static class JCVoidType extends Type implements NoType { 2081 2082 public JCVoidType() { 2083 // Void cannot be annotated 2084 super(null, TypeMetadata.EMPTY); 2085 } 2086 2087 @Override 2088 public JCVoidType cloneWithMetadata(TypeMetadata md) { 2089 throw new AssertionError("Cannot add metadata to a void type"); 2090 } 2091 2092 @Override 2093 public TypeTag getTag() { 2094 return VOID; 2095 } 2096 2097 @Override @DefinedBy(Api.LANGUAGE_MODEL) 2098 public TypeKind getKind() { 2099 return TypeKind.VOID; 2100 } 2101 2102 @Override 2103 public boolean isCompound() { return false; } 2104 2105 @Override @DefinedBy(Api.LANGUAGE_MODEL) 2106 public <R, P> R accept(TypeVisitor<R, P> v, P p) { 2107 return v.visitNoType(this, p); 2108 } 2109 2110 @Override 2111 public boolean isPrimitiveOrVoid() { 2112 return true; 2113 } 2114 } 2115 2116 static class BottomType extends Type implements NullType { 2117 public BottomType() { 2118 // Bottom is a synthesized internal type, so it cannot be annotated 2119 super(null, TypeMetadata.EMPTY); 2120 } 2121 2122 @Override 2123 public BottomType cloneWithMetadata(TypeMetadata md) { 2124 throw new AssertionError("Cannot add metadata to a bottom type"); 2125 } 2126 2127 @Override 2128 public TypeTag getTag() { 2129 return BOT; 2130 } 2131 2132 @Override @DefinedBy(Api.LANGUAGE_MODEL) 2133 public TypeKind getKind() { 2134 return TypeKind.NULL; 2135 } 2136 2137 @Override 2138 public boolean isCompound() { return false; } 2139 2140 @Override @DefinedBy(Api.LANGUAGE_MODEL) 2141 public <R, P> R accept(TypeVisitor<R, P> v, P p) { 2142 return v.visitNull(this, p); 2143 } 2144 2145 @Override 2146 public Type constType(Object value) { 2147 return this; 2148 } 2149 2150 @Override 2151 public String stringValue() { 2152 return "null"; 2153 } 2154 2155 @Override 2156 public boolean isNullOrReference() { 2157 return true; 2158 } 2159 2160 } 2161 2162 public static class ErrorType extends ClassType 2163 implements javax.lang.model.type.ErrorType { 2164 2165 private Type originalType = null; 2166 2167 public ErrorType(ClassSymbol c, Type originalType) { 2168 this(originalType, c); 2169 c.type = this; 2170 c.kind = ERR; 2171 c.members_field = new Scope.ErrorScope(c); 2172 } 2173 2174 public ErrorType(Type originalType, TypeSymbol tsym) { 2175 super(noType, List.<Type>nil(), null); 2176 this.tsym = tsym; 2177 this.originalType = (originalType == null ? noType : originalType); 2178 } 2179 2180 private ErrorType(Type originalType, TypeSymbol tsym, 2181 TypeMetadata metadata) { 2182 super(noType, List.<Type>nil(), null, metadata); 2183 this.tsym = tsym; 2184 this.originalType = (originalType == null ? noType : originalType); 2185 } 2186 2187 @Override 2188 public ErrorType cloneWithMetadata(TypeMetadata md) { 2189 return new ErrorType(originalType, tsym, md) { 2190 @Override 2191 public Type baseType() { return ErrorType.this.baseType(); } 2192 }; 2193 } 2194 2195 @Override 2196 public TypeTag getTag() { 2197 return ERROR; 2198 } 2199 2200 @Override 2201 public boolean isPartial() { 2202 return true; 2203 } 2204 2205 @Override 2206 public boolean isReference() { 2207 return true; 2208 } 2209 2210 @Override 2211 public boolean isNullOrReference() { 2212 return true; 2213 } 2214 2215 public ErrorType(Name name, TypeSymbol container, Type originalType) { 2216 this(new ClassSymbol(PUBLIC|STATIC|ACYCLIC, name, null, container), originalType); 2217 } 2218 2219 @Override 2220 public <R,S> R accept(Type.Visitor<R,S> v, S s) { 2221 return v.visitErrorType(this, s); 2222 } 2223 2224 public Type constType(Object constValue) { return this; } 2225 @DefinedBy(Api.LANGUAGE_MODEL) 2226 public Type getEnclosingType() { return this; } 2227 public Type getReturnType() { return this; } 2228 public Type asSub(Symbol sym) { return this; } 2229 2230 public boolean isGenType(Type t) { return true; } 2231 public boolean isErroneous() { return true; } 2232 public boolean isCompound() { return false; } 2233 public boolean isInterface() { return false; } 2234 2235 public List<Type> allparams() { return List.nil(); } 2236 @DefinedBy(Api.LANGUAGE_MODEL) 2237 public List<Type> getTypeArguments() { return List.nil(); } 2238 2239 @DefinedBy(Api.LANGUAGE_MODEL) 2240 public TypeKind getKind() { 2241 return TypeKind.ERROR; 2242 } 2243 2244 public Type getOriginalType() { 2245 return originalType; 2246 } 2247 2248 @DefinedBy(Api.LANGUAGE_MODEL) 2249 public <R, P> R accept(TypeVisitor<R, P> v, P p) { 2250 return v.visitError(this, p); 2251 } 2252 } 2253 2254 public static class UnknownType extends Type { 2255 2256 public UnknownType() { 2257 // Unknown is a synthesized internal type, so it cannot be 2258 // annotated. 2259 super(null, TypeMetadata.EMPTY); 2260 } 2261 2262 @Override 2263 public UnknownType cloneWithMetadata(TypeMetadata md) { 2264 throw new AssertionError("Cannot add metadata to an unknown type"); 2265 } 2266 2267 @Override 2268 public TypeTag getTag() { 2269 return UNKNOWN; 2270 } 2271 2272 @Override @DefinedBy(Api.LANGUAGE_MODEL) 2273 public <R, P> R accept(TypeVisitor<R, P> v, P p) { 2274 return v.visitUnknown(this, p); 2275 } 2276 2277 @Override 2278 public boolean isPartial() { 2279 return true; 2280 } 2281 } 2282 2283 /** 2284 * A visitor for types. A visitor is used to implement operations 2285 * (or relations) on types. Most common operations on types are 2286 * binary relations and this interface is designed for binary 2287 * relations, that is, operations of the form 2288 * Type × S → R. 2289 * <!-- In plain text: Type x S -> R --> 2290 * 2291 * @param <R> the return type of the operation implemented by this 2292 * visitor; use Void if no return type is needed. 2293 * @param <S> the type of the second argument (the first being the 2294 * type itself) of the operation implemented by this visitor; use 2295 * Void if a second argument is not needed. 2296 */ 2297 public interface Visitor<R,S> { 2298 R visitClassType(ClassType t, S s); 2299 R visitWildcardType(WildcardType t, S s); 2300 R visitArrayType(ArrayType t, S s); 2301 R visitMethodType(MethodType t, S s); 2302 R visitPackageType(PackageType t, S s); 2303 R visitTypeVar(TypeVar t, S s); 2304 R visitCapturedType(CapturedType t, S s); 2305 R visitForAll(ForAll t, S s); 2306 R visitUndetVar(UndetVar t, S s); 2307 R visitErrorType(ErrorType t, S s); 2308 R visitType(Type t, S s); 2309 } 2310} 2311