src/share/classes/com/sun/tools/javac/code/Type.java

Tue, 15 Oct 2013 15:57:13 -0700

author
jjg
date
Tue, 15 Oct 2013 15:57:13 -0700
changeset 2134
b0c086cd4520
parent 2108
872c4a898b38
child 2200
7c89d200781b
permissions
-rw-r--r--

8026564: import changes from type-annotations forest
Reviewed-by: jjg
Contributed-by: wdietl@gmail.com, steve.sides@oracle.com

     1 /*
     2  * Copyright (c) 1999, 2013, 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  */
    26 package com.sun.tools.javac.code;
    28 import java.lang.annotation.Annotation;
    29 import java.util.Collections;
    30 import java.util.EnumMap;
    31 import java.util.EnumSet;
    32 import java.util.Map;
    33 import java.util.Set;
    35 import javax.lang.model.type.*;
    37 import com.sun.tools.javac.code.Symbol.*;
    38 import com.sun.tools.javac.util.*;
    39 import static com.sun.tools.javac.code.BoundKind.*;
    40 import static com.sun.tools.javac.code.Flags.*;
    41 import static com.sun.tools.javac.code.Kinds.*;
    42 import static com.sun.tools.javac.code.TypeTag.*;
    44 /** This class represents Java types. The class itself defines the behavior of
    45  *  the following types:
    46  *  <pre>
    47  *  base types (tags: BYTE, CHAR, SHORT, INT, LONG, FLOAT, DOUBLE, BOOLEAN),
    48  *  type `void' (tag: VOID),
    49  *  the bottom type (tag: BOT),
    50  *  the missing type (tag: NONE).
    51  *  </pre>
    52  *  <p>The behavior of the following types is defined in subclasses, which are
    53  *  all static inner classes of this class:
    54  *  <pre>
    55  *  class types (tag: CLASS, class: ClassType),
    56  *  array types (tag: ARRAY, class: ArrayType),
    57  *  method types (tag: METHOD, class: MethodType),
    58  *  package types (tag: PACKAGE, class: PackageType),
    59  *  type variables (tag: TYPEVAR, class: TypeVar),
    60  *  type arguments (tag: WILDCARD, class: WildcardType),
    61  *  generic method types (tag: FORALL, class: ForAll),
    62  *  the error type (tag: ERROR, class: ErrorType).
    63  *  </pre>
    64  *
    65  *  <p><b>This is NOT part of any supported API.
    66  *  If you write code that depends on this, you do so at your own risk.
    67  *  This code and its internal interfaces are subject to change or
    68  *  deletion without notice.</b>
    69  *
    70  *  @see TypeTag
    71  */
    72 public abstract class Type extends AnnoConstruct implements TypeMirror {
    74     /** Constant type: no type at all. */
    75     public static final JCNoType noType = new JCNoType();
    77     /** Constant type: special type to be used during recovery of deferred expressions. */
    78     public static final JCNoType recoveryType = new JCNoType();
    80     /** Constant type: special type to be used for marking stuck trees. */
    81     public static final JCNoType stuckType = new JCNoType();
    83     /** If this switch is turned on, the names of type variables
    84      *  and anonymous classes are printed with hashcodes appended.
    85      */
    86     public static boolean moreInfo = false;
    88     /** The defining class / interface / package / type variable.
    89      */
    90     public TypeSymbol tsym;
    92     /**
    93      * Checks if the current type tag is equal to the given tag.
    94      * @return true if tag is equal to the current type tag.
    95      */
    96     public boolean hasTag(TypeTag tag) {
    97         return tag == getTag();
    98     }
   100     /**
   101      * Returns the current type tag.
   102      * @return the value of the current type tag.
   103      */
   104     public abstract TypeTag getTag();
   106     public boolean isNumeric() {
   107         return false;
   108     }
   110     public boolean isPrimitive() {
   111         return false;
   112     }
   114     public boolean isPrimitiveOrVoid() {
   115         return false;
   116     }
   118     public boolean isReference() {
   119         return false;
   120     }
   122     public boolean isNullOrReference() {
   123         return false;
   124     }
   126     public boolean isPartial() {
   127         return false;
   128     }
   130     /**
   131      * The constant value of this type, null if this type does not
   132      * have a constant value attribute. Only primitive types and
   133      * strings (ClassType) can have a constant value attribute.
   134      * @return the constant value attribute of this type
   135      */
   136     public Object constValue() {
   137         return null;
   138     }
   140     /** Is this a constant type whose value is false?
   141      */
   142     public boolean isFalse() {
   143         return false;
   144     }
   146     /** Is this a constant type whose value is true?
   147      */
   148     public boolean isTrue() {
   149         return false;
   150     }
   152     /**
   153      * Get the representation of this type used for modelling purposes.
   154      * By default, this is itself. For ErrorType, a different value
   155      * may be provided.
   156      */
   157     public Type getModelType() {
   158         return this;
   159     }
   161     public static List<Type> getModelTypes(List<Type> ts) {
   162         ListBuffer<Type> lb = new ListBuffer<>();
   163         for (Type t: ts)
   164             lb.append(t.getModelType());
   165         return lb.toList();
   166     }
   168     /**For ErrorType, returns the original type, otherwise returns the type itself.
   169      */
   170     public Type getOriginalType() {
   171         return this;
   172     }
   174     public <R,S> R accept(Type.Visitor<R,S> v, S s) { return v.visitType(this, s); }
   176     /** Define a type given its tag and type symbol
   177      */
   178     public Type(TypeSymbol tsym) {
   179         this.tsym = tsym;
   180     }
   182     /** An abstract class for mappings from types to types
   183      */
   184     public static abstract class Mapping {
   185         private String name;
   186         public Mapping(String name) {
   187             this.name = name;
   188         }
   189         public abstract Type apply(Type t);
   190         public String toString() {
   191             return name;
   192         }
   193     }
   195     /** map a type function over all immediate descendants of this type
   196      */
   197     public Type map(Mapping f) {
   198         return this;
   199     }
   201     /** map a type function over a list of types
   202      */
   203     public static List<Type> map(List<Type> ts, Mapping f) {
   204         if (ts.nonEmpty()) {
   205             List<Type> tail1 = map(ts.tail, f);
   206             Type t = f.apply(ts.head);
   207             if (tail1 != ts.tail || t != ts.head)
   208                 return tail1.prepend(t);
   209         }
   210         return ts;
   211     }
   213     /** Define a constant type, of the same kind as this type
   214      *  and with given constant value
   215      */
   216     public Type constType(Object constValue) {
   217         throw new AssertionError();
   218     }
   220     /**
   221      * If this is a constant type, return its underlying type.
   222      * Otherwise, return the type itself.
   223      */
   224     public Type baseType() {
   225         return this;
   226     }
   228     public Type annotatedType(List<Attribute.TypeCompound> annos) {
   229         return new AnnotatedType(annos, this);
   230     }
   232     public boolean isAnnotated() {
   233         return false;
   234     }
   236     /**
   237      * If this is an annotated type, return the underlying type.
   238      * Otherwise, return the type itself.
   239      */
   240     public Type unannotatedType() {
   241         return this;
   242     }
   244     @Override
   245     public List<Attribute.TypeCompound> getAnnotationMirrors() {
   246         return List.nil();
   247     }
   250     @Override
   251     public <A extends Annotation> A getAnnotation(Class<A> annotationType) {
   252         return null;
   253     }
   256     @Override
   257     public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationType) {
   258         @SuppressWarnings("unchecked")
   259         A[] tmp = (A[]) java.lang.reflect.Array.newInstance(annotationType, 0);
   260         return tmp;
   261     }
   263     /** Return the base types of a list of types.
   264      */
   265     public static List<Type> baseTypes(List<Type> ts) {
   266         if (ts.nonEmpty()) {
   267             Type t = ts.head.baseType();
   268             List<Type> baseTypes = baseTypes(ts.tail);
   269             if (t != ts.head || baseTypes != ts.tail)
   270                 return baseTypes.prepend(t);
   271         }
   272         return ts;
   273     }
   275     /** The Java source which this type represents.
   276      */
   277     public String toString() {
   278         String s = (tsym == null || tsym.name == null)
   279             ? "<none>"
   280             : tsym.name.toString();
   281         if (moreInfo && hasTag(TYPEVAR)) {
   282             s = s + hashCode();
   283         }
   284         return s;
   285     }
   287     /**
   288      * The Java source which this type list represents.  A List is
   289      * represented as a comma-spearated listing of the elements in
   290      * that list.
   291      */
   292     public static String toString(List<Type> ts) {
   293         if (ts.isEmpty()) {
   294             return "";
   295         } else {
   296             StringBuilder buf = new StringBuilder();
   297             buf.append(ts.head.toString());
   298             for (List<Type> l = ts.tail; l.nonEmpty(); l = l.tail)
   299                 buf.append(",").append(l.head.toString());
   300             return buf.toString();
   301         }
   302     }
   304     /**
   305      * The constant value of this type, converted to String
   306      */
   307     public String stringValue() {
   308         Object cv = Assert.checkNonNull(constValue());
   309         return cv.toString();
   310     }
   312     /**
   313      * This method is analogous to isSameType, but weaker, since we
   314      * never complete classes. Where isSameType would complete a
   315      * class, equals assumes that the two types are different.
   316      */
   317     @Override
   318     public boolean equals(Object t) {
   319         return super.equals(t);
   320     }
   322     @Override
   323     public int hashCode() {
   324         return super.hashCode();
   325     }
   327     public String argtypes(boolean varargs) {
   328         List<Type> args = getParameterTypes();
   329         if (!varargs) return args.toString();
   330         StringBuilder buf = new StringBuilder();
   331         while (args.tail.nonEmpty()) {
   332             buf.append(args.head);
   333             args = args.tail;
   334             buf.append(',');
   335         }
   336         if (args.head.unannotatedType().hasTag(ARRAY)) {
   337             buf.append(((ArrayType)args.head.unannotatedType()).elemtype);
   338             if (args.head.getAnnotationMirrors().nonEmpty()) {
   339                 buf.append(args.head.getAnnotationMirrors());
   340             }
   341             buf.append("...");
   342         } else {
   343             buf.append(args.head);
   344         }
   345         return buf.toString();
   346     }
   348     /** Access methods.
   349      */
   350     public List<Type>        getTypeArguments()  { return List.nil(); }
   351     public Type              getEnclosingType()  { return null; }
   352     public List<Type>        getParameterTypes() { return List.nil(); }
   353     public Type              getReturnType()     { return null; }
   354     public Type              getReceiverType()   { return null; }
   355     public List<Type>        getThrownTypes()    { return List.nil(); }
   356     public Type              getUpperBound()     { return null; }
   357     public Type              getLowerBound()     { return null; }
   359     /** Navigation methods, these will work for classes, type variables,
   360      *  foralls, but will return null for arrays and methods.
   361      */
   363    /** Return all parameters of this type and all its outer types in order
   364     *  outer (first) to inner (last).
   365     */
   366     public List<Type> allparams() { return List.nil(); }
   368     /** Does this type contain "error" elements?
   369      */
   370     public boolean isErroneous() {
   371         return false;
   372     }
   374     public static boolean isErroneous(List<Type> ts) {
   375         for (List<Type> l = ts; l.nonEmpty(); l = l.tail)
   376             if (l.head.isErroneous()) return true;
   377         return false;
   378     }
   380     /** Is this type parameterized?
   381      *  A class type is parameterized if it has some parameters.
   382      *  An array type is parameterized if its element type is parameterized.
   383      *  All other types are not parameterized.
   384      */
   385     public boolean isParameterized() {
   386         return false;
   387     }
   389     /** Is this type a raw type?
   390      *  A class type is a raw type if it misses some of its parameters.
   391      *  An array type is a raw type if its element type is raw.
   392      *  All other types are not raw.
   393      *  Type validation will ensure that the only raw types
   394      *  in a program are types that miss all their type variables.
   395      */
   396     public boolean isRaw() {
   397         return false;
   398     }
   400     public boolean isCompound() {
   401         return tsym.completer == null
   402             // Compound types can't have a completer.  Calling
   403             // flags() will complete the symbol causing the
   404             // compiler to load classes unnecessarily.  This led
   405             // to regression 6180021.
   406             && (tsym.flags() & COMPOUND) != 0;
   407     }
   409     public boolean isInterface() {
   410         return (tsym.flags() & INTERFACE) != 0;
   411     }
   413     public boolean isFinal() {
   414         return (tsym.flags() & FINAL) != 0;
   415     }
   417     /**
   418      * Does this type contain occurrences of type t?
   419      */
   420     public boolean contains(Type t) {
   421         return t == this;
   422     }
   424     public static boolean contains(List<Type> ts, Type t) {
   425         for (List<Type> l = ts;
   426              l.tail != null /*inlined: l.nonEmpty()*/;
   427              l = l.tail)
   428             if (l.head.contains(t)) return true;
   429         return false;
   430     }
   432     /** Does this type contain an occurrence of some type in 'ts'?
   433      */
   434     public boolean containsAny(List<Type> ts) {
   435         for (Type t : ts)
   436             if (this.contains(t)) return true;
   437         return false;
   438     }
   440     public static boolean containsAny(List<Type> ts1, List<Type> ts2) {
   441         for (Type t : ts1)
   442             if (t.containsAny(ts2)) return true;
   443         return false;
   444     }
   446     public static List<Type> filter(List<Type> ts, Filter<Type> tf) {
   447         ListBuffer<Type> buf = new ListBuffer<>();
   448         for (Type t : ts) {
   449             if (tf.accepts(t)) {
   450                 buf.append(t);
   451             }
   452         }
   453         return buf.toList();
   454     }
   456     public boolean isSuperBound() { return false; }
   457     public boolean isExtendsBound() { return false; }
   458     public boolean isUnbound() { return false; }
   459     public Type withTypeVar(Type t) { return this; }
   461     /** The underlying method type of this type.
   462      */
   463     public MethodType asMethodType() { throw new AssertionError(); }
   465     /** Complete loading all classes in this type.
   466      */
   467     public void complete() {}
   469     public TypeSymbol asElement() {
   470         return tsym;
   471     }
   473     @Override
   474     public TypeKind getKind() {
   475         return TypeKind.OTHER;
   476     }
   478     @Override
   479     public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   480         throw new AssertionError();
   481     }
   483     public static class JCPrimitiveType extends Type
   484             implements javax.lang.model.type.PrimitiveType {
   486         TypeTag tag;
   488         public JCPrimitiveType(TypeTag tag, TypeSymbol tsym) {
   489             super(tsym);
   490             this.tag = tag;
   491             Assert.check(tag.isPrimitive);
   492         }
   494         @Override
   495         public boolean isNumeric() {
   496             return tag != BOOLEAN;
   497         }
   499         @Override
   500         public boolean isPrimitive() {
   501             return true;
   502         }
   504         @Override
   505         public TypeTag getTag() {
   506             return tag;
   507         }
   509         @Override
   510         public boolean isPrimitiveOrVoid() {
   511             return true;
   512         }
   514         /** Define a constant type, of the same kind as this type
   515          *  and with given constant value
   516          */
   517         @Override
   518         public Type constType(Object constValue) {
   519             final Object value = constValue;
   520             return new JCPrimitiveType(tag, tsym) {
   521                     @Override
   522                     public Object constValue() {
   523                         return value;
   524                     }
   525                     @Override
   526                     public Type baseType() {
   527                         return tsym.type;
   528                     }
   529                 };
   530         }
   532         /**
   533          * The constant value of this type, converted to String
   534          */
   535         @Override
   536         public String stringValue() {
   537             Object cv = Assert.checkNonNull(constValue());
   538             if (tag == BOOLEAN) {
   539                 return ((Integer) cv).intValue() == 0 ? "false" : "true";
   540             }
   541             else if (tag == CHAR) {
   542                 return String.valueOf((char) ((Integer) cv).intValue());
   543             }
   544             else {
   545                 return cv.toString();
   546             }
   547         }
   549         /** Is this a constant type whose value is false?
   550          */
   551         @Override
   552         public boolean isFalse() {
   553             return
   554                 tag == BOOLEAN &&
   555                 constValue() != null &&
   556                 ((Integer)constValue()).intValue() == 0;
   557         }
   559         /** Is this a constant type whose value is true?
   560          */
   561         @Override
   562         public boolean isTrue() {
   563             return
   564                 tag == BOOLEAN &&
   565                 constValue() != null &&
   566                 ((Integer)constValue()).intValue() != 0;
   567         }
   569         @Override
   570         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   571             return v.visitPrimitive(this, p);
   572         }
   574         @Override
   575         public TypeKind getKind() {
   576             switch (tag) {
   577                 case BYTE:      return TypeKind.BYTE;
   578                 case CHAR:      return TypeKind.CHAR;
   579                 case SHORT:     return TypeKind.SHORT;
   580                 case INT:       return TypeKind.INT;
   581                 case LONG:      return TypeKind.LONG;
   582                 case FLOAT:     return TypeKind.FLOAT;
   583                 case DOUBLE:    return TypeKind.DOUBLE;
   584                 case BOOLEAN:   return TypeKind.BOOLEAN;
   585             }
   586             throw new AssertionError();
   587         }
   589     }
   591     public static class WildcardType extends Type
   592             implements javax.lang.model.type.WildcardType {
   594         public Type type;
   595         public BoundKind kind;
   596         public TypeVar bound;
   598         @Override
   599         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   600             return v.visitWildcardType(this, s);
   601         }
   603         public WildcardType(Type type, BoundKind kind, TypeSymbol tsym) {
   604             super(tsym);
   605             this.type = Assert.checkNonNull(type);
   606             this.kind = kind;
   607         }
   608         public WildcardType(WildcardType t, TypeVar bound) {
   609             this(t.type, t.kind, t.tsym, bound);
   610         }
   612         public WildcardType(Type type, BoundKind kind, TypeSymbol tsym, TypeVar bound) {
   613             this(type, kind, tsym);
   614             this.bound = bound;
   615         }
   617         @Override
   618         public TypeTag getTag() {
   619             return WILDCARD;
   620         }
   622         @Override
   623         public boolean contains(Type t) {
   624             return kind != UNBOUND && type.contains(t);
   625         }
   627         public boolean isSuperBound() {
   628             return kind == SUPER ||
   629                 kind == UNBOUND;
   630         }
   631         public boolean isExtendsBound() {
   632             return kind == EXTENDS ||
   633                 kind == UNBOUND;
   634         }
   635         public boolean isUnbound() {
   636             return kind == UNBOUND;
   637         }
   639         @Override
   640         public boolean isReference() {
   641             return true;
   642         }
   644         @Override
   645         public boolean isNullOrReference() {
   646             return true;
   647         }
   649         @Override
   650         public Type withTypeVar(Type t) {
   651             //-System.err.println(this+".withTypeVar("+t+");");//DEBUG
   652             if (bound == t)
   653                 return this;
   654             bound = (TypeVar)t;
   655             return this;
   656         }
   658         boolean isPrintingBound = false;
   659         public String toString() {
   660             StringBuilder s = new StringBuilder();
   661             s.append(kind.toString());
   662             if (kind != UNBOUND)
   663                 s.append(type);
   664             if (moreInfo && bound != null && !isPrintingBound)
   665                 try {
   666                     isPrintingBound = true;
   667                     s.append("{:").append(bound.bound).append(":}");
   668                 } finally {
   669                     isPrintingBound = false;
   670                 }
   671             return s.toString();
   672         }
   674         public Type map(Mapping f) {
   675             //- System.err.println("   (" + this + ").map(" + f + ")");//DEBUG
   676             Type t = type;
   677             if (t != null)
   678                 t = f.apply(t);
   679             if (t == type)
   680                 return this;
   681             else
   682                 return new WildcardType(t, kind, tsym, bound);
   683         }
   685         public Type getExtendsBound() {
   686             if (kind == EXTENDS)
   687                 return type;
   688             else
   689                 return null;
   690         }
   692         public Type getSuperBound() {
   693             if (kind == SUPER)
   694                 return type;
   695             else
   696                 return null;
   697         }
   699         public TypeKind getKind() {
   700             return TypeKind.WILDCARD;
   701         }
   703         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   704             return v.visitWildcard(this, p);
   705         }
   706     }
   708     public static class ClassType extends Type implements DeclaredType {
   710         /** The enclosing type of this type. If this is the type of an inner
   711          *  class, outer_field refers to the type of its enclosing
   712          *  instance class, in all other cases it refers to noType.
   713          */
   714         private Type outer_field;
   716         /** The type parameters of this type (to be set once class is loaded).
   717          */
   718         public List<Type> typarams_field;
   720         /** A cache variable for the type parameters of this type,
   721          *  appended to all parameters of its enclosing class.
   722          *  @see #allparams
   723          */
   724         public List<Type> allparams_field;
   726         /** The supertype of this class (to be set once class is loaded).
   727          */
   728         public Type supertype_field;
   730         /** The interfaces of this class (to be set once class is loaded).
   731          */
   732         public List<Type> interfaces_field;
   734         /** All the interfaces of this class, including missing ones.
   735          */
   736         public List<Type> all_interfaces_field;
   738         public ClassType(Type outer, List<Type> typarams, TypeSymbol tsym) {
   739             super(tsym);
   740             this.outer_field = outer;
   741             this.typarams_field = typarams;
   742             this.allparams_field = null;
   743             this.supertype_field = null;
   744             this.interfaces_field = null;
   745             /*
   746             // this can happen during error recovery
   747             assert
   748                 outer.isParameterized() ?
   749                 typarams.length() == tsym.type.typarams().length() :
   750                 outer.isRaw() ?
   751                 typarams.length() == 0 :
   752                 true;
   753             */
   754         }
   756         @Override
   757         public TypeTag getTag() {
   758             return CLASS;
   759         }
   761         @Override
   762         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   763             return v.visitClassType(this, s);
   764         }
   766         public Type constType(Object constValue) {
   767             final Object value = constValue;
   768             return new ClassType(getEnclosingType(), typarams_field, tsym) {
   769                     @Override
   770                     public Object constValue() {
   771                         return value;
   772                     }
   773                     @Override
   774                     public Type baseType() {
   775                         return tsym.type;
   776                     }
   777                 };
   778         }
   780         /** The Java source which this type represents.
   781          */
   782         public String toString() {
   783             StringBuilder buf = new StringBuilder();
   784             if (getEnclosingType().hasTag(CLASS) && tsym.owner.kind == TYP) {
   785                 buf.append(getEnclosingType().toString());
   786                 buf.append(".");
   787                 buf.append(className(tsym, false));
   788             } else {
   789                 buf.append(className(tsym, true));
   790             }
   791             if (getTypeArguments().nonEmpty()) {
   792                 buf.append('<');
   793                 buf.append(getTypeArguments().toString());
   794                 buf.append(">");
   795             }
   796             return buf.toString();
   797         }
   798 //where
   799             private String className(Symbol sym, boolean longform) {
   800                 if (sym.name.isEmpty() && (sym.flags() & COMPOUND) != 0) {
   801                     StringBuilder s = new StringBuilder(supertype_field.toString());
   802                     for (List<Type> is=interfaces_field; is.nonEmpty(); is = is.tail) {
   803                         s.append("&");
   804                         s.append(is.head.toString());
   805                     }
   806                     return s.toString();
   807                 } else if (sym.name.isEmpty()) {
   808                     String s;
   809                     ClassType norm = (ClassType) tsym.type.unannotatedType();
   810                     if (norm == null) {
   811                         s = Log.getLocalizedString("anonymous.class", (Object)null);
   812                     } else if (norm.interfaces_field != null && norm.interfaces_field.nonEmpty()) {
   813                         s = Log.getLocalizedString("anonymous.class",
   814                                                    norm.interfaces_field.head);
   815                     } else {
   816                         s = Log.getLocalizedString("anonymous.class",
   817                                                    norm.supertype_field);
   818                     }
   819                     if (moreInfo)
   820                         s += String.valueOf(sym.hashCode());
   821                     return s;
   822                 } else if (longform) {
   823                     return sym.getQualifiedName().toString();
   824                 } else {
   825                     return sym.name.toString();
   826                 }
   827             }
   829         public List<Type> getTypeArguments() {
   830             if (typarams_field == null) {
   831                 complete();
   832                 if (typarams_field == null)
   833                     typarams_field = List.nil();
   834             }
   835             return typarams_field;
   836         }
   838         public boolean hasErasedSupertypes() {
   839             return isRaw();
   840         }
   842         public Type getEnclosingType() {
   843             return outer_field;
   844         }
   846         public void setEnclosingType(Type outer) {
   847             outer_field = outer;
   848         }
   850         public List<Type> allparams() {
   851             if (allparams_field == null) {
   852                 allparams_field = getTypeArguments().prependList(getEnclosingType().allparams());
   853             }
   854             return allparams_field;
   855         }
   857         public boolean isErroneous() {
   858             return
   859                 getEnclosingType().isErroneous() ||
   860                 isErroneous(getTypeArguments()) ||
   861                 this != tsym.type.unannotatedType() && tsym.type.isErroneous();
   862         }
   864         public boolean isParameterized() {
   865             return allparams().tail != null;
   866             // optimization, was: allparams().nonEmpty();
   867         }
   869         @Override
   870         public boolean isReference() {
   871             return true;
   872         }
   874         @Override
   875         public boolean isNullOrReference() {
   876             return true;
   877         }
   879         /** A cache for the rank. */
   880         int rank_field = -1;
   882         /** A class type is raw if it misses some
   883          *  of its type parameter sections.
   884          *  After validation, this is equivalent to:
   885          *  {@code allparams.isEmpty() && tsym.type.allparams.nonEmpty(); }
   886          */
   887         public boolean isRaw() {
   888             return
   889                 this != tsym.type && // necessary, but not sufficient condition
   890                 tsym.type.allparams().nonEmpty() &&
   891                 allparams().isEmpty();
   892         }
   894         public Type map(Mapping f) {
   895             Type outer = getEnclosingType();
   896             Type outer1 = f.apply(outer);
   897             List<Type> typarams = getTypeArguments();
   898             List<Type> typarams1 = map(typarams, f);
   899             if (outer1 == outer && typarams1 == typarams) return this;
   900             else return new ClassType(outer1, typarams1, tsym);
   901         }
   903         public boolean contains(Type elem) {
   904             return
   905                 elem == this
   906                 || (isParameterized()
   907                     && (getEnclosingType().contains(elem) || contains(getTypeArguments(), elem)))
   908                 || (isCompound()
   909                     && (supertype_field.contains(elem) || contains(interfaces_field, elem)));
   910         }
   912         public void complete() {
   913             if (tsym.completer != null) tsym.complete();
   914         }
   916         public TypeKind getKind() {
   917             return TypeKind.DECLARED;
   918         }
   920         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   921             return v.visitDeclared(this, p);
   922         }
   923     }
   925     public static class ErasedClassType extends ClassType {
   926         public ErasedClassType(Type outer, TypeSymbol tsym) {
   927             super(outer, List.<Type>nil(), tsym);
   928         }
   930         @Override
   931         public boolean hasErasedSupertypes() {
   932             return true;
   933         }
   934     }
   936     // a clone of a ClassType that knows about the alternatives of a union type.
   937     public static class UnionClassType extends ClassType implements UnionType {
   938         final List<? extends Type> alternatives_field;
   940         public UnionClassType(ClassType ct, List<? extends Type> alternatives) {
   941             super(ct.outer_field, ct.typarams_field, ct.tsym);
   942             allparams_field = ct.allparams_field;
   943             supertype_field = ct.supertype_field;
   944             interfaces_field = ct.interfaces_field;
   945             all_interfaces_field = ct.interfaces_field;
   946             alternatives_field = alternatives;
   947         }
   949         public Type getLub() {
   950             return tsym.type;
   951         }
   953         public java.util.List<? extends TypeMirror> getAlternatives() {
   954             return Collections.unmodifiableList(alternatives_field);
   955         }
   957         @Override
   958         public TypeKind getKind() {
   959             return TypeKind.UNION;
   960         }
   962         @Override
   963         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   964             return v.visitUnion(this, p);
   965         }
   966     }
   968     // a clone of a ClassType that knows about the bounds of an intersection type.
   969     public static class IntersectionClassType extends ClassType implements IntersectionType {
   971         public boolean allInterfaces;
   973         public enum IntersectionKind {
   974             EXPLICIT,
   975             IMPLICT;
   976         }
   978         public IntersectionKind intersectionKind;
   980         public IntersectionClassType(List<Type> bounds, ClassSymbol csym, boolean allInterfaces) {
   981             super(Type.noType, List.<Type>nil(), csym);
   982             this.allInterfaces = allInterfaces;
   983             Assert.check((csym.flags() & COMPOUND) != 0);
   984             supertype_field = bounds.head;
   985             interfaces_field = bounds.tail;
   986             Assert.check(supertype_field.tsym.completer != null ||
   987                     !supertype_field.isInterface(), supertype_field);
   988         }
   990         public java.util.List<? extends TypeMirror> getBounds() {
   991             return Collections.unmodifiableList(getExplicitComponents());
   992         }
   994         public List<Type> getComponents() {
   995             return interfaces_field.prepend(supertype_field);
   996         }
   998         public List<Type> getExplicitComponents() {
   999             return allInterfaces ?
  1000                     interfaces_field :
  1001                     getComponents();
  1004         @Override
  1005         public TypeKind getKind() {
  1006             return TypeKind.INTERSECTION;
  1009         @Override
  1010         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1011             return intersectionKind == IntersectionKind.EXPLICIT ?
  1012                 v.visitIntersection(this, p) :
  1013                 v.visitDeclared(this, p);
  1017     public static class ArrayType extends Type
  1018             implements javax.lang.model.type.ArrayType {
  1020         public Type elemtype;
  1022         public ArrayType(Type elemtype, TypeSymbol arrayClass) {
  1023             super(arrayClass);
  1024             this.elemtype = elemtype;
  1027         @Override
  1028         public TypeTag getTag() {
  1029             return ARRAY;
  1032         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1033             return v.visitArrayType(this, s);
  1036         public String toString() {
  1037             return elemtype + "[]";
  1040         public boolean equals(Object obj) {
  1041             return
  1042                 this == obj ||
  1043                 (obj instanceof ArrayType &&
  1044                  this.elemtype.equals(((ArrayType)obj).elemtype));
  1047         public int hashCode() {
  1048             return (ARRAY.ordinal() << 5) + elemtype.hashCode();
  1051         public boolean isVarargs() {
  1052             return false;
  1055         public List<Type> allparams() { return elemtype.allparams(); }
  1057         public boolean isErroneous() {
  1058             return elemtype.isErroneous();
  1061         public boolean isParameterized() {
  1062             return elemtype.isParameterized();
  1065         @Override
  1066         public boolean isReference() {
  1067             return true;
  1070         @Override
  1071         public boolean isNullOrReference() {
  1072             return true;
  1075         public boolean isRaw() {
  1076             return elemtype.isRaw();
  1079         public ArrayType makeVarargs() {
  1080             return new ArrayType(elemtype, tsym) {
  1081                 @Override
  1082                 public boolean isVarargs() {
  1083                     return true;
  1085             };
  1088         public Type map(Mapping f) {
  1089             Type elemtype1 = f.apply(elemtype);
  1090             if (elemtype1 == elemtype) return this;
  1091             else return new ArrayType(elemtype1, tsym);
  1094         public boolean contains(Type elem) {
  1095             return elem == this || elemtype.contains(elem);
  1098         public void complete() {
  1099             elemtype.complete();
  1102         public Type getComponentType() {
  1103             return elemtype;
  1106         public TypeKind getKind() {
  1107             return TypeKind.ARRAY;
  1110         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1111             return v.visitArray(this, p);
  1115     public static class MethodType extends Type implements ExecutableType {
  1117         public List<Type> argtypes;
  1118         public Type restype;
  1119         public List<Type> thrown;
  1121         /** The type annotations on the method receiver.
  1122          */
  1123         public Type recvtype;
  1125         public MethodType(List<Type> argtypes,
  1126                           Type restype,
  1127                           List<Type> thrown,
  1128                           TypeSymbol methodClass) {
  1129             super(methodClass);
  1130             this.argtypes = argtypes;
  1131             this.restype = restype;
  1132             this.thrown = thrown;
  1135         @Override
  1136         public TypeTag getTag() {
  1137             return METHOD;
  1140         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1141             return v.visitMethodType(this, s);
  1144         /** The Java source which this type represents.
  1146          *  XXX 06/09/99 iris This isn't correct Java syntax, but it probably
  1147          *  should be.
  1148          */
  1149         public String toString() {
  1150             return "(" + argtypes + ")" + restype;
  1153         public List<Type>        getParameterTypes() { return argtypes; }
  1154         public Type              getReturnType()     { return restype; }
  1155         public Type              getReceiverType()   { return recvtype; }
  1156         public List<Type>        getThrownTypes()    { return thrown; }
  1158         public boolean isErroneous() {
  1159             return
  1160                 isErroneous(argtypes) ||
  1161                 restype != null && restype.isErroneous();
  1164         public Type map(Mapping f) {
  1165             List<Type> argtypes1 = map(argtypes, f);
  1166             Type restype1 = f.apply(restype);
  1167             List<Type> thrown1 = map(thrown, f);
  1168             if (argtypes1 == argtypes &&
  1169                 restype1 == restype &&
  1170                 thrown1 == thrown) return this;
  1171             else return new MethodType(argtypes1, restype1, thrown1, tsym);
  1174         public boolean contains(Type elem) {
  1175             return elem == this || contains(argtypes, elem) || restype.contains(elem) || contains(thrown, elem);
  1178         public MethodType asMethodType() { return this; }
  1180         public void complete() {
  1181             for (List<Type> l = argtypes; l.nonEmpty(); l = l.tail)
  1182                 l.head.complete();
  1183             restype.complete();
  1184             recvtype.complete();
  1185             for (List<Type> l = thrown; l.nonEmpty(); l = l.tail)
  1186                 l.head.complete();
  1189         public List<TypeVar> getTypeVariables() {
  1190             return List.nil();
  1193         public TypeSymbol asElement() {
  1194             return null;
  1197         public TypeKind getKind() {
  1198             return TypeKind.EXECUTABLE;
  1201         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1202             return v.visitExecutable(this, p);
  1206     public static class PackageType extends Type implements NoType {
  1208         PackageType(TypeSymbol tsym) {
  1209             super(tsym);
  1212         @Override
  1213         public TypeTag getTag() {
  1214             return PACKAGE;
  1217         @Override
  1218         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1219             return v.visitPackageType(this, s);
  1222         public String toString() {
  1223             return tsym.getQualifiedName().toString();
  1226         public TypeKind getKind() {
  1227             return TypeKind.PACKAGE;
  1230         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1231             return v.visitNoType(this, p);
  1235     public static class TypeVar extends Type implements TypeVariable {
  1237         /** The upper bound of this type variable; set from outside.
  1238          *  Must be nonempty once it is set.
  1239          *  For a bound, `bound' is the bound type itself.
  1240          *  Multiple bounds are expressed as a single class type which has the
  1241          *  individual bounds as superclass, respectively interfaces.
  1242          *  The class type then has as `tsym' a compiler generated class `c',
  1243          *  which has a flag COMPOUND and whose owner is the type variable
  1244          *  itself. Furthermore, the erasure_field of the class
  1245          *  points to the first class or interface bound.
  1246          */
  1247         public Type bound = null;
  1249         /** The lower bound of this type variable.
  1250          *  TypeVars don't normally have a lower bound, so it is normally set
  1251          *  to syms.botType.
  1252          *  Subtypes, such as CapturedType, may provide a different value.
  1253          */
  1254         public Type lower;
  1256         public TypeVar(Name name, Symbol owner, Type lower) {
  1257             super(null);
  1258             tsym = new TypeVariableSymbol(0, name, this, owner);
  1259             this.lower = lower;
  1262         public TypeVar(TypeSymbol tsym, Type bound, Type lower) {
  1263             super(tsym);
  1264             this.bound = bound;
  1265             this.lower = lower;
  1268         @Override
  1269         public TypeTag getTag() {
  1270             return TYPEVAR;
  1273         @Override
  1274         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1275             return v.visitTypeVar(this, s);
  1278         @Override
  1279         public Type getUpperBound() {
  1280             if ((bound == null || bound.hasTag(NONE)) && this != tsym.type) {
  1281                 bound = tsym.type.getUpperBound();
  1283             return bound;
  1286         int rank_field = -1;
  1288         @Override
  1289         public Type getLowerBound() {
  1290             return lower;
  1293         public TypeKind getKind() {
  1294             return TypeKind.TYPEVAR;
  1297         public boolean isCaptured() {
  1298             return false;
  1301         @Override
  1302         public boolean isReference() {
  1303             return true;
  1306         @Override
  1307         public boolean isNullOrReference() {
  1308             return true;
  1311         @Override
  1312         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1313             return v.visitTypeVariable(this, p);
  1317     /** A captured type variable comes from wildcards which can have
  1318      *  both upper and lower bound.  CapturedType extends TypeVar with
  1319      *  a lower bound.
  1320      */
  1321     public static class CapturedType extends TypeVar {
  1323         public WildcardType wildcard;
  1325         public CapturedType(Name name,
  1326                             Symbol owner,
  1327                             Type upper,
  1328                             Type lower,
  1329                             WildcardType wildcard) {
  1330             super(name, owner, lower);
  1331             this.lower = Assert.checkNonNull(lower);
  1332             this.bound = upper;
  1333             this.wildcard = wildcard;
  1336         @Override
  1337         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1338             return v.visitCapturedType(this, s);
  1341         @Override
  1342         public boolean isCaptured() {
  1343             return true;
  1346         @Override
  1347         public String toString() {
  1348             return "capture#"
  1349                 + (hashCode() & 0xFFFFFFFFL) % Printer.PRIME
  1350                 + " of "
  1351                 + wildcard;
  1355     public static abstract class DelegatedType extends Type {
  1356         public Type qtype;
  1357         public TypeTag tag;
  1358         public DelegatedType(TypeTag tag, Type qtype) {
  1359             super(qtype.tsym);
  1360             this.tag = tag;
  1361             this.qtype = qtype;
  1363         public TypeTag getTag() { return tag; }
  1364         public String toString() { return qtype.toString(); }
  1365         public List<Type> getTypeArguments() { return qtype.getTypeArguments(); }
  1366         public Type getEnclosingType() { return qtype.getEnclosingType(); }
  1367         public List<Type> getParameterTypes() { return qtype.getParameterTypes(); }
  1368         public Type getReturnType() { return qtype.getReturnType(); }
  1369         public Type getReceiverType() { return qtype.getReceiverType(); }
  1370         public List<Type> getThrownTypes() { return qtype.getThrownTypes(); }
  1371         public List<Type> allparams() { return qtype.allparams(); }
  1372         public Type getUpperBound() { return qtype.getUpperBound(); }
  1373         public boolean isErroneous() { return qtype.isErroneous(); }
  1376     /**
  1377      * The type of a generic method type. It consists of a method type and
  1378      * a list of method type-parameters that are used within the method
  1379      * type.
  1380      */
  1381     public static class ForAll extends DelegatedType implements ExecutableType {
  1382         public List<Type> tvars;
  1384         public ForAll(List<Type> tvars, Type qtype) {
  1385             super(FORALL, (MethodType)qtype);
  1386             this.tvars = tvars;
  1389         @Override
  1390         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1391             return v.visitForAll(this, s);
  1394         public String toString() {
  1395             return "<" + tvars + ">" + qtype;
  1398         public List<Type> getTypeArguments()   { return tvars; }
  1400         public boolean isErroneous()  {
  1401             return qtype.isErroneous();
  1404         public Type map(Mapping f) {
  1405             return f.apply(qtype);
  1408         public boolean contains(Type elem) {
  1409             return qtype.contains(elem);
  1412         public MethodType asMethodType() {
  1413             return (MethodType)qtype;
  1416         public void complete() {
  1417             for (List<Type> l = tvars; l.nonEmpty(); l = l.tail) {
  1418                 ((TypeVar)l.head).bound.complete();
  1420             qtype.complete();
  1423         public List<TypeVar> getTypeVariables() {
  1424             return List.convert(TypeVar.class, getTypeArguments());
  1427         public TypeKind getKind() {
  1428             return TypeKind.EXECUTABLE;
  1431         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1432             return v.visitExecutable(this, p);
  1436     /** A class for inference variables, for use during method/diamond type
  1437      *  inference. An inference variable has upper/lower bounds and a set
  1438      *  of equality constraints. Such bounds are set during subtyping, type-containment,
  1439      *  type-equality checks, when the types being tested contain inference variables.
  1440      *  A change listener can be attached to an inference variable, to receive notifications
  1441      *  whenever the bounds of an inference variable change.
  1442      */
  1443     public static class UndetVar extends DelegatedType {
  1445         /** Inference variable change listener. The listener method is called
  1446          *  whenever a change to the inference variable's bounds occurs
  1447          */
  1448         public interface UndetVarListener {
  1449             /** called when some inference variable bounds (of given kinds ibs) change */
  1450             void varChanged(UndetVar uv, Set<InferenceBound> ibs);
  1453         /**
  1454          * Inference variable bound kinds
  1455          */
  1456         public enum InferenceBound {
  1457             /** upper bounds */
  1458             UPPER,
  1459             /** lower bounds */
  1460             LOWER,
  1461             /** equality constraints */
  1462             EQ;
  1465         /** inference variable bounds */
  1466         protected Map<InferenceBound, List<Type>> bounds;
  1468         /** inference variable's inferred type (set from Infer.java) */
  1469         public Type inst = null;
  1471         /** number of declared (upper) bounds */
  1472         public int declaredCount;
  1474         /** inference variable's change listener */
  1475         public UndetVarListener listener = null;
  1477         @Override
  1478         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1479             return v.visitUndetVar(this, s);
  1482         public UndetVar(TypeVar origin, Types types) {
  1483             super(UNDETVAR, origin);
  1484             bounds = new EnumMap<InferenceBound, List<Type>>(InferenceBound.class);
  1485             List<Type> declaredBounds = types.getBounds(origin);
  1486             declaredCount = declaredBounds.length();
  1487             bounds.put(InferenceBound.UPPER, declaredBounds);
  1488             bounds.put(InferenceBound.LOWER, List.<Type>nil());
  1489             bounds.put(InferenceBound.EQ, List.<Type>nil());
  1492         public String toString() {
  1493             if (inst != null) return inst.toString();
  1494             else return qtype + "?";
  1497         @Override
  1498         public boolean isPartial() {
  1499             return true;
  1502         @Override
  1503         public Type baseType() {
  1504             if (inst != null) return inst.baseType();
  1505             else return this;
  1508         /** get all bounds of a given kind */
  1509         public List<Type> getBounds(InferenceBound... ibs) {
  1510             ListBuffer<Type> buf = new ListBuffer<>();
  1511             for (InferenceBound ib : ibs) {
  1512                 buf.appendList(bounds.get(ib));
  1514             return buf.toList();
  1517         /** get the list of declared (upper) bounds */
  1518         public List<Type> getDeclaredBounds() {
  1519             ListBuffer<Type> buf = new ListBuffer<>();
  1520             int count = 0;
  1521             for (Type b : getBounds(InferenceBound.UPPER)) {
  1522                 if (count++ == declaredCount) break;
  1523                 buf.append(b);
  1525             return buf.toList();
  1528         /** internal method used to override an undetvar bounds */
  1529         public void setBounds(InferenceBound ib, List<Type> newBounds) {
  1530             bounds.put(ib, newBounds);
  1533         /** add a bound of a given kind - this might trigger listener notification */
  1534         public final void addBound(InferenceBound ib, Type bound, Types types) {
  1535             addBound(ib, bound, types, false);
  1538         protected void addBound(InferenceBound ib, Type bound, Types types, boolean update) {
  1539             Type bound2 = toTypeVarMap.apply(bound).baseType();
  1540             List<Type> prevBounds = bounds.get(ib);
  1541             for (Type b : prevBounds) {
  1542                 //check for redundancy - use strict version of isSameType on tvars
  1543                 //(as the standard version will lead to false positives w.r.t. clones ivars)
  1544                 if (types.isSameType(b, bound2, true) || bound == qtype) return;
  1546             bounds.put(ib, prevBounds.prepend(bound2));
  1547             notifyChange(EnumSet.of(ib));
  1549         //where
  1550             Type.Mapping toTypeVarMap = new Mapping("toTypeVarMap") {
  1551                 @Override
  1552                 public Type apply(Type t) {
  1553                     if (t.hasTag(UNDETVAR)) {
  1554                         UndetVar uv = (UndetVar)t;
  1555                         return uv.inst != null ? uv.inst : uv.qtype;
  1556                     } else {
  1557                         return t.map(this);
  1560             };
  1562         /** replace types in all bounds - this might trigger listener notification */
  1563         public void substBounds(List<Type> from, List<Type> to, Types types) {
  1564             List<Type> instVars = from.diff(to);
  1565             //if set of instantiated ivars is empty, there's nothing to do!
  1566             if (instVars.isEmpty()) return;
  1567             final EnumSet<InferenceBound> boundsChanged = EnumSet.noneOf(InferenceBound.class);
  1568             UndetVarListener prevListener = listener;
  1569             try {
  1570                 //setup new listener for keeping track of changed bounds
  1571                 listener = new UndetVarListener() {
  1572                     public void varChanged(UndetVar uv, Set<InferenceBound> ibs) {
  1573                         boundsChanged.addAll(ibs);
  1575                 };
  1576                 for (Map.Entry<InferenceBound, List<Type>> _entry : bounds.entrySet()) {
  1577                     InferenceBound ib = _entry.getKey();
  1578                     List<Type> prevBounds = _entry.getValue();
  1579                     ListBuffer<Type> newBounds = new ListBuffer<>();
  1580                     ListBuffer<Type> deps = new ListBuffer<>();
  1581                     //step 1 - re-add bounds that are not dependent on ivars
  1582                     for (Type t : prevBounds) {
  1583                         if (!t.containsAny(instVars)) {
  1584                             newBounds.append(t);
  1585                         } else {
  1586                             deps.append(t);
  1589                     //step 2 - replace bounds
  1590                     bounds.put(ib, newBounds.toList());
  1591                     //step 3 - for each dependency, add new replaced bound
  1592                     for (Type dep : deps) {
  1593                         addBound(ib, types.subst(dep, from, to), types, true);
  1596             } finally {
  1597                 listener = prevListener;
  1598                 if (!boundsChanged.isEmpty()) {
  1599                     notifyChange(boundsChanged);
  1604         private void notifyChange(EnumSet<InferenceBound> ibs) {
  1605             if (listener != null) {
  1606                 listener.varChanged(this, ibs);
  1610         public boolean isCaptured() {
  1611             return false;
  1615     /**
  1616      * This class is used to represent synthetic captured inference variables
  1617      * that can be generated during nested generic method calls. The only difference
  1618      * between these inference variables and ordinary ones is that captured inference
  1619      * variables cannot get new bounds through incorporation.
  1620      */
  1621     public static class CapturedUndetVar extends UndetVar {
  1623         public CapturedUndetVar(CapturedType origin, Types types) {
  1624             super(origin, types);
  1625             if (!origin.lower.hasTag(BOT)) {
  1626                 bounds.put(InferenceBound.LOWER, List.of(origin.lower));
  1630         @Override
  1631         public void addBound(InferenceBound ib, Type bound, Types types, boolean update) {
  1632             if (update) {
  1633                 //only change bounds if request comes from substBounds
  1634                 super.addBound(ib, bound, types, update);
  1638         @Override
  1639         public boolean isCaptured() {
  1640             return true;
  1644     /** Represents NONE.
  1645      */
  1646     public static class JCNoType extends Type implements NoType {
  1647         public JCNoType() {
  1648             super(null);
  1651         @Override
  1652         public TypeTag getTag() {
  1653             return NONE;
  1656         @Override
  1657         public TypeKind getKind() {
  1658             return TypeKind.NONE;
  1661         @Override
  1662         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1663             return v.visitNoType(this, p);
  1666         @Override
  1667         public boolean isCompound() { return false; }
  1670     /** Represents VOID.
  1671      */
  1672     public static class JCVoidType extends Type implements NoType {
  1674         public JCVoidType() {
  1675             super(null);
  1678         @Override
  1679         public TypeTag getTag() {
  1680             return VOID;
  1683         @Override
  1684         public TypeKind getKind() {
  1685             return TypeKind.VOID;
  1688         @Override
  1689         public boolean isCompound() { return false; }
  1691         @Override
  1692         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1693             return v.visitNoType(this, p);
  1696         @Override
  1697         public boolean isPrimitiveOrVoid() {
  1698             return true;
  1702     static class BottomType extends Type implements NullType {
  1703         public BottomType() {
  1704             super(null);
  1707         @Override
  1708         public TypeTag getTag() {
  1709             return BOT;
  1712         @Override
  1713         public TypeKind getKind() {
  1714             return TypeKind.NULL;
  1717         @Override
  1718         public boolean isCompound() { return false; }
  1720         @Override
  1721         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1722             return v.visitNull(this, p);
  1725         @Override
  1726         public Type constType(Object value) {
  1727             return this;
  1730         @Override
  1731         public String stringValue() {
  1732             return "null";
  1735         @Override
  1736         public boolean isNullOrReference() {
  1737             return true;
  1742     public static class ErrorType extends ClassType
  1743             implements javax.lang.model.type.ErrorType {
  1745         private Type originalType = null;
  1747         public ErrorType(Type originalType, TypeSymbol tsym) {
  1748             super(noType, List.<Type>nil(), null);
  1749             this.tsym = tsym;
  1750             this.originalType = (originalType == null ? noType : originalType);
  1753         public ErrorType(ClassSymbol c, Type originalType) {
  1754             this(originalType, c);
  1755             c.type = this;
  1756             c.kind = ERR;
  1757             c.members_field = new Scope.ErrorScope(c);
  1760         @Override
  1761         public TypeTag getTag() {
  1762             return ERROR;
  1765         @Override
  1766         public boolean isPartial() {
  1767             return true;
  1770         @Override
  1771         public boolean isReference() {
  1772             return true;
  1775         @Override
  1776         public boolean isNullOrReference() {
  1777             return true;
  1780         public ErrorType(Name name, TypeSymbol container, Type originalType) {
  1781             this(new ClassSymbol(PUBLIC|STATIC|ACYCLIC, name, null, container), originalType);
  1784         @Override
  1785         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1786             return v.visitErrorType(this, s);
  1789         public Type constType(Object constValue) { return this; }
  1790         public Type getEnclosingType()           { return this; }
  1791         public Type getReturnType()              { return this; }
  1792         public Type asSub(Symbol sym)            { return this; }
  1793         public Type map(Mapping f)               { return this; }
  1795         public boolean isGenType(Type t)         { return true; }
  1796         public boolean isErroneous()             { return true; }
  1797         public boolean isCompound()              { return false; }
  1798         public boolean isInterface()             { return false; }
  1800         public List<Type> allparams()            { return List.nil(); }
  1801         public List<Type> getTypeArguments()     { return List.nil(); }
  1803         public TypeKind getKind() {
  1804             return TypeKind.ERROR;
  1807         public Type getOriginalType() {
  1808             return originalType;
  1811         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1812             return v.visitError(this, p);
  1816     public static class AnnotatedType extends Type
  1817             implements
  1818                 javax.lang.model.type.ArrayType,
  1819                 javax.lang.model.type.DeclaredType,
  1820                 javax.lang.model.type.PrimitiveType,
  1821                 javax.lang.model.type.TypeVariable,
  1822                 javax.lang.model.type.WildcardType {
  1823         /** The type annotations on this type.
  1824          */
  1825         private List<Attribute.TypeCompound> typeAnnotations;
  1827         /** The underlying type that is annotated.
  1828          */
  1829         private Type underlyingType;
  1831         protected AnnotatedType(List<Attribute.TypeCompound> typeAnnotations,
  1832                 Type underlyingType) {
  1833             super(underlyingType.tsym);
  1834             this.typeAnnotations = typeAnnotations;
  1835             this.underlyingType = underlyingType;
  1836             Assert.check(typeAnnotations != null && typeAnnotations.nonEmpty(),
  1837                     "Can't create AnnotatedType without annotations: " + underlyingType);
  1838             Assert.check(!underlyingType.isAnnotated(),
  1839                     "Can't annotate already annotated type: " + underlyingType +
  1840                     "; adding: " + typeAnnotations);
  1843         @Override
  1844         public TypeTag getTag() {
  1845             return underlyingType.getTag();
  1848         @Override
  1849         public boolean isAnnotated() {
  1850             return true;
  1853         @Override
  1854         public List<Attribute.TypeCompound> getAnnotationMirrors() {
  1855             return typeAnnotations;
  1859         @Override
  1860         public TypeKind getKind() {
  1861             return underlyingType.getKind();
  1864         @Override
  1865         public Type unannotatedType() {
  1866             return underlyingType;
  1869         @Override
  1870         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1871             return v.visitAnnotatedType(this, s);
  1874         @Override
  1875         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1876             return underlyingType.accept(v, p);
  1879         @Override
  1880         public Type map(Mapping f) {
  1881             underlyingType.map(f);
  1882             return this;
  1885         @Override
  1886         public Type constType(Object constValue) { return underlyingType.constType(constValue); }
  1887         @Override
  1888         public Type getEnclosingType()           { return underlyingType.getEnclosingType(); }
  1890         @Override
  1891         public Type getReturnType()              { return underlyingType.getReturnType(); }
  1892         @Override
  1893         public List<Type> getTypeArguments()     { return underlyingType.getTypeArguments(); }
  1894         @Override
  1895         public List<Type> getParameterTypes()    { return underlyingType.getParameterTypes(); }
  1896         @Override
  1897         public Type getReceiverType()            { return underlyingType.getReceiverType(); }
  1898         @Override
  1899         public List<Type> getThrownTypes()       { return underlyingType.getThrownTypes(); }
  1900         @Override
  1901         public Type getUpperBound()              { return underlyingType.getUpperBound(); }
  1902         @Override
  1903         public Type getLowerBound()              { return underlyingType.getLowerBound(); }
  1905         @Override
  1906         public boolean isErroneous()             { return underlyingType.isErroneous(); }
  1907         @Override
  1908         public boolean isCompound()              { return underlyingType.isCompound(); }
  1909         @Override
  1910         public boolean isInterface()             { return underlyingType.isInterface(); }
  1911         @Override
  1912         public List<Type> allparams()            { return underlyingType.allparams(); }
  1913         @Override
  1914         public boolean isPrimitive()             { return underlyingType.isPrimitive(); }
  1915         @Override
  1916         public boolean isPrimitiveOrVoid()       { return underlyingType.isPrimitiveOrVoid(); }
  1917         @Override
  1918         public boolean isNumeric()               { return underlyingType.isNumeric(); }
  1919         @Override
  1920         public boolean isReference()             { return underlyingType.isReference(); }
  1921         @Override
  1922         public boolean isNullOrReference()       { return underlyingType.isNullOrReference(); }
  1923         @Override
  1924         public boolean isPartial()               { return underlyingType.isPartial(); }
  1925         @Override
  1926         public boolean isParameterized()         { return underlyingType.isParameterized(); }
  1927         @Override
  1928         public boolean isRaw()                   { return underlyingType.isRaw(); }
  1929         @Override
  1930         public boolean isFinal()                 { return underlyingType.isFinal(); }
  1931         @Override
  1932         public boolean isSuperBound()            { return underlyingType.isSuperBound(); }
  1933         @Override
  1934         public boolean isExtendsBound()          { return underlyingType.isExtendsBound(); }
  1935         @Override
  1936         public boolean isUnbound()               { return underlyingType.isUnbound(); }
  1938         @Override
  1939         public String toString() {
  1940             // This method is only used for internal debugging output.
  1941             // See
  1942             // com.sun.tools.javac.code.Printer.visitAnnotatedType(AnnotatedType, Locale)
  1943             // for the user-visible logic.
  1944             if (typeAnnotations != null &&
  1945                     !typeAnnotations.isEmpty()) {
  1946                 return "(" + typeAnnotations.toString() + " :: " + underlyingType.toString() + ")";
  1947             } else {
  1948                 return "({} :: " + underlyingType.toString() +")";
  1952         @Override
  1953         public boolean contains(Type t)          { return underlyingType.contains(t); }
  1955         @Override
  1956         public Type withTypeVar(Type t) {
  1957             // Don't create a new AnnotatedType, as 'this' will
  1958             // get its annotations set later.
  1959             underlyingType = underlyingType.withTypeVar(t);
  1960             return this;
  1963         // TODO: attach annotations?
  1964         @Override
  1965         public TypeSymbol asElement()            { return underlyingType.asElement(); }
  1967         // TODO: attach annotations?
  1968         @Override
  1969         public MethodType asMethodType()         { return underlyingType.asMethodType(); }
  1971         @Override
  1972         public void complete()                   { underlyingType.complete(); }
  1974         @Override
  1975         public TypeMirror getComponentType()     { return ((ArrayType)underlyingType).getComponentType(); }
  1977         // The result is an ArrayType, but only in the model sense, not the Type sense.
  1978         public Type makeVarargs() {
  1979             return ((ArrayType) underlyingType).makeVarargs().annotatedType(typeAnnotations);
  1982         @Override
  1983         public TypeMirror getExtendsBound()      { return ((WildcardType)underlyingType).getExtendsBound(); }
  1984         @Override
  1985         public TypeMirror getSuperBound()        { return ((WildcardType)underlyingType).getSuperBound(); }
  1988     public static class UnknownType extends Type {
  1990         public UnknownType() {
  1991             super(null);
  1994         @Override
  1995         public TypeTag getTag() {
  1996             return UNKNOWN;
  1999         @Override
  2000         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  2001             return v.visitUnknown(this, p);
  2004         @Override
  2005         public boolean isPartial() {
  2006             return true;
  2010     /**
  2011      * A visitor for types.  A visitor is used to implement operations
  2012      * (or relations) on types.  Most common operations on types are
  2013      * binary relations and this interface is designed for binary
  2014      * relations, that is, operations of the form
  2015      * Type&nbsp;&times;&nbsp;S&nbsp;&rarr;&nbsp;R.
  2016      * <!-- In plain text: Type x S -> R -->
  2018      * @param <R> the return type of the operation implemented by this
  2019      * visitor; use Void if no return type is needed.
  2020      * @param <S> the type of the second argument (the first being the
  2021      * type itself) of the operation implemented by this visitor; use
  2022      * Void if a second argument is not needed.
  2023      */
  2024     public interface Visitor<R,S> {
  2025         R visitClassType(ClassType t, S s);
  2026         R visitWildcardType(WildcardType t, S s);
  2027         R visitArrayType(ArrayType t, S s);
  2028         R visitMethodType(MethodType t, S s);
  2029         R visitPackageType(PackageType t, S s);
  2030         R visitTypeVar(TypeVar t, S s);
  2031         R visitCapturedType(CapturedType t, S s);
  2032         R visitForAll(ForAll t, S s);
  2033         R visitUndetVar(UndetVar t, S s);
  2034         R visitErrorType(ErrorType t, S s);
  2035         R visitAnnotatedType(AnnotatedType t, S s);
  2036         R visitType(Type t, S s);

mercurial