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

Sun, 17 Feb 2013 16:44:55 -0500

author
dholmes
date
Sun, 17 Feb 2013 16:44:55 -0500
changeset 1571
af8417e590f4
parent 1550
1df20330f6bd
child 1628
5ddecb91d843
permissions
-rw-r--r--

Merge

     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.util.Collections;
    29 import java.util.EnumMap;
    30 import java.util.EnumSet;
    31 import java.util.Map;
    32 import java.util.Set;
    34 import javax.lang.model.element.AnnotationMirror;
    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 class Type implements PrimitiveType {
    74     /** Constant type: no type at all. */
    75     public static final JCNoType noType = new JCNoType(NONE);
    77     /** Constant type: special type to be used during recovery of deferred expressions. */
    78     public static final JCNoType recoveryType = new JCNoType(NONE);
    80     /** If this switch is turned on, the names of type variables
    81      *  and anonymous classes are printed with hashcodes appended.
    82      */
    83     public static boolean moreInfo = false;
    85     /** The tag of this type.
    86      *
    87      *  @see TypeTag
    88      */
    89     protected TypeTag tag;
    91     /** The defining class / interface / package / type variable.
    92      */
    93     public TypeSymbol tsym;
    95     /**
    96      * Checks if the current type tag is equal to the given tag.
    97      * @return true if tag is equal to the current type tag.
    98      */
    99     public boolean hasTag(TypeTag tag) {
   100         return this.tag == tag;
   101     }
   103     /**
   104      * Returns the current type tag.
   105      * @return the value of the current type tag.
   106      */
   107     public TypeTag getTag() {
   108         return tag;
   109     }
   111     public boolean isNumeric() {
   112         switch (tag) {
   113             case BYTE: case CHAR:
   114             case SHORT:
   115             case INT: case LONG:
   116             case FLOAT: case DOUBLE:
   117                 return true;
   118             default:
   119                 return false;
   120         }
   121     }
   123     public boolean isPrimitive() {
   124         return (isNumeric() || tag == BOOLEAN);
   125     }
   127     public boolean isPrimitiveOrVoid() {
   128         return (isPrimitive() || tag == VOID);
   129     }
   131     public boolean isReference() {
   132         switch (tag) {
   133         case CLASS:
   134         case ARRAY:
   135         case TYPEVAR:
   136         case WILDCARD:
   137         case ERROR:
   138             return true;
   139         default:
   140             return false;
   141         }
   142     }
   144     public boolean isNullOrReference() {
   145         return (tag == BOT || isReference());
   146     }
   148     public boolean isPartial() {
   149         switch(tag) {
   150             case ERROR: case UNKNOWN: case UNDETVAR:
   151                 return true;
   152             default:
   153                 return false;
   154         }
   155     }
   157     /**
   158      * The constant value of this type, null if this type does not
   159      * have a constant value attribute. Only primitive types and
   160      * strings (ClassType) can have a constant value attribute.
   161      * @return the constant value attribute of this type
   162      */
   163     public Object constValue() {
   164         return null;
   165     }
   167     /**
   168      * Get the representation of this type used for modelling purposes.
   169      * By default, this is itself. For ErrorType, a different value
   170      * may be provided.
   171      */
   172     public Type getModelType() {
   173         return this;
   174     }
   176     public static List<Type> getModelTypes(List<Type> ts) {
   177         ListBuffer<Type> lb = new ListBuffer<Type>();
   178         for (Type t: ts)
   179             lb.append(t.getModelType());
   180         return lb.toList();
   181     }
   183     public <R,S> R accept(Type.Visitor<R,S> v, S s) { return v.visitType(this, s); }
   185     /** Define a type given its tag and type symbol
   186      */
   187     public Type(TypeTag tag, TypeSymbol tsym) {
   188         this.tag = tag;
   189         this.tsym = tsym;
   190     }
   192     /** An abstract class for mappings from types to types
   193      */
   194     public static abstract class Mapping {
   195         private String name;
   196         public Mapping(String name) {
   197             this.name = name;
   198         }
   199         public abstract Type apply(Type t);
   200         public String toString() {
   201             return name;
   202         }
   203     }
   205     /** map a type function over all immediate descendants of this type
   206      */
   207     public Type map(Mapping f) {
   208         return this;
   209     }
   211     /** map a type function over a list of types
   212      */
   213     public static List<Type> map(List<Type> ts, Mapping f) {
   214         if (ts.nonEmpty()) {
   215             List<Type> tail1 = map(ts.tail, f);
   216             Type t = f.apply(ts.head);
   217             if (tail1 != ts.tail || t != ts.head)
   218                 return tail1.prepend(t);
   219         }
   220         return ts;
   221     }
   223     /** Define a constant type, of the same kind as this type
   224      *  and with given constant value
   225      */
   226     public Type constType(Object constValue) {
   227         final Object value = constValue;
   228         Assert.check(isPrimitive());
   229         return new Type(tag, tsym) {
   230                 @Override
   231                 public Object constValue() {
   232                     return value;
   233                 }
   234                 @Override
   235                 public Type baseType() {
   236                     return tsym.type;
   237                 }
   238             };
   239     }
   241     /**
   242      * If this is a constant type, return its underlying type.
   243      * Otherwise, return the type itself.
   244      */
   245     public Type baseType() {
   246         return this;
   247     }
   249     /**
   250      * If this is an annotated type, return the underlying type.
   251      * Otherwise, return the type itself.
   252      */
   253     public Type unannotatedType() {
   254         return this;
   255     }
   257     /** Return the base types of a list of types.
   258      */
   259     public static List<Type> baseTypes(List<Type> ts) {
   260         if (ts.nonEmpty()) {
   261             Type t = ts.head.baseType();
   262             List<Type> baseTypes = baseTypes(ts.tail);
   263             if (t != ts.head || baseTypes != ts.tail)
   264                 return baseTypes.prepend(t);
   265         }
   266         return ts;
   267     }
   269     /** The Java source which this type represents.
   270      */
   271     public String toString() {
   272         String s = (tsym == null || tsym.name == null)
   273             ? "<none>"
   274             : tsym.name.toString();
   275         if (moreInfo && tag == TYPEVAR) s = s + hashCode();
   276         return s;
   277     }
   279     /**
   280      * The Java source which this type list represents.  A List is
   281      * represented as a comma-spearated listing of the elements in
   282      * that list.
   283      */
   284     public static String toString(List<Type> ts) {
   285         if (ts.isEmpty()) {
   286             return "";
   287         } else {
   288             StringBuilder buf = new StringBuilder();
   289             buf.append(ts.head.toString());
   290             for (List<Type> l = ts.tail; l.nonEmpty(); l = l.tail)
   291                 buf.append(",").append(l.head.toString());
   292             return buf.toString();
   293         }
   294     }
   296     /**
   297      * The constant value of this type, converted to String
   298      */
   299     public String stringValue() {
   300         Object cv = Assert.checkNonNull(constValue());
   301         if (tag == BOOLEAN)
   302             return ((Integer) cv).intValue() == 0 ? "false" : "true";
   303         else if (tag == CHAR)
   304             return String.valueOf((char) ((Integer) cv).intValue());
   305         else
   306             return cv.toString();
   307     }
   309     /**
   310      * This method is analogous to isSameType, but weaker, since we
   311      * never complete classes. Where isSameType would complete a
   312      * class, equals assumes that the two types are different.
   313      */
   314     @Override
   315     public boolean equals(Object t) {
   316         return super.equals(t);
   317     }
   319     @Override
   320     public int hashCode() {
   321         return super.hashCode();
   322     }
   324     /** Is this a constant type whose value is false?
   325      */
   326     public boolean isFalse() {
   327         return
   328             tag == BOOLEAN &&
   329             constValue() != null &&
   330             ((Integer)constValue()).intValue() == 0;
   331     }
   333     /** Is this a constant type whose value is true?
   334      */
   335     public boolean isTrue() {
   336         return
   337             tag == BOOLEAN &&
   338             constValue() != null &&
   339             ((Integer)constValue()).intValue() != 0;
   340     }
   342     public String argtypes(boolean varargs) {
   343         List<Type> args = getParameterTypes();
   344         if (!varargs) return args.toString();
   345         StringBuilder buf = new StringBuilder();
   346         while (args.tail.nonEmpty()) {
   347             buf.append(args.head);
   348             args = args.tail;
   349             buf.append(',');
   350         }
   351         if (args.head.unannotatedType().tag == ARRAY) {
   352             buf.append(((ArrayType)args.head.unannotatedType()).elemtype);
   353             if (args.head.getAnnotations().nonEmpty()) {
   354                 buf.append(args.head.getAnnotations());
   355             }
   356             buf.append("...");
   357         } else {
   358             buf.append(args.head);
   359         }
   360         return buf.toString();
   361     }
   363     /** Access methods.
   364      */
   365     public List<? extends AnnotationMirror> getAnnotations() { return List.nil(); }
   366     public List<Type>        getTypeArguments()  { return List.nil(); }
   367     public Type              getEnclosingType()  { return null; }
   368     public List<Type>        getParameterTypes() { return List.nil(); }
   369     public Type              getReturnType()     { return null; }
   370     public Type              getReceiverType()   { return null; }
   371     public List<Type>        getThrownTypes()    { return List.nil(); }
   372     public Type              getUpperBound()     { return null; }
   373     public Type              getLowerBound()     { return null; }
   375     /** Navigation methods, these will work for classes, type variables,
   376      *  foralls, but will return null for arrays and methods.
   377      */
   379    /** Return all parameters of this type and all its outer types in order
   380     *  outer (first) to inner (last).
   381     */
   382     public List<Type> allparams() { return List.nil(); }
   384     /** Does this type contain "error" elements?
   385      */
   386     public boolean isErroneous() {
   387         return false;
   388     }
   390     public static boolean isErroneous(List<Type> ts) {
   391         for (List<Type> l = ts; l.nonEmpty(); l = l.tail)
   392             if (l.head.isErroneous()) return true;
   393         return false;
   394     }
   396     /** Is this type parameterized?
   397      *  A class type is parameterized if it has some parameters.
   398      *  An array type is parameterized if its element type is parameterized.
   399      *  All other types are not parameterized.
   400      */
   401     public boolean isParameterized() {
   402         return false;
   403     }
   405     /** Is this type a raw type?
   406      *  A class type is a raw type if it misses some of its parameters.
   407      *  An array type is a raw type if its element type is raw.
   408      *  All other types are not raw.
   409      *  Type validation will ensure that the only raw types
   410      *  in a program are types that miss all their type variables.
   411      */
   412     public boolean isRaw() {
   413         return false;
   414     }
   416     public boolean isCompound() {
   417         return tsym.completer == null
   418             // Compound types can't have a completer.  Calling
   419             // flags() will complete the symbol causing the
   420             // compiler to load classes unnecessarily.  This led
   421             // to regression 6180021.
   422             && (tsym.flags() & COMPOUND) != 0;
   423     }
   425     public boolean isInterface() {
   426         return (tsym.flags() & INTERFACE) != 0;
   427     }
   429     public boolean isFinal() {
   430         return (tsym.flags() & FINAL) != 0;
   431     }
   433     /**
   434      * Does this type contain occurrences of type t?
   435      */
   436     public boolean contains(Type t) {
   437         return t == this;
   438     }
   440     public static boolean contains(List<Type> ts, Type t) {
   441         for (List<Type> l = ts;
   442              l.tail != null /*inlined: l.nonEmpty()*/;
   443              l = l.tail)
   444             if (l.head.contains(t)) return true;
   445         return false;
   446     }
   448     /** Does this type contain an occurrence of some type in 'ts'?
   449      */
   450     public boolean containsAny(List<Type> ts) {
   451         for (Type t : ts)
   452             if (this.contains(t)) return true;
   453         return false;
   454     }
   456     public static boolean containsAny(List<Type> ts1, List<Type> ts2) {
   457         for (Type t : ts1)
   458             if (t.containsAny(ts2)) return true;
   459         return false;
   460     }
   462     public static List<Type> filter(List<Type> ts, Filter<Type> tf) {
   463         ListBuffer<Type> buf = ListBuffer.lb();
   464         for (Type t : ts) {
   465             if (tf.accepts(t)) {
   466                 buf.append(t);
   467             }
   468         }
   469         return buf.toList();
   470     }
   472     public boolean isSuperBound() { return false; }
   473     public boolean isExtendsBound() { return false; }
   474     public boolean isUnbound() { return false; }
   475     public Type withTypeVar(Type t) { return this; }
   477     /** The underlying method type of this type.
   478      */
   479     public MethodType asMethodType() { throw new AssertionError(); }
   481     /** Complete loading all classes in this type.
   482      */
   483     public void complete() {}
   485     public TypeSymbol asElement() {
   486         return tsym;
   487     }
   489     public TypeKind getKind() {
   490         switch (tag) {
   491         case BYTE:      return TypeKind.BYTE;
   492         case CHAR:      return TypeKind.CHAR;
   493         case SHORT:     return TypeKind.SHORT;
   494         case INT:       return TypeKind.INT;
   495         case LONG:      return TypeKind.LONG;
   496         case FLOAT:     return TypeKind.FLOAT;
   497         case DOUBLE:    return TypeKind.DOUBLE;
   498         case BOOLEAN:   return TypeKind.BOOLEAN;
   499         case VOID:      return TypeKind.VOID;
   500         case BOT:       return TypeKind.NULL;
   501         case NONE:      return TypeKind.NONE;
   502         default:        return TypeKind.OTHER;
   503         }
   504     }
   506     public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   507         if (isPrimitive())
   508             return v.visitPrimitive(this, p);
   509         else
   510             throw new AssertionError();
   511     }
   513     public static class WildcardType extends Type
   514             implements javax.lang.model.type.WildcardType {
   516         public Type type;
   517         public BoundKind kind;
   518         public TypeVar bound;
   520         @Override
   521         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   522             return v.visitWildcardType(this, s);
   523         }
   525         public WildcardType(Type type, BoundKind kind, TypeSymbol tsym) {
   526             super(WILDCARD, tsym);
   527             this.type = Assert.checkNonNull(type);
   528             this.kind = kind;
   529         }
   530         public WildcardType(WildcardType t, TypeVar bound) {
   531             this(t.type, t.kind, t.tsym, bound);
   532         }
   534         public WildcardType(Type type, BoundKind kind, TypeSymbol tsym, TypeVar bound) {
   535             this(type, kind, tsym);
   536             this.bound = bound;
   537         }
   539         public boolean contains(Type t) {
   540             return kind != UNBOUND && type.contains(t);
   541         }
   543         public boolean isSuperBound() {
   544             return kind == SUPER ||
   545                 kind == UNBOUND;
   546         }
   547         public boolean isExtendsBound() {
   548             return kind == EXTENDS ||
   549                 kind == UNBOUND;
   550         }
   551         public boolean isUnbound() {
   552             return kind == UNBOUND;
   553         }
   555         public Type withTypeVar(Type t) {
   556             //-System.err.println(this+".withTypeVar("+t+");");//DEBUG
   557             if (bound == t)
   558                 return this;
   559             bound = (TypeVar)t;
   560             return this;
   561         }
   563         boolean isPrintingBound = false;
   564         public String toString() {
   565             StringBuilder s = new StringBuilder();
   566             s.append(kind.toString());
   567             if (kind != UNBOUND)
   568                 s.append(type);
   569             if (moreInfo && bound != null && !isPrintingBound)
   570                 try {
   571                     isPrintingBound = true;
   572                     s.append("{:").append(bound.bound).append(":}");
   573                 } finally {
   574                     isPrintingBound = false;
   575                 }
   576             return s.toString();
   577         }
   579         public Type map(Mapping f) {
   580             //- System.err.println("   (" + this + ").map(" + f + ")");//DEBUG
   581             Type t = type;
   582             if (t != null)
   583                 t = f.apply(t);
   584             if (t == type)
   585                 return this;
   586             else
   587                 return new WildcardType(t, kind, tsym, bound);
   588         }
   590         public Type getExtendsBound() {
   591             if (kind == EXTENDS)
   592                 return type;
   593             else
   594                 return null;
   595         }
   597         public Type getSuperBound() {
   598             if (kind == SUPER)
   599                 return type;
   600             else
   601                 return null;
   602         }
   604         public TypeKind getKind() {
   605             return TypeKind.WILDCARD;
   606         }
   608         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   609             return v.visitWildcard(this, p);
   610         }
   611     }
   613     public static class ClassType extends Type implements DeclaredType {
   615         /** The enclosing type of this type. If this is the type of an inner
   616          *  class, outer_field refers to the type of its enclosing
   617          *  instance class, in all other cases it refers to noType.
   618          */
   619         private Type outer_field;
   621         /** The type parameters of this type (to be set once class is loaded).
   622          */
   623         public List<Type> typarams_field;
   625         /** A cache variable for the type parameters of this type,
   626          *  appended to all parameters of its enclosing class.
   627          *  @see #allparams
   628          */
   629         public List<Type> allparams_field;
   631         /** The supertype of this class (to be set once class is loaded).
   632          */
   633         public Type supertype_field;
   635         /** The interfaces of this class (to be set once class is loaded).
   636          */
   637         public List<Type> interfaces_field;
   639         /** All the interfaces of this class, including missing ones.
   640          */
   641         public List<Type> all_interfaces_field;
   643         public ClassType(Type outer, List<Type> typarams, TypeSymbol tsym) {
   644             super(CLASS, tsym);
   645             this.outer_field = outer;
   646             this.typarams_field = typarams;
   647             this.allparams_field = null;
   648             this.supertype_field = null;
   649             this.interfaces_field = null;
   650             /*
   651             // this can happen during error recovery
   652             assert
   653                 outer.isParameterized() ?
   654                 typarams.length() == tsym.type.typarams().length() :
   655                 outer.isRaw() ?
   656                 typarams.length() == 0 :
   657                 true;
   658             */
   659         }
   661         @Override
   662         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   663             return v.visitClassType(this, s);
   664         }
   666         public Type constType(Object constValue) {
   667             final Object value = constValue;
   668             return new ClassType(getEnclosingType(), typarams_field, tsym) {
   669                     @Override
   670                     public Object constValue() {
   671                         return value;
   672                     }
   673                     @Override
   674                     public Type baseType() {
   675                         return tsym.type;
   676                     }
   677                 };
   678         }
   680         /** The Java source which this type represents.
   681          */
   682         public String toString() {
   683             StringBuilder buf = new StringBuilder();
   684             if (getEnclosingType().tag == CLASS && tsym.owner.kind == TYP) {
   685                 buf.append(getEnclosingType().toString());
   686                 buf.append(".");
   687                 buf.append(className(tsym, false));
   688             } else {
   689                 buf.append(className(tsym, true));
   690             }
   691             if (getTypeArguments().nonEmpty()) {
   692                 buf.append('<');
   693                 buf.append(getTypeArguments().toString());
   694                 buf.append(">");
   695             }
   696             return buf.toString();
   697         }
   698 //where
   699             private String className(Symbol sym, boolean longform) {
   700                 if (sym.name.isEmpty() && (sym.flags() & COMPOUND) != 0) {
   701                     StringBuilder s = new StringBuilder(supertype_field.toString());
   702                     for (List<Type> is=interfaces_field; is.nonEmpty(); is = is.tail) {
   703                         s.append("&");
   704                         s.append(is.head.toString());
   705                     }
   706                     return s.toString();
   707                 } else if (sym.name.isEmpty()) {
   708                     String s;
   709                     ClassType norm = (ClassType) tsym.type;
   710                     if (norm == null) {
   711                         s = Log.getLocalizedString("anonymous.class", (Object)null);
   712                     } else if (norm.interfaces_field != null && norm.interfaces_field.nonEmpty()) {
   713                         s = Log.getLocalizedString("anonymous.class",
   714                                                    norm.interfaces_field.head);
   715                     } else {
   716                         s = Log.getLocalizedString("anonymous.class",
   717                                                    norm.supertype_field);
   718                     }
   719                     if (moreInfo)
   720                         s += String.valueOf(sym.hashCode());
   721                     return s;
   722                 } else if (longform) {
   723                     return sym.getQualifiedName().toString();
   724                 } else {
   725                     return sym.name.toString();
   726                 }
   727             }
   729         public List<Type> getTypeArguments() {
   730             if (typarams_field == null) {
   731                 complete();
   732                 if (typarams_field == null)
   733                     typarams_field = List.nil();
   734             }
   735             return typarams_field;
   736         }
   738         public boolean hasErasedSupertypes() {
   739             return isRaw();
   740         }
   742         public Type getEnclosingType() {
   743             return outer_field;
   744         }
   746         public void setEnclosingType(Type outer) {
   747             outer_field = outer;
   748         }
   750         public List<Type> allparams() {
   751             if (allparams_field == null) {
   752                 allparams_field = getTypeArguments().prependList(getEnclosingType().allparams());
   753             }
   754             return allparams_field;
   755         }
   757         public boolean isErroneous() {
   758             return
   759                 getEnclosingType().isErroneous() ||
   760                 isErroneous(getTypeArguments()) ||
   761                 this != tsym.type && tsym.type.isErroneous();
   762         }
   764         public boolean isParameterized() {
   765             return allparams().tail != null;
   766             // optimization, was: allparams().nonEmpty();
   767         }
   769         /** A cache for the rank. */
   770         int rank_field = -1;
   772         /** A class type is raw if it misses some
   773          *  of its type parameter sections.
   774          *  After validation, this is equivalent to:
   775          *  {@code allparams.isEmpty() && tsym.type.allparams.nonEmpty(); }
   776          */
   777         public boolean isRaw() {
   778             return
   779                 this != tsym.type && // necessary, but not sufficient condition
   780                 tsym.type.allparams().nonEmpty() &&
   781                 allparams().isEmpty();
   782         }
   784         public Type map(Mapping f) {
   785             Type outer = getEnclosingType();
   786             Type outer1 = f.apply(outer);
   787             List<Type> typarams = getTypeArguments();
   788             List<Type> typarams1 = map(typarams, f);
   789             if (outer1 == outer && typarams1 == typarams) return this;
   790             else return new ClassType(outer1, typarams1, tsym);
   791         }
   793         public boolean contains(Type elem) {
   794             return
   795                 elem == this
   796                 || (isParameterized()
   797                     && (getEnclosingType().contains(elem) || contains(getTypeArguments(), elem)))
   798                 || (isCompound()
   799                     && (supertype_field.contains(elem) || contains(interfaces_field, elem)));
   800         }
   802         public void complete() {
   803             if (tsym.completer != null) tsym.complete();
   804         }
   806         public TypeKind getKind() {
   807             return TypeKind.DECLARED;
   808         }
   810         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   811             return v.visitDeclared(this, p);
   812         }
   813     }
   815     public static class ErasedClassType extends ClassType {
   816         public ErasedClassType(Type outer, TypeSymbol tsym) {
   817             super(outer, List.<Type>nil(), tsym);
   818         }
   820         @Override
   821         public boolean hasErasedSupertypes() {
   822             return true;
   823         }
   824     }
   826     // a clone of a ClassType that knows about the alternatives of a union type.
   827     public static class UnionClassType extends ClassType implements UnionType {
   828         final List<? extends Type> alternatives_field;
   830         public UnionClassType(ClassType ct, List<? extends Type> alternatives) {
   831             super(ct.outer_field, ct.typarams_field, ct.tsym);
   832             allparams_field = ct.allparams_field;
   833             supertype_field = ct.supertype_field;
   834             interfaces_field = ct.interfaces_field;
   835             all_interfaces_field = ct.interfaces_field;
   836             alternatives_field = alternatives;
   837         }
   839         public Type getLub() {
   840             return tsym.type;
   841         }
   843         public java.util.List<? extends TypeMirror> getAlternatives() {
   844             return Collections.unmodifiableList(alternatives_field);
   845         }
   847         @Override
   848         public TypeKind getKind() {
   849             return TypeKind.UNION;
   850         }
   852         @Override
   853         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   854             return v.visitUnion(this, p);
   855         }
   856     }
   858     // a clone of a ClassType that knows about the bounds of an intersection type.
   859     public static class IntersectionClassType extends ClassType implements IntersectionType {
   861         public boolean allInterfaces;
   863         public enum IntersectionKind {
   864             EXPLICIT,
   865             IMPLICT;
   866         }
   868         public IntersectionKind intersectionKind;
   870         public IntersectionClassType(List<Type> bounds, ClassSymbol csym, boolean allInterfaces) {
   871             super(Type.noType, List.<Type>nil(), csym);
   872             this.allInterfaces = allInterfaces;
   873             Assert.check((csym.flags() & COMPOUND) != 0);
   874             supertype_field = bounds.head;
   875             interfaces_field = bounds.tail;
   876             Assert.check(supertype_field.tsym.completer != null ||
   877                     !supertype_field.isInterface(), supertype_field);
   878         }
   880         public java.util.List<? extends TypeMirror> getBounds() {
   881             return Collections.unmodifiableList(getComponents());
   882         }
   884         public List<Type> getComponents() {
   885             return interfaces_field.prepend(supertype_field);
   886         }
   888         @Override
   889         public TypeKind getKind() {
   890             return TypeKind.INTERSECTION;
   891         }
   893         @Override
   894         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   895             return intersectionKind == IntersectionKind.EXPLICIT ?
   896                 v.visitIntersection(this, p) :
   897                 v.visitDeclared(this, p);
   898         }
   899     }
   901     public static class ArrayType extends Type
   902             implements javax.lang.model.type.ArrayType {
   904         public Type elemtype;
   906         public ArrayType(Type elemtype, TypeSymbol arrayClass) {
   907             super(ARRAY, arrayClass);
   908             this.elemtype = elemtype;
   909         }
   911         @Override
   912         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   913             return v.visitArrayType(this, s);
   914         }
   916         public String toString() {
   917             return elemtype + "[]";
   918         }
   920         public boolean equals(Object obj) {
   921             return
   922                 this == obj ||
   923                 (obj instanceof ArrayType &&
   924                  this.elemtype.equals(((ArrayType)obj).elemtype));
   925         }
   927         public int hashCode() {
   928             return (ARRAY.ordinal() << 5) + elemtype.hashCode();
   929         }
   931         public boolean isVarargs() {
   932             return false;
   933         }
   935         public List<Type> allparams() { return elemtype.allparams(); }
   937         public boolean isErroneous() {
   938             return elemtype.isErroneous();
   939         }
   941         public boolean isParameterized() {
   942             return elemtype.isParameterized();
   943         }
   945         public boolean isRaw() {
   946             return elemtype.isRaw();
   947         }
   949         public ArrayType makeVarargs() {
   950             return new ArrayType(elemtype, tsym) {
   951                 @Override
   952                 public boolean isVarargs() {
   953                     return true;
   954                 }
   955             };
   956         }
   958         public Type map(Mapping f) {
   959             Type elemtype1 = f.apply(elemtype);
   960             if (elemtype1 == elemtype) return this;
   961             else return new ArrayType(elemtype1, tsym);
   962         }
   964         public boolean contains(Type elem) {
   965             return elem == this || elemtype.contains(elem);
   966         }
   968         public void complete() {
   969             elemtype.complete();
   970         }
   972         public Type getComponentType() {
   973             return elemtype;
   974         }
   976         public TypeKind getKind() {
   977             return TypeKind.ARRAY;
   978         }
   980         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   981             return v.visitArray(this, p);
   982         }
   983     }
   985     public static class MethodType extends Type implements ExecutableType {
   987         public List<Type> argtypes;
   988         public Type restype;
   989         public List<Type> thrown;
   991         /** The type annotations on the method receiver.
   992          */
   993         public Type recvtype;
   995         public MethodType(List<Type> argtypes,
   996                           Type restype,
   997                           List<Type> thrown,
   998                           TypeSymbol methodClass) {
   999             super(METHOD, methodClass);
  1000             this.argtypes = argtypes;
  1001             this.restype = restype;
  1002             this.thrown = thrown;
  1005         @Override
  1006         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1007             return v.visitMethodType(this, s);
  1010         /** The Java source which this type represents.
  1012          *  XXX 06/09/99 iris This isn't correct Java syntax, but it probably
  1013          *  should be.
  1014          */
  1015         public String toString() {
  1016             return "(" + argtypes + ")" + restype;
  1019         public List<Type>        getParameterTypes() { return argtypes; }
  1020         public Type              getReturnType()     { return restype; }
  1021         public Type              getReceiverType()   { return recvtype; }
  1022         public List<Type>        getThrownTypes()    { return thrown; }
  1024         public boolean isErroneous() {
  1025             return
  1026                 isErroneous(argtypes) ||
  1027                 restype != null && restype.isErroneous();
  1030         public Type map(Mapping f) {
  1031             List<Type> argtypes1 = map(argtypes, f);
  1032             Type restype1 = f.apply(restype);
  1033             List<Type> thrown1 = map(thrown, f);
  1034             if (argtypes1 == argtypes &&
  1035                 restype1 == restype &&
  1036                 thrown1 == thrown) return this;
  1037             else return new MethodType(argtypes1, restype1, thrown1, tsym);
  1040         public boolean contains(Type elem) {
  1041             return elem == this || contains(argtypes, elem) || restype.contains(elem);
  1044         public MethodType asMethodType() { return this; }
  1046         public void complete() {
  1047             for (List<Type> l = argtypes; l.nonEmpty(); l = l.tail)
  1048                 l.head.complete();
  1049             restype.complete();
  1050             recvtype.complete();
  1051             for (List<Type> l = thrown; l.nonEmpty(); l = l.tail)
  1052                 l.head.complete();
  1055         public List<TypeVar> getTypeVariables() {
  1056             return List.nil();
  1059         public TypeSymbol asElement() {
  1060             return null;
  1063         public TypeKind getKind() {
  1064             return TypeKind.EXECUTABLE;
  1067         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1068             return v.visitExecutable(this, p);
  1072     public static class PackageType extends Type implements NoType {
  1074         PackageType(TypeSymbol tsym) {
  1075             super(PACKAGE, tsym);
  1078         @Override
  1079         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1080             return v.visitPackageType(this, s);
  1083         public String toString() {
  1084             return tsym.getQualifiedName().toString();
  1087         public TypeKind getKind() {
  1088             return TypeKind.PACKAGE;
  1091         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1092             return v.visitNoType(this, p);
  1096     public static class TypeVar extends Type implements TypeVariable {
  1098         /** The upper bound of this type variable; set from outside.
  1099          *  Must be nonempty once it is set.
  1100          *  For a bound, `bound' is the bound type itself.
  1101          *  Multiple bounds are expressed as a single class type which has the
  1102          *  individual bounds as superclass, respectively interfaces.
  1103          *  The class type then has as `tsym' a compiler generated class `c',
  1104          *  which has a flag COMPOUND and whose owner is the type variable
  1105          *  itself. Furthermore, the erasure_field of the class
  1106          *  points to the first class or interface bound.
  1107          */
  1108         public Type bound = null;
  1110         /** The lower bound of this type variable.
  1111          *  TypeVars don't normally have a lower bound, so it is normally set
  1112          *  to syms.botType.
  1113          *  Subtypes, such as CapturedType, may provide a different value.
  1114          */
  1115         public Type lower;
  1117         public TypeVar(Name name, Symbol owner, Type lower) {
  1118             super(TYPEVAR, null);
  1119             tsym = new TypeSymbol(0, name, this, owner);
  1120             this.lower = lower;
  1123         public TypeVar(TypeSymbol tsym, Type bound, Type lower) {
  1124             super(TYPEVAR, tsym);
  1125             this.bound = bound;
  1126             this.lower = lower;
  1129         @Override
  1130         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1131             return v.visitTypeVar(this, s);
  1134         @Override
  1135         public Type getUpperBound() {
  1136             if ((bound == null || bound.tag == NONE) && this != tsym.type)
  1137                 bound = tsym.type.getUpperBound();
  1138             return bound;
  1141         int rank_field = -1;
  1143         @Override
  1144         public Type getLowerBound() {
  1145             return lower;
  1148         public TypeKind getKind() {
  1149             return TypeKind.TYPEVAR;
  1152         public boolean isCaptured() {
  1153             return false;
  1156         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1157             return v.visitTypeVariable(this, p);
  1161     /** A captured type variable comes from wildcards which can have
  1162      *  both upper and lower bound.  CapturedType extends TypeVar with
  1163      *  a lower bound.
  1164      */
  1165     public static class CapturedType extends TypeVar {
  1167         public WildcardType wildcard;
  1169         public CapturedType(Name name,
  1170                             Symbol owner,
  1171                             Type upper,
  1172                             Type lower,
  1173                             WildcardType wildcard) {
  1174             super(name, owner, lower);
  1175             this.lower = Assert.checkNonNull(lower);
  1176             this.bound = upper;
  1177             this.wildcard = wildcard;
  1180         @Override
  1181         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1182             return v.visitCapturedType(this, s);
  1185         @Override
  1186         public boolean isCaptured() {
  1187             return true;
  1190         @Override
  1191         public String toString() {
  1192             return "capture#"
  1193                 + (hashCode() & 0xFFFFFFFFL) % Printer.PRIME
  1194                 + " of "
  1195                 + wildcard;
  1199     public static abstract class DelegatedType extends Type {
  1200         public Type qtype;
  1201         public DelegatedType(TypeTag tag, Type qtype) {
  1202             super(tag, qtype.tsym);
  1203             this.qtype = qtype;
  1205         public String toString() { return qtype.toString(); }
  1206         public List<Type> getTypeArguments() { return qtype.getTypeArguments(); }
  1207         public Type getEnclosingType() { return qtype.getEnclosingType(); }
  1208         public List<Type> getParameterTypes() { return qtype.getParameterTypes(); }
  1209         public Type getReturnType() { return qtype.getReturnType(); }
  1210         public Type getReceiverType() { return qtype.getReceiverType(); }
  1211         public List<Type> getThrownTypes() { return qtype.getThrownTypes(); }
  1212         public List<Type> allparams() { return qtype.allparams(); }
  1213         public Type getUpperBound() { return qtype.getUpperBound(); }
  1214         public boolean isErroneous() { return qtype.isErroneous(); }
  1217     /**
  1218      * The type of a generic method type. It consists of a method type and
  1219      * a list of method type-parameters that are used within the method
  1220      * type.
  1221      */
  1222     public static class ForAll extends DelegatedType implements ExecutableType {
  1223         public List<Type> tvars;
  1225         public ForAll(List<Type> tvars, Type qtype) {
  1226             super(FORALL, (MethodType)qtype);
  1227             this.tvars = tvars;
  1230         @Override
  1231         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1232             return v.visitForAll(this, s);
  1235         public String toString() {
  1236             return "<" + tvars + ">" + qtype;
  1239         public List<Type> getTypeArguments()   { return tvars; }
  1241         public boolean isErroneous()  {
  1242             return qtype.isErroneous();
  1245         public Type map(Mapping f) {
  1246             return f.apply(qtype);
  1249         public boolean contains(Type elem) {
  1250             return qtype.contains(elem);
  1253         public MethodType asMethodType() {
  1254             return (MethodType)qtype;
  1257         public void complete() {
  1258             for (List<Type> l = tvars; l.nonEmpty(); l = l.tail) {
  1259                 ((TypeVar)l.head).bound.complete();
  1261             qtype.complete();
  1264         public List<TypeVar> getTypeVariables() {
  1265             return List.convert(TypeVar.class, getTypeArguments());
  1268         public TypeKind getKind() {
  1269             return TypeKind.EXECUTABLE;
  1272         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1273             return v.visitExecutable(this, p);
  1277     /** A class for inference variables, for use during method/diamond type
  1278      *  inference. An inference variable has upper/lower bounds and a set
  1279      *  of equality constraints. Such bounds are set during subtyping, type-containment,
  1280      *  type-equality checks, when the types being tested contain inference variables.
  1281      *  A change listener can be attached to an inference variable, to receive notifications
  1282      *  whenever the bounds of an inference variable change.
  1283      */
  1284     public static class UndetVar extends DelegatedType {
  1286         /** Inference variable change listener. The listener method is called
  1287          *  whenever a change to the inference variable's bounds occurs
  1288          */
  1289         public interface UndetVarListener {
  1290             /** called when some inference variable bounds (of given kinds ibs) change */
  1291             void varChanged(UndetVar uv, Set<InferenceBound> ibs);
  1294         /**
  1295          * Inference variable bound kinds
  1296          */
  1297         public enum InferenceBound {
  1298             /** upper bounds */
  1299             UPPER,
  1300             /** lower bounds */
  1301             LOWER,
  1302             /** equality constraints */
  1303             EQ;
  1306         /** inference variable bounds */
  1307         private Map<InferenceBound, List<Type>> bounds;
  1309         /** inference variable's inferred type (set from Infer.java) */
  1310         public Type inst = null;
  1312         /** number of declared (upper) bounds */
  1313         public int declaredCount;
  1315         /** inference variable's change listener */
  1316         public UndetVarListener listener = null;
  1318         @Override
  1319         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1320             return v.visitUndetVar(this, s);
  1323         public UndetVar(TypeVar origin, Types types) {
  1324             super(UNDETVAR, origin);
  1325             bounds = new EnumMap<InferenceBound, List<Type>>(InferenceBound.class);
  1326             List<Type> declaredBounds = types.getBounds(origin);
  1327             declaredCount = declaredBounds.length();
  1328             bounds.put(InferenceBound.UPPER, declaredBounds);
  1329             bounds.put(InferenceBound.LOWER, List.<Type>nil());
  1330             bounds.put(InferenceBound.EQ, List.<Type>nil());
  1333         public String toString() {
  1334             if (inst != null) return inst.toString();
  1335             else return qtype + "?";
  1338         public Type baseType() {
  1339             if (inst != null) return inst.baseType();
  1340             else return this;
  1343         /** get all bounds of a given kind */
  1344         public List<Type> getBounds(InferenceBound... ibs) {
  1345             ListBuffer<Type> buf = ListBuffer.lb();
  1346             for (InferenceBound ib : ibs) {
  1347                 buf.appendList(bounds.get(ib));
  1349             return buf.toList();
  1352         /** get the list of declared (upper) bounds */
  1353         public List<Type> getDeclaredBounds() {
  1354             ListBuffer<Type> buf = ListBuffer.lb();
  1355             int count = 0;
  1356             for (Type b : getBounds(InferenceBound.UPPER)) {
  1357                 if (count++ == declaredCount) break;
  1358                 buf.append(b);
  1360             return buf.toList();
  1363         /** add a bound of a given kind - this might trigger listener notification */
  1364         public void addBound(InferenceBound ib, Type bound, Types types) {
  1365             Type bound2 = toTypeVarMap.apply(bound);
  1366             List<Type> prevBounds = bounds.get(ib);
  1367             for (Type b : prevBounds) {
  1368                 //check for redundancy - use strict version of isSameType on tvars
  1369                 //(as the standard version will lead to false positives w.r.t. clones ivars)
  1370                 if (types.isSameType(b, bound2, true)) return;
  1372             bounds.put(ib, prevBounds.prepend(bound2));
  1373             notifyChange(EnumSet.of(ib));
  1375         //where
  1376             Type.Mapping toTypeVarMap = new Mapping("toTypeVarMap") {
  1377                 @Override
  1378                 public Type apply(Type t) {
  1379                     if (t.hasTag(UNDETVAR)) {
  1380                         UndetVar uv = (UndetVar)t;
  1381                         return uv.qtype;
  1382                     } else {
  1383                         return t.map(this);
  1386             };
  1388         /** replace types in all bounds - this might trigger listener notification */
  1389         public void substBounds(List<Type> from, List<Type> to, Types types) {
  1390             List<Type> instVars = from.diff(to);
  1391             //if set of instantiated ivars is empty, there's nothing to do!
  1392             if (instVars.isEmpty()) return;
  1393             final EnumSet<InferenceBound> boundsChanged = EnumSet.noneOf(InferenceBound.class);
  1394             UndetVarListener prevListener = listener;
  1395             try {
  1396                 //setup new listener for keeping track of changed bounds
  1397                 listener = new UndetVarListener() {
  1398                     public void varChanged(UndetVar uv, Set<InferenceBound> ibs) {
  1399                         boundsChanged.addAll(ibs);
  1401                 };
  1402                 for (Map.Entry<InferenceBound, List<Type>> _entry : bounds.entrySet()) {
  1403                     InferenceBound ib = _entry.getKey();
  1404                     List<Type> prevBounds = _entry.getValue();
  1405                     ListBuffer<Type> newBounds = ListBuffer.lb();
  1406                     ListBuffer<Type> deps = ListBuffer.lb();
  1407                     //step 1 - re-add bounds that are not dependent on ivars
  1408                     for (Type t : prevBounds) {
  1409                         if (!t.containsAny(instVars)) {
  1410                             newBounds.append(t);
  1411                         } else {
  1412                             deps.append(t);
  1415                     //step 2 - replace bounds
  1416                     bounds.put(ib, newBounds.toList());
  1417                     //step 3 - for each dependency, add new replaced bound
  1418                     for (Type dep : deps) {
  1419                         addBound(ib, types.subst(dep, from, to), types);
  1422             } finally {
  1423                 listener = prevListener;
  1424                 if (!boundsChanged.isEmpty()) {
  1425                     notifyChange(boundsChanged);
  1430         private void notifyChange(EnumSet<InferenceBound> ibs) {
  1431             if (listener != null) {
  1432                 listener.varChanged(this, ibs);
  1437     /** Represents VOID or NONE.
  1438      */
  1439     static class JCNoType extends Type implements NoType {
  1440         public JCNoType(TypeTag tag) {
  1441             super(tag, null);
  1444         @Override
  1445         public TypeKind getKind() {
  1446             switch (tag) {
  1447             case VOID:  return TypeKind.VOID;
  1448             case NONE:  return TypeKind.NONE;
  1449             default:
  1450                 throw new AssertionError("Unexpected tag: " + tag);
  1454         @Override
  1455         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1456             return v.visitNoType(this, p);
  1460     static class BottomType extends Type implements NullType {
  1461         public BottomType() {
  1462             super(BOT, null);
  1465         @Override
  1466         public TypeKind getKind() {
  1467             return TypeKind.NULL;
  1470         @Override
  1471         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1472             return v.visitNull(this, p);
  1475         @Override
  1476         public Type constType(Object value) {
  1477             return this;
  1480         @Override
  1481         public String stringValue() {
  1482             return "null";
  1486     public static class ErrorType extends ClassType
  1487             implements javax.lang.model.type.ErrorType {
  1489         private Type originalType = null;
  1491         public ErrorType(Type originalType, TypeSymbol tsym) {
  1492             super(noType, List.<Type>nil(), null);
  1493             tag = ERROR;
  1494             this.tsym = tsym;
  1495             this.originalType = (originalType == null ? noType : originalType);
  1498         public ErrorType(ClassSymbol c, Type originalType) {
  1499             this(originalType, c);
  1500             c.type = this;
  1501             c.kind = ERR;
  1502             c.members_field = new Scope.ErrorScope(c);
  1505         public ErrorType(Name name, TypeSymbol container, Type originalType) {
  1506             this(new ClassSymbol(PUBLIC|STATIC|ACYCLIC, name, null, container), originalType);
  1509         @Override
  1510         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1511             return v.visitErrorType(this, s);
  1514         public Type constType(Object constValue) { return this; }
  1515         public Type getEnclosingType()           { return this; }
  1516         public Type getReturnType()              { return this; }
  1517         public Type asSub(Symbol sym)            { return this; }
  1518         public Type map(Mapping f)               { return this; }
  1520         public boolean isGenType(Type t)         { return true; }
  1521         public boolean isErroneous()             { return true; }
  1522         public boolean isCompound()              { return false; }
  1523         public boolean isInterface()             { return false; }
  1525         public List<Type> allparams()            { return List.nil(); }
  1526         public List<Type> getTypeArguments()     { return List.nil(); }
  1528         public TypeKind getKind() {
  1529             return TypeKind.ERROR;
  1532         public Type getOriginalType() {
  1533             return originalType;
  1536         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1537             return v.visitError(this, p);
  1541     public static class AnnotatedType extends Type
  1542             implements javax.lang.model.type.AnnotatedType {
  1543         /** The type annotations on this type.
  1544          */
  1545         public List<Attribute.TypeCompound> typeAnnotations;
  1547         /** The underlying type that is annotated.
  1548          */
  1549         public Type underlyingType;
  1551         public AnnotatedType(Type underlyingType) {
  1552             super(underlyingType.tag, underlyingType.tsym);
  1553             this.typeAnnotations = List.nil();
  1554             this.underlyingType = underlyingType;
  1555             Assert.check(underlyingType.getKind() != TypeKind.ANNOTATED,
  1556                     "Can't annotate already annotated type: " + underlyingType);
  1559         public AnnotatedType(List<Attribute.TypeCompound> typeAnnotations,
  1560                 Type underlyingType) {
  1561             super(underlyingType.tag, underlyingType.tsym);
  1562             this.typeAnnotations = typeAnnotations;
  1563             this.underlyingType = underlyingType;
  1564             Assert.check(underlyingType.getKind() != TypeKind.ANNOTATED,
  1565                     "Can't annotate already annotated type: " + underlyingType +
  1566                     "; adding: " + typeAnnotations);
  1569         @Override
  1570         public TypeKind getKind() {
  1571             return TypeKind.ANNOTATED;
  1574         @Override
  1575         public List<? extends AnnotationMirror> getAnnotations() {
  1576             return typeAnnotations;
  1579         @Override
  1580         public TypeMirror getUnderlyingType() {
  1581             return underlyingType;
  1584         @Override
  1585         public Type unannotatedType() {
  1586             return underlyingType;
  1589         @Override
  1590         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1591             return v.visitAnnotatedType(this, s);
  1594         @Override
  1595         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1596             return v.visitAnnotated(this, p);
  1599         @Override
  1600         public Type map(Mapping f) {
  1601             underlyingType.map(f);
  1602             return this;
  1605         @Override
  1606         public Type constType(Object constValue) { return underlyingType.constType(constValue); }
  1607         @Override
  1608         public Type getEnclosingType()           { return underlyingType.getEnclosingType(); }
  1610         @Override
  1611         public Type getReturnType()              { return underlyingType.getReturnType(); }
  1612         @Override
  1613         public List<Type> getTypeArguments()     { return underlyingType.getTypeArguments(); }
  1614         @Override
  1615         public List<Type> getParameterTypes()    { return underlyingType.getParameterTypes(); }
  1616         @Override
  1617         public Type getReceiverType()            { return underlyingType.getReceiverType(); }
  1618         @Override
  1619         public List<Type> getThrownTypes()       { return underlyingType.getThrownTypes(); }
  1620         @Override
  1621         public Type getUpperBound()              { return underlyingType.getUpperBound(); }
  1622         @Override
  1623         public Type getLowerBound()              { return underlyingType.getLowerBound(); }
  1625         @Override
  1626         public boolean isErroneous()             { return underlyingType.isErroneous(); }
  1627         @Override
  1628         public boolean isCompound()              { return underlyingType.isCompound(); }
  1629         @Override
  1630         public boolean isInterface()             { return underlyingType.isInterface(); }
  1631         @Override
  1632         public List<Type> allparams()            { return underlyingType.allparams(); }
  1633         @Override
  1634         public boolean isNumeric()               { return underlyingType.isNumeric(); }
  1635         @Override
  1636         public boolean isReference()             { return underlyingType.isReference(); }
  1637         @Override
  1638         public boolean isParameterized()         { return underlyingType.isParameterized(); }
  1639         @Override
  1640         public boolean isRaw()                   { return underlyingType.isRaw(); }
  1641         @Override
  1642         public boolean isFinal()                 { return underlyingType.isFinal(); }
  1643         @Override
  1644         public boolean isSuperBound()            { return underlyingType.isSuperBound(); }
  1645         @Override
  1646         public boolean isExtendsBound()          { return underlyingType.isExtendsBound(); }
  1647         @Override
  1648         public boolean isUnbound()               { return underlyingType.isUnbound(); }
  1650         @Override
  1651         public String toString() {
  1652             // TODO more logic for arrays, etc.
  1653             if (typeAnnotations != null &&
  1654                     !typeAnnotations.isEmpty()) {
  1655                 return "(" + typeAnnotations.toString() + " :: " + underlyingType.toString() + ")";
  1656             } else {
  1657                 return "({} :: " + underlyingType.toString() +")";
  1661         @Override
  1662         public boolean contains(Type t)          { return underlyingType.contains(t); }
  1664         // TODO: attach annotations?
  1665         @Override
  1666         public Type withTypeVar(Type t)          { return underlyingType.withTypeVar(t); }
  1668         // TODO: attach annotations?
  1669         @Override
  1670         public TypeSymbol asElement()            { return underlyingType.asElement(); }
  1672         // TODO: attach annotations?
  1673         @Override
  1674         public MethodType asMethodType()         { return underlyingType.asMethodType(); }
  1676         @Override
  1677         public void complete()                   { underlyingType.complete(); }
  1679         @Override
  1680         public TypeMirror getComponentType()     { return ((ArrayType)underlyingType).getComponentType(); }
  1682         // The result is an ArrayType, but only in the model sense, not the Type sense.
  1683         public AnnotatedType makeVarargs() {
  1684             AnnotatedType atype = new AnnotatedType(((ArrayType)underlyingType).makeVarargs());
  1685             atype.typeAnnotations = this.typeAnnotations;
  1686             return atype;
  1689         @Override
  1690         public TypeMirror getExtendsBound()      { return ((WildcardType)underlyingType).getExtendsBound(); }
  1691         @Override
  1692         public TypeMirror getSuperBound()        { return ((WildcardType)underlyingType).getSuperBound(); }
  1695     /**
  1696      * A visitor for types.  A visitor is used to implement operations
  1697      * (or relations) on types.  Most common operations on types are
  1698      * binary relations and this interface is designed for binary
  1699      * relations, that is, operations of the form
  1700      * Type&nbsp;&times;&nbsp;S&nbsp;&rarr;&nbsp;R.
  1701      * <!-- In plain text: Type x S -> R -->
  1703      * @param <R> the return type of the operation implemented by this
  1704      * visitor; use Void if no return type is needed.
  1705      * @param <S> the type of the second argument (the first being the
  1706      * type itself) of the operation implemented by this visitor; use
  1707      * Void if a second argument is not needed.
  1708      */
  1709     public interface Visitor<R,S> {
  1710         R visitClassType(ClassType t, S s);
  1711         R visitWildcardType(WildcardType t, S s);
  1712         R visitArrayType(ArrayType t, S s);
  1713         R visitMethodType(MethodType t, S s);
  1714         R visitPackageType(PackageType t, S s);
  1715         R visitTypeVar(TypeVar t, S s);
  1716         R visitCapturedType(CapturedType t, S s);
  1717         R visitForAll(ForAll t, S s);
  1718         R visitUndetVar(UndetVar t, S s);
  1719         R visitErrorType(ErrorType t, S s);
  1720         R visitAnnotatedType(AnnotatedType t, S s);
  1721         R visitType(Type t, S s);

mercurial