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