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