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

Thu, 24 Jul 2008 11:12:41 +0100

author
mcimadamore
date
Thu, 24 Jul 2008 11:12:41 +0100
changeset 79
36df13bde238
parent 54
eaf608c64fec
child 104
5e89c4ca637c
permissions
-rw-r--r--

6594284: NPE thrown when calling a method on an intersection type
Summary: javac should report an error when the capture of an actual type parameter does not exist
Reviewed-by: jjg

     1 /*
     2  * Copyright 1999-2008 Sun Microsystems, Inc.  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.  Sun designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
    23  * have any questions.
    24  */
    26 package com.sun.tools.javac.code;
    28 import javax.lang.model.element.Element;
    29 import javax.lang.model.type.*;
    30 import com.sun.tools.javac.util.*;
    31 import com.sun.tools.javac.code.Symbol.*;
    32 import javax.lang.model.element.Element;
    34 import javax.lang.model.type.*;
    36 import static com.sun.tools.javac.code.Flags.*;
    37 import static com.sun.tools.javac.code.Kinds.*;
    38 import static com.sun.tools.javac.code.BoundKind.*;
    39 import static com.sun.tools.javac.code.TypeTags.*;
    41 /** This class represents Java types. The class itself defines the behavior of
    42  *  the following types:
    43  *  <pre>
    44  *  base types (tags: BYTE, CHAR, SHORT, INT, LONG, FLOAT, DOUBLE, BOOLEAN),
    45  *  type `void' (tag: VOID),
    46  *  the bottom type (tag: BOT),
    47  *  the missing type (tag: NONE).
    48  *  </pre>
    49  *  <p>The behavior of the following types is defined in subclasses, which are
    50  *  all static inner classes of this class:
    51  *  <pre>
    52  *  class types (tag: CLASS, class: ClassType),
    53  *  array types (tag: ARRAY, class: ArrayType),
    54  *  method types (tag: METHOD, class: MethodType),
    55  *  package types (tag: PACKAGE, class: PackageType),
    56  *  type variables (tag: TYPEVAR, class: TypeVar),
    57  *  type arguments (tag: WILDCARD, class: WildcardType),
    58  *  polymorphic types (tag: FORALL, class: ForAll),
    59  *  the error type (tag: ERROR, class: ErrorType).
    60  *  </pre>
    61  *
    62  *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
    63  *  you write code that depends on this, you do so at your own risk.
    64  *  This code and its internal interfaces are subject to change or
    65  *  deletion without notice.</b>
    66  *
    67  *  @see TypeTags
    68  */
    69 public class Type implements PrimitiveType {
    71     /** Constant type: no type at all. */
    72     public static final JCNoType noType = new JCNoType(NONE);
    74     /** If this switch is turned on, the names of type variables
    75      *  and anonymous classes are printed with hashcodes appended.
    76      */
    77     public static boolean moreInfo = false;
    79     /** The tag of this type.
    80      *
    81      *  @see TypeTags
    82      */
    83     public int tag;
    85     /** The defining class / interface / package / type variable
    86      */
    87     public TypeSymbol tsym;
    89     /**
    90      * The constant value of this type, null if this type does not
    91      * have a constant value attribute. Only primitive types and
    92      * strings (ClassType) can have a constant value attribute.
    93      * @return the constant value attribute of this type
    94      */
    95     public Object constValue() {
    96         return null;
    97     }
    99     public <R,S> R accept(Type.Visitor<R,S> v, S s) { return v.visitType(this, s); }
   101     /** Define a type given its tag and type symbol
   102      */
   103     public Type(int tag, TypeSymbol tsym) {
   104         this.tag = tag;
   105         this.tsym = tsym;
   106     }
   108     /** An abstract class for mappings from types to types
   109      */
   110     public static abstract class Mapping {
   111         private String name;
   112         public Mapping(String name) {
   113             this.name = name;
   114         }
   115         public abstract Type apply(Type t);
   116         public String toString() {
   117             return name;
   118         }
   119     }
   121     /** map a type function over all immediate descendants of this type
   122      */
   123     public Type map(Mapping f) {
   124         return this;
   125     }
   127     /** map a type function over a list of types
   128      */
   129     public static List<Type> map(List<Type> ts, Mapping f) {
   130         if (ts.nonEmpty()) {
   131             List<Type> tail1 = map(ts.tail, f);
   132             Type t = f.apply(ts.head);
   133             if (tail1 != ts.tail || t != ts.head)
   134                 return tail1.prepend(t);
   135         }
   136         return ts;
   137     }
   139     /** Define a constant type, of the same kind as this type
   140      *  and with given constant value
   141      */
   142     public Type constType(Object constValue) {
   143         final Object value = constValue;
   144         assert tag <= BOOLEAN;
   145         return new Type(tag, tsym) {
   146                 @Override
   147                 public Object constValue() {
   148                     return value;
   149                 }
   150                 @Override
   151                 public Type baseType() {
   152                     return tsym.type;
   153                 }
   154             };
   155     }
   157     /**
   158      * If this is a constant type, return its underlying type.
   159      * Otherwise, return the type itself.
   160      */
   161     public Type baseType() {
   162         return this;
   163     }
   165     /** Return the base types of a list of types.
   166      */
   167     public static List<Type> baseTypes(List<Type> ts) {
   168         if (ts.nonEmpty()) {
   169             Type t = ts.head.baseType();
   170             List<Type> baseTypes = baseTypes(ts.tail);
   171             if (t != ts.head || baseTypes != ts.tail)
   172                 return baseTypes.prepend(t);
   173         }
   174         return ts;
   175     }
   177     /** The Java source which this type represents.
   178      */
   179     public String toString() {
   180         String s = (tsym == null || tsym.name == null)
   181             ? "<none>"
   182             : tsym.name.toString();
   183         if (moreInfo && tag == TYPEVAR) s = s + hashCode();
   184         return s;
   185     }
   187     /**
   188      * The Java source which this type list represents.  A List is
   189      * represented as a comma-spearated listing of the elements in
   190      * that list.
   191      */
   192     public static String toString(List<Type> ts) {
   193         if (ts.isEmpty()) {
   194             return "";
   195         } else {
   196             StringBuffer buf = new StringBuffer();
   197             buf.append(ts.head.toString());
   198             for (List<Type> l = ts.tail; l.nonEmpty(); l = l.tail)
   199                 buf.append(",").append(l.head.toString());
   200             return buf.toString();
   201         }
   202     }
   204     /**
   205      * The constant value of this type, converted to String
   206      */
   207     public String stringValue() {
   208         assert constValue() != null;
   209         if (tag == BOOLEAN)
   210             return ((Integer) constValue()).intValue() == 0 ? "false" : "true";
   211         else if (tag == CHAR)
   212             return String.valueOf((char) ((Integer) constValue()).intValue());
   213         else
   214             return constValue().toString();
   215     }
   217     /**
   218      * This method is analogous to isSameType, but weaker, since we
   219      * never complete classes. Where isSameType would complete a
   220      * class, equals assumes that the two types are different.
   221      */
   222     public boolean equals(Object t) {
   223         return super.equals(t);
   224     }
   226     public int hashCode() {
   227         return super.hashCode();
   228     }
   230     /** Is this a constant type whose value is false?
   231      */
   232     public boolean isFalse() {
   233         return
   234             tag == BOOLEAN &&
   235             constValue() != null &&
   236             ((Integer)constValue()).intValue() == 0;
   237     }
   239     /** Is this a constant type whose value is true?
   240      */
   241     public boolean isTrue() {
   242         return
   243             tag == BOOLEAN &&
   244             constValue() != null &&
   245             ((Integer)constValue()).intValue() != 0;
   246     }
   248     public String argtypes(boolean varargs) {
   249         List<Type> args = getParameterTypes();
   250         if (!varargs) return args.toString();
   251         StringBuffer buf = new StringBuffer();
   252         while (args.tail.nonEmpty()) {
   253             buf.append(args.head);
   254             args = args.tail;
   255             buf.append(',');
   256         }
   257         if (args.head.tag == ARRAY) {
   258             buf.append(((ArrayType)args.head).elemtype);
   259             buf.append("...");
   260         } else {
   261             buf.append(args.head);
   262         }
   263         return buf.toString();
   264     }
   266     /** Access methods.
   267      */
   268     public List<Type>        getTypeArguments()  { return List.nil(); }
   269     public Type              getEnclosingType() { return null; }
   270     public List<Type>        getParameterTypes() { return List.nil(); }
   271     public Type              getReturnType()     { return null; }
   272     public List<Type>        getThrownTypes()    { return List.nil(); }
   273     public Type              getUpperBound()     { return null; }
   274     public Type              getLowerBound()     { return null; }
   276     public void setThrown(List<Type> ts) {
   277         throw new AssertionError();
   278     }
   280     /** Navigation methods, these will work for classes, type variables,
   281      *  foralls, but will return null for arrays and methods.
   282      */
   284    /** Return all parameters of this type and all its outer types in order
   285     *  outer (first) to inner (last).
   286     */
   287     public List<Type> allparams() { return List.nil(); }
   289     /** Does this type contain "error" elements?
   290      */
   291     public boolean isErroneous() {
   292         return false;
   293     }
   295     public static boolean isErroneous(List<Type> ts) {
   296         for (List<Type> l = ts; l.nonEmpty(); l = l.tail)
   297             if (l.head.isErroneous()) return true;
   298         return false;
   299     }
   301     /** Is this type parameterized?
   302      *  A class type is parameterized if it has some parameters.
   303      *  An array type is parameterized if its element type is parameterized.
   304      *  All other types are not parameterized.
   305      */
   306     public boolean isParameterized() {
   307         return false;
   308     }
   310     /** Is this type a raw type?
   311      *  A class type is a raw type if it misses some of its parameters.
   312      *  An array type is a raw type if its element type is raw.
   313      *  All other types are not raw.
   314      *  Type validation will ensure that the only raw types
   315      *  in a program are types that miss all their type variables.
   316      */
   317     public boolean isRaw() {
   318         return false;
   319     }
   321     public boolean isCompound() {
   322         return tsym.completer == null
   323             // Compound types can't have a completer.  Calling
   324             // flags() will complete the symbol causing the
   325             // compiler to load classes unnecessarily.  This led
   326             // to regression 6180021.
   327             && (tsym.flags() & COMPOUND) != 0;
   328     }
   330     public boolean isInterface() {
   331         return (tsym.flags() & INTERFACE) != 0;
   332     }
   334     public boolean isPrimitive() {
   335         return tag < VOID;
   336     }
   338     /**
   339      * Does this type contain occurrences of type t?
   340      */
   341     public boolean contains(Type t) {
   342         return t == this;
   343     }
   345     public static boolean contains(List<Type> ts, Type t) {
   346         for (List<Type> l = ts;
   347              l.tail != null /*inlined: l.nonEmpty()*/;
   348              l = l.tail)
   349             if (l.head.contains(t)) return true;
   350         return false;
   351     }
   353     /** Does this type contain an occurrence of some type in `elems'?
   354      */
   355     public boolean containsSome(List<Type> ts) {
   356         for (List<Type> l = ts; l.nonEmpty(); l = l.tail)
   357             if (this.contains(ts.head)) return true;
   358         return false;
   359     }
   361     public boolean isSuperBound() { return false; }
   362     public boolean isExtendsBound() { return false; }
   363     public boolean isUnbound() { return false; }
   364     public Type withTypeVar(Type t) { return this; }
   366     public static List<Type> removeBounds(List<Type> ts) {
   367         ListBuffer<Type> result = new ListBuffer<Type>();
   368         for(;ts.nonEmpty(); ts = ts.tail) {
   369             result.append(ts.head.removeBounds());
   370         }
   371         return result.toList();
   372     }
   373     public Type removeBounds() {
   374         return this;
   375     }
   377     /** The underlying method type of this type.
   378      */
   379     public MethodType asMethodType() { throw new AssertionError(); }
   381     /** Complete loading all classes in this type.
   382      */
   383     public void complete() {}
   385     public Object clone() {
   386         try {
   387             return super.clone();
   388         } catch (CloneNotSupportedException e) {
   389             throw new AssertionError(e);
   390         }
   391     }
   393     public TypeSymbol asElement() {
   394         return tsym;
   395     }
   397     public TypeKind getKind() {
   398         switch (tag) {
   399         case BYTE:      return TypeKind.BYTE;
   400         case CHAR:      return TypeKind.CHAR;
   401         case SHORT:     return TypeKind.SHORT;
   402         case INT:       return TypeKind.INT;
   403         case LONG:      return TypeKind.LONG;
   404         case FLOAT:     return TypeKind.FLOAT;
   405         case DOUBLE:    return TypeKind.DOUBLE;
   406         case BOOLEAN:   return TypeKind.BOOLEAN;
   407         case VOID:      return TypeKind.VOID;
   408         case BOT:       return TypeKind.NULL;
   409         case NONE:      return TypeKind.NONE;
   410         default:        return TypeKind.OTHER;
   411         }
   412     }
   414     public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   415         if (isPrimitive())
   416             return v.visitPrimitive(this, p);
   417         else
   418             throw new AssertionError();
   419     }
   421     public static class WildcardType extends Type
   422             implements javax.lang.model.type.WildcardType {
   424         public Type type;
   425         public BoundKind kind;
   426         public TypeVar bound;
   428         @Override
   429         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   430             return v.visitWildcardType(this, s);
   431         }
   433         public WildcardType(Type type, BoundKind kind, TypeSymbol tsym) {
   434             super(WILDCARD, tsym);
   435             assert(type != null);
   436             this.kind = kind;
   437             this.type = type;
   438         }
   439         public WildcardType(WildcardType t, TypeVar bound) {
   440             this(t.type, t.kind, t.tsym, bound);
   441         }
   443         public WildcardType(Type type, BoundKind kind, TypeSymbol tsym, TypeVar bound) {
   444             this(type, kind, tsym);
   445             this.bound = bound;
   446         }
   448         public boolean isSuperBound() {
   449             return kind == SUPER ||
   450                 kind == UNBOUND;
   451         }
   452         public boolean isExtendsBound() {
   453             return kind == EXTENDS ||
   454                 kind == UNBOUND;
   455         }
   456         public boolean isUnbound() {
   457             return kind == UNBOUND;
   458         }
   460         public Type withTypeVar(Type t) {
   461             //-System.err.println(this+".withTypeVar("+t+");");//DEBUG
   462             if (bound == t)
   463                 return this;
   464             bound = (TypeVar)t;
   465             return this;
   466         }
   468         boolean isPrintingBound = false;
   469         public String toString() {
   470             StringBuffer s = new StringBuffer();
   471             s.append(kind.toString());
   472             if (kind != UNBOUND)
   473                 s.append(type);
   474             if (moreInfo && bound != null && !isPrintingBound)
   475                 try {
   476                     isPrintingBound = true;
   477                     s.append("{:").append(bound.bound).append(":}");
   478                 } finally {
   479                     isPrintingBound = false;
   480                 }
   481             return s.toString();
   482         }
   484         public Type map(Mapping f) {
   485             //- System.err.println("   (" + this + ").map(" + f + ")");//DEBUG
   486             Type t = type;
   487             if (t != null)
   488                 t = f.apply(t);
   489             if (t == type)
   490                 return this;
   491             else
   492                 return new WildcardType(t, kind, tsym, bound);
   493         }
   495         public Type removeBounds() {
   496             return isUnbound() ? this : type;
   497         }
   499         public Type getExtendsBound() {
   500             if (kind == EXTENDS)
   501                 return type;
   502             else
   503                 return null;
   504         }
   506         public Type getSuperBound() {
   507             if (kind == SUPER)
   508                 return type;
   509             else
   510                 return null;
   511         }
   513         public TypeKind getKind() {
   514             return TypeKind.WILDCARD;
   515         }
   517         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   518             return v.visitWildcard(this, p);
   519         }
   520     }
   522     public static class ClassType extends Type implements DeclaredType {
   524         /** The enclosing type of this type. If this is the type of an inner
   525          *  class, outer_field refers to the type of its enclosing
   526          *  instance class, in all other cases it referes to noType.
   527          */
   528         private Type outer_field;
   530         /** The type parameters of this type (to be set once class is loaded).
   531          */
   532         public List<Type> typarams_field;
   534         /** A cache variable for the type parameters of this type,
   535          *  appended to all parameters of its enclosing class.
   536          *  @see #allparams
   537          */
   538         public List<Type> allparams_field;
   540         /** The supertype of this class (to be set once class is loaded).
   541          */
   542         public Type supertype_field;
   544         /** The interfaces of this class (to be set once class is loaded).
   545          */
   546         public List<Type> interfaces_field;
   548         public ClassType(Type outer, List<Type> typarams, TypeSymbol tsym) {
   549             super(CLASS, tsym);
   550             this.outer_field = outer;
   551             this.typarams_field = typarams;
   552             this.allparams_field = null;
   553             this.supertype_field = null;
   554             this.interfaces_field = null;
   555             /*
   556             // this can happen during error recovery
   557             assert
   558                 outer.isParameterized() ?
   559                 typarams.length() == tsym.type.typarams().length() :
   560                 outer.isRaw() ?
   561                 typarams.length() == 0 :
   562                 true;
   563             */
   564         }
   566         @Override
   567         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   568             return v.visitClassType(this, s);
   569         }
   571         public Type constType(Object constValue) {
   572             final Object value = constValue;
   573             return new ClassType(getEnclosingType(), typarams_field, tsym) {
   574                     @Override
   575                     public Object constValue() {
   576                         return value;
   577                     }
   578                     @Override
   579                     public Type baseType() {
   580                         return tsym.type;
   581                     }
   582                 };
   583         }
   585         /** The Java source which this type represents.
   586          */
   587         public String toString() {
   588             StringBuffer buf = new StringBuffer();
   589             if (getEnclosingType().tag == CLASS && tsym.owner.kind == TYP) {
   590                 buf.append(getEnclosingType().toString());
   591                 buf.append(".");
   592                 buf.append(className(tsym, false));
   593             } else {
   594                 buf.append(className(tsym, true));
   595             }
   596             if (getTypeArguments().nonEmpty()) {
   597                 buf.append('<');
   598                 buf.append(getTypeArguments().toString());
   599                 buf.append(">");
   600             }
   601             return buf.toString();
   602         }
   603 //where
   604             private String className(Symbol sym, boolean longform) {
   605                 if (sym.name.len == 0 && (sym.flags() & COMPOUND) != 0) {
   606                     StringBuffer s = new StringBuffer(supertype_field.toString());
   607                     for (List<Type> is=interfaces_field; is.nonEmpty(); is = is.tail) {
   608                         s.append("&");
   609                         s.append(is.head.toString());
   610                     }
   611                     return s.toString();
   612                 } else if (sym.name.len == 0) {
   613                     String s;
   614                     ClassType norm = (ClassType) tsym.type;
   615                     if (norm == null) {
   616                         s = Log.getLocalizedString("anonymous.class", (Object)null);
   617                     } else if (norm.interfaces_field != null && norm.interfaces_field.nonEmpty()) {
   618                         s = Log.getLocalizedString("anonymous.class",
   619                                                    norm.interfaces_field.head);
   620                     } else {
   621                         s = Log.getLocalizedString("anonymous.class",
   622                                                    norm.supertype_field);
   623                     }
   624                     if (moreInfo)
   625                         s += String.valueOf(sym.hashCode());
   626                     return s;
   627                 } else if (longform) {
   628                     return sym.getQualifiedName().toString();
   629                 } else {
   630                     return sym.name.toString();
   631                 }
   632             }
   634         public List<Type> getTypeArguments() {
   635             if (typarams_field == null) {
   636                 complete();
   637                 if (typarams_field == null)
   638                     typarams_field = List.nil();
   639             }
   640             return typarams_field;
   641         }
   643         public boolean hasErasedSupertypes() {
   644             return isRaw();
   645         }
   647         public Type getEnclosingType() {
   648             return outer_field;
   649         }
   651         public void setEnclosingType(Type outer) {
   652             outer_field = outer;
   653         }
   655         public List<Type> allparams() {
   656             if (allparams_field == null) {
   657                 allparams_field = getTypeArguments().prependList(getEnclosingType().allparams());
   658             }
   659             return allparams_field;
   660         }
   662         public boolean isErroneous() {
   663             return
   664                 getEnclosingType().isErroneous() ||
   665                 isErroneous(getTypeArguments()) ||
   666                 this != tsym.type && tsym.type.isErroneous();
   667         }
   669         public boolean isParameterized() {
   670             return allparams().tail != null;
   671             // optimization, was: allparams().nonEmpty();
   672         }
   674         /** A cache for the rank. */
   675         int rank_field = -1;
   677         /** A class type is raw if it misses some
   678          *  of its type parameter sections.
   679          *  After validation, this is equivalent to:
   680          *  allparams.isEmpty() && tsym.type.allparams.nonEmpty();
   681          */
   682         public boolean isRaw() {
   683             return
   684                 this != tsym.type && // necessary, but not sufficient condition
   685                 tsym.type.allparams().nonEmpty() &&
   686                 allparams().isEmpty();
   687         }
   689         public Type map(Mapping f) {
   690             Type outer = getEnclosingType();
   691             Type outer1 = f.apply(outer);
   692             List<Type> typarams = getTypeArguments();
   693             List<Type> typarams1 = map(typarams, f);
   694             if (outer1 == outer && typarams1 == typarams) return this;
   695             else return new ClassType(outer1, typarams1, tsym);
   696         }
   698         public boolean contains(Type elem) {
   699             return
   700                 elem == this
   701                 || (isParameterized()
   702                     && (getEnclosingType().contains(elem) || contains(getTypeArguments(), elem)));
   703         }
   705         public void complete() {
   706             if (tsym.completer != null) tsym.complete();
   707         }
   709         public TypeKind getKind() {
   710             return TypeKind.DECLARED;
   711         }
   713         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   714             return v.visitDeclared(this, p);
   715         }
   716     }
   718     public static class ErasedClassType extends ClassType {
   719         public ErasedClassType(Type outer, TypeSymbol tsym) {
   720             super(outer, List.<Type>nil(), tsym);
   721         }
   723         @Override
   724         public boolean hasErasedSupertypes() {
   725             return true;
   726         }
   727     }
   729     public static class ArrayType extends Type
   730             implements javax.lang.model.type.ArrayType {
   732         public Type elemtype;
   734         public ArrayType(Type elemtype, TypeSymbol arrayClass) {
   735             super(ARRAY, arrayClass);
   736             this.elemtype = elemtype;
   737         }
   739         @Override
   740         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   741             return v.visitArrayType(this, s);
   742         }
   744         public String toString() {
   745             return elemtype + "[]";
   746         }
   748         public boolean equals(Object obj) {
   749             return
   750                 this == obj ||
   751                 (obj instanceof ArrayType &&
   752                  this.elemtype.equals(((ArrayType)obj).elemtype));
   753         }
   755         public int hashCode() {
   756             return (ARRAY << 5) + elemtype.hashCode();
   757         }
   759         public List<Type> allparams() { return elemtype.allparams(); }
   761         public boolean isErroneous() {
   762             return elemtype.isErroneous();
   763         }
   765         public boolean isParameterized() {
   766             return elemtype.isParameterized();
   767         }
   769         public boolean isRaw() {
   770             return elemtype.isRaw();
   771         }
   773         public Type map(Mapping f) {
   774             Type elemtype1 = f.apply(elemtype);
   775             if (elemtype1 == elemtype) return this;
   776             else return new ArrayType(elemtype1, tsym);
   777         }
   779         public boolean contains(Type elem) {
   780             return elem == this || elemtype.contains(elem);
   781         }
   783         public void complete() {
   784             elemtype.complete();
   785         }
   787         public Type getComponentType() {
   788             return elemtype;
   789         }
   791         public TypeKind getKind() {
   792             return TypeKind.ARRAY;
   793         }
   795         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   796             return v.visitArray(this, p);
   797         }
   798     }
   800     public static class MethodType extends Type
   801                     implements Cloneable, ExecutableType {
   803         public List<Type> argtypes;
   804         public Type restype;
   805         public List<Type> thrown;
   807         public MethodType(List<Type> argtypes,
   808                           Type restype,
   809                           List<Type> thrown,
   810                           TypeSymbol methodClass) {
   811             super(METHOD, methodClass);
   812             this.argtypes = argtypes;
   813             this.restype = restype;
   814             this.thrown = thrown;
   815         }
   817         @Override
   818         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   819             return v.visitMethodType(this, s);
   820         }
   822         /** The Java source which this type represents.
   823          *
   824          *  XXX 06/09/99 iris This isn't correct Java syntax, but it probably
   825          *  should be.
   826          */
   827         public String toString() {
   828             return "(" + argtypes + ")" + restype;
   829         }
   831         public boolean equals(Object obj) {
   832             if (this == obj)
   833                 return true;
   834             if (!(obj instanceof MethodType))
   835                 return false;
   836             MethodType m = (MethodType)obj;
   837             List<Type> args1 = argtypes;
   838             List<Type> args2 = m.argtypes;
   839             while (!args1.isEmpty() && !args2.isEmpty()) {
   840                 if (!args1.head.equals(args2.head))
   841                     return false;
   842                 args1 = args1.tail;
   843                 args2 = args2.tail;
   844             }
   845             if (!args1.isEmpty() || !args2.isEmpty())
   846                 return false;
   847             return restype.equals(m.restype);
   848         }
   850         public int hashCode() {
   851             int h = METHOD;
   852             for (List<Type> thisargs = this.argtypes;
   853                  thisargs.tail != null; /*inlined: thisargs.nonEmpty()*/
   854                  thisargs = thisargs.tail)
   855                 h = (h << 5) + thisargs.head.hashCode();
   856             return (h << 5) + this.restype.hashCode();
   857         }
   859         public List<Type>        getParameterTypes() { return argtypes; }
   860         public Type              getReturnType()     { return restype; }
   861         public List<Type>        getThrownTypes()    { return thrown; }
   863         public void setThrown(List<Type> t) {
   864             thrown = t;
   865         }
   867         public boolean isErroneous() {
   868             return
   869                 isErroneous(argtypes) ||
   870                 restype != null && restype.isErroneous();
   871         }
   873         public Type map(Mapping f) {
   874             List<Type> argtypes1 = map(argtypes, f);
   875             Type restype1 = f.apply(restype);
   876             List<Type> thrown1 = map(thrown, f);
   877             if (argtypes1 == argtypes &&
   878                 restype1 == restype &&
   879                 thrown1 == thrown) return this;
   880             else return new MethodType(argtypes1, restype1, thrown1, tsym);
   881         }
   883         public boolean contains(Type elem) {
   884             return elem == this || contains(argtypes, elem) || restype.contains(elem);
   885         }
   887         public MethodType asMethodType() { return this; }
   889         public void complete() {
   890             for (List<Type> l = argtypes; l.nonEmpty(); l = l.tail)
   891                 l.head.complete();
   892             restype.complete();
   893             for (List<Type> l = thrown; l.nonEmpty(); l = l.tail)
   894                 l.head.complete();
   895         }
   897         public List<TypeVar> getTypeVariables() {
   898             return List.nil();
   899         }
   901         public TypeSymbol asElement() {
   902             return null;
   903         }
   905         public TypeKind getKind() {
   906             return TypeKind.EXECUTABLE;
   907         }
   909         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   910             return v.visitExecutable(this, p);
   911         }
   912     }
   914     public static class PackageType extends Type implements NoType {
   916         PackageType(TypeSymbol tsym) {
   917             super(PACKAGE, tsym);
   918         }
   920         @Override
   921         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   922             return v.visitPackageType(this, s);
   923         }
   925         public String toString() {
   926             return tsym.getQualifiedName().toString();
   927         }
   929         public TypeKind getKind() {
   930             return TypeKind.PACKAGE;
   931         }
   933         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   934             return v.visitNoType(this, p);
   935         }
   936     }
   938     public static class TypeVar extends Type implements TypeVariable {
   940         /** The bound of this type variable; set from outside.
   941          *  Must be nonempty once it is set.
   942          *  For a bound, `bound' is the bound type itself.
   943          *  Multiple bounds are expressed as a single class type which has the
   944          *  individual bounds as superclass, respectively interfaces.
   945          *  The class type then has as `tsym' a compiler generated class `c',
   946          *  which has a flag COMPOUND and whose owner is the type variable
   947          *  itself. Furthermore, the erasure_field of the class
   948          *  points to the first class or interface bound.
   949          */
   950         public Type bound = null;
   951         public Type lower;
   953         public TypeVar(Name name, Symbol owner, Type lower) {
   954             super(TYPEVAR, null);
   955             tsym = new TypeSymbol(0, name, this, owner);
   956             this.lower = lower;
   957         }
   959         public TypeVar(TypeSymbol tsym, Type bound, Type lower) {
   960             super(TYPEVAR, tsym);
   961             this.bound = bound;
   962             this.lower = lower;
   963         }
   965         @Override
   966         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   967             return v.visitTypeVar(this, s);
   968         }
   970         public Type getUpperBound() { return bound; }
   972         int rank_field = -1;
   974         public Type getLowerBound() {
   975             return lower;
   976         }
   978         public TypeKind getKind() {
   979             return TypeKind.TYPEVAR;
   980         }
   982         public boolean isCaptured() {
   983             return false;
   984         }
   986         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   987             return v.visitTypeVariable(this, p);
   988         }
   989     }
   991     /** A captured type variable comes from wildcards which can have
   992      *  both upper and lower bound.  CapturedType extends TypeVar with
   993      *  a lower bound.
   994      */
   995     public static class CapturedType extends TypeVar {
   997         public Type lower;
   998         public WildcardType wildcard;
  1000         public CapturedType(Name name,
  1001                             Symbol owner,
  1002                             Type upper,
  1003                             Type lower,
  1004                             WildcardType wildcard) {
  1005             super(name, owner, lower);
  1006             assert lower != null;
  1007             this.bound = upper;
  1008             this.lower = lower;
  1009             this.wildcard = wildcard;
  1012         @Override
  1013         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1014             return v.visitCapturedType(this, s);
  1017         public Type getLowerBound() {
  1018             return lower;
  1021         @Override
  1022         public boolean isCaptured() {
  1023             return true;
  1026         @Override
  1027         public String toString() {
  1028             return "capture#"
  1029                 + (hashCode() & 0xFFFFFFFFL) % PRIME
  1030                 + " of "
  1031                 + wildcard;
  1033         static final int PRIME = 997;  // largest prime less than 1000
  1036     public static abstract class DelegatedType extends Type {
  1037         public Type qtype;
  1038         public DelegatedType(int tag, Type qtype) {
  1039             super(tag, qtype.tsym);
  1040             this.qtype = qtype;
  1042         public String toString() { return qtype.toString(); }
  1043         public List<Type> getTypeArguments() { return qtype.getTypeArguments(); }
  1044         public Type getEnclosingType() { return qtype.getEnclosingType(); }
  1045         public List<Type> getParameterTypes() { return qtype.getParameterTypes(); }
  1046         public Type getReturnType() { return qtype.getReturnType(); }
  1047         public List<Type> getThrownTypes() { return qtype.getThrownTypes(); }
  1048         public List<Type> allparams() { return qtype.allparams(); }
  1049         public Type getUpperBound() { return qtype.getUpperBound(); }
  1050         public Object clone() { DelegatedType t = (DelegatedType)super.clone(); t.qtype = (Type)qtype.clone(); return t; }
  1051         public boolean isErroneous() { return qtype.isErroneous(); }
  1054     public static class ForAll extends DelegatedType
  1055             implements Cloneable, ExecutableType {
  1056         public List<Type> tvars;
  1058         public ForAll(List<Type> tvars, Type qtype) {
  1059             super(FORALL, qtype);
  1060             this.tvars = tvars;
  1063         @Override
  1064         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1065             return v.visitForAll(this, s);
  1068         public String toString() {
  1069             return "<" + tvars + ">" + qtype;
  1072         public List<Type> getTypeArguments()   { return tvars; }
  1074         public void setThrown(List<Type> t) {
  1075             qtype.setThrown(t);
  1078         public Object clone() {
  1079             ForAll result = (ForAll)super.clone();
  1080             result.qtype = (Type)result.qtype.clone();
  1081             return result;
  1084         public boolean isErroneous()  {
  1085             return qtype.isErroneous();
  1088         public Type map(Mapping f) {
  1089             return f.apply(qtype);
  1092         public boolean contains(Type elem) {
  1093             return qtype.contains(elem);
  1096         public MethodType asMethodType() {
  1097             return qtype.asMethodType();
  1100         public void complete() {
  1101             for (List<Type> l = tvars; l.nonEmpty(); l = l.tail) {
  1102                 ((TypeVar)l.head).bound.complete();
  1104             qtype.complete();
  1107         public List<TypeVar> getTypeVariables() {
  1108             return List.convert(TypeVar.class, getTypeArguments());
  1111         public TypeKind getKind() {
  1112             return TypeKind.EXECUTABLE;
  1115         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1116             return v.visitExecutable(this, p);
  1120     /** A class for instantiatable variables, for use during type
  1121      *  inference.
  1122      */
  1123     public static class UndetVar extends DelegatedType {
  1124         public List<Type> lobounds = List.nil();
  1125         public List<Type> hibounds = List.nil();
  1126         public Type inst = null;
  1128         @Override
  1129         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1130             return v.visitUndetVar(this, s);
  1133         public UndetVar(Type origin) {
  1134             super(UNDETVAR, origin);
  1137         public String toString() {
  1138             if (inst != null) return inst.toString();
  1139             else return qtype + "?";
  1142         public Type baseType() {
  1143             if (inst != null) return inst.baseType();
  1144             else return this;
  1148     /** Represents VOID or NONE.
  1149      */
  1150     static class JCNoType extends Type implements NoType {
  1151         public JCNoType(int tag) {
  1152             super(tag, null);
  1155         @Override
  1156         public TypeKind getKind() {
  1157             switch (tag) {
  1158             case VOID:  return TypeKind.VOID;
  1159             case NONE:  return TypeKind.NONE;
  1160             default:
  1161                 throw new AssertionError("Unexpected tag: " + tag);
  1165         @Override
  1166         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1167             return v.visitNoType(this, p);
  1171     static class BottomType extends Type implements NullType {
  1172         public BottomType() {
  1173             super(TypeTags.BOT, null);
  1176         @Override
  1177         public TypeKind getKind() {
  1178             return TypeKind.NULL;
  1181         @Override
  1182         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1183             return v.visitNull(this, p);
  1186         @Override
  1187         public Type constType(Object value) {
  1188             return this;
  1191         @Override
  1192         public String stringValue() {
  1193             return "null";
  1197     public static class ErrorType extends ClassType
  1198             implements javax.lang.model.type.ErrorType {
  1200         public ErrorType() {
  1201             super(noType, List.<Type>nil(), null);
  1202             tag = ERROR;
  1205         public ErrorType(ClassSymbol c) {
  1206             this();
  1207             tsym = c;
  1208             c.type = this;
  1209             c.kind = ERR;
  1210             c.members_field = new Scope.ErrorScope(c);
  1213         public ErrorType(Name name, TypeSymbol container) {
  1214             this(new ClassSymbol(PUBLIC|STATIC|ACYCLIC, name, null, container));
  1217         @Override
  1218         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1219             return v.visitErrorType(this, s);
  1222         public Type constType(Object constValue) { return this; }
  1223         public Type getEnclosingType()          { return this; }
  1224         public Type getReturnType()              { return this; }
  1225         public Type asSub(Symbol sym)            { return this; }
  1226         public Type map(Mapping f)               { return this; }
  1228         public boolean isGenType(Type t)         { return true; }
  1229         public boolean isErroneous()             { return true; }
  1230         public boolean isCompound()              { return false; }
  1231         public boolean isInterface()             { return false; }
  1233         public List<Type> allparams()            { return List.nil(); }
  1234         public List<Type> getTypeArguments()     { return List.nil(); }
  1236         public TypeKind getKind() {
  1237             return TypeKind.ERROR;
  1240         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1241             return v.visitError(this, p);
  1245     /**
  1246      * A visitor for types.  A visitor is used to implement operations
  1247      * (or relations) on types.  Most common operations on types are
  1248      * binary relations and this interface is designed for binary
  1249      * relations, that is, operations on the form
  1250      * Type&nbsp;&times;&nbsp;S&nbsp;&rarr;&nbsp;R.
  1251      * <!-- In plain text: Type x S -> R -->
  1253      * @param <R> the return type of the operation implemented by this
  1254      * visitor; use Void if no return type is needed.
  1255      * @param <S> the type of the second argument (the first being the
  1256      * type itself) of the operation implemented by this visitor; use
  1257      * Void if a second argument is not needed.
  1258      */
  1259     public interface Visitor<R,S> {
  1260         R visitClassType(ClassType t, S s);
  1261         R visitWildcardType(WildcardType t, S s);
  1262         R visitArrayType(ArrayType t, S s);
  1263         R visitMethodType(MethodType t, S s);
  1264         R visitPackageType(PackageType t, S s);
  1265         R visitTypeVar(TypeVar t, S s);
  1266         R visitCapturedType(CapturedType t, S s);
  1267         R visitForAll(ForAll t, S s);
  1268         R visitUndetVar(UndetVar t, S s);
  1269         R visitErrorType(ErrorType t, S s);
  1270         R visitType(Type t, S s);

mercurial