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

Thu, 01 Nov 2012 10:48:36 +0100

author
ohrstrom
date
Thu, 01 Nov 2012 10:48:36 +0100
changeset 1384
bf54daa9dcd8
parent 1374
c002fdee76fd
child 1436
f6f1fd261f57
permissions
-rw-r--r--

7153951: Add new lint option -Xlint:auxiliaryclass
Reviewed-by: jjg, mcimadamore, forax

     1 /*
     2  * Copyright (c) 1999, 2012, 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.type.*;
    36 import com.sun.tools.javac.code.Symbol.*;
    37 import com.sun.tools.javac.util.*;
    38 import static com.sun.tools.javac.code.BoundKind.*;
    39 import static com.sun.tools.javac.code.Flags.*;
    40 import static com.sun.tools.javac.code.Kinds.*;
    41 import static com.sun.tools.javac.code.TypeTag.*;
    43 /** This class represents Java types. The class itself defines the behavior of
    44  *  the following types:
    45  *  <pre>
    46  *  base types (tags: BYTE, CHAR, SHORT, INT, LONG, FLOAT, DOUBLE, BOOLEAN),
    47  *  type `void' (tag: VOID),
    48  *  the bottom type (tag: BOT),
    49  *  the missing type (tag: NONE).
    50  *  </pre>
    51  *  <p>The behavior of the following types is defined in subclasses, which are
    52  *  all static inner classes of this class:
    53  *  <pre>
    54  *  class types (tag: CLASS, class: ClassType),
    55  *  array types (tag: ARRAY, class: ArrayType),
    56  *  method types (tag: METHOD, class: MethodType),
    57  *  package types (tag: PACKAGE, class: PackageType),
    58  *  type variables (tag: TYPEVAR, class: TypeVar),
    59  *  type arguments (tag: WILDCARD, class: WildcardType),
    60  *  generic method types (tag: FORALL, class: ForAll),
    61  *  the error type (tag: ERROR, class: ErrorType).
    62  *  </pre>
    63  *
    64  *  <p><b>This is NOT part of any supported API.
    65  *  If you write code that depends on this, you do so at your own risk.
    66  *  This code and its internal interfaces are subject to change or
    67  *  deletion without notice.</b>
    68  *
    69  *  @see TypeTag
    70  */
    71 public class Type implements PrimitiveType {
    73     /** Constant type: no type at all. */
    74     public static final JCNoType noType = new JCNoType(NONE);
    76     /** Constant type: special type to be used during recovery of deferred expressions. */
    77     public static final JCNoType recoveryType = new JCNoType(NONE);
    79     /** If this switch is turned on, the names of type variables
    80      *  and anonymous classes are printed with hashcodes appended.
    81      */
    82     public static boolean moreInfo = false;
    84     /** The tag of this type.
    85      *
    86      *  @see TypeTag
    87      */
    88     protected TypeTag tag;
    90     /** The defining class / interface / package / type variable
    91      */
    92     public TypeSymbol tsym;
    94     /**
    95      * Checks if the current type tag is equal to the given tag.
    96      * @return true if tag is equal to the current type tag.
    97      */
    98     public boolean hasTag(TypeTag tag) {
    99         return this.tag == tag;
   100     }
   102     /**
   103      * Returns the current type tag.
   104      * @return the value of the current type tag.
   105      */
   106     public TypeTag getTag() {
   107         return tag;
   108     }
   110     public boolean isNumeric() {
   111         switch (tag) {
   112             case BYTE: case CHAR:
   113             case SHORT:
   114             case INT: case LONG:
   115             case FLOAT: case DOUBLE:
   116                 return true;
   117             default:
   118                 return false;
   119         }
   120     }
   122     public boolean isPrimitive() {
   123         return (isNumeric() || tag == BOOLEAN);
   124     }
   126     public boolean isPrimitiveOrVoid() {
   127         return (isPrimitive() || tag == VOID);
   128     }
   130     public boolean isReference() {
   131         switch (tag) {
   132         case CLASS:
   133         case ARRAY:
   134         case TYPEVAR:
   135         case WILDCARD:
   136         case ERROR:
   137             return true;
   138         default:
   139             return false;
   140         }
   141     }
   143     public boolean isNullOrReference() {
   144         return (tag == BOT || isReference());
   145     }
   147     public boolean isPartial() {
   148         switch(tag) {
   149             case ERROR: case UNKNOWN: case UNDETVAR:
   150                 return true;
   151             default:
   152                 return false;
   153         }
   154     }
   156     /**
   157      * The constant value of this type, null if this type does not
   158      * have a constant value attribute. Only primitive types and
   159      * strings (ClassType) can have a constant value attribute.
   160      * @return the constant value attribute of this type
   161      */
   162     public Object constValue() {
   163         return null;
   164     }
   166     /**
   167      * Get the representation of this type used for modelling purposes.
   168      * By default, this is itself. For ErrorType, a different value
   169      * may be provided,
   170      */
   171     public Type getModelType() {
   172         return this;
   173     }
   175     public static List<Type> getModelTypes(List<Type> ts) {
   176         ListBuffer<Type> lb = new ListBuffer<Type>();
   177         for (Type t: ts)
   178             lb.append(t.getModelType());
   179         return lb.toList();
   180     }
   182     public <R,S> R accept(Type.Visitor<R,S> v, S s) { return v.visitType(this, s); }
   184     /** Define a type given its tag and type symbol
   185      */
   186     public Type(TypeTag tag, TypeSymbol tsym) {
   187         this.tag = tag;
   188         this.tsym = tsym;
   189     }
   191     /** An abstract class for mappings from types to types
   192      */
   193     public static abstract class Mapping {
   194         private String name;
   195         public Mapping(String name) {
   196             this.name = name;
   197         }
   198         public abstract Type apply(Type t);
   199         public String toString() {
   200             return name;
   201         }
   202     }
   204     /** map a type function over all immediate descendants of this type
   205      */
   206     public Type map(Mapping f) {
   207         return this;
   208     }
   210     /** map a type function over a list of types
   211      */
   212     public static List<Type> map(List<Type> ts, Mapping f) {
   213         if (ts.nonEmpty()) {
   214             List<Type> tail1 = map(ts.tail, f);
   215             Type t = f.apply(ts.head);
   216             if (tail1 != ts.tail || t != ts.head)
   217                 return tail1.prepend(t);
   218         }
   219         return ts;
   220     }
   222     /** Define a constant type, of the same kind as this type
   223      *  and with given constant value
   224      */
   225     public Type constType(Object constValue) {
   226         final Object value = constValue;
   227         Assert.check(isPrimitive());
   228         return new Type(tag, tsym) {
   229                 @Override
   230                 public Object constValue() {
   231                     return value;
   232                 }
   233                 @Override
   234                 public Type baseType() {
   235                     return tsym.type;
   236                 }
   237             };
   238     }
   240     /**
   241      * If this is a constant type, return its underlying type.
   242      * Otherwise, return the type itself.
   243      */
   244     public Type baseType() {
   245         return this;
   246     }
   248     /** Return the base types of a list of types.
   249      */
   250     public static List<Type> baseTypes(List<Type> ts) {
   251         if (ts.nonEmpty()) {
   252             Type t = ts.head.baseType();
   253             List<Type> baseTypes = baseTypes(ts.tail);
   254             if (t != ts.head || baseTypes != ts.tail)
   255                 return baseTypes.prepend(t);
   256         }
   257         return ts;
   258     }
   260     /** The Java source which this type represents.
   261      */
   262     public String toString() {
   263         String s = (tsym == null || tsym.name == null)
   264             ? "<none>"
   265             : tsym.name.toString();
   266         if (moreInfo && tag == TYPEVAR) s = s + hashCode();
   267         return s;
   268     }
   270     /**
   271      * The Java source which this type list represents.  A List is
   272      * represented as a comma-spearated listing of the elements in
   273      * that list.
   274      */
   275     public static String toString(List<Type> ts) {
   276         if (ts.isEmpty()) {
   277             return "";
   278         } else {
   279             StringBuilder buf = new StringBuilder();
   280             buf.append(ts.head.toString());
   281             for (List<Type> l = ts.tail; l.nonEmpty(); l = l.tail)
   282                 buf.append(",").append(l.head.toString());
   283             return buf.toString();
   284         }
   285     }
   287     /**
   288      * The constant value of this type, converted to String
   289      */
   290     public String stringValue() {
   291         Object cv = Assert.checkNonNull(constValue());
   292         if (tag == BOOLEAN)
   293             return ((Integer) cv).intValue() == 0 ? "false" : "true";
   294         else if (tag == CHAR)
   295             return String.valueOf((char) ((Integer) cv).intValue());
   296         else
   297             return cv.toString();
   298     }
   300     /**
   301      * This method is analogous to isSameType, but weaker, since we
   302      * never complete classes. Where isSameType would complete a
   303      * class, equals assumes that the two types are different.
   304      */
   305     public boolean equals(Object t) {
   306         return super.equals(t);
   307     }
   309     public int hashCode() {
   310         return super.hashCode();
   311     }
   313     /** Is this a constant type whose value is false?
   314      */
   315     public boolean isFalse() {
   316         return
   317             tag == BOOLEAN &&
   318             constValue() != null &&
   319             ((Integer)constValue()).intValue() == 0;
   320     }
   322     /** Is this a constant type whose value is true?
   323      */
   324     public boolean isTrue() {
   325         return
   326             tag == BOOLEAN &&
   327             constValue() != null &&
   328             ((Integer)constValue()).intValue() != 0;
   329     }
   331     public String argtypes(boolean varargs) {
   332         List<Type> args = getParameterTypes();
   333         if (!varargs) return args.toString();
   334         StringBuilder buf = new StringBuilder();
   335         while (args.tail.nonEmpty()) {
   336             buf.append(args.head);
   337             args = args.tail;
   338             buf.append(',');
   339         }
   340         if (args.head.tag == ARRAY) {
   341             buf.append(((ArrayType)args.head).elemtype);
   342             buf.append("...");
   343         } else {
   344             buf.append(args.head);
   345         }
   346         return buf.toString();
   347     }
   349     /** Access methods.
   350      */
   351     public List<Type>        getTypeArguments()  { return List.nil(); }
   352     public Type              getEnclosingType() { return null; }
   353     public List<Type>        getParameterTypes() { return List.nil(); }
   354     public Type              getReturnType()     { return null; }
   355     public List<Type>        getThrownTypes()    { return List.nil(); }
   356     public Type              getUpperBound()     { return null; }
   357     public Type              getLowerBound()     { return null; }
   359     /** Navigation methods, these will work for classes, type variables,
   360      *  foralls, but will return null for arrays and methods.
   361      */
   363    /** Return all parameters of this type and all its outer types in order
   364     *  outer (first) to inner (last).
   365     */
   366     public List<Type> allparams() { return List.nil(); }
   368     /** Does this type contain "error" elements?
   369      */
   370     public boolean isErroneous() {
   371         return false;
   372     }
   374     public static boolean isErroneous(List<Type> ts) {
   375         for (List<Type> l = ts; l.nonEmpty(); l = l.tail)
   376             if (l.head.isErroneous()) return true;
   377         return false;
   378     }
   380     /** Is this type parameterized?
   381      *  A class type is parameterized if it has some parameters.
   382      *  An array type is parameterized if its element type is parameterized.
   383      *  All other types are not parameterized.
   384      */
   385     public boolean isParameterized() {
   386         return false;
   387     }
   389     /** Is this type a raw type?
   390      *  A class type is a raw type if it misses some of its parameters.
   391      *  An array type is a raw type if its element type is raw.
   392      *  All other types are not raw.
   393      *  Type validation will ensure that the only raw types
   394      *  in a program are types that miss all their type variables.
   395      */
   396     public boolean isRaw() {
   397         return false;
   398     }
   400     public boolean isCompound() {
   401         return tsym.completer == null
   402             // Compound types can't have a completer.  Calling
   403             // flags() will complete the symbol causing the
   404             // compiler to load classes unnecessarily.  This led
   405             // to regression 6180021.
   406             && (tsym.flags() & COMPOUND) != 0;
   407     }
   409     public boolean isInterface() {
   410         return (tsym.flags() & INTERFACE) != 0;
   411     }
   413     public boolean isFinal() {
   414         return (tsym.flags() & FINAL) != 0;
   415     }
   417     /**
   418      * Does this type contain occurrences of type t?
   419      */
   420     public boolean contains(Type t) {
   421         return t == this;
   422     }
   424     public static boolean contains(List<Type> ts, Type t) {
   425         for (List<Type> l = ts;
   426              l.tail != null /*inlined: l.nonEmpty()*/;
   427              l = l.tail)
   428             if (l.head.contains(t)) return true;
   429         return false;
   430     }
   432     /** Does this type contain an occurrence of some type in 'ts'?
   433      */
   434     public boolean containsAny(List<Type> ts) {
   435         for (Type t : ts)
   436             if (this.contains(t)) return true;
   437         return false;
   438     }
   440     public static boolean containsAny(List<Type> ts1, List<Type> ts2) {
   441         for (Type t : ts1)
   442             if (t.containsAny(ts2)) return true;
   443         return false;
   444     }
   446     public static List<Type> filter(List<Type> ts, Filter<Type> tf) {
   447         ListBuffer<Type> buf = ListBuffer.lb();
   448         for (Type t : ts) {
   449             if (tf.accepts(t)) {
   450                 buf.append(t);
   451             }
   452         }
   453         return buf.toList();
   454     }
   456     public boolean isSuperBound() { return false; }
   457     public boolean isExtendsBound() { return false; }
   458     public boolean isUnbound() { return false; }
   459     public Type withTypeVar(Type t) { return this; }
   461     /** The underlying method type of this type.
   462      */
   463     public MethodType asMethodType() { throw new AssertionError(); }
   465     /** Complete loading all classes in this type.
   466      */
   467     public void complete() {}
   469     public TypeSymbol asElement() {
   470         return tsym;
   471     }
   473     public TypeKind getKind() {
   474         switch (tag) {
   475         case BYTE:      return TypeKind.BYTE;
   476         case CHAR:      return TypeKind.CHAR;
   477         case SHORT:     return TypeKind.SHORT;
   478         case INT:       return TypeKind.INT;
   479         case LONG:      return TypeKind.LONG;
   480         case FLOAT:     return TypeKind.FLOAT;
   481         case DOUBLE:    return TypeKind.DOUBLE;
   482         case BOOLEAN:   return TypeKind.BOOLEAN;
   483         case VOID:      return TypeKind.VOID;
   484         case BOT:       return TypeKind.NULL;
   485         case NONE:      return TypeKind.NONE;
   486         default:        return TypeKind.OTHER;
   487         }
   488     }
   490     public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   491         if (isPrimitive())
   492             return v.visitPrimitive(this, p);
   493         else
   494             throw new AssertionError();
   495     }
   497     public static class WildcardType extends Type
   498             implements javax.lang.model.type.WildcardType {
   500         public Type type;
   501         public BoundKind kind;
   502         public TypeVar bound;
   504         @Override
   505         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   506             return v.visitWildcardType(this, s);
   507         }
   509         public WildcardType(Type type, BoundKind kind, TypeSymbol tsym) {
   510             super(WILDCARD, tsym);
   511             this.type = Assert.checkNonNull(type);
   512             this.kind = kind;
   513         }
   514         public WildcardType(WildcardType t, TypeVar bound) {
   515             this(t.type, t.kind, t.tsym, bound);
   516         }
   518         public WildcardType(Type type, BoundKind kind, TypeSymbol tsym, TypeVar bound) {
   519             this(type, kind, tsym);
   520             this.bound = bound;
   521         }
   523         public boolean contains(Type t) {
   524             return kind != UNBOUND && type.contains(t);
   525         }
   527         public boolean isSuperBound() {
   528             return kind == SUPER ||
   529                 kind == UNBOUND;
   530         }
   531         public boolean isExtendsBound() {
   532             return kind == EXTENDS ||
   533                 kind == UNBOUND;
   534         }
   535         public boolean isUnbound() {
   536             return kind == UNBOUND;
   537         }
   539         public Type withTypeVar(Type t) {
   540             //-System.err.println(this+".withTypeVar("+t+");");//DEBUG
   541             if (bound == t)
   542                 return this;
   543             bound = (TypeVar)t;
   544             return this;
   545         }
   547         boolean isPrintingBound = false;
   548         public String toString() {
   549             StringBuilder s = new StringBuilder();
   550             s.append(kind.toString());
   551             if (kind != UNBOUND)
   552                 s.append(type);
   553             if (moreInfo && bound != null && !isPrintingBound)
   554                 try {
   555                     isPrintingBound = true;
   556                     s.append("{:").append(bound.bound).append(":}");
   557                 } finally {
   558                     isPrintingBound = false;
   559                 }
   560             return s.toString();
   561         }
   563         public Type map(Mapping f) {
   564             //- System.err.println("   (" + this + ").map(" + f + ")");//DEBUG
   565             Type t = type;
   566             if (t != null)
   567                 t = f.apply(t);
   568             if (t == type)
   569                 return this;
   570             else
   571                 return new WildcardType(t, kind, tsym, bound);
   572         }
   574         public Type getExtendsBound() {
   575             if (kind == EXTENDS)
   576                 return type;
   577             else
   578                 return null;
   579         }
   581         public Type getSuperBound() {
   582             if (kind == SUPER)
   583                 return type;
   584             else
   585                 return null;
   586         }
   588         public TypeKind getKind() {
   589             return TypeKind.WILDCARD;
   590         }
   592         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   593             return v.visitWildcard(this, p);
   594         }
   595     }
   597     public static class ClassType extends Type implements DeclaredType {
   599         /** The enclosing type of this type. If this is the type of an inner
   600          *  class, outer_field refers to the type of its enclosing
   601          *  instance class, in all other cases it referes to noType.
   602          */
   603         private Type outer_field;
   605         /** The type parameters of this type (to be set once class is loaded).
   606          */
   607         public List<Type> typarams_field;
   609         /** A cache variable for the type parameters of this type,
   610          *  appended to all parameters of its enclosing class.
   611          *  @see #allparams
   612          */
   613         public List<Type> allparams_field;
   615         /** The supertype of this class (to be set once class is loaded).
   616          */
   617         public Type supertype_field;
   619         /** The interfaces of this class (to be set once class is loaded).
   620          */
   621         public List<Type> interfaces_field;
   623         /** All the interfaces of this class, including missing ones.
   624          */
   625         public List<Type> all_interfaces_field;
   627         public ClassType(Type outer, List<Type> typarams, TypeSymbol tsym) {
   628             super(CLASS, tsym);
   629             this.outer_field = outer;
   630             this.typarams_field = typarams;
   631             this.allparams_field = null;
   632             this.supertype_field = null;
   633             this.interfaces_field = null;
   634             /*
   635             // this can happen during error recovery
   636             assert
   637                 outer.isParameterized() ?
   638                 typarams.length() == tsym.type.typarams().length() :
   639                 outer.isRaw() ?
   640                 typarams.length() == 0 :
   641                 true;
   642             */
   643         }
   645         @Override
   646         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   647             return v.visitClassType(this, s);
   648         }
   650         public Type constType(Object constValue) {
   651             final Object value = constValue;
   652             return new ClassType(getEnclosingType(), typarams_field, tsym) {
   653                     @Override
   654                     public Object constValue() {
   655                         return value;
   656                     }
   657                     @Override
   658                     public Type baseType() {
   659                         return tsym.type;
   660                     }
   661                 };
   662         }
   664         /** The Java source which this type represents.
   665          */
   666         public String toString() {
   667             StringBuilder buf = new StringBuilder();
   668             if (getEnclosingType().tag == CLASS && tsym.owner.kind == TYP) {
   669                 buf.append(getEnclosingType().toString());
   670                 buf.append(".");
   671                 buf.append(className(tsym, false));
   672             } else {
   673                 buf.append(className(tsym, true));
   674             }
   675             if (getTypeArguments().nonEmpty()) {
   676                 buf.append('<');
   677                 buf.append(getTypeArguments().toString());
   678                 buf.append(">");
   679             }
   680             return buf.toString();
   681         }
   682 //where
   683             private String className(Symbol sym, boolean longform) {
   684                 if (sym.name.isEmpty() && (sym.flags() & COMPOUND) != 0) {
   685                     StringBuilder s = new StringBuilder(supertype_field.toString());
   686                     for (List<Type> is=interfaces_field; is.nonEmpty(); is = is.tail) {
   687                         s.append("&");
   688                         s.append(is.head.toString());
   689                     }
   690                     return s.toString();
   691                 } else if (sym.name.isEmpty()) {
   692                     String s;
   693                     ClassType norm = (ClassType) tsym.type;
   694                     if (norm == null) {
   695                         s = Log.getLocalizedString("anonymous.class", (Object)null);
   696                     } else if (norm.interfaces_field != null && norm.interfaces_field.nonEmpty()) {
   697                         s = Log.getLocalizedString("anonymous.class",
   698                                                    norm.interfaces_field.head);
   699                     } else {
   700                         s = Log.getLocalizedString("anonymous.class",
   701                                                    norm.supertype_field);
   702                     }
   703                     if (moreInfo)
   704                         s += String.valueOf(sym.hashCode());
   705                     return s;
   706                 } else if (longform) {
   707                     return sym.getQualifiedName().toString();
   708                 } else {
   709                     return sym.name.toString();
   710                 }
   711             }
   713         public List<Type> getTypeArguments() {
   714             if (typarams_field == null) {
   715                 complete();
   716                 if (typarams_field == null)
   717                     typarams_field = List.nil();
   718             }
   719             return typarams_field;
   720         }
   722         public boolean hasErasedSupertypes() {
   723             return isRaw();
   724         }
   726         public Type getEnclosingType() {
   727             return outer_field;
   728         }
   730         public void setEnclosingType(Type outer) {
   731             outer_field = outer;
   732         }
   734         public List<Type> allparams() {
   735             if (allparams_field == null) {
   736                 allparams_field = getTypeArguments().prependList(getEnclosingType().allparams());
   737             }
   738             return allparams_field;
   739         }
   741         public boolean isErroneous() {
   742             return
   743                 getEnclosingType().isErroneous() ||
   744                 isErroneous(getTypeArguments()) ||
   745                 this != tsym.type && tsym.type.isErroneous();
   746         }
   748         public boolean isParameterized() {
   749             return allparams().tail != null;
   750             // optimization, was: allparams().nonEmpty();
   751         }
   753         /** A cache for the rank. */
   754         int rank_field = -1;
   756         /** A class type is raw if it misses some
   757          *  of its type parameter sections.
   758          *  After validation, this is equivalent to:
   759          *  {@code allparams.isEmpty() && tsym.type.allparams.nonEmpty(); }
   760          */
   761         public boolean isRaw() {
   762             return
   763                 this != tsym.type && // necessary, but not sufficient condition
   764                 tsym.type.allparams().nonEmpty() &&
   765                 allparams().isEmpty();
   766         }
   768         public Type map(Mapping f) {
   769             Type outer = getEnclosingType();
   770             Type outer1 = f.apply(outer);
   771             List<Type> typarams = getTypeArguments();
   772             List<Type> typarams1 = map(typarams, f);
   773             if (outer1 == outer && typarams1 == typarams) return this;
   774             else return new ClassType(outer1, typarams1, tsym);
   775         }
   777         public boolean contains(Type elem) {
   778             return
   779                 elem == this
   780                 || (isParameterized()
   781                     && (getEnclosingType().contains(elem) || contains(getTypeArguments(), elem)))
   782                 || (isCompound()
   783                     && (supertype_field.contains(elem) || contains(interfaces_field, elem)));
   784         }
   786         public void complete() {
   787             if (tsym.completer != null) tsym.complete();
   788         }
   790         public TypeKind getKind() {
   791             return TypeKind.DECLARED;
   792         }
   794         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   795             return v.visitDeclared(this, p);
   796         }
   797     }
   799     public static class ErasedClassType extends ClassType {
   800         public ErasedClassType(Type outer, TypeSymbol tsym) {
   801             super(outer, List.<Type>nil(), tsym);
   802         }
   804         @Override
   805         public boolean hasErasedSupertypes() {
   806             return true;
   807         }
   808     }
   810     // a clone of a ClassType that knows about the alternatives of a union type.
   811     public static class UnionClassType extends ClassType implements UnionType {
   812         final List<? extends Type> alternatives_field;
   814         public UnionClassType(ClassType ct, List<? extends Type> alternatives) {
   815             super(ct.outer_field, ct.typarams_field, ct.tsym);
   816             allparams_field = ct.allparams_field;
   817             supertype_field = ct.supertype_field;
   818             interfaces_field = ct.interfaces_field;
   819             all_interfaces_field = ct.interfaces_field;
   820             alternatives_field = alternatives;
   821         }
   823         public Type getLub() {
   824             return tsym.type;
   825         }
   827         public java.util.List<? extends TypeMirror> getAlternatives() {
   828             return Collections.unmodifiableList(alternatives_field);
   829         }
   831         @Override
   832         public TypeKind getKind() {
   833             return TypeKind.UNION;
   834         }
   836         @Override
   837         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   838             return v.visitUnion(this, p);
   839         }
   840     }
   842     public static class ArrayType extends Type
   843             implements javax.lang.model.type.ArrayType {
   845         public Type elemtype;
   847         public ArrayType(Type elemtype, TypeSymbol arrayClass) {
   848             super(ARRAY, arrayClass);
   849             this.elemtype = elemtype;
   850         }
   852         @Override
   853         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   854             return v.visitArrayType(this, s);
   855         }
   857         public String toString() {
   858             return elemtype + "[]";
   859         }
   861         public boolean equals(Object obj) {
   862             return
   863                 this == obj ||
   864                 (obj instanceof ArrayType &&
   865                  this.elemtype.equals(((ArrayType)obj).elemtype));
   866         }
   868         public int hashCode() {
   869             return (ARRAY.ordinal() << 5) + elemtype.hashCode();
   870         }
   872         public boolean isVarargs() {
   873             return false;
   874         }
   876         public List<Type> allparams() { return elemtype.allparams(); }
   878         public boolean isErroneous() {
   879             return elemtype.isErroneous();
   880         }
   882         public boolean isParameterized() {
   883             return elemtype.isParameterized();
   884         }
   886         public boolean isRaw() {
   887             return elemtype.isRaw();
   888         }
   890         public ArrayType makeVarargs() {
   891             return new ArrayType(elemtype, tsym) {
   892                 @Override
   893                 public boolean isVarargs() {
   894                     return true;
   895                 }
   896             };
   897         }
   899         public Type map(Mapping f) {
   900             Type elemtype1 = f.apply(elemtype);
   901             if (elemtype1 == elemtype) return this;
   902             else return new ArrayType(elemtype1, tsym);
   903         }
   905         public boolean contains(Type elem) {
   906             return elem == this || elemtype.contains(elem);
   907         }
   909         public void complete() {
   910             elemtype.complete();
   911         }
   913         public Type getComponentType() {
   914             return elemtype;
   915         }
   917         public TypeKind getKind() {
   918             return TypeKind.ARRAY;
   919         }
   921         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   922             return v.visitArray(this, p);
   923         }
   924     }
   926     public static class MethodType extends Type implements ExecutableType {
   928         public List<Type> argtypes;
   929         public Type restype;
   930         public List<Type> thrown;
   932         public MethodType(List<Type> argtypes,
   933                           Type restype,
   934                           List<Type> thrown,
   935                           TypeSymbol methodClass) {
   936             super(METHOD, methodClass);
   937             this.argtypes = argtypes;
   938             this.restype = restype;
   939             this.thrown = thrown;
   940         }
   942         @Override
   943         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   944             return v.visitMethodType(this, s);
   945         }
   947         /** The Java source which this type represents.
   948          *
   949          *  XXX 06/09/99 iris This isn't correct Java syntax, but it probably
   950          *  should be.
   951          */
   952         public String toString() {
   953             return "(" + argtypes + ")" + restype;
   954         }
   956         public boolean equals(Object obj) {
   957             if (this == obj)
   958                 return true;
   959             if (!(obj instanceof MethodType))
   960                 return false;
   961             MethodType m = (MethodType)obj;
   962             List<Type> args1 = argtypes;
   963             List<Type> args2 = m.argtypes;
   964             while (!args1.isEmpty() && !args2.isEmpty()) {
   965                 if (!args1.head.equals(args2.head))
   966                     return false;
   967                 args1 = args1.tail;
   968                 args2 = args2.tail;
   969             }
   970             if (!args1.isEmpty() || !args2.isEmpty())
   971                 return false;
   972             return restype.equals(m.restype);
   973         }
   975         public int hashCode() {
   976             int h = METHOD.ordinal();
   977             for (List<Type> thisargs = this.argtypes;
   978                  thisargs.tail != null; /*inlined: thisargs.nonEmpty()*/
   979                  thisargs = thisargs.tail)
   980                 h = (h << 5) + thisargs.head.hashCode();
   981             return (h << 5) + this.restype.hashCode();
   982         }
   984         public List<Type>        getParameterTypes() { return argtypes; }
   985         public Type              getReturnType()     { return restype; }
   986         public List<Type>        getThrownTypes()    { return thrown; }
   988         public boolean isErroneous() {
   989             return
   990                 isErroneous(argtypes) ||
   991                 restype != null && restype.isErroneous();
   992         }
   994         public Type map(Mapping f) {
   995             List<Type> argtypes1 = map(argtypes, f);
   996             Type restype1 = f.apply(restype);
   997             List<Type> thrown1 = map(thrown, f);
   998             if (argtypes1 == argtypes &&
   999                 restype1 == restype &&
  1000                 thrown1 == thrown) return this;
  1001             else return new MethodType(argtypes1, restype1, thrown1, tsym);
  1004         public boolean contains(Type elem) {
  1005             return elem == this || contains(argtypes, elem) || restype.contains(elem);
  1008         public MethodType asMethodType() { return this; }
  1010         public void complete() {
  1011             for (List<Type> l = argtypes; l.nonEmpty(); l = l.tail)
  1012                 l.head.complete();
  1013             restype.complete();
  1014             for (List<Type> l = thrown; l.nonEmpty(); l = l.tail)
  1015                 l.head.complete();
  1018         public List<TypeVar> getTypeVariables() {
  1019             return List.nil();
  1022         public TypeSymbol asElement() {
  1023             return null;
  1026         public TypeKind getKind() {
  1027             return TypeKind.EXECUTABLE;
  1030         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1031             return v.visitExecutable(this, p);
  1035     public static class PackageType extends Type implements NoType {
  1037         PackageType(TypeSymbol tsym) {
  1038             super(PACKAGE, tsym);
  1041         @Override
  1042         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1043             return v.visitPackageType(this, s);
  1046         public String toString() {
  1047             return tsym.getQualifiedName().toString();
  1050         public TypeKind getKind() {
  1051             return TypeKind.PACKAGE;
  1054         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1055             return v.visitNoType(this, p);
  1059     public static class TypeVar extends Type implements TypeVariable {
  1061         /** The upper bound of this type variable; set from outside.
  1062          *  Must be nonempty once it is set.
  1063          *  For a bound, `bound' is the bound type itself.
  1064          *  Multiple bounds are expressed as a single class type which has the
  1065          *  individual bounds as superclass, respectively interfaces.
  1066          *  The class type then has as `tsym' a compiler generated class `c',
  1067          *  which has a flag COMPOUND and whose owner is the type variable
  1068          *  itself. Furthermore, the erasure_field of the class
  1069          *  points to the first class or interface bound.
  1070          */
  1071         public Type bound = null;
  1073         /** The lower bound of this type variable.
  1074          *  TypeVars don't normally have a lower bound, so it is normally set
  1075          *  to syms.botType.
  1076          *  Subtypes, such as CapturedType, may provide a different value.
  1077          */
  1078         public Type lower;
  1080         public TypeVar(Name name, Symbol owner, Type lower) {
  1081             super(TYPEVAR, null);
  1082             tsym = new TypeSymbol(0, name, this, owner);
  1083             this.lower = lower;
  1086         public TypeVar(TypeSymbol tsym, Type bound, Type lower) {
  1087             super(TYPEVAR, tsym);
  1088             this.bound = bound;
  1089             this.lower = lower;
  1092         @Override
  1093         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1094             return v.visitTypeVar(this, s);
  1097         @Override
  1098         public Type getUpperBound() { return bound; }
  1100         int rank_field = -1;
  1102         @Override
  1103         public Type getLowerBound() {
  1104             return lower;
  1107         public TypeKind getKind() {
  1108             return TypeKind.TYPEVAR;
  1111         public boolean isCaptured() {
  1112             return false;
  1115         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1116             return v.visitTypeVariable(this, p);
  1120     /** A captured type variable comes from wildcards which can have
  1121      *  both upper and lower bound.  CapturedType extends TypeVar with
  1122      *  a lower bound.
  1123      */
  1124     public static class CapturedType extends TypeVar {
  1126         public WildcardType wildcard;
  1128         public CapturedType(Name name,
  1129                             Symbol owner,
  1130                             Type upper,
  1131                             Type lower,
  1132                             WildcardType wildcard) {
  1133             super(name, owner, lower);
  1134             this.lower = Assert.checkNonNull(lower);
  1135             this.bound = upper;
  1136             this.wildcard = wildcard;
  1139         @Override
  1140         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1141             return v.visitCapturedType(this, s);
  1144         @Override
  1145         public boolean isCaptured() {
  1146             return true;
  1149         @Override
  1150         public String toString() {
  1151             return "capture#"
  1152                 + (hashCode() & 0xFFFFFFFFL) % Printer.PRIME
  1153                 + " of "
  1154                 + wildcard;
  1158     public static abstract class DelegatedType extends Type {
  1159         public Type qtype;
  1160         public DelegatedType(TypeTag tag, Type qtype) {
  1161             super(tag, qtype.tsym);
  1162             this.qtype = qtype;
  1164         public String toString() { return qtype.toString(); }
  1165         public List<Type> getTypeArguments() { return qtype.getTypeArguments(); }
  1166         public Type getEnclosingType() { return qtype.getEnclosingType(); }
  1167         public List<Type> getParameterTypes() { return qtype.getParameterTypes(); }
  1168         public Type getReturnType() { return qtype.getReturnType(); }
  1169         public List<Type> getThrownTypes() { return qtype.getThrownTypes(); }
  1170         public List<Type> allparams() { return qtype.allparams(); }
  1171         public Type getUpperBound() { return qtype.getUpperBound(); }
  1172         public boolean isErroneous() { return qtype.isErroneous(); }
  1175     /**
  1176      * The type of a generic method type. It consists of a method type and
  1177      * a list of method type-parameters that are used within the method
  1178      * type.
  1179      */
  1180     public static class ForAll extends DelegatedType implements ExecutableType {
  1181         public List<Type> tvars;
  1183         public ForAll(List<Type> tvars, Type qtype) {
  1184             super(FORALL, (MethodType)qtype);
  1185             this.tvars = tvars;
  1188         @Override
  1189         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1190             return v.visitForAll(this, s);
  1193         public String toString() {
  1194             return "<" + tvars + ">" + qtype;
  1197         public List<Type> getTypeArguments()   { return tvars; }
  1199         public boolean isErroneous()  {
  1200             return qtype.isErroneous();
  1203         public Type map(Mapping f) {
  1204             return f.apply(qtype);
  1207         public boolean contains(Type elem) {
  1208             return qtype.contains(elem);
  1211         public MethodType asMethodType() {
  1212             return (MethodType)qtype;
  1215         public void complete() {
  1216             for (List<Type> l = tvars; l.nonEmpty(); l = l.tail) {
  1217                 ((TypeVar)l.head).bound.complete();
  1219             qtype.complete();
  1222         public List<TypeVar> getTypeVariables() {
  1223             return List.convert(TypeVar.class, getTypeArguments());
  1226         public TypeKind getKind() {
  1227             return TypeKind.EXECUTABLE;
  1230         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1231             return v.visitExecutable(this, p);
  1235     /** A class for inference variables, for use during method/diamond type
  1236      *  inference. An inference variable has upper/lower bounds and a set
  1237      *  of equality constraints. Such bounds are set during subtyping, type-containment,
  1238      *  type-equality checks, when the types being tested contain inference variables.
  1239      *  A change listener can be attached to an inference variable, to receive notifications
  1240      *  whenever the bounds of an inference variable change.
  1241      */
  1242     public static class UndetVar extends DelegatedType {
  1244         /** Inference variable change listener. The listener method is called
  1245          *  whenever a change to the inference variable's bounds occurs
  1246          */
  1247         public interface UndetVarListener {
  1248             /** called when some inference variable bounds (of given kinds ibs) change */
  1249             void varChanged(UndetVar uv, Set<InferenceBound> ibs);
  1252         /**
  1253          * Inference variable bound kinds
  1254          */
  1255         public enum InferenceBound {
  1256             /** upper bounds */
  1257             UPPER,
  1258             /** lower bounds */
  1259             LOWER,
  1260             /** equality constraints */
  1261             EQ;
  1264         /** inference variable bounds */
  1265         private Map<InferenceBound, List<Type>> bounds;
  1267         /** inference variable's inferred type (set from Infer.java) */
  1268         public Type inst = null;
  1270         /** inference variable's change listener */
  1271         public UndetVarListener listener = null;
  1273         @Override
  1274         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1275             return v.visitUndetVar(this, s);
  1278         public UndetVar(TypeVar origin, Types types) {
  1279             this(origin, types, true);
  1282         public UndetVar(TypeVar origin, Types types, boolean includeBounds) {
  1283             super(UNDETVAR, origin);
  1284             bounds = new EnumMap<InferenceBound, List<Type>>(InferenceBound.class);
  1285             bounds.put(InferenceBound.UPPER, includeBounds ? types.getBounds(origin) : List.<Type>nil());
  1286             bounds.put(InferenceBound.LOWER, List.<Type>nil());
  1287             bounds.put(InferenceBound.EQ, List.<Type>nil());
  1290         public String toString() {
  1291             if (inst != null) return inst.toString();
  1292             else return qtype + "?";
  1295         public Type baseType() {
  1296             if (inst != null) return inst.baseType();
  1297             else return this;
  1300         /** get all bounds of a given kind */
  1301         public List<Type> getBounds(InferenceBound ib) {
  1302             return bounds.get(ib);
  1305         /** add a bound of a given kind - this might trigger listener notification */
  1306         public void addBound(InferenceBound ib, Type bound, Types types) {
  1307             List<Type> prevBounds = bounds.get(ib);
  1308             for (Type b : prevBounds) {
  1309                 if (types.isSameType(b, bound)) {
  1310                     return;
  1313             bounds.put(ib, prevBounds.prepend(bound));
  1314             notifyChange(EnumSet.of(ib));
  1317         /** replace types in all bounds - this might trigger listener notification */
  1318         public void substBounds(List<Type> from, List<Type> to, Types types) {
  1319             EnumSet<InferenceBound> changed = EnumSet.noneOf(InferenceBound.class);
  1320             Map<InferenceBound, List<Type>> bounds2 = new EnumMap<InferenceBound, List<Type>>(InferenceBound.class);
  1321             for (Map.Entry<InferenceBound, List<Type>> _entry : bounds.entrySet()) {
  1322                 InferenceBound ib = _entry.getKey();
  1323                 List<Type> prevBounds = _entry.getValue();
  1324                 List<Type> newBounds = types.subst(prevBounds, from, to);
  1325                 bounds2.put(ib, newBounds);
  1326                 if (prevBounds != newBounds) {
  1327                     changed.add(ib);
  1330             if (!changed.isEmpty()) {
  1331                 bounds = bounds2;
  1332                 notifyChange(changed);
  1336         private void notifyChange(EnumSet<InferenceBound> ibs) {
  1337             if (listener != null) {
  1338                 listener.varChanged(this, ibs);
  1343     /** Represents VOID or NONE.
  1344      */
  1345     static class JCNoType extends Type implements NoType {
  1346         public JCNoType(TypeTag tag) {
  1347             super(tag, null);
  1350         @Override
  1351         public TypeKind getKind() {
  1352             switch (tag) {
  1353             case VOID:  return TypeKind.VOID;
  1354             case NONE:  return TypeKind.NONE;
  1355             default:
  1356                 throw new AssertionError("Unexpected tag: " + tag);
  1360         @Override
  1361         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1362             return v.visitNoType(this, p);
  1366     static class BottomType extends Type implements NullType {
  1367         public BottomType() {
  1368             super(BOT, null);
  1371         @Override
  1372         public TypeKind getKind() {
  1373             return TypeKind.NULL;
  1376         @Override
  1377         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1378             return v.visitNull(this, p);
  1381         @Override
  1382         public Type constType(Object value) {
  1383             return this;
  1386         @Override
  1387         public String stringValue() {
  1388             return "null";
  1392     public static class ErrorType extends ClassType
  1393             implements javax.lang.model.type.ErrorType {
  1395         private Type originalType = null;
  1397         public ErrorType(Type originalType, TypeSymbol tsym) {
  1398             super(noType, List.<Type>nil(), null);
  1399             tag = ERROR;
  1400             this.tsym = tsym;
  1401             this.originalType = (originalType == null ? noType : originalType);
  1404         public ErrorType(ClassSymbol c, Type originalType) {
  1405             this(originalType, c);
  1406             c.type = this;
  1407             c.kind = ERR;
  1408             c.members_field = new Scope.ErrorScope(c);
  1411         public ErrorType(Name name, TypeSymbol container, Type originalType) {
  1412             this(new ClassSymbol(PUBLIC|STATIC|ACYCLIC, name, null, container), originalType);
  1415         @Override
  1416         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1417             return v.visitErrorType(this, s);
  1420         public Type constType(Object constValue) { return this; }
  1421         public Type getEnclosingType()          { return this; }
  1422         public Type getReturnType()              { return this; }
  1423         public Type asSub(Symbol sym)            { return this; }
  1424         public Type map(Mapping f)               { return this; }
  1426         public boolean isGenType(Type t)         { return true; }
  1427         public boolean isErroneous()             { return true; }
  1428         public boolean isCompound()              { return false; }
  1429         public boolean isInterface()             { return false; }
  1431         public List<Type> allparams()            { return List.nil(); }
  1432         public List<Type> getTypeArguments()     { return List.nil(); }
  1434         public TypeKind getKind() {
  1435             return TypeKind.ERROR;
  1438         public Type getOriginalType() {
  1439             return originalType;
  1442         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1443             return v.visitError(this, p);
  1447     /**
  1448      * A visitor for types.  A visitor is used to implement operations
  1449      * (or relations) on types.  Most common operations on types are
  1450      * binary relations and this interface is designed for binary
  1451      * relations, that is, operations on the form
  1452      * Type&nbsp;&times;&nbsp;S&nbsp;&rarr;&nbsp;R.
  1453      * <!-- In plain text: Type x S -> R -->
  1455      * @param <R> the return type of the operation implemented by this
  1456      * visitor; use Void if no return type is needed.
  1457      * @param <S> the type of the second argument (the first being the
  1458      * type itself) of the operation implemented by this visitor; use
  1459      * Void if a second argument is not needed.
  1460      */
  1461     public interface Visitor<R,S> {
  1462         R visitClassType(ClassType t, S s);
  1463         R visitWildcardType(WildcardType t, S s);
  1464         R visitArrayType(ArrayType t, S s);
  1465         R visitMethodType(MethodType t, S s);
  1466         R visitPackageType(PackageType t, S s);
  1467         R visitTypeVar(TypeVar t, S s);
  1468         R visitCapturedType(CapturedType t, S s);
  1469         R visitForAll(ForAll t, S s);
  1470         R visitUndetVar(UndetVar t, S s);
  1471         R visitErrorType(ErrorType t, S s);
  1472         R visitType(Type t, S s);

mercurial