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

Fri, 12 Apr 2013 12:05:04 +0200

author
jfranck
date
Fri, 12 Apr 2013 12:05:04 +0200
changeset 1689
137994c189e5
parent 1678
c635a966ce84
child 1755
ddb4a2bfcd82
permissions
-rw-r--r--

7015104: use new subtype of TypeSymbol for type parameters
Reviewed-by: jjg, mcimadamore

     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 com.sun.tools.javac.model.JavacAnnoConstructs;
    29 import com.sun.tools.javac.model.JavacTypes;
    30 import java.lang.annotation.Annotation;
    31 import java.util.Collections;
    32 import java.util.EnumMap;
    33 import java.util.EnumSet;
    34 import java.util.Map;
    35 import java.util.Set;
    37 import javax.lang.model.element.AnnotationMirror;
    38 import javax.lang.model.type.*;
    40 import com.sun.tools.javac.code.Symbol.*;
    41 import com.sun.tools.javac.util.*;
    42 import static com.sun.tools.javac.code.BoundKind.*;
    43 import static com.sun.tools.javac.code.Flags.*;
    44 import static com.sun.tools.javac.code.Kinds.*;
    45 import static com.sun.tools.javac.code.TypeTag.*;
    47 /** This class represents Java types. The class itself defines the behavior of
    48  *  the following types:
    49  *  <pre>
    50  *  base types (tags: BYTE, CHAR, SHORT, INT, LONG, FLOAT, DOUBLE, BOOLEAN),
    51  *  type `void' (tag: VOID),
    52  *  the bottom type (tag: BOT),
    53  *  the missing type (tag: NONE).
    54  *  </pre>
    55  *  <p>The behavior of the following types is defined in subclasses, which are
    56  *  all static inner classes of this class:
    57  *  <pre>
    58  *  class types (tag: CLASS, class: ClassType),
    59  *  array types (tag: ARRAY, class: ArrayType),
    60  *  method types (tag: METHOD, class: MethodType),
    61  *  package types (tag: PACKAGE, class: PackageType),
    62  *  type variables (tag: TYPEVAR, class: TypeVar),
    63  *  type arguments (tag: WILDCARD, class: WildcardType),
    64  *  generic method types (tag: FORALL, class: ForAll),
    65  *  the error type (tag: ERROR, class: ErrorType).
    66  *  </pre>
    67  *
    68  *  <p><b>This is NOT part of any supported API.
    69  *  If you write code that depends on this, you do so at your own risk.
    70  *  This code and its internal interfaces are subject to change or
    71  *  deletion without notice.</b>
    72  *
    73  *  @see TypeTag
    74  */
    75 public class Type implements PrimitiveType {
    77     /** Constant type: no type at all. */
    78     public static final JCNoType noType = new JCNoType(NONE);
    80     /** Constant type: special type to be used during recovery of deferred expressions. */
    81     public static final JCNoType recoveryType = new JCNoType(NONE);
    83     /** If this switch is turned on, the names of type variables
    84      *  and anonymous classes are printed with hashcodes appended.
    85      */
    86     public static boolean moreInfo = false;
    88     /** The tag of this type.
    89      *
    90      *  @see TypeTag
    91      */
    92     protected TypeTag tag;
    94     /** The defining class / interface / package / type variable.
    95      */
    96     public TypeSymbol tsym;
    98     /**
    99      * Checks if the current type tag is equal to the given tag.
   100      * @return true if tag is equal to the current type tag.
   101      */
   102     public boolean hasTag(TypeTag tag) {
   103         return this.tag == tag;
   104     }
   106     /**
   107      * Returns the current type tag.
   108      * @return the value of the current type tag.
   109      */
   110     public TypeTag getTag() {
   111         return tag;
   112     }
   114     public boolean isNumeric() {
   115         switch (tag) {
   116             case BYTE: case CHAR:
   117             case SHORT:
   118             case INT: case LONG:
   119             case FLOAT: case DOUBLE:
   120                 return true;
   121             default:
   122                 return false;
   123         }
   124     }
   126     public boolean isPrimitive() {
   127         return (isNumeric() || tag == BOOLEAN);
   128     }
   130     public boolean isPrimitiveOrVoid() {
   131         return (isPrimitive() || tag == VOID);
   132     }
   134     public boolean isReference() {
   135         switch (tag) {
   136         case CLASS:
   137         case ARRAY:
   138         case TYPEVAR:
   139         case WILDCARD:
   140         case ERROR:
   141             return true;
   142         default:
   143             return false;
   144         }
   145     }
   147     public boolean isNullOrReference() {
   148         return (tag == BOT || isReference());
   149     }
   151     public boolean isPartial() {
   152         switch(tag) {
   153             case ERROR: case UNKNOWN: case UNDETVAR:
   154                 return true;
   155             default:
   156                 return false;
   157         }
   158     }
   160     /**
   161      * The constant value of this type, null if this type does not
   162      * have a constant value attribute. Only primitive types and
   163      * strings (ClassType) can have a constant value attribute.
   164      * @return the constant value attribute of this type
   165      */
   166     public Object constValue() {
   167         return null;
   168     }
   170     /**
   171      * Get the representation of this type used for modelling purposes.
   172      * By default, this is itself. For ErrorType, a different value
   173      * may be provided.
   174      */
   175     public Type getModelType() {
   176         return this;
   177     }
   179     public static List<Type> getModelTypes(List<Type> ts) {
   180         ListBuffer<Type> lb = new ListBuffer<Type>();
   181         for (Type t: ts)
   182             lb.append(t.getModelType());
   183         return lb.toList();
   184     }
   186     public <R,S> R accept(Type.Visitor<R,S> v, S s) { return v.visitType(this, s); }
   188     /** Define a type given its tag and type symbol
   189      */
   190     public Type(TypeTag tag, TypeSymbol tsym) {
   191         this.tag = tag;
   192         this.tsym = tsym;
   193     }
   195     /** An abstract class for mappings from types to types
   196      */
   197     public static abstract class Mapping {
   198         private String name;
   199         public Mapping(String name) {
   200             this.name = name;
   201         }
   202         public abstract Type apply(Type t);
   203         public String toString() {
   204             return name;
   205         }
   206     }
   208     /** map a type function over all immediate descendants of this type
   209      */
   210     public Type map(Mapping f) {
   211         return this;
   212     }
   214     /** map a type function over a list of types
   215      */
   216     public static List<Type> map(List<Type> ts, Mapping f) {
   217         if (ts.nonEmpty()) {
   218             List<Type> tail1 = map(ts.tail, f);
   219             Type t = f.apply(ts.head);
   220             if (tail1 != ts.tail || t != ts.head)
   221                 return tail1.prepend(t);
   222         }
   223         return ts;
   224     }
   226     /** Define a constant type, of the same kind as this type
   227      *  and with given constant value
   228      */
   229     public Type constType(Object constValue) {
   230         final Object value = constValue;
   231         Assert.check(isPrimitive());
   232         return new Type(tag, tsym) {
   233                 @Override
   234                 public Object constValue() {
   235                     return value;
   236                 }
   237                 @Override
   238                 public Type baseType() {
   239                     return tsym.type;
   240                 }
   241             };
   242     }
   244     /**
   245      * If this is a constant type, return its underlying type.
   246      * Otherwise, return the type itself.
   247      */
   248     public Type baseType() {
   249         return this;
   250     }
   252     public boolean isAnnotated() {
   253         return false;
   254     }
   256     /**
   257      * If this is an annotated type, return the underlying type.
   258      * Otherwise, return the type itself.
   259      */
   260     public Type unannotatedType() {
   261         return this;
   262     }
   264     @Override
   265     public List<? extends Attribute.TypeCompound> getAnnotationMirrors() {
   266         return List.nil();
   267     }
   269     @Override
   270     public <A extends Annotation> A getAnnotation(Class<A> annotationType) {
   271         return null;
   272     }
   274     @Override
   275     public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationType) {
   276         @SuppressWarnings("unchecked")
   277         A[] tmp = (A[]) java.lang.reflect.Array.newInstance(annotationType, 0);
   278         return tmp;
   279     }
   281     /** Return the base types of a list of types.
   282      */
   283     public static List<Type> baseTypes(List<Type> ts) {
   284         if (ts.nonEmpty()) {
   285             Type t = ts.head.baseType();
   286             List<Type> baseTypes = baseTypes(ts.tail);
   287             if (t != ts.head || baseTypes != ts.tail)
   288                 return baseTypes.prepend(t);
   289         }
   290         return ts;
   291     }
   293     /** The Java source which this type represents.
   294      */
   295     public String toString() {
   296         String s = (tsym == null || tsym.name == null)
   297             ? "<none>"
   298             : tsym.name.toString();
   299         if (moreInfo && tag == TYPEVAR) s = s + hashCode();
   300         return s;
   301     }
   303     /**
   304      * The Java source which this type list represents.  A List is
   305      * represented as a comma-spearated listing of the elements in
   306      * that list.
   307      */
   308     public static String toString(List<Type> ts) {
   309         if (ts.isEmpty()) {
   310             return "";
   311         } else {
   312             StringBuilder buf = new StringBuilder();
   313             buf.append(ts.head.toString());
   314             for (List<Type> l = ts.tail; l.nonEmpty(); l = l.tail)
   315                 buf.append(",").append(l.head.toString());
   316             return buf.toString();
   317         }
   318     }
   320     /**
   321      * The constant value of this type, converted to String
   322      */
   323     public String stringValue() {
   324         Object cv = Assert.checkNonNull(constValue());
   325         if (tag == BOOLEAN)
   326             return ((Integer) cv).intValue() == 0 ? "false" : "true";
   327         else if (tag == CHAR)
   328             return String.valueOf((char) ((Integer) cv).intValue());
   329         else
   330             return cv.toString();
   331     }
   333     /**
   334      * This method is analogous to isSameType, but weaker, since we
   335      * never complete classes. Where isSameType would complete a
   336      * class, equals assumes that the two types are different.
   337      */
   338     @Override
   339     public boolean equals(Object t) {
   340         return super.equals(t);
   341     }
   343     @Override
   344     public int hashCode() {
   345         return super.hashCode();
   346     }
   348     /** Is this a constant type whose value is false?
   349      */
   350     public boolean isFalse() {
   351         return
   352             tag == BOOLEAN &&
   353             constValue() != null &&
   354             ((Integer)constValue()).intValue() == 0;
   355     }
   357     /** Is this a constant type whose value is true?
   358      */
   359     public boolean isTrue() {
   360         return
   361             tag == BOOLEAN &&
   362             constValue() != null &&
   363             ((Integer)constValue()).intValue() != 0;
   364     }
   366     public String argtypes(boolean varargs) {
   367         List<Type> args = getParameterTypes();
   368         if (!varargs) return args.toString();
   369         StringBuilder buf = new StringBuilder();
   370         while (args.tail.nonEmpty()) {
   371             buf.append(args.head);
   372             args = args.tail;
   373             buf.append(',');
   374         }
   375         if (args.head.unannotatedType().tag == ARRAY) {
   376             buf.append(((ArrayType)args.head.unannotatedType()).elemtype);
   377             if (args.head.getAnnotationMirrors().nonEmpty()) {
   378                 buf.append(args.head.getAnnotationMirrors());
   379             }
   380             buf.append("...");
   381         } else {
   382             buf.append(args.head);
   383         }
   384         return buf.toString();
   385     }
   387     /** Access methods.
   388      */
   389     public List<Type>        getTypeArguments()  { return List.nil(); }
   390     public Type              getEnclosingType()  { return null; }
   391     public List<Type>        getParameterTypes() { return List.nil(); }
   392     public Type              getReturnType()     { return null; }
   393     public Type              getReceiverType()   { return null; }
   394     public List<Type>        getThrownTypes()    { return List.nil(); }
   395     public Type              getUpperBound()     { return null; }
   396     public Type              getLowerBound()     { return null; }
   398     /** Navigation methods, these will work for classes, type variables,
   399      *  foralls, but will return null for arrays and methods.
   400      */
   402    /** Return all parameters of this type and all its outer types in order
   403     *  outer (first) to inner (last).
   404     */
   405     public List<Type> allparams() { return List.nil(); }
   407     /** Does this type contain "error" elements?
   408      */
   409     public boolean isErroneous() {
   410         return false;
   411     }
   413     public static boolean isErroneous(List<Type> ts) {
   414         for (List<Type> l = ts; l.nonEmpty(); l = l.tail)
   415             if (l.head.isErroneous()) return true;
   416         return false;
   417     }
   419     /** Is this type parameterized?
   420      *  A class type is parameterized if it has some parameters.
   421      *  An array type is parameterized if its element type is parameterized.
   422      *  All other types are not parameterized.
   423      */
   424     public boolean isParameterized() {
   425         return false;
   426     }
   428     /** Is this type a raw type?
   429      *  A class type is a raw type if it misses some of its parameters.
   430      *  An array type is a raw type if its element type is raw.
   431      *  All other types are not raw.
   432      *  Type validation will ensure that the only raw types
   433      *  in a program are types that miss all their type variables.
   434      */
   435     public boolean isRaw() {
   436         return false;
   437     }
   439     public boolean isCompound() {
   440         return tsym.completer == null
   441             // Compound types can't have a completer.  Calling
   442             // flags() will complete the symbol causing the
   443             // compiler to load classes unnecessarily.  This led
   444             // to regression 6180021.
   445             && (tsym.flags() & COMPOUND) != 0;
   446     }
   448     public boolean isInterface() {
   449         return (tsym.flags() & INTERFACE) != 0;
   450     }
   452     public boolean isFinal() {
   453         return (tsym.flags() & FINAL) != 0;
   454     }
   456     /**
   457      * Does this type contain occurrences of type t?
   458      */
   459     public boolean contains(Type t) {
   460         return t == this;
   461     }
   463     public static boolean contains(List<Type> ts, Type t) {
   464         for (List<Type> l = ts;
   465              l.tail != null /*inlined: l.nonEmpty()*/;
   466              l = l.tail)
   467             if (l.head.contains(t)) return true;
   468         return false;
   469     }
   471     /** Does this type contain an occurrence of some type in 'ts'?
   472      */
   473     public boolean containsAny(List<Type> ts) {
   474         for (Type t : ts)
   475             if (this.contains(t)) return true;
   476         return false;
   477     }
   479     public static boolean containsAny(List<Type> ts1, List<Type> ts2) {
   480         for (Type t : ts1)
   481             if (t.containsAny(ts2)) return true;
   482         return false;
   483     }
   485     public static List<Type> filter(List<Type> ts, Filter<Type> tf) {
   486         ListBuffer<Type> buf = ListBuffer.lb();
   487         for (Type t : ts) {
   488             if (tf.accepts(t)) {
   489                 buf.append(t);
   490             }
   491         }
   492         return buf.toList();
   493     }
   495     public boolean isSuperBound() { return false; }
   496     public boolean isExtendsBound() { return false; }
   497     public boolean isUnbound() { return false; }
   498     public Type withTypeVar(Type t) { return this; }
   500     /** The underlying method type of this type.
   501      */
   502     public MethodType asMethodType() { throw new AssertionError(); }
   504     /** Complete loading all classes in this type.
   505      */
   506     public void complete() {}
   508     public TypeSymbol asElement() {
   509         return tsym;
   510     }
   512     public TypeKind getKind() {
   513         switch (tag) {
   514         case BYTE:      return TypeKind.BYTE;
   515         case CHAR:      return TypeKind.CHAR;
   516         case SHORT:     return TypeKind.SHORT;
   517         case INT:       return TypeKind.INT;
   518         case LONG:      return TypeKind.LONG;
   519         case FLOAT:     return TypeKind.FLOAT;
   520         case DOUBLE:    return TypeKind.DOUBLE;
   521         case BOOLEAN:   return TypeKind.BOOLEAN;
   522         case VOID:      return TypeKind.VOID;
   523         case BOT:       return TypeKind.NULL;
   524         case NONE:      return TypeKind.NONE;
   525         default:        return TypeKind.OTHER;
   526         }
   527     }
   529     public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   530         if (isPrimitive())
   531             return v.visitPrimitive(this, p);
   532         else
   533             throw new AssertionError();
   534     }
   536     public static class WildcardType extends Type
   537             implements javax.lang.model.type.WildcardType {
   539         public Type type;
   540         public BoundKind kind;
   541         public TypeVar bound;
   543         @Override
   544         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   545             return v.visitWildcardType(this, s);
   546         }
   548         public WildcardType(Type type, BoundKind kind, TypeSymbol tsym) {
   549             super(WILDCARD, tsym);
   550             this.type = Assert.checkNonNull(type);
   551             this.kind = kind;
   552         }
   553         public WildcardType(WildcardType t, TypeVar bound) {
   554             this(t.type, t.kind, t.tsym, bound);
   555         }
   557         public WildcardType(Type type, BoundKind kind, TypeSymbol tsym, TypeVar bound) {
   558             this(type, kind, tsym);
   559             this.bound = bound;
   560         }
   562         public boolean contains(Type t) {
   563             return kind != UNBOUND && type.contains(t);
   564         }
   566         public boolean isSuperBound() {
   567             return kind == SUPER ||
   568                 kind == UNBOUND;
   569         }
   570         public boolean isExtendsBound() {
   571             return kind == EXTENDS ||
   572                 kind == UNBOUND;
   573         }
   574         public boolean isUnbound() {
   575             return kind == UNBOUND;
   576         }
   578         public Type withTypeVar(Type t) {
   579             //-System.err.println(this+".withTypeVar("+t+");");//DEBUG
   580             if (bound == t)
   581                 return this;
   582             bound = (TypeVar)t;
   583             return this;
   584         }
   586         boolean isPrintingBound = false;
   587         public String toString() {
   588             StringBuilder s = new StringBuilder();
   589             s.append(kind.toString());
   590             if (kind != UNBOUND)
   591                 s.append(type);
   592             if (moreInfo && bound != null && !isPrintingBound)
   593                 try {
   594                     isPrintingBound = true;
   595                     s.append("{:").append(bound.bound).append(":}");
   596                 } finally {
   597                     isPrintingBound = false;
   598                 }
   599             return s.toString();
   600         }
   602         public Type map(Mapping f) {
   603             //- System.err.println("   (" + this + ").map(" + f + ")");//DEBUG
   604             Type t = type;
   605             if (t != null)
   606                 t = f.apply(t);
   607             if (t == type)
   608                 return this;
   609             else
   610                 return new WildcardType(t, kind, tsym, bound);
   611         }
   613         public Type getExtendsBound() {
   614             if (kind == EXTENDS)
   615                 return type;
   616             else
   617                 return null;
   618         }
   620         public Type getSuperBound() {
   621             if (kind == SUPER)
   622                 return type;
   623             else
   624                 return null;
   625         }
   627         public TypeKind getKind() {
   628             return TypeKind.WILDCARD;
   629         }
   631         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   632             return v.visitWildcard(this, p);
   633         }
   634     }
   636     public static class ClassType extends Type implements DeclaredType {
   638         /** The enclosing type of this type. If this is the type of an inner
   639          *  class, outer_field refers to the type of its enclosing
   640          *  instance class, in all other cases it refers to noType.
   641          */
   642         private Type outer_field;
   644         /** The type parameters of this type (to be set once class is loaded).
   645          */
   646         public List<Type> typarams_field;
   648         /** A cache variable for the type parameters of this type,
   649          *  appended to all parameters of its enclosing class.
   650          *  @see #allparams
   651          */
   652         public List<Type> allparams_field;
   654         /** The supertype of this class (to be set once class is loaded).
   655          */
   656         public Type supertype_field;
   658         /** The interfaces of this class (to be set once class is loaded).
   659          */
   660         public List<Type> interfaces_field;
   662         /** All the interfaces of this class, including missing ones.
   663          */
   664         public List<Type> all_interfaces_field;
   666         public ClassType(Type outer, List<Type> typarams, TypeSymbol tsym) {
   667             super(CLASS, tsym);
   668             this.outer_field = outer;
   669             this.typarams_field = typarams;
   670             this.allparams_field = null;
   671             this.supertype_field = null;
   672             this.interfaces_field = null;
   673             /*
   674             // this can happen during error recovery
   675             assert
   676                 outer.isParameterized() ?
   677                 typarams.length() == tsym.type.typarams().length() :
   678                 outer.isRaw() ?
   679                 typarams.length() == 0 :
   680                 true;
   681             */
   682         }
   684         @Override
   685         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   686             return v.visitClassType(this, s);
   687         }
   689         public Type constType(Object constValue) {
   690             final Object value = constValue;
   691             return new ClassType(getEnclosingType(), typarams_field, tsym) {
   692                     @Override
   693                     public Object constValue() {
   694                         return value;
   695                     }
   696                     @Override
   697                     public Type baseType() {
   698                         return tsym.type;
   699                     }
   700                 };
   701         }
   703         /** The Java source which this type represents.
   704          */
   705         public String toString() {
   706             StringBuilder buf = new StringBuilder();
   707             if (getEnclosingType().tag == CLASS && tsym.owner.kind == TYP) {
   708                 buf.append(getEnclosingType().toString());
   709                 buf.append(".");
   710                 buf.append(className(tsym, false));
   711             } else {
   712                 buf.append(className(tsym, true));
   713             }
   714             if (getTypeArguments().nonEmpty()) {
   715                 buf.append('<');
   716                 buf.append(getTypeArguments().toString());
   717                 buf.append(">");
   718             }
   719             return buf.toString();
   720         }
   721 //where
   722             private String className(Symbol sym, boolean longform) {
   723                 if (sym.name.isEmpty() && (sym.flags() & COMPOUND) != 0) {
   724                     StringBuilder s = new StringBuilder(supertype_field.toString());
   725                     for (List<Type> is=interfaces_field; is.nonEmpty(); is = is.tail) {
   726                         s.append("&");
   727                         s.append(is.head.toString());
   728                     }
   729                     return s.toString();
   730                 } else if (sym.name.isEmpty()) {
   731                     String s;
   732                     ClassType norm = (ClassType) tsym.type;
   733                     if (norm == null) {
   734                         s = Log.getLocalizedString("anonymous.class", (Object)null);
   735                     } else if (norm.interfaces_field != null && norm.interfaces_field.nonEmpty()) {
   736                         s = Log.getLocalizedString("anonymous.class",
   737                                                    norm.interfaces_field.head);
   738                     } else {
   739                         s = Log.getLocalizedString("anonymous.class",
   740                                                    norm.supertype_field);
   741                     }
   742                     if (moreInfo)
   743                         s += String.valueOf(sym.hashCode());
   744                     return s;
   745                 } else if (longform) {
   746                     return sym.getQualifiedName().toString();
   747                 } else {
   748                     return sym.name.toString();
   749                 }
   750             }
   752         public List<Type> getTypeArguments() {
   753             if (typarams_field == null) {
   754                 complete();
   755                 if (typarams_field == null)
   756                     typarams_field = List.nil();
   757             }
   758             return typarams_field;
   759         }
   761         public boolean hasErasedSupertypes() {
   762             return isRaw();
   763         }
   765         public Type getEnclosingType() {
   766             return outer_field;
   767         }
   769         public void setEnclosingType(Type outer) {
   770             outer_field = outer;
   771         }
   773         public List<Type> allparams() {
   774             if (allparams_field == null) {
   775                 allparams_field = getTypeArguments().prependList(getEnclosingType().allparams());
   776             }
   777             return allparams_field;
   778         }
   780         public boolean isErroneous() {
   781             return
   782                 getEnclosingType().isErroneous() ||
   783                 isErroneous(getTypeArguments()) ||
   784                 this != tsym.type && tsym.type.isErroneous();
   785         }
   787         public boolean isParameterized() {
   788             return allparams().tail != null;
   789             // optimization, was: allparams().nonEmpty();
   790         }
   792         /** A cache for the rank. */
   793         int rank_field = -1;
   795         /** A class type is raw if it misses some
   796          *  of its type parameter sections.
   797          *  After validation, this is equivalent to:
   798          *  {@code allparams.isEmpty() && tsym.type.allparams.nonEmpty(); }
   799          */
   800         public boolean isRaw() {
   801             return
   802                 this != tsym.type && // necessary, but not sufficient condition
   803                 tsym.type.allparams().nonEmpty() &&
   804                 allparams().isEmpty();
   805         }
   807         public Type map(Mapping f) {
   808             Type outer = getEnclosingType();
   809             Type outer1 = f.apply(outer);
   810             List<Type> typarams = getTypeArguments();
   811             List<Type> typarams1 = map(typarams, f);
   812             if (outer1 == outer && typarams1 == typarams) return this;
   813             else return new ClassType(outer1, typarams1, tsym);
   814         }
   816         public boolean contains(Type elem) {
   817             return
   818                 elem == this
   819                 || (isParameterized()
   820                     && (getEnclosingType().contains(elem) || contains(getTypeArguments(), elem)))
   821                 || (isCompound()
   822                     && (supertype_field.contains(elem) || contains(interfaces_field, elem)));
   823         }
   825         public void complete() {
   826             if (tsym.completer != null) tsym.complete();
   827         }
   829         public TypeKind getKind() {
   830             return TypeKind.DECLARED;
   831         }
   833         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   834             return v.visitDeclared(this, p);
   835         }
   836     }
   838     public static class ErasedClassType extends ClassType {
   839         public ErasedClassType(Type outer, TypeSymbol tsym) {
   840             super(outer, List.<Type>nil(), tsym);
   841         }
   843         @Override
   844         public boolean hasErasedSupertypes() {
   845             return true;
   846         }
   847     }
   849     // a clone of a ClassType that knows about the alternatives of a union type.
   850     public static class UnionClassType extends ClassType implements UnionType {
   851         final List<? extends Type> alternatives_field;
   853         public UnionClassType(ClassType ct, List<? extends Type> alternatives) {
   854             super(ct.outer_field, ct.typarams_field, ct.tsym);
   855             allparams_field = ct.allparams_field;
   856             supertype_field = ct.supertype_field;
   857             interfaces_field = ct.interfaces_field;
   858             all_interfaces_field = ct.interfaces_field;
   859             alternatives_field = alternatives;
   860         }
   862         public Type getLub() {
   863             return tsym.type;
   864         }
   866         public java.util.List<? extends TypeMirror> getAlternatives() {
   867             return Collections.unmodifiableList(alternatives_field);
   868         }
   870         @Override
   871         public TypeKind getKind() {
   872             return TypeKind.UNION;
   873         }
   875         @Override
   876         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   877             return v.visitUnion(this, p);
   878         }
   879     }
   881     // a clone of a ClassType that knows about the bounds of an intersection type.
   882     public static class IntersectionClassType extends ClassType implements IntersectionType {
   884         public boolean allInterfaces;
   886         public enum IntersectionKind {
   887             EXPLICIT,
   888             IMPLICT;
   889         }
   891         public IntersectionKind intersectionKind;
   893         public IntersectionClassType(List<Type> bounds, ClassSymbol csym, boolean allInterfaces) {
   894             super(Type.noType, List.<Type>nil(), csym);
   895             this.allInterfaces = allInterfaces;
   896             Assert.check((csym.flags() & COMPOUND) != 0);
   897             supertype_field = bounds.head;
   898             interfaces_field = bounds.tail;
   899             Assert.check(supertype_field.tsym.completer != null ||
   900                     !supertype_field.isInterface(), supertype_field);
   901         }
   903         public java.util.List<? extends TypeMirror> getBounds() {
   904             return Collections.unmodifiableList(getComponents());
   905         }
   907         public List<Type> getComponents() {
   908             return interfaces_field.prepend(supertype_field);
   909         }
   911         public List<Type> getExplicitComponents() {
   912             return allInterfaces ?
   913                     interfaces_field :
   914                     getComponents();
   915         }
   917         @Override
   918         public TypeKind getKind() {
   919             return TypeKind.INTERSECTION;
   920         }
   922         @Override
   923         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   924             return intersectionKind == IntersectionKind.EXPLICIT ?
   925                 v.visitIntersection(this, p) :
   926                 v.visitDeclared(this, p);
   927         }
   928     }
   930     public static class ArrayType extends Type
   931             implements javax.lang.model.type.ArrayType {
   933         public Type elemtype;
   935         public ArrayType(Type elemtype, TypeSymbol arrayClass) {
   936             super(ARRAY, arrayClass);
   937             this.elemtype = elemtype;
   938         }
   940         @Override
   941         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   942             return v.visitArrayType(this, s);
   943         }
   945         public String toString() {
   946             return elemtype + "[]";
   947         }
   949         public boolean equals(Object obj) {
   950             return
   951                 this == obj ||
   952                 (obj instanceof ArrayType &&
   953                  this.elemtype.equals(((ArrayType)obj).elemtype));
   954         }
   956         public int hashCode() {
   957             return (ARRAY.ordinal() << 5) + elemtype.hashCode();
   958         }
   960         public boolean isVarargs() {
   961             return false;
   962         }
   964         public List<Type> allparams() { return elemtype.allparams(); }
   966         public boolean isErroneous() {
   967             return elemtype.isErroneous();
   968         }
   970         public boolean isParameterized() {
   971             return elemtype.isParameterized();
   972         }
   974         public boolean isRaw() {
   975             return elemtype.isRaw();
   976         }
   978         public ArrayType makeVarargs() {
   979             return new ArrayType(elemtype, tsym) {
   980                 @Override
   981                 public boolean isVarargs() {
   982                     return true;
   983                 }
   984             };
   985         }
   987         public Type map(Mapping f) {
   988             Type elemtype1 = f.apply(elemtype);
   989             if (elemtype1 == elemtype) return this;
   990             else return new ArrayType(elemtype1, tsym);
   991         }
   993         public boolean contains(Type elem) {
   994             return elem == this || elemtype.contains(elem);
   995         }
   997         public void complete() {
   998             elemtype.complete();
   999         }
  1001         public Type getComponentType() {
  1002             return elemtype;
  1005         public TypeKind getKind() {
  1006             return TypeKind.ARRAY;
  1009         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1010             return v.visitArray(this, p);
  1014     public static class MethodType extends Type implements ExecutableType {
  1016         public List<Type> argtypes;
  1017         public Type restype;
  1018         public List<Type> thrown;
  1020         /** The type annotations on the method receiver.
  1021          */
  1022         public Type recvtype;
  1024         public MethodType(List<Type> argtypes,
  1025                           Type restype,
  1026                           List<Type> thrown,
  1027                           TypeSymbol methodClass) {
  1028             super(METHOD, methodClass);
  1029             this.argtypes = argtypes;
  1030             this.restype = restype;
  1031             this.thrown = thrown;
  1034         @Override
  1035         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1036             return v.visitMethodType(this, s);
  1039         /** The Java source which this type represents.
  1041          *  XXX 06/09/99 iris This isn't correct Java syntax, but it probably
  1042          *  should be.
  1043          */
  1044         public String toString() {
  1045             return "(" + argtypes + ")" + restype;
  1048         public List<Type>        getParameterTypes() { return argtypes; }
  1049         public Type              getReturnType()     { return restype; }
  1050         public Type              getReceiverType()   { return recvtype; }
  1051         public List<Type>        getThrownTypes()    { return thrown; }
  1053         public boolean isErroneous() {
  1054             return
  1055                 isErroneous(argtypes) ||
  1056                 restype != null && restype.isErroneous();
  1059         public Type map(Mapping f) {
  1060             List<Type> argtypes1 = map(argtypes, f);
  1061             Type restype1 = f.apply(restype);
  1062             List<Type> thrown1 = map(thrown, f);
  1063             if (argtypes1 == argtypes &&
  1064                 restype1 == restype &&
  1065                 thrown1 == thrown) return this;
  1066             else return new MethodType(argtypes1, restype1, thrown1, tsym);
  1069         public boolean contains(Type elem) {
  1070             return elem == this || contains(argtypes, elem) || restype.contains(elem);
  1073         public MethodType asMethodType() { return this; }
  1075         public void complete() {
  1076             for (List<Type> l = argtypes; l.nonEmpty(); l = l.tail)
  1077                 l.head.complete();
  1078             restype.complete();
  1079             recvtype.complete();
  1080             for (List<Type> l = thrown; l.nonEmpty(); l = l.tail)
  1081                 l.head.complete();
  1084         public List<TypeVar> getTypeVariables() {
  1085             return List.nil();
  1088         public TypeSymbol asElement() {
  1089             return null;
  1092         public TypeKind getKind() {
  1093             return TypeKind.EXECUTABLE;
  1096         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1097             return v.visitExecutable(this, p);
  1101     public static class PackageType extends Type implements NoType {
  1103         PackageType(TypeSymbol tsym) {
  1104             super(PACKAGE, tsym);
  1107         @Override
  1108         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1109             return v.visitPackageType(this, s);
  1112         public String toString() {
  1113             return tsym.getQualifiedName().toString();
  1116         public TypeKind getKind() {
  1117             return TypeKind.PACKAGE;
  1120         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1121             return v.visitNoType(this, p);
  1125     public static class TypeVar extends Type implements TypeVariable {
  1127         /** The upper bound of this type variable; set from outside.
  1128          *  Must be nonempty once it is set.
  1129          *  For a bound, `bound' is the bound type itself.
  1130          *  Multiple bounds are expressed as a single class type which has the
  1131          *  individual bounds as superclass, respectively interfaces.
  1132          *  The class type then has as `tsym' a compiler generated class `c',
  1133          *  which has a flag COMPOUND and whose owner is the type variable
  1134          *  itself. Furthermore, the erasure_field of the class
  1135          *  points to the first class or interface bound.
  1136          */
  1137         public Type bound = null;
  1139         /** The lower bound of this type variable.
  1140          *  TypeVars don't normally have a lower bound, so it is normally set
  1141          *  to syms.botType.
  1142          *  Subtypes, such as CapturedType, may provide a different value.
  1143          */
  1144         public Type lower;
  1146         public TypeVar(Name name, Symbol owner, Type lower) {
  1147             super(TYPEVAR, null);
  1148             tsym = new TypeVariableSymbol(0, name, this, owner);
  1149             this.lower = lower;
  1152         public TypeVar(TypeSymbol tsym, Type bound, Type lower) {
  1153             super(TYPEVAR, tsym);
  1154             this.bound = bound;
  1155             this.lower = lower;
  1158         @Override
  1159         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1160             return v.visitTypeVar(this, s);
  1163         @Override
  1164         public Type getUpperBound() {
  1165             if ((bound == null || bound.tag == NONE) && this != tsym.type)
  1166                 bound = tsym.type.getUpperBound();
  1167             return bound;
  1170         int rank_field = -1;
  1172         @Override
  1173         public Type getLowerBound() {
  1174             return lower;
  1177         public TypeKind getKind() {
  1178             return TypeKind.TYPEVAR;
  1181         public boolean isCaptured() {
  1182             return false;
  1185         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1186             return v.visitTypeVariable(this, p);
  1190     /** A captured type variable comes from wildcards which can have
  1191      *  both upper and lower bound.  CapturedType extends TypeVar with
  1192      *  a lower bound.
  1193      */
  1194     public static class CapturedType extends TypeVar {
  1196         public WildcardType wildcard;
  1198         public CapturedType(Name name,
  1199                             Symbol owner,
  1200                             Type upper,
  1201                             Type lower,
  1202                             WildcardType wildcard) {
  1203             super(name, owner, lower);
  1204             this.lower = Assert.checkNonNull(lower);
  1205             this.bound = upper;
  1206             this.wildcard = wildcard;
  1209         @Override
  1210         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1211             return v.visitCapturedType(this, s);
  1214         @Override
  1215         public boolean isCaptured() {
  1216             return true;
  1219         @Override
  1220         public String toString() {
  1221             return "capture#"
  1222                 + (hashCode() & 0xFFFFFFFFL) % Printer.PRIME
  1223                 + " of "
  1224                 + wildcard;
  1228     public static abstract class DelegatedType extends Type {
  1229         public Type qtype;
  1230         public DelegatedType(TypeTag tag, Type qtype) {
  1231             super(tag, qtype.tsym);
  1232             this.qtype = qtype;
  1234         public String toString() { return qtype.toString(); }
  1235         public List<Type> getTypeArguments() { return qtype.getTypeArguments(); }
  1236         public Type getEnclosingType() { return qtype.getEnclosingType(); }
  1237         public List<Type> getParameterTypes() { return qtype.getParameterTypes(); }
  1238         public Type getReturnType() { return qtype.getReturnType(); }
  1239         public Type getReceiverType() { return qtype.getReceiverType(); }
  1240         public List<Type> getThrownTypes() { return qtype.getThrownTypes(); }
  1241         public List<Type> allparams() { return qtype.allparams(); }
  1242         public Type getUpperBound() { return qtype.getUpperBound(); }
  1243         public boolean isErroneous() { return qtype.isErroneous(); }
  1246     /**
  1247      * The type of a generic method type. It consists of a method type and
  1248      * a list of method type-parameters that are used within the method
  1249      * type.
  1250      */
  1251     public static class ForAll extends DelegatedType implements ExecutableType {
  1252         public List<Type> tvars;
  1254         public ForAll(List<Type> tvars, Type qtype) {
  1255             super(FORALL, (MethodType)qtype);
  1256             this.tvars = tvars;
  1259         @Override
  1260         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1261             return v.visitForAll(this, s);
  1264         public String toString() {
  1265             return "<" + tvars + ">" + qtype;
  1268         public List<Type> getTypeArguments()   { return tvars; }
  1270         public boolean isErroneous()  {
  1271             return qtype.isErroneous();
  1274         public Type map(Mapping f) {
  1275             return f.apply(qtype);
  1278         public boolean contains(Type elem) {
  1279             return qtype.contains(elem);
  1282         public MethodType asMethodType() {
  1283             return (MethodType)qtype;
  1286         public void complete() {
  1287             for (List<Type> l = tvars; l.nonEmpty(); l = l.tail) {
  1288                 ((TypeVar)l.head).bound.complete();
  1290             qtype.complete();
  1293         public List<TypeVar> getTypeVariables() {
  1294             return List.convert(TypeVar.class, getTypeArguments());
  1297         public TypeKind getKind() {
  1298             return TypeKind.EXECUTABLE;
  1301         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1302             return v.visitExecutable(this, p);
  1306     /** A class for inference variables, for use during method/diamond type
  1307      *  inference. An inference variable has upper/lower bounds and a set
  1308      *  of equality constraints. Such bounds are set during subtyping, type-containment,
  1309      *  type-equality checks, when the types being tested contain inference variables.
  1310      *  A change listener can be attached to an inference variable, to receive notifications
  1311      *  whenever the bounds of an inference variable change.
  1312      */
  1313     public static class UndetVar extends DelegatedType {
  1315         /** Inference variable change listener. The listener method is called
  1316          *  whenever a change to the inference variable's bounds occurs
  1317          */
  1318         public interface UndetVarListener {
  1319             /** called when some inference variable bounds (of given kinds ibs) change */
  1320             void varChanged(UndetVar uv, Set<InferenceBound> ibs);
  1323         /**
  1324          * Inference variable bound kinds
  1325          */
  1326         public enum InferenceBound {
  1327             /** upper bounds */
  1328             UPPER,
  1329             /** lower bounds */
  1330             LOWER,
  1331             /** equality constraints */
  1332             EQ;
  1335         /** inference variable bounds */
  1336         private Map<InferenceBound, List<Type>> bounds;
  1338         /** inference variable's inferred type (set from Infer.java) */
  1339         public Type inst = null;
  1341         /** number of declared (upper) bounds */
  1342         public int declaredCount;
  1344         /** inference variable's change listener */
  1345         public UndetVarListener listener = null;
  1347         @Override
  1348         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1349             return v.visitUndetVar(this, s);
  1352         public UndetVar(TypeVar origin, Types types) {
  1353             super(UNDETVAR, origin);
  1354             bounds = new EnumMap<InferenceBound, List<Type>>(InferenceBound.class);
  1355             List<Type> declaredBounds = types.getBounds(origin);
  1356             declaredCount = declaredBounds.length();
  1357             bounds.put(InferenceBound.UPPER, declaredBounds);
  1358             bounds.put(InferenceBound.LOWER, List.<Type>nil());
  1359             bounds.put(InferenceBound.EQ, List.<Type>nil());
  1362         public String toString() {
  1363             if (inst != null) return inst.toString();
  1364             else return qtype + "?";
  1367         public Type baseType() {
  1368             if (inst != null) return inst.baseType();
  1369             else return this;
  1372         /** get all bounds of a given kind */
  1373         public List<Type> getBounds(InferenceBound... ibs) {
  1374             ListBuffer<Type> buf = ListBuffer.lb();
  1375             for (InferenceBound ib : ibs) {
  1376                 buf.appendList(bounds.get(ib));
  1378             return buf.toList();
  1381         /** get the list of declared (upper) bounds */
  1382         public List<Type> getDeclaredBounds() {
  1383             ListBuffer<Type> buf = ListBuffer.lb();
  1384             int count = 0;
  1385             for (Type b : getBounds(InferenceBound.UPPER)) {
  1386                 if (count++ == declaredCount) break;
  1387                 buf.append(b);
  1389             return buf.toList();
  1392         /** add a bound of a given kind - this might trigger listener notification */
  1393         public void addBound(InferenceBound ib, Type bound, Types types) {
  1394             Type bound2 = toTypeVarMap.apply(bound);
  1395             List<Type> prevBounds = bounds.get(ib);
  1396             for (Type b : prevBounds) {
  1397                 //check for redundancy - use strict version of isSameType on tvars
  1398                 //(as the standard version will lead to false positives w.r.t. clones ivars)
  1399                 if (types.isSameType(b, bound2, true) || bound == qtype) return;
  1401             bounds.put(ib, prevBounds.prepend(bound2));
  1402             notifyChange(EnumSet.of(ib));
  1404         //where
  1405             Type.Mapping toTypeVarMap = new Mapping("toTypeVarMap") {
  1406                 @Override
  1407                 public Type apply(Type t) {
  1408                     if (t.hasTag(UNDETVAR)) {
  1409                         UndetVar uv = (UndetVar)t;
  1410                         return uv.qtype;
  1411                     } else {
  1412                         return t.map(this);
  1415             };
  1417         /** replace types in all bounds - this might trigger listener notification */
  1418         public void substBounds(List<Type> from, List<Type> to, Types types) {
  1419             List<Type> instVars = from.diff(to);
  1420             //if set of instantiated ivars is empty, there's nothing to do!
  1421             if (instVars.isEmpty()) return;
  1422             final EnumSet<InferenceBound> boundsChanged = EnumSet.noneOf(InferenceBound.class);
  1423             UndetVarListener prevListener = listener;
  1424             try {
  1425                 //setup new listener for keeping track of changed bounds
  1426                 listener = new UndetVarListener() {
  1427                     public void varChanged(UndetVar uv, Set<InferenceBound> ibs) {
  1428                         boundsChanged.addAll(ibs);
  1430                 };
  1431                 for (Map.Entry<InferenceBound, List<Type>> _entry : bounds.entrySet()) {
  1432                     InferenceBound ib = _entry.getKey();
  1433                     List<Type> prevBounds = _entry.getValue();
  1434                     ListBuffer<Type> newBounds = ListBuffer.lb();
  1435                     ListBuffer<Type> deps = ListBuffer.lb();
  1436                     //step 1 - re-add bounds that are not dependent on ivars
  1437                     for (Type t : prevBounds) {
  1438                         if (!t.containsAny(instVars)) {
  1439                             newBounds.append(t);
  1440                         } else {
  1441                             deps.append(t);
  1444                     //step 2 - replace bounds
  1445                     bounds.put(ib, newBounds.toList());
  1446                     //step 3 - for each dependency, add new replaced bound
  1447                     for (Type dep : deps) {
  1448                         addBound(ib, types.subst(dep, from, to), types);
  1451             } finally {
  1452                 listener = prevListener;
  1453                 if (!boundsChanged.isEmpty()) {
  1454                     notifyChange(boundsChanged);
  1459         private void notifyChange(EnumSet<InferenceBound> ibs) {
  1460             if (listener != null) {
  1461                 listener.varChanged(this, ibs);
  1466     /** Represents VOID or NONE.
  1467      */
  1468     static class JCNoType extends Type implements NoType {
  1469         public JCNoType(TypeTag tag) {
  1470             super(tag, null);
  1473         @Override
  1474         public TypeKind getKind() {
  1475             switch (tag) {
  1476             case VOID:  return TypeKind.VOID;
  1477             case NONE:  return TypeKind.NONE;
  1478             default:
  1479                 throw new AssertionError("Unexpected tag: " + tag);
  1483         @Override
  1484         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1485             return v.visitNoType(this, p);
  1489     static class BottomType extends Type implements NullType {
  1490         public BottomType() {
  1491             super(BOT, null);
  1494         @Override
  1495         public TypeKind getKind() {
  1496             return TypeKind.NULL;
  1499         @Override
  1500         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1501             return v.visitNull(this, p);
  1504         @Override
  1505         public Type constType(Object value) {
  1506             return this;
  1509         @Override
  1510         public String stringValue() {
  1511             return "null";
  1515     public static class ErrorType extends ClassType
  1516             implements javax.lang.model.type.ErrorType {
  1518         private Type originalType = null;
  1520         public ErrorType(Type originalType, TypeSymbol tsym) {
  1521             super(noType, List.<Type>nil(), null);
  1522             tag = ERROR;
  1523             this.tsym = tsym;
  1524             this.originalType = (originalType == null ? noType : originalType);
  1527         public ErrorType(ClassSymbol c, Type originalType) {
  1528             this(originalType, c);
  1529             c.type = this;
  1530             c.kind = ERR;
  1531             c.members_field = new Scope.ErrorScope(c);
  1534         public ErrorType(Name name, TypeSymbol container, Type originalType) {
  1535             this(new ClassSymbol(PUBLIC|STATIC|ACYCLIC, name, null, container), originalType);
  1538         @Override
  1539         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1540             return v.visitErrorType(this, s);
  1543         public Type constType(Object constValue) { return this; }
  1544         public Type getEnclosingType()           { return this; }
  1545         public Type getReturnType()              { return this; }
  1546         public Type asSub(Symbol sym)            { return this; }
  1547         public Type map(Mapping f)               { return this; }
  1549         public boolean isGenType(Type t)         { return true; }
  1550         public boolean isErroneous()             { return true; }
  1551         public boolean isCompound()              { return false; }
  1552         public boolean isInterface()             { return false; }
  1554         public List<Type> allparams()            { return List.nil(); }
  1555         public List<Type> getTypeArguments()     { return List.nil(); }
  1557         public TypeKind getKind() {
  1558             return TypeKind.ERROR;
  1561         public Type getOriginalType() {
  1562             return originalType;
  1565         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1566             return v.visitError(this, p);
  1570     public static class AnnotatedType extends Type
  1571             implements
  1572                 javax.lang.model.type.ArrayType,
  1573                 javax.lang.model.type.DeclaredType,
  1574                 javax.lang.model.type.PrimitiveType,
  1575                 javax.lang.model.type.TypeVariable,
  1576                 javax.lang.model.type.WildcardType {
  1577         /** The type annotations on this type.
  1578          */
  1579         public List<Attribute.TypeCompound> typeAnnotations;
  1581         /** The underlying type that is annotated.
  1582          */
  1583         public Type underlyingType;
  1585         public AnnotatedType(Type underlyingType) {
  1586             super(underlyingType.tag, underlyingType.tsym);
  1587             this.typeAnnotations = List.nil();
  1588             this.underlyingType = underlyingType;
  1589             Assert.check(!underlyingType.isAnnotated(),
  1590                     "Can't annotate already annotated type: " + underlyingType);
  1593         public AnnotatedType(List<Attribute.TypeCompound> typeAnnotations,
  1594                 Type underlyingType) {
  1595             super(underlyingType.tag, underlyingType.tsym);
  1596             this.typeAnnotations = typeAnnotations;
  1597             this.underlyingType = underlyingType;
  1598             Assert.check(!underlyingType.isAnnotated(),
  1599                     "Can't annotate already annotated type: " + underlyingType +
  1600                     "; adding: " + typeAnnotations);
  1603         @Override
  1604         public boolean isAnnotated() {
  1605             return true;
  1608         @Override
  1609         public List<? extends Attribute.TypeCompound> getAnnotationMirrors() {
  1610             return typeAnnotations;
  1613         @Override
  1614         public <A extends Annotation> A getAnnotation(Class<A> annotationType) {
  1615             return JavacAnnoConstructs.getAnnotation(this, annotationType);
  1618         @Override
  1619         public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationType) {
  1620             return JavacAnnoConstructs.getAnnotationsByType(this, annotationType);
  1623         @Override
  1624         public TypeKind getKind() {
  1625             return underlyingType.getKind();
  1628         @Override
  1629         public Type unannotatedType() {
  1630             return underlyingType;
  1633         @Override
  1634         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1635             return v.visitAnnotatedType(this, s);
  1638         @Override
  1639         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1640             return underlyingType.accept(v, p);
  1643         @Override
  1644         public Type map(Mapping f) {
  1645             underlyingType.map(f);
  1646             return this;
  1649         @Override
  1650         public Type constType(Object constValue) { return underlyingType.constType(constValue); }
  1651         @Override
  1652         public Type getEnclosingType()           { return underlyingType.getEnclosingType(); }
  1654         @Override
  1655         public Type getReturnType()              { return underlyingType.getReturnType(); }
  1656         @Override
  1657         public List<Type> getTypeArguments()     { return underlyingType.getTypeArguments(); }
  1658         @Override
  1659         public List<Type> getParameterTypes()    { return underlyingType.getParameterTypes(); }
  1660         @Override
  1661         public Type getReceiverType()            { return underlyingType.getReceiverType(); }
  1662         @Override
  1663         public List<Type> getThrownTypes()       { return underlyingType.getThrownTypes(); }
  1664         @Override
  1665         public Type getUpperBound()              { return underlyingType.getUpperBound(); }
  1666         @Override
  1667         public Type getLowerBound()              { return underlyingType.getLowerBound(); }
  1669         @Override
  1670         public boolean isErroneous()             { return underlyingType.isErroneous(); }
  1671         @Override
  1672         public boolean isCompound()              { return underlyingType.isCompound(); }
  1673         @Override
  1674         public boolean isInterface()             { return underlyingType.isInterface(); }
  1675         @Override
  1676         public List<Type> allparams()            { return underlyingType.allparams(); }
  1677         @Override
  1678         public boolean isNumeric()               { return underlyingType.isNumeric(); }
  1679         @Override
  1680         public boolean isReference()             { return underlyingType.isReference(); }
  1681         @Override
  1682         public boolean isParameterized()         { return underlyingType.isParameterized(); }
  1683         @Override
  1684         public boolean isRaw()                   { return underlyingType.isRaw(); }
  1685         @Override
  1686         public boolean isFinal()                 { return underlyingType.isFinal(); }
  1687         @Override
  1688         public boolean isSuperBound()            { return underlyingType.isSuperBound(); }
  1689         @Override
  1690         public boolean isExtendsBound()          { return underlyingType.isExtendsBound(); }
  1691         @Override
  1692         public boolean isUnbound()               { return underlyingType.isUnbound(); }
  1694         @Override
  1695         public String toString() {
  1696             // TODO more logic for arrays, etc.
  1697             if (typeAnnotations != null &&
  1698                     !typeAnnotations.isEmpty()) {
  1699                 return "(" + typeAnnotations.toString() + " :: " + underlyingType.toString() + ")";
  1700             } else {
  1701                 return "({} :: " + underlyingType.toString() +")";
  1705         @Override
  1706         public boolean contains(Type t)          { return underlyingType.contains(t); }
  1708         // TODO: attach annotations?
  1709         @Override
  1710         public Type withTypeVar(Type t)          { return underlyingType.withTypeVar(t); }
  1712         // TODO: attach annotations?
  1713         @Override
  1714         public TypeSymbol asElement()            { return underlyingType.asElement(); }
  1716         // TODO: attach annotations?
  1717         @Override
  1718         public MethodType asMethodType()         { return underlyingType.asMethodType(); }
  1720         @Override
  1721         public void complete()                   { underlyingType.complete(); }
  1723         @Override
  1724         public TypeMirror getComponentType()     { return ((ArrayType)underlyingType).getComponentType(); }
  1726         // The result is an ArrayType, but only in the model sense, not the Type sense.
  1727         public AnnotatedType makeVarargs() {
  1728             AnnotatedType atype = new AnnotatedType(((ArrayType)underlyingType).makeVarargs());
  1729             atype.typeAnnotations = this.typeAnnotations;
  1730             return atype;
  1733         @Override
  1734         public TypeMirror getExtendsBound()      { return ((WildcardType)underlyingType).getExtendsBound(); }
  1735         @Override
  1736         public TypeMirror getSuperBound()        { return ((WildcardType)underlyingType).getSuperBound(); }
  1739     /**
  1740      * A visitor for types.  A visitor is used to implement operations
  1741      * (or relations) on types.  Most common operations on types are
  1742      * binary relations and this interface is designed for binary
  1743      * relations, that is, operations of the form
  1744      * Type&nbsp;&times;&nbsp;S&nbsp;&rarr;&nbsp;R.
  1745      * <!-- In plain text: Type x S -> R -->
  1747      * @param <R> the return type of the operation implemented by this
  1748      * visitor; use Void if no return type is needed.
  1749      * @param <S> the type of the second argument (the first being the
  1750      * type itself) of the operation implemented by this visitor; use
  1751      * Void if a second argument is not needed.
  1752      */
  1753     public interface Visitor<R,S> {
  1754         R visitClassType(ClassType t, S s);
  1755         R visitWildcardType(WildcardType t, S s);
  1756         R visitArrayType(ArrayType t, S s);
  1757         R visitMethodType(MethodType t, S s);
  1758         R visitPackageType(PackageType t, S s);
  1759         R visitTypeVar(TypeVar t, S s);
  1760         R visitCapturedType(CapturedType t, S s);
  1761         R visitForAll(ForAll t, S s);
  1762         R visitUndetVar(UndetVar t, S s);
  1763         R visitErrorType(ErrorType t, S s);
  1764         R visitAnnotatedType(AnnotatedType t, S s);
  1765         R visitType(Type t, S s);

mercurial