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