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

Mon, 18 Mar 2013 14:40:32 -0700

author
jjg
date
Mon, 18 Mar 2013 14:40:32 -0700
changeset 1644
40adaf938847
parent 1628
5ddecb91d843
child 1645
97f6839673d6
permissions
-rw-r--r--

8008425: Remove interim new javax.lang.model API for type-annotations
Reviewed-by: darcy

     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     public boolean isAnnotated() {
   250         return false;
   251     }
   253     /**
   254      * If this is an annotated type, return the underlying type.
   255      * Otherwise, return the type itself.
   256      */
   257     public Type unannotatedType() {
   258         return this;
   259     }
   261     /** Return the base types of a list of types.
   262      */
   263     public static List<Type> baseTypes(List<Type> ts) {
   264         if (ts.nonEmpty()) {
   265             Type t = ts.head.baseType();
   266             List<Type> baseTypes = baseTypes(ts.tail);
   267             if (t != ts.head || baseTypes != ts.tail)
   268                 return baseTypes.prepend(t);
   269         }
   270         return ts;
   271     }
   273     /** The Java source which this type represents.
   274      */
   275     public String toString() {
   276         String s = (tsym == null || tsym.name == null)
   277             ? "<none>"
   278             : tsym.name.toString();
   279         if (moreInfo && tag == TYPEVAR) s = s + hashCode();
   280         return s;
   281     }
   283     /**
   284      * The Java source which this type list represents.  A List is
   285      * represented as a comma-spearated listing of the elements in
   286      * that list.
   287      */
   288     public static String toString(List<Type> ts) {
   289         if (ts.isEmpty()) {
   290             return "";
   291         } else {
   292             StringBuilder buf = new StringBuilder();
   293             buf.append(ts.head.toString());
   294             for (List<Type> l = ts.tail; l.nonEmpty(); l = l.tail)
   295                 buf.append(",").append(l.head.toString());
   296             return buf.toString();
   297         }
   298     }
   300     /**
   301      * The constant value of this type, converted to String
   302      */
   303     public String stringValue() {
   304         Object cv = Assert.checkNonNull(constValue());
   305         if (tag == BOOLEAN)
   306             return ((Integer) cv).intValue() == 0 ? "false" : "true";
   307         else if (tag == CHAR)
   308             return String.valueOf((char) ((Integer) cv).intValue());
   309         else
   310             return cv.toString();
   311     }
   313     /**
   314      * This method is analogous to isSameType, but weaker, since we
   315      * never complete classes. Where isSameType would complete a
   316      * class, equals assumes that the two types are different.
   317      */
   318     @Override
   319     public boolean equals(Object t) {
   320         return super.equals(t);
   321     }
   323     @Override
   324     public int hashCode() {
   325         return super.hashCode();
   326     }
   328     /** Is this a constant type whose value is false?
   329      */
   330     public boolean isFalse() {
   331         return
   332             tag == BOOLEAN &&
   333             constValue() != null &&
   334             ((Integer)constValue()).intValue() == 0;
   335     }
   337     /** Is this a constant type whose value is true?
   338      */
   339     public boolean isTrue() {
   340         return
   341             tag == BOOLEAN &&
   342             constValue() != null &&
   343             ((Integer)constValue()).intValue() != 0;
   344     }
   346     public String argtypes(boolean varargs) {
   347         List<Type> args = getParameterTypes();
   348         if (!varargs) return args.toString();
   349         StringBuilder buf = new StringBuilder();
   350         while (args.tail.nonEmpty()) {
   351             buf.append(args.head);
   352             args = args.tail;
   353             buf.append(',');
   354         }
   355         if (args.head.unannotatedType().tag == ARRAY) {
   356             buf.append(((ArrayType)args.head.unannotatedType()).elemtype);
   357             if (args.head.getAnnotations().nonEmpty()) {
   358                 buf.append(args.head.getAnnotations());
   359             }
   360             buf.append("...");
   361         } else {
   362             buf.append(args.head);
   363         }
   364         return buf.toString();
   365     }
   367     /** Access methods.
   368      */
   369     public List<? extends AnnotationMirror> getAnnotations() { return List.nil(); }
   370     public List<Type>        getTypeArguments()  { return List.nil(); }
   371     public Type              getEnclosingType()  { return null; }
   372     public List<Type>        getParameterTypes() { return List.nil(); }
   373     public Type              getReturnType()     { return null; }
   374     public Type              getReceiverType()   { return null; }
   375     public List<Type>        getThrownTypes()    { return List.nil(); }
   376     public Type              getUpperBound()     { return null; }
   377     public Type              getLowerBound()     { return null; }
   379     /** Navigation methods, these will work for classes, type variables,
   380      *  foralls, but will return null for arrays and methods.
   381      */
   383    /** Return all parameters of this type and all its outer types in order
   384     *  outer (first) to inner (last).
   385     */
   386     public List<Type> allparams() { return List.nil(); }
   388     /** Does this type contain "error" elements?
   389      */
   390     public boolean isErroneous() {
   391         return false;
   392     }
   394     public static boolean isErroneous(List<Type> ts) {
   395         for (List<Type> l = ts; l.nonEmpty(); l = l.tail)
   396             if (l.head.isErroneous()) return true;
   397         return false;
   398     }
   400     /** Is this type parameterized?
   401      *  A class type is parameterized if it has some parameters.
   402      *  An array type is parameterized if its element type is parameterized.
   403      *  All other types are not parameterized.
   404      */
   405     public boolean isParameterized() {
   406         return false;
   407     }
   409     /** Is this type a raw type?
   410      *  A class type is a raw type if it misses some of its parameters.
   411      *  An array type is a raw type if its element type is raw.
   412      *  All other types are not raw.
   413      *  Type validation will ensure that the only raw types
   414      *  in a program are types that miss all their type variables.
   415      */
   416     public boolean isRaw() {
   417         return false;
   418     }
   420     public boolean isCompound() {
   421         return tsym.completer == null
   422             // Compound types can't have a completer.  Calling
   423             // flags() will complete the symbol causing the
   424             // compiler to load classes unnecessarily.  This led
   425             // to regression 6180021.
   426             && (tsym.flags() & COMPOUND) != 0;
   427     }
   429     public boolean isInterface() {
   430         return (tsym.flags() & INTERFACE) != 0;
   431     }
   433     public boolean isFinal() {
   434         return (tsym.flags() & FINAL) != 0;
   435     }
   437     /**
   438      * Does this type contain occurrences of type t?
   439      */
   440     public boolean contains(Type t) {
   441         return t == this;
   442     }
   444     public static boolean contains(List<Type> ts, Type t) {
   445         for (List<Type> l = ts;
   446              l.tail != null /*inlined: l.nonEmpty()*/;
   447              l = l.tail)
   448             if (l.head.contains(t)) return true;
   449         return false;
   450     }
   452     /** Does this type contain an occurrence of some type in 'ts'?
   453      */
   454     public boolean containsAny(List<Type> ts) {
   455         for (Type t : ts)
   456             if (this.contains(t)) return true;
   457         return false;
   458     }
   460     public static boolean containsAny(List<Type> ts1, List<Type> ts2) {
   461         for (Type t : ts1)
   462             if (t.containsAny(ts2)) return true;
   463         return false;
   464     }
   466     public static List<Type> filter(List<Type> ts, Filter<Type> tf) {
   467         ListBuffer<Type> buf = ListBuffer.lb();
   468         for (Type t : ts) {
   469             if (tf.accepts(t)) {
   470                 buf.append(t);
   471             }
   472         }
   473         return buf.toList();
   474     }
   476     public boolean isSuperBound() { return false; }
   477     public boolean isExtendsBound() { return false; }
   478     public boolean isUnbound() { return false; }
   479     public Type withTypeVar(Type t) { return this; }
   481     /** The underlying method type of this type.
   482      */
   483     public MethodType asMethodType() { throw new AssertionError(); }
   485     /** Complete loading all classes in this type.
   486      */
   487     public void complete() {}
   489     public TypeSymbol asElement() {
   490         return tsym;
   491     }
   493     public TypeKind getKind() {
   494         switch (tag) {
   495         case BYTE:      return TypeKind.BYTE;
   496         case CHAR:      return TypeKind.CHAR;
   497         case SHORT:     return TypeKind.SHORT;
   498         case INT:       return TypeKind.INT;
   499         case LONG:      return TypeKind.LONG;
   500         case FLOAT:     return TypeKind.FLOAT;
   501         case DOUBLE:    return TypeKind.DOUBLE;
   502         case BOOLEAN:   return TypeKind.BOOLEAN;
   503         case VOID:      return TypeKind.VOID;
   504         case BOT:       return TypeKind.NULL;
   505         case NONE:      return TypeKind.NONE;
   506         default:        return TypeKind.OTHER;
   507         }
   508     }
   510     public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   511         if (isPrimitive())
   512             return v.visitPrimitive(this, p);
   513         else
   514             throw new AssertionError();
   515     }
   517     public static class WildcardType extends Type
   518             implements javax.lang.model.type.WildcardType {
   520         public Type type;
   521         public BoundKind kind;
   522         public TypeVar bound;
   524         @Override
   525         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   526             return v.visitWildcardType(this, s);
   527         }
   529         public WildcardType(Type type, BoundKind kind, TypeSymbol tsym) {
   530             super(WILDCARD, tsym);
   531             this.type = Assert.checkNonNull(type);
   532             this.kind = kind;
   533         }
   534         public WildcardType(WildcardType t, TypeVar bound) {
   535             this(t.type, t.kind, t.tsym, bound);
   536         }
   538         public WildcardType(Type type, BoundKind kind, TypeSymbol tsym, TypeVar bound) {
   539             this(type, kind, tsym);
   540             this.bound = bound;
   541         }
   543         public boolean contains(Type t) {
   544             return kind != UNBOUND && type.contains(t);
   545         }
   547         public boolean isSuperBound() {
   548             return kind == SUPER ||
   549                 kind == UNBOUND;
   550         }
   551         public boolean isExtendsBound() {
   552             return kind == EXTENDS ||
   553                 kind == UNBOUND;
   554         }
   555         public boolean isUnbound() {
   556             return kind == UNBOUND;
   557         }
   559         public Type withTypeVar(Type t) {
   560             //-System.err.println(this+".withTypeVar("+t+");");//DEBUG
   561             if (bound == t)
   562                 return this;
   563             bound = (TypeVar)t;
   564             return this;
   565         }
   567         boolean isPrintingBound = false;
   568         public String toString() {
   569             StringBuilder s = new StringBuilder();
   570             s.append(kind.toString());
   571             if (kind != UNBOUND)
   572                 s.append(type);
   573             if (moreInfo && bound != null && !isPrintingBound)
   574                 try {
   575                     isPrintingBound = true;
   576                     s.append("{:").append(bound.bound).append(":}");
   577                 } finally {
   578                     isPrintingBound = false;
   579                 }
   580             return s.toString();
   581         }
   583         public Type map(Mapping f) {
   584             //- System.err.println("   (" + this + ").map(" + f + ")");//DEBUG
   585             Type t = type;
   586             if (t != null)
   587                 t = f.apply(t);
   588             if (t == type)
   589                 return this;
   590             else
   591                 return new WildcardType(t, kind, tsym, bound);
   592         }
   594         public Type getExtendsBound() {
   595             if (kind == EXTENDS)
   596                 return type;
   597             else
   598                 return null;
   599         }
   601         public Type getSuperBound() {
   602             if (kind == SUPER)
   603                 return type;
   604             else
   605                 return null;
   606         }
   608         public TypeKind getKind() {
   609             return TypeKind.WILDCARD;
   610         }
   612         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   613             return v.visitWildcard(this, p);
   614         }
   615     }
   617     public static class ClassType extends Type implements DeclaredType {
   619         /** The enclosing type of this type. If this is the type of an inner
   620          *  class, outer_field refers to the type of its enclosing
   621          *  instance class, in all other cases it refers to noType.
   622          */
   623         private Type outer_field;
   625         /** The type parameters of this type (to be set once class is loaded).
   626          */
   627         public List<Type> typarams_field;
   629         /** A cache variable for the type parameters of this type,
   630          *  appended to all parameters of its enclosing class.
   631          *  @see #allparams
   632          */
   633         public List<Type> allparams_field;
   635         /** The supertype of this class (to be set once class is loaded).
   636          */
   637         public Type supertype_field;
   639         /** The interfaces of this class (to be set once class is loaded).
   640          */
   641         public List<Type> interfaces_field;
   643         /** All the interfaces of this class, including missing ones.
   644          */
   645         public List<Type> all_interfaces_field;
   647         public ClassType(Type outer, List<Type> typarams, TypeSymbol tsym) {
   648             super(CLASS, tsym);
   649             this.outer_field = outer;
   650             this.typarams_field = typarams;
   651             this.allparams_field = null;
   652             this.supertype_field = null;
   653             this.interfaces_field = null;
   654             /*
   655             // this can happen during error recovery
   656             assert
   657                 outer.isParameterized() ?
   658                 typarams.length() == tsym.type.typarams().length() :
   659                 outer.isRaw() ?
   660                 typarams.length() == 0 :
   661                 true;
   662             */
   663         }
   665         @Override
   666         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   667             return v.visitClassType(this, s);
   668         }
   670         public Type constType(Object constValue) {
   671             final Object value = constValue;
   672             return new ClassType(getEnclosingType(), typarams_field, tsym) {
   673                     @Override
   674                     public Object constValue() {
   675                         return value;
   676                     }
   677                     @Override
   678                     public Type baseType() {
   679                         return tsym.type;
   680                     }
   681                 };
   682         }
   684         /** The Java source which this type represents.
   685          */
   686         public String toString() {
   687             StringBuilder buf = new StringBuilder();
   688             if (getEnclosingType().tag == CLASS && tsym.owner.kind == TYP) {
   689                 buf.append(getEnclosingType().toString());
   690                 buf.append(".");
   691                 buf.append(className(tsym, false));
   692             } else {
   693                 buf.append(className(tsym, true));
   694             }
   695             if (getTypeArguments().nonEmpty()) {
   696                 buf.append('<');
   697                 buf.append(getTypeArguments().toString());
   698                 buf.append(">");
   699             }
   700             return buf.toString();
   701         }
   702 //where
   703             private String className(Symbol sym, boolean longform) {
   704                 if (sym.name.isEmpty() && (sym.flags() & COMPOUND) != 0) {
   705                     StringBuilder s = new StringBuilder(supertype_field.toString());
   706                     for (List<Type> is=interfaces_field; is.nonEmpty(); is = is.tail) {
   707                         s.append("&");
   708                         s.append(is.head.toString());
   709                     }
   710                     return s.toString();
   711                 } else if (sym.name.isEmpty()) {
   712                     String s;
   713                     ClassType norm = (ClassType) tsym.type;
   714                     if (norm == null) {
   715                         s = Log.getLocalizedString("anonymous.class", (Object)null);
   716                     } else if (norm.interfaces_field != null && norm.interfaces_field.nonEmpty()) {
   717                         s = Log.getLocalizedString("anonymous.class",
   718                                                    norm.interfaces_field.head);
   719                     } else {
   720                         s = Log.getLocalizedString("anonymous.class",
   721                                                    norm.supertype_field);
   722                     }
   723                     if (moreInfo)
   724                         s += String.valueOf(sym.hashCode());
   725                     return s;
   726                 } else if (longform) {
   727                     return sym.getQualifiedName().toString();
   728                 } else {
   729                     return sym.name.toString();
   730                 }
   731             }
   733         public List<Type> getTypeArguments() {
   734             if (typarams_field == null) {
   735                 complete();
   736                 if (typarams_field == null)
   737                     typarams_field = List.nil();
   738             }
   739             return typarams_field;
   740         }
   742         public boolean hasErasedSupertypes() {
   743             return isRaw();
   744         }
   746         public Type getEnclosingType() {
   747             return outer_field;
   748         }
   750         public void setEnclosingType(Type outer) {
   751             outer_field = outer;
   752         }
   754         public List<Type> allparams() {
   755             if (allparams_field == null) {
   756                 allparams_field = getTypeArguments().prependList(getEnclosingType().allparams());
   757             }
   758             return allparams_field;
   759         }
   761         public boolean isErroneous() {
   762             return
   763                 getEnclosingType().isErroneous() ||
   764                 isErroneous(getTypeArguments()) ||
   765                 this != tsym.type && tsym.type.isErroneous();
   766         }
   768         public boolean isParameterized() {
   769             return allparams().tail != null;
   770             // optimization, was: allparams().nonEmpty();
   771         }
   773         /** A cache for the rank. */
   774         int rank_field = -1;
   776         /** A class type is raw if it misses some
   777          *  of its type parameter sections.
   778          *  After validation, this is equivalent to:
   779          *  {@code allparams.isEmpty() && tsym.type.allparams.nonEmpty(); }
   780          */
   781         public boolean isRaw() {
   782             return
   783                 this != tsym.type && // necessary, but not sufficient condition
   784                 tsym.type.allparams().nonEmpty() &&
   785                 allparams().isEmpty();
   786         }
   788         public Type map(Mapping f) {
   789             Type outer = getEnclosingType();
   790             Type outer1 = f.apply(outer);
   791             List<Type> typarams = getTypeArguments();
   792             List<Type> typarams1 = map(typarams, f);
   793             if (outer1 == outer && typarams1 == typarams) return this;
   794             else return new ClassType(outer1, typarams1, tsym);
   795         }
   797         public boolean contains(Type elem) {
   798             return
   799                 elem == this
   800                 || (isParameterized()
   801                     && (getEnclosingType().contains(elem) || contains(getTypeArguments(), elem)))
   802                 || (isCompound()
   803                     && (supertype_field.contains(elem) || contains(interfaces_field, elem)));
   804         }
   806         public void complete() {
   807             if (tsym.completer != null) tsym.complete();
   808         }
   810         public TypeKind getKind() {
   811             return TypeKind.DECLARED;
   812         }
   814         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   815             return v.visitDeclared(this, p);
   816         }
   817     }
   819     public static class ErasedClassType extends ClassType {
   820         public ErasedClassType(Type outer, TypeSymbol tsym) {
   821             super(outer, List.<Type>nil(), tsym);
   822         }
   824         @Override
   825         public boolean hasErasedSupertypes() {
   826             return true;
   827         }
   828     }
   830     // a clone of a ClassType that knows about the alternatives of a union type.
   831     public static class UnionClassType extends ClassType implements UnionType {
   832         final List<? extends Type> alternatives_field;
   834         public UnionClassType(ClassType ct, List<? extends Type> alternatives) {
   835             super(ct.outer_field, ct.typarams_field, ct.tsym);
   836             allparams_field = ct.allparams_field;
   837             supertype_field = ct.supertype_field;
   838             interfaces_field = ct.interfaces_field;
   839             all_interfaces_field = ct.interfaces_field;
   840             alternatives_field = alternatives;
   841         }
   843         public Type getLub() {
   844             return tsym.type;
   845         }
   847         public java.util.List<? extends TypeMirror> getAlternatives() {
   848             return Collections.unmodifiableList(alternatives_field);
   849         }
   851         @Override
   852         public TypeKind getKind() {
   853             return TypeKind.UNION;
   854         }
   856         @Override
   857         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   858             return v.visitUnion(this, p);
   859         }
   860     }
   862     // a clone of a ClassType that knows about the bounds of an intersection type.
   863     public static class IntersectionClassType extends ClassType implements IntersectionType {
   865         public boolean allInterfaces;
   867         public enum IntersectionKind {
   868             EXPLICIT,
   869             IMPLICT;
   870         }
   872         public IntersectionKind intersectionKind;
   874         public IntersectionClassType(List<Type> bounds, ClassSymbol csym, boolean allInterfaces) {
   875             super(Type.noType, List.<Type>nil(), csym);
   876             this.allInterfaces = allInterfaces;
   877             Assert.check((csym.flags() & COMPOUND) != 0);
   878             supertype_field = bounds.head;
   879             interfaces_field = bounds.tail;
   880             Assert.check(supertype_field.tsym.completer != null ||
   881                     !supertype_field.isInterface(), supertype_field);
   882         }
   884         public java.util.List<? extends TypeMirror> getBounds() {
   885             return Collections.unmodifiableList(getComponents());
   886         }
   888         public List<Type> getComponents() {
   889             return interfaces_field.prepend(supertype_field);
   890         }
   892         @Override
   893         public TypeKind getKind() {
   894             return TypeKind.INTERSECTION;
   895         }
   897         @Override
   898         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   899             return intersectionKind == IntersectionKind.EXPLICIT ?
   900                 v.visitIntersection(this, p) :
   901                 v.visitDeclared(this, p);
   902         }
   903     }
   905     public static class ArrayType extends Type
   906             implements javax.lang.model.type.ArrayType {
   908         public Type elemtype;
   910         public ArrayType(Type elemtype, TypeSymbol arrayClass) {
   911             super(ARRAY, arrayClass);
   912             this.elemtype = elemtype;
   913         }
   915         @Override
   916         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   917             return v.visitArrayType(this, s);
   918         }
   920         public String toString() {
   921             return elemtype + "[]";
   922         }
   924         public boolean equals(Object obj) {
   925             return
   926                 this == obj ||
   927                 (obj instanceof ArrayType &&
   928                  this.elemtype.equals(((ArrayType)obj).elemtype));
   929         }
   931         public int hashCode() {
   932             return (ARRAY.ordinal() << 5) + elemtype.hashCode();
   933         }
   935         public boolean isVarargs() {
   936             return false;
   937         }
   939         public List<Type> allparams() { return elemtype.allparams(); }
   941         public boolean isErroneous() {
   942             return elemtype.isErroneous();
   943         }
   945         public boolean isParameterized() {
   946             return elemtype.isParameterized();
   947         }
   949         public boolean isRaw() {
   950             return elemtype.isRaw();
   951         }
   953         public ArrayType makeVarargs() {
   954             return new ArrayType(elemtype, tsym) {
   955                 @Override
   956                 public boolean isVarargs() {
   957                     return true;
   958                 }
   959             };
   960         }
   962         public Type map(Mapping f) {
   963             Type elemtype1 = f.apply(elemtype);
   964             if (elemtype1 == elemtype) return this;
   965             else return new ArrayType(elemtype1, tsym);
   966         }
   968         public boolean contains(Type elem) {
   969             return elem == this || elemtype.contains(elem);
   970         }
   972         public void complete() {
   973             elemtype.complete();
   974         }
   976         public Type getComponentType() {
   977             return elemtype;
   978         }
   980         public TypeKind getKind() {
   981             return TypeKind.ARRAY;
   982         }
   984         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   985             return v.visitArray(this, p);
   986         }
   987     }
   989     public static class MethodType extends Type implements ExecutableType {
   991         public List<Type> argtypes;
   992         public Type restype;
   993         public List<Type> thrown;
   995         /** The type annotations on the method receiver.
   996          */
   997         public Type recvtype;
   999         public MethodType(List<Type> argtypes,
  1000                           Type restype,
  1001                           List<Type> thrown,
  1002                           TypeSymbol methodClass) {
  1003             super(METHOD, methodClass);
  1004             this.argtypes = argtypes;
  1005             this.restype = restype;
  1006             this.thrown = thrown;
  1009         @Override
  1010         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1011             return v.visitMethodType(this, s);
  1014         /** The Java source which this type represents.
  1016          *  XXX 06/09/99 iris This isn't correct Java syntax, but it probably
  1017          *  should be.
  1018          */
  1019         public String toString() {
  1020             return "(" + argtypes + ")" + restype;
  1023         public List<Type>        getParameterTypes() { return argtypes; }
  1024         public Type              getReturnType()     { return restype; }
  1025         public Type              getReceiverType()   { return recvtype; }
  1026         public List<Type>        getThrownTypes()    { return thrown; }
  1028         public boolean isErroneous() {
  1029             return
  1030                 isErroneous(argtypes) ||
  1031                 restype != null && restype.isErroneous();
  1034         public Type map(Mapping f) {
  1035             List<Type> argtypes1 = map(argtypes, f);
  1036             Type restype1 = f.apply(restype);
  1037             List<Type> thrown1 = map(thrown, f);
  1038             if (argtypes1 == argtypes &&
  1039                 restype1 == restype &&
  1040                 thrown1 == thrown) return this;
  1041             else return new MethodType(argtypes1, restype1, thrown1, tsym);
  1044         public boolean contains(Type elem) {
  1045             return elem == this || contains(argtypes, elem) || restype.contains(elem);
  1048         public MethodType asMethodType() { return this; }
  1050         public void complete() {
  1051             for (List<Type> l = argtypes; l.nonEmpty(); l = l.tail)
  1052                 l.head.complete();
  1053             restype.complete();
  1054             recvtype.complete();
  1055             for (List<Type> l = thrown; l.nonEmpty(); l = l.tail)
  1056                 l.head.complete();
  1059         public List<TypeVar> getTypeVariables() {
  1060             return List.nil();
  1063         public TypeSymbol asElement() {
  1064             return null;
  1067         public TypeKind getKind() {
  1068             return TypeKind.EXECUTABLE;
  1071         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1072             return v.visitExecutable(this, p);
  1076     public static class PackageType extends Type implements NoType {
  1078         PackageType(TypeSymbol tsym) {
  1079             super(PACKAGE, tsym);
  1082         @Override
  1083         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1084             return v.visitPackageType(this, s);
  1087         public String toString() {
  1088             return tsym.getQualifiedName().toString();
  1091         public TypeKind getKind() {
  1092             return TypeKind.PACKAGE;
  1095         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1096             return v.visitNoType(this, p);
  1100     public static class TypeVar extends Type implements TypeVariable {
  1102         /** The upper bound of this type variable; set from outside.
  1103          *  Must be nonempty once it is set.
  1104          *  For a bound, `bound' is the bound type itself.
  1105          *  Multiple bounds are expressed as a single class type which has the
  1106          *  individual bounds as superclass, respectively interfaces.
  1107          *  The class type then has as `tsym' a compiler generated class `c',
  1108          *  which has a flag COMPOUND and whose owner is the type variable
  1109          *  itself. Furthermore, the erasure_field of the class
  1110          *  points to the first class or interface bound.
  1111          */
  1112         public Type bound = null;
  1114         /** The lower bound of this type variable.
  1115          *  TypeVars don't normally have a lower bound, so it is normally set
  1116          *  to syms.botType.
  1117          *  Subtypes, such as CapturedType, may provide a different value.
  1118          */
  1119         public Type lower;
  1121         public TypeVar(Name name, Symbol owner, Type lower) {
  1122             super(TYPEVAR, null);
  1123             tsym = new TypeSymbol(0, name, this, owner);
  1124             this.lower = lower;
  1127         public TypeVar(TypeSymbol tsym, Type bound, Type lower) {
  1128             super(TYPEVAR, tsym);
  1129             this.bound = bound;
  1130             this.lower = lower;
  1133         @Override
  1134         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1135             return v.visitTypeVar(this, s);
  1138         @Override
  1139         public Type getUpperBound() {
  1140             if ((bound == null || bound.tag == NONE) && this != tsym.type)
  1141                 bound = tsym.type.getUpperBound();
  1142             return bound;
  1145         int rank_field = -1;
  1147         @Override
  1148         public Type getLowerBound() {
  1149             return lower;
  1152         public TypeKind getKind() {
  1153             return TypeKind.TYPEVAR;
  1156         public boolean isCaptured() {
  1157             return false;
  1160         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1161             return v.visitTypeVariable(this, p);
  1165     /** A captured type variable comes from wildcards which can have
  1166      *  both upper and lower bound.  CapturedType extends TypeVar with
  1167      *  a lower bound.
  1168      */
  1169     public static class CapturedType extends TypeVar {
  1171         public WildcardType wildcard;
  1173         public CapturedType(Name name,
  1174                             Symbol owner,
  1175                             Type upper,
  1176                             Type lower,
  1177                             WildcardType wildcard) {
  1178             super(name, owner, lower);
  1179             this.lower = Assert.checkNonNull(lower);
  1180             this.bound = upper;
  1181             this.wildcard = wildcard;
  1184         @Override
  1185         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1186             return v.visitCapturedType(this, s);
  1189         @Override
  1190         public boolean isCaptured() {
  1191             return true;
  1194         @Override
  1195         public String toString() {
  1196             return "capture#"
  1197                 + (hashCode() & 0xFFFFFFFFL) % Printer.PRIME
  1198                 + " of "
  1199                 + wildcard;
  1203     public static abstract class DelegatedType extends Type {
  1204         public Type qtype;
  1205         public DelegatedType(TypeTag tag, Type qtype) {
  1206             super(tag, qtype.tsym);
  1207             this.qtype = qtype;
  1209         public String toString() { return qtype.toString(); }
  1210         public List<Type> getTypeArguments() { return qtype.getTypeArguments(); }
  1211         public Type getEnclosingType() { return qtype.getEnclosingType(); }
  1212         public List<Type> getParameterTypes() { return qtype.getParameterTypes(); }
  1213         public Type getReturnType() { return qtype.getReturnType(); }
  1214         public Type getReceiverType() { return qtype.getReceiverType(); }
  1215         public List<Type> getThrownTypes() { return qtype.getThrownTypes(); }
  1216         public List<Type> allparams() { return qtype.allparams(); }
  1217         public Type getUpperBound() { return qtype.getUpperBound(); }
  1218         public boolean isErroneous() { return qtype.isErroneous(); }
  1221     /**
  1222      * The type of a generic method type. It consists of a method type and
  1223      * a list of method type-parameters that are used within the method
  1224      * type.
  1225      */
  1226     public static class ForAll extends DelegatedType implements ExecutableType {
  1227         public List<Type> tvars;
  1229         public ForAll(List<Type> tvars, Type qtype) {
  1230             super(FORALL, (MethodType)qtype);
  1231             this.tvars = tvars;
  1234         @Override
  1235         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1236             return v.visitForAll(this, s);
  1239         public String toString() {
  1240             return "<" + tvars + ">" + qtype;
  1243         public List<Type> getTypeArguments()   { return tvars; }
  1245         public boolean isErroneous()  {
  1246             return qtype.isErroneous();
  1249         public Type map(Mapping f) {
  1250             return f.apply(qtype);
  1253         public boolean contains(Type elem) {
  1254             return qtype.contains(elem);
  1257         public MethodType asMethodType() {
  1258             return (MethodType)qtype;
  1261         public void complete() {
  1262             for (List<Type> l = tvars; l.nonEmpty(); l = l.tail) {
  1263                 ((TypeVar)l.head).bound.complete();
  1265             qtype.complete();
  1268         public List<TypeVar> getTypeVariables() {
  1269             return List.convert(TypeVar.class, getTypeArguments());
  1272         public TypeKind getKind() {
  1273             return TypeKind.EXECUTABLE;
  1276         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1277             return v.visitExecutable(this, p);
  1281     /** A class for inference variables, for use during method/diamond type
  1282      *  inference. An inference variable has upper/lower bounds and a set
  1283      *  of equality constraints. Such bounds are set during subtyping, type-containment,
  1284      *  type-equality checks, when the types being tested contain inference variables.
  1285      *  A change listener can be attached to an inference variable, to receive notifications
  1286      *  whenever the bounds of an inference variable change.
  1287      */
  1288     public static class UndetVar extends DelegatedType {
  1290         /** Inference variable change listener. The listener method is called
  1291          *  whenever a change to the inference variable's bounds occurs
  1292          */
  1293         public interface UndetVarListener {
  1294             /** called when some inference variable bounds (of given kinds ibs) change */
  1295             void varChanged(UndetVar uv, Set<InferenceBound> ibs);
  1298         /**
  1299          * Inference variable bound kinds
  1300          */
  1301         public enum InferenceBound {
  1302             /** upper bounds */
  1303             UPPER,
  1304             /** lower bounds */
  1305             LOWER,
  1306             /** equality constraints */
  1307             EQ;
  1310         /** inference variable bounds */
  1311         private Map<InferenceBound, List<Type>> bounds;
  1313         /** inference variable's inferred type (set from Infer.java) */
  1314         public Type inst = null;
  1316         /** number of declared (upper) bounds */
  1317         public int declaredCount;
  1319         /** inference variable's change listener */
  1320         public UndetVarListener listener = null;
  1322         @Override
  1323         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1324             return v.visitUndetVar(this, s);
  1327         public UndetVar(TypeVar origin, Types types) {
  1328             super(UNDETVAR, origin);
  1329             bounds = new EnumMap<InferenceBound, List<Type>>(InferenceBound.class);
  1330             List<Type> declaredBounds = types.getBounds(origin);
  1331             declaredCount = declaredBounds.length();
  1332             bounds.put(InferenceBound.UPPER, declaredBounds);
  1333             bounds.put(InferenceBound.LOWER, List.<Type>nil());
  1334             bounds.put(InferenceBound.EQ, List.<Type>nil());
  1337         public String toString() {
  1338             if (inst != null) return inst.toString();
  1339             else return qtype + "?";
  1342         public Type baseType() {
  1343             if (inst != null) return inst.baseType();
  1344             else return this;
  1347         /** get all bounds of a given kind */
  1348         public List<Type> getBounds(InferenceBound... ibs) {
  1349             ListBuffer<Type> buf = ListBuffer.lb();
  1350             for (InferenceBound ib : ibs) {
  1351                 buf.appendList(bounds.get(ib));
  1353             return buf.toList();
  1356         /** get the list of declared (upper) bounds */
  1357         public List<Type> getDeclaredBounds() {
  1358             ListBuffer<Type> buf = ListBuffer.lb();
  1359             int count = 0;
  1360             for (Type b : getBounds(InferenceBound.UPPER)) {
  1361                 if (count++ == declaredCount) break;
  1362                 buf.append(b);
  1364             return buf.toList();
  1367         /** add a bound of a given kind - this might trigger listener notification */
  1368         public void addBound(InferenceBound ib, Type bound, Types types) {
  1369             Type bound2 = toTypeVarMap.apply(bound);
  1370             List<Type> prevBounds = bounds.get(ib);
  1371             for (Type b : prevBounds) {
  1372                 //check for redundancy - use strict version of isSameType on tvars
  1373                 //(as the standard version will lead to false positives w.r.t. clones ivars)
  1374                 if (types.isSameType(b, bound2, true) || bound == qtype) return;
  1376             bounds.put(ib, prevBounds.prepend(bound2));
  1377             notifyChange(EnumSet.of(ib));
  1379         //where
  1380             Type.Mapping toTypeVarMap = new Mapping("toTypeVarMap") {
  1381                 @Override
  1382                 public Type apply(Type t) {
  1383                     if (t.hasTag(UNDETVAR)) {
  1384                         UndetVar uv = (UndetVar)t;
  1385                         return uv.qtype;
  1386                     } else {
  1387                         return t.map(this);
  1390             };
  1392         /** replace types in all bounds - this might trigger listener notification */
  1393         public void substBounds(List<Type> from, List<Type> to, Types types) {
  1394             List<Type> instVars = from.diff(to);
  1395             //if set of instantiated ivars is empty, there's nothing to do!
  1396             if (instVars.isEmpty()) return;
  1397             final EnumSet<InferenceBound> boundsChanged = EnumSet.noneOf(InferenceBound.class);
  1398             UndetVarListener prevListener = listener;
  1399             try {
  1400                 //setup new listener for keeping track of changed bounds
  1401                 listener = new UndetVarListener() {
  1402                     public void varChanged(UndetVar uv, Set<InferenceBound> ibs) {
  1403                         boundsChanged.addAll(ibs);
  1405                 };
  1406                 for (Map.Entry<InferenceBound, List<Type>> _entry : bounds.entrySet()) {
  1407                     InferenceBound ib = _entry.getKey();
  1408                     List<Type> prevBounds = _entry.getValue();
  1409                     ListBuffer<Type> newBounds = ListBuffer.lb();
  1410                     ListBuffer<Type> deps = ListBuffer.lb();
  1411                     //step 1 - re-add bounds that are not dependent on ivars
  1412                     for (Type t : prevBounds) {
  1413                         if (!t.containsAny(instVars)) {
  1414                             newBounds.append(t);
  1415                         } else {
  1416                             deps.append(t);
  1419                     //step 2 - replace bounds
  1420                     bounds.put(ib, newBounds.toList());
  1421                     //step 3 - for each dependency, add new replaced bound
  1422                     for (Type dep : deps) {
  1423                         addBound(ib, types.subst(dep, from, to), types);
  1426             } finally {
  1427                 listener = prevListener;
  1428                 if (!boundsChanged.isEmpty()) {
  1429                     notifyChange(boundsChanged);
  1434         private void notifyChange(EnumSet<InferenceBound> ibs) {
  1435             if (listener != null) {
  1436                 listener.varChanged(this, ibs);
  1441     /** Represents VOID or NONE.
  1442      */
  1443     static class JCNoType extends Type implements NoType {
  1444         public JCNoType(TypeTag tag) {
  1445             super(tag, null);
  1448         @Override
  1449         public TypeKind getKind() {
  1450             switch (tag) {
  1451             case VOID:  return TypeKind.VOID;
  1452             case NONE:  return TypeKind.NONE;
  1453             default:
  1454                 throw new AssertionError("Unexpected tag: " + tag);
  1458         @Override
  1459         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1460             return v.visitNoType(this, p);
  1464     static class BottomType extends Type implements NullType {
  1465         public BottomType() {
  1466             super(BOT, null);
  1469         @Override
  1470         public TypeKind getKind() {
  1471             return TypeKind.NULL;
  1474         @Override
  1475         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1476             return v.visitNull(this, p);
  1479         @Override
  1480         public Type constType(Object value) {
  1481             return this;
  1484         @Override
  1485         public String stringValue() {
  1486             return "null";
  1490     public static class ErrorType extends ClassType
  1491             implements javax.lang.model.type.ErrorType {
  1493         private Type originalType = null;
  1495         public ErrorType(Type originalType, TypeSymbol tsym) {
  1496             super(noType, List.<Type>nil(), null);
  1497             tag = ERROR;
  1498             this.tsym = tsym;
  1499             this.originalType = (originalType == null ? noType : originalType);
  1502         public ErrorType(ClassSymbol c, Type originalType) {
  1503             this(originalType, c);
  1504             c.type = this;
  1505             c.kind = ERR;
  1506             c.members_field = new Scope.ErrorScope(c);
  1509         public ErrorType(Name name, TypeSymbol container, Type originalType) {
  1510             this(new ClassSymbol(PUBLIC|STATIC|ACYCLIC, name, null, container), originalType);
  1513         @Override
  1514         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1515             return v.visitErrorType(this, s);
  1518         public Type constType(Object constValue) { return this; }
  1519         public Type getEnclosingType()           { return this; }
  1520         public Type getReturnType()              { return this; }
  1521         public Type asSub(Symbol sym)            { return this; }
  1522         public Type map(Mapping f)               { return this; }
  1524         public boolean isGenType(Type t)         { return true; }
  1525         public boolean isErroneous()             { return true; }
  1526         public boolean isCompound()              { return false; }
  1527         public boolean isInterface()             { return false; }
  1529         public List<Type> allparams()            { return List.nil(); }
  1530         public List<Type> getTypeArguments()     { return List.nil(); }
  1532         public TypeKind getKind() {
  1533             return TypeKind.ERROR;
  1536         public Type getOriginalType() {
  1537             return originalType;
  1540         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1541             return v.visitError(this, p);
  1545     public static class AnnotatedType extends Type
  1546             implements
  1547                 javax.lang.model.type.ArrayType,
  1548                 javax.lang.model.type.DeclaredType,
  1549                 javax.lang.model.type.PrimitiveType,
  1550                 javax.lang.model.type.TypeVariable,
  1551                 javax.lang.model.type.WildcardType {
  1552         /** The type annotations on this type.
  1553          */
  1554         public List<Attribute.TypeCompound> typeAnnotations;
  1556         /** The underlying type that is annotated.
  1557          */
  1558         public Type underlyingType;
  1560         public AnnotatedType(Type underlyingType) {
  1561             super(underlyingType.tag, underlyingType.tsym);
  1562             this.typeAnnotations = List.nil();
  1563             this.underlyingType = underlyingType;
  1564             Assert.check(!underlyingType.isAnnotated(),
  1565                     "Can't annotate already annotated type: " + underlyingType);
  1568         public AnnotatedType(List<Attribute.TypeCompound> typeAnnotations,
  1569                 Type underlyingType) {
  1570             super(underlyingType.tag, underlyingType.tsym);
  1571             this.typeAnnotations = typeAnnotations;
  1572             this.underlyingType = underlyingType;
  1573             Assert.check(!underlyingType.isAnnotated(),
  1574                     "Can't annotate already annotated type: " + underlyingType +
  1575                     "; adding: " + typeAnnotations);
  1578         @Override
  1579         public boolean isAnnotated() {
  1580             return true;
  1583         @Override
  1584         public TypeKind getKind() {
  1585             return underlyingType.getKind();
  1588         @Override
  1589         public List<? extends AnnotationMirror> getAnnotations() {
  1590             return typeAnnotations;
  1593         @Override
  1594         public Type unannotatedType() {
  1595             return underlyingType;
  1598         @Override
  1599         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1600             return v.visitAnnotatedType(this, s);
  1603         @Override
  1604         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1605             return underlyingType.accept(v, p);
  1608         @Override
  1609         public Type map(Mapping f) {
  1610             underlyingType.map(f);
  1611             return this;
  1614         @Override
  1615         public Type constType(Object constValue) { return underlyingType.constType(constValue); }
  1616         @Override
  1617         public Type getEnclosingType()           { return underlyingType.getEnclosingType(); }
  1619         @Override
  1620         public Type getReturnType()              { return underlyingType.getReturnType(); }
  1621         @Override
  1622         public List<Type> getTypeArguments()     { return underlyingType.getTypeArguments(); }
  1623         @Override
  1624         public List<Type> getParameterTypes()    { return underlyingType.getParameterTypes(); }
  1625         @Override
  1626         public Type getReceiverType()            { return underlyingType.getReceiverType(); }
  1627         @Override
  1628         public List<Type> getThrownTypes()       { return underlyingType.getThrownTypes(); }
  1629         @Override
  1630         public Type getUpperBound()              { return underlyingType.getUpperBound(); }
  1631         @Override
  1632         public Type getLowerBound()              { return underlyingType.getLowerBound(); }
  1634         @Override
  1635         public boolean isErroneous()             { return underlyingType.isErroneous(); }
  1636         @Override
  1637         public boolean isCompound()              { return underlyingType.isCompound(); }
  1638         @Override
  1639         public boolean isInterface()             { return underlyingType.isInterface(); }
  1640         @Override
  1641         public List<Type> allparams()            { return underlyingType.allparams(); }
  1642         @Override
  1643         public boolean isNumeric()               { return underlyingType.isNumeric(); }
  1644         @Override
  1645         public boolean isReference()             { return underlyingType.isReference(); }
  1646         @Override
  1647         public boolean isParameterized()         { return underlyingType.isParameterized(); }
  1648         @Override
  1649         public boolean isRaw()                   { return underlyingType.isRaw(); }
  1650         @Override
  1651         public boolean isFinal()                 { return underlyingType.isFinal(); }
  1652         @Override
  1653         public boolean isSuperBound()            { return underlyingType.isSuperBound(); }
  1654         @Override
  1655         public boolean isExtendsBound()          { return underlyingType.isExtendsBound(); }
  1656         @Override
  1657         public boolean isUnbound()               { return underlyingType.isUnbound(); }
  1659         @Override
  1660         public String toString() {
  1661             // TODO more logic for arrays, etc.
  1662             if (typeAnnotations != null &&
  1663                     !typeAnnotations.isEmpty()) {
  1664                 return "(" + typeAnnotations.toString() + " :: " + underlyingType.toString() + ")";
  1665             } else {
  1666                 return "({} :: " + underlyingType.toString() +")";
  1670         @Override
  1671         public boolean contains(Type t)          { return underlyingType.contains(t); }
  1673         // TODO: attach annotations?
  1674         @Override
  1675         public Type withTypeVar(Type t)          { return underlyingType.withTypeVar(t); }
  1677         // TODO: attach annotations?
  1678         @Override
  1679         public TypeSymbol asElement()            { return underlyingType.asElement(); }
  1681         // TODO: attach annotations?
  1682         @Override
  1683         public MethodType asMethodType()         { return underlyingType.asMethodType(); }
  1685         @Override
  1686         public void complete()                   { underlyingType.complete(); }
  1688         @Override
  1689         public TypeMirror getComponentType()     { return ((ArrayType)underlyingType).getComponentType(); }
  1691         // The result is an ArrayType, but only in the model sense, not the Type sense.
  1692         public AnnotatedType makeVarargs() {
  1693             AnnotatedType atype = new AnnotatedType(((ArrayType)underlyingType).makeVarargs());
  1694             atype.typeAnnotations = this.typeAnnotations;
  1695             return atype;
  1698         @Override
  1699         public TypeMirror getExtendsBound()      { return ((WildcardType)underlyingType).getExtendsBound(); }
  1700         @Override
  1701         public TypeMirror getSuperBound()        { return ((WildcardType)underlyingType).getSuperBound(); }
  1704     /**
  1705      * A visitor for types.  A visitor is used to implement operations
  1706      * (or relations) on types.  Most common operations on types are
  1707      * binary relations and this interface is designed for binary
  1708      * relations, that is, operations of the form
  1709      * Type&nbsp;&times;&nbsp;S&nbsp;&rarr;&nbsp;R.
  1710      * <!-- In plain text: Type x S -> R -->
  1712      * @param <R> the return type of the operation implemented by this
  1713      * visitor; use Void if no return type is needed.
  1714      * @param <S> the type of the second argument (the first being the
  1715      * type itself) of the operation implemented by this visitor; use
  1716      * Void if a second argument is not needed.
  1717      */
  1718     public interface Visitor<R,S> {
  1719         R visitClassType(ClassType t, S s);
  1720         R visitWildcardType(WildcardType t, S s);
  1721         R visitArrayType(ArrayType t, S s);
  1722         R visitMethodType(MethodType t, S s);
  1723         R visitPackageType(PackageType t, S s);
  1724         R visitTypeVar(TypeVar t, S s);
  1725         R visitCapturedType(CapturedType t, S s);
  1726         R visitForAll(ForAll t, S s);
  1727         R visitUndetVar(UndetVar t, S s);
  1728         R visitErrorType(ErrorType t, S s);
  1729         R visitAnnotatedType(AnnotatedType t, S s);
  1730         R visitType(Type t, S s);

mercurial