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