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