< prev index next >

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

Print this page

  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  */
  25 
  26 package com.sun.tools.javac.code;
  27 
  28 import java.lang.annotation.Annotation;
  29 import java.util.ArrayDeque;
  30 import java.util.Collections;
  31 import java.util.EnumMap;
  32 import java.util.Map;
  33 import java.util.function.Predicate;
  34 
  35 import javax.lang.model.type.*;
  36 
  37 import com.sun.tools.javac.code.Symbol.*;

  38 import com.sun.tools.javac.code.TypeMetadata.Entry;
  39 import com.sun.tools.javac.code.Types.TypeMapping;
  40 import com.sun.tools.javac.code.Types.UniqueType;
  41 import com.sun.tools.javac.comp.Infer.IncorporationAction;
  42 import com.sun.tools.javac.jvm.ClassFile;
  43 import com.sun.tools.javac.jvm.PoolConstant;
  44 import com.sun.tools.javac.util.*;
  45 import com.sun.tools.javac.util.DefinedBy.Api;
  46 
  47 import static com.sun.tools.javac.code.BoundKind.*;
  48 import static com.sun.tools.javac.code.Flags.*;
  49 import static com.sun.tools.javac.code.Kinds.Kind.*;
  50 import static com.sun.tools.javac.code.TypeTag.*;
  51 
  52 /** This class represents Java types. The class itself defines the behavior of
  53  *  the following types:
  54  *  <pre>
  55  *  base types (tags: BYTE, CHAR, SHORT, INT, LONG, FLOAT, DOUBLE, BOOLEAN),
  56  *  type `void' (tag: VOID),
  57  *  the bottom type (tag: BOT),

 219         return lb.toList();
 220     }
 221 
 222     /**For ErrorType, returns the original type, otherwise returns the type itself.
 223      */
 224     public Type getOriginalType() {
 225         return this;
 226     }
 227 
 228     public <R,S> R accept(Type.Visitor<R,S> v, S s) { return v.visitType(this, s); }
 229 
 230     /** Define a type given its tag, type symbol, and type annotations
 231      */
 232 
 233     public Type(TypeSymbol tsym, TypeMetadata metadata) {
 234         Assert.checkNonNull(metadata);
 235         this.tsym = tsym;
 236         this.metadata = metadata;
 237     }
 238 





































































 239     /**
 240      * A subclass of {@link Types.TypeMapping} which applies a mapping recursively to the subterms
 241      * of a given type expression. This mapping returns the original type is no changes occurred
 242      * when recursively mapping the original type's subterms.
 243      */
 244     public abstract static class StructuralTypeMapping<S> extends Types.TypeMapping<S> {
 245 
 246         @Override
 247         public Type visitClassType(ClassType t, S s) {
 248             Type outer = t.getEnclosingType();
 249             Type outer1 = visit(outer, s);
 250             List<Type> typarams = t.getTypeArguments();
 251             List<Type> typarams1 = visit(typarams, s);
 252             if (outer1 == outer && typarams1 == typarams) return t;
 253             else return new ClassType(outer1, typarams1, t.tsym, t.metadata) {
 254                 @Override
 255                 protected boolean needsStripping() {
 256                     return true;
 257                 }
 258             };
 259         }
 260 
 261         @Override
 262         public Type visitWildcardType(WildcardType wt, S s) {
 263             Type t = wt.type;
 264             if (t != null)
 265                 t = visit(t, s);
 266             if (t == wt.type)
 267                 return wt;
 268             else
 269                 return new WildcardType(t, wt.kind, wt.tsym, wt.bound, wt.metadata) {
 270                     @Override
 271                     protected boolean needsStripping() {
 272                         return true;
 273                     }

 929 
 930         @DefinedBy(Api.LANGUAGE_MODEL)
 931         public Type getSuperBound() {
 932             if (kind == SUPER)
 933                 return type;
 934             else
 935                 return null;
 936         }
 937 
 938         @DefinedBy(Api.LANGUAGE_MODEL)
 939         public TypeKind getKind() {
 940             return TypeKind.WILDCARD;
 941         }
 942 
 943         @DefinedBy(Api.LANGUAGE_MODEL)
 944         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
 945             return v.visitWildcard(this, p);
 946         }
 947     }
 948 


































 949     public static class ClassType extends Type implements DeclaredType, LoadableConstant,
 950                                                           javax.lang.model.type.ErrorType {
 951 














































































 952         /** The enclosing type of this type. If this is the type of an inner
 953          *  class, outer_field refers to the type of its enclosing
 954          *  instance class, in all other cases it refers to noType.
 955          */
 956         private Type outer_field;
 957 
 958         /** The type parameters of this type (to be set once class is loaded).
 959          */
 960         public List<Type> typarams_field;
 961 
 962         /** A cache variable for the type parameters of this type,
 963          *  appended to all parameters of its enclosing class.
 964          *  @see #allparams
 965          */
 966         public List<Type> allparams_field;
 967 
 968         /** The supertype of this class (to be set once class is loaded).
 969          */
 970         public Type supertype_field;
 971 
 972         /** The interfaces of this class (to be set once class is loaded).
 973          */
 974         public List<Type> interfaces_field;
 975 
 976         /** All the interfaces of this class, including missing ones.
 977          */
 978         public List<Type> all_interfaces_field;
 979 













 980         public ClassType(Type outer, List<Type> typarams, TypeSymbol tsym) {
 981             this(outer, typarams, tsym, TypeMetadata.EMPTY);
 982         }
 983 
 984         public ClassType(Type outer, List<Type> typarams, TypeSymbol tsym,
 985                          TypeMetadata metadata) {
 986             super(tsym, metadata);
 987             this.outer_field = outer;
 988             this.typarams_field = typarams;
 989             this.allparams_field = null;
 990             this.supertype_field = null;
 991             this.interfaces_field = null;

 992         }
 993 
 994         public int poolTag() {
 995             return ClassFile.CONSTANT_Class;
 996         }
 997 
 998         @Override
 999         public ClassType cloneWithMetadata(TypeMetadata md) {
1000             return new ClassType(outer_field, typarams_field, tsym, md) {
1001                 @Override
1002                 public Type baseType() { return ClassType.this.baseType(); }
1003             };
1004         }
1005 
1006         @Override
1007         public TypeTag getTag() {
1008             return CLASS;
1009         }
1010 
1011         @Override
1012         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1013             return v.visitClassType(this, s);
1014         }
1015 
1016         public Type constType(Object constValue) {
1017             final Object value = constValue;
1018             return new ClassType(getEnclosingType(), typarams_field, tsym, metadata) {
1019                     @Override
1020                     public Object constValue() {
1021                         return value;
1022                     }
1023                     @Override
1024                     public Type baseType() {
1025                         return tsym.type;
1026                     }
1027                 };
1028         }
1029 
1030         /** The Java source which this type represents.
1031          */
1032         @DefinedBy(Api.LANGUAGE_MODEL)
1033         public String toString() {
1034             StringBuilder buf = new StringBuilder();
1035             if (getEnclosingType().hasTag(CLASS) && tsym.owner.kind == TYP) {
1036                 buf.append(getEnclosingType().toString());
1037                 buf.append(".");
1038                 appendAnnotationsString(buf);

1041                 if (isAnnotated()) {
1042                     if (!tsym.packge().isUnnamed()) {
1043                         buf.append(tsym.packge());
1044                         buf.append(".");
1045                     }
1046                     ListBuffer<Name> names = new ListBuffer<>();
1047                     for (Symbol sym = tsym.owner; sym != null && sym.kind == TYP; sym = sym.owner) {
1048                         names.prepend(sym.name);
1049                     }
1050                     for (Name name : names) {
1051                         buf.append(name);
1052                         buf.append(".");
1053                     }
1054                     appendAnnotationsString(buf);
1055                     buf.append(tsym.name);
1056                 } else {
1057                     buf.append(className(tsym, true));
1058                 }
1059             }
1060 











1061             if (getTypeArguments().nonEmpty()) {
1062                 buf.append('<');
1063                 buf.append(getTypeArguments().toString());
1064                 buf.append(">");
1065             }
1066             return buf.toString();
1067         }
1068 //where
1069             private String className(Symbol sym, boolean longform) {
1070                 if (sym.name.isEmpty() && (sym.flags() & COMPOUND) != 0) {
1071                     StringBuilder s = new StringBuilder(supertype_field.toString());
1072                     for (List<Type> is=interfaces_field; is.nonEmpty(); is = is.tail) {
1073                         s.append("&");
1074                         s.append(is.head.toString());
1075                     }
1076                     return s.toString();
1077                 } else if (sym.name.isEmpty()) {
1078                     String s;
1079                     ClassType norm = (ClassType) tsym.type;
1080                     if (norm == null) {
1081                         s = Log.getLocalizedString("anonymous.class", (Object)null);
1082                     } else if (norm.interfaces_field != null && norm.interfaces_field.nonEmpty()) {
1083                         s = Log.getLocalizedString("anonymous.class",
1084                                                    norm.interfaces_field.head);
1085                     } else {
1086                         s = Log.getLocalizedString("anonymous.class",
1087                                                    norm.supertype_field);
1088                     }
1089                     if (moreInfo)
1090                         s += String.valueOf(sym.hashCode());
1091                     return s;
1092                 } else if (longform) {
1093                     return sym.getQualifiedName().toString();


1094                 } else {
1095                     return sym.name.toString();
1096                 }

1097             }
1098 




1099         @DefinedBy(Api.LANGUAGE_MODEL)
1100         public List<Type> getTypeArguments() {
1101             if (typarams_field == null) {
1102                 complete();
1103                 if (typarams_field == null)
1104                     typarams_field = List.nil();
1105             }
1106             return typarams_field;
1107         }
1108 
1109         public boolean hasErasedSupertypes() {
1110             return isRaw();
1111         }
1112 
1113         @DefinedBy(Api.LANGUAGE_MODEL)
1114         public Type getEnclosingType() {



1115             return outer_field;
1116         }
1117 
1118         public void setEnclosingType(Type outer) {
1119             outer_field = outer;
1120         }
1121 
1122         public List<Type> allparams() {
1123             if (allparams_field == null) {
1124                 allparams_field = getTypeArguments().prependList(getEnclosingType().allparams());
1125             }
1126             return allparams_field;
1127         }
1128 
1129         public boolean isErroneous() {
1130             return
1131                 getEnclosingType().isErroneous() ||
1132                 isErroneous(getTypeArguments()) ||
1133                 this != tsym.type && tsym.type.isErroneous();
1134         }
1135 
1136         public boolean isParameterized() {
1137             return allparams().tail != null;
1138             // optimization, was: allparams().nonEmpty();
1139         }
1140 
1141         @Override
1142         public boolean isReference() {
1143             return true;
1144         }
1145 










































































1146         @Override
1147         public boolean isNullOrReference() {
1148             return true;
1149         }
1150 
1151         /** A cache for the rank. */
1152         int rank_field = -1;
1153 
1154         /** A class type is raw if it misses some
1155          *  of its type parameter sections.
1156          *  After validation, this is equivalent to:
1157          *  {@code allparams.isEmpty() && tsym.type.allparams.nonEmpty(); }
1158          */
1159         public boolean isRaw() {
1160             return
1161                 this != tsym.type && // necessary, but not sufficient condition
1162                 tsym.type.allparams().nonEmpty() &&
1163                 allparams().isEmpty();
1164         }
1165 

1174 
1175         public void complete() {
1176             tsym.complete();
1177         }
1178 
1179         @DefinedBy(Api.LANGUAGE_MODEL)
1180         public TypeKind getKind() {
1181             tsym.apiComplete();
1182             return tsym.kind == TYP ? TypeKind.DECLARED : TypeKind.ERROR;
1183         }
1184 
1185         @DefinedBy(Api.LANGUAGE_MODEL)
1186         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1187             return v.visitDeclared(this, p);
1188         }
1189     }
1190 
1191     public static class ErasedClassType extends ClassType {
1192         public ErasedClassType(Type outer, TypeSymbol tsym,
1193                                TypeMetadata metadata) {
1194             super(outer, List.nil(), tsym, metadata);
1195         }
1196 
1197         @Override
1198         public boolean hasErasedSupertypes() {
1199             return true;
1200         }
1201     }
1202 
1203     // a clone of a ClassType that knows about the alternatives of a union type.
1204     public static class UnionClassType extends ClassType implements UnionType {
1205         final List<? extends Type> alternatives_field;
1206 
1207         public UnionClassType(ClassType ct, List<? extends Type> alternatives) {
1208             // Presently no way to refer to this type directly, so we
1209             // cannot put annotations directly on it.
1210             super(ct.outer_field, ct.typarams_field, ct.tsym);
1211             allparams_field = ct.allparams_field;
1212             supertype_field = ct.supertype_field;
1213             interfaces_field = ct.interfaces_field;
1214             all_interfaces_field = ct.interfaces_field;

2326         @Override
2327         public boolean isNullOrReference() {
2328             return true;
2329         }
2330 
2331     }
2332 
2333     public static class ErrorType extends ClassType
2334             implements javax.lang.model.type.ErrorType {
2335 
2336         private Type originalType = null;
2337 
2338         public ErrorType(ClassSymbol c, Type originalType) {
2339             this(originalType, c);
2340             c.type = this;
2341             c.kind = ERR;
2342             c.members_field = new Scope.ErrorScope(c);
2343         }
2344 
2345         public ErrorType(Type originalType, TypeSymbol tsym) {
2346             super(noType, List.nil(), null);
2347             this.tsym = tsym;
2348             this.originalType = (originalType == null ? noType : originalType);
2349         }
2350 
2351         private ErrorType(Type originalType, TypeSymbol tsym,
2352                           TypeMetadata metadata) {
2353             super(noType, List.nil(), null, metadata);
2354             this.tsym = tsym;
2355             this.originalType = (originalType == null ? noType : originalType);
2356         }
2357 
2358         @Override
2359         public ErrorType cloneWithMetadata(TypeMetadata md) {
2360             return new ErrorType(originalType, tsym, md) {
2361                 @Override
2362                 public Type baseType() { return ErrorType.this.baseType(); }
2363             };
2364         }
2365 
2366         @Override
2367         public TypeTag getTag() {
2368             return ERROR;
2369         }
2370 
2371         @Override
2372         public boolean isPartial() {
2373             return true;
2374         }
2375 
2376         @Override
2377         public boolean isReference() {
2378             return true;
2379         }
2380 

  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  */
  25 
  26 package com.sun.tools.javac.code;
  27 
  28 import java.lang.annotation.Annotation;
  29 import java.util.ArrayDeque;
  30 import java.util.Collections;
  31 import java.util.EnumMap;
  32 import java.util.Map;
  33 import java.util.function.Predicate;
  34 
  35 import javax.lang.model.type.*;
  36 
  37 import com.sun.tools.javac.code.Symbol.*;
  38 import com.sun.tools.javac.code.Type.ClassType.Flavor;
  39 import com.sun.tools.javac.code.TypeMetadata.Entry;
  40 import com.sun.tools.javac.code.Types.TypeMapping;
  41 import com.sun.tools.javac.code.Types.UniqueType;
  42 import com.sun.tools.javac.comp.Infer.IncorporationAction;
  43 import com.sun.tools.javac.jvm.ClassFile;
  44 import com.sun.tools.javac.jvm.PoolConstant;
  45 import com.sun.tools.javac.util.*;
  46 import com.sun.tools.javac.util.DefinedBy.Api;
  47 
  48 import static com.sun.tools.javac.code.BoundKind.*;
  49 import static com.sun.tools.javac.code.Flags.*;
  50 import static com.sun.tools.javac.code.Kinds.Kind.*;
  51 import static com.sun.tools.javac.code.TypeTag.*;
  52 
  53 /** This class represents Java types. The class itself defines the behavior of
  54  *  the following types:
  55  *  <pre>
  56  *  base types (tags: BYTE, CHAR, SHORT, INT, LONG, FLOAT, DOUBLE, BOOLEAN),
  57  *  type `void' (tag: VOID),
  58  *  the bottom type (tag: BOT),

 220         return lb.toList();
 221     }
 222 
 223     /**For ErrorType, returns the original type, otherwise returns the type itself.
 224      */
 225     public Type getOriginalType() {
 226         return this;
 227     }
 228 
 229     public <R,S> R accept(Type.Visitor<R,S> v, S s) { return v.visitType(this, s); }
 230 
 231     /** Define a type given its tag, type symbol, and type annotations
 232      */
 233 
 234     public Type(TypeSymbol tsym, TypeMetadata metadata) {
 235         Assert.checkNonNull(metadata);
 236         this.tsym = tsym;
 237         this.metadata = metadata;
 238     }
 239 
 240     public boolean isPrimitiveClass() {
 241         return false;
 242     }
 243 
 244     public boolean isValueClass() {
 245         return false;
 246     }
 247 
 248     public boolean isValueInterface() {
 249         return false;
 250     }
 251 
 252     public boolean isIdentityClass() {
 253         return false;
 254     }
 255 
 256     public boolean isIdentityInterface() {
 257         return false;
 258     }
 259 
 260     // Does this type need to be preloaded in the context of the referring class ??
 261     public boolean requiresPreload(Symbol referringClass) {
 262         if (this.tsym == referringClass)
 263             return false; // pointless
 264         if (this.isReferenceProjection())
 265             return true;
 266         return this.isValueClass() && !this.isPrimitiveClass();
 267     }
 268 
 269     /**
 270      * Return the `flavor' associated with a ClassType.
 271      * @see ClassType.Flavor
 272      */
 273     public Flavor getFlavor() {
 274         throw new AssertionError("Unexpected call to getFlavor() on a Type that is not a ClassType: " + this);
 275     }
 276 
 277     /**
 278      * @return true IFF the receiver is a reference projection of a primitive class type and false
 279      * for primitives or plain references
 280      */
 281     public boolean isReferenceProjection() {
 282         return false;
 283     }
 284 
 285     /**
 286      * @return the value projection type IFF the receiver is a reference projection of a primitive class type
 287      * and null otherwise
 288      */
 289     public Type valueProjection() {
 290         return null;
 291     }
 292 
 293     /**
 294      * @return the reference projection type IFF the receiver is a primitive class type
 295      * and null otherwise
 296      */
 297     public Type referenceProjection() {
 298         return null;
 299     }
 300 
 301     /**
 302      * @return the reference projection type IFF the receiver is a primitive class type or self otherwise.
 303      */
 304     public Type referenceProjectionOrSelf() {
 305         Type projection = referenceProjection();
 306         return projection != null ? projection : this;
 307     }
 308 
 309     /**
 310      * A subclass of {@link Types.TypeMapping} which applies a mapping recursively to the subterms
 311      * of a given type expression. This mapping returns the original type is no changes occurred
 312      * when recursively mapping the original type's subterms.
 313      */
 314     public abstract static class StructuralTypeMapping<S> extends Types.TypeMapping<S> {
 315 
 316         @Override
 317         public Type visitClassType(ClassType t, S s) {
 318             Type outer = t.getEnclosingType();
 319             Type outer1 = visit(outer, s);
 320             List<Type> typarams = t.getTypeArguments();
 321             List<Type> typarams1 = visit(typarams, s);
 322             if (outer1 == outer && typarams1 == typarams) return t;
 323             else return new ClassType(outer1, typarams1, t.tsym, t.metadata, t.getFlavor()) {
 324                 @Override
 325                 protected boolean needsStripping() {
 326                     return true;
 327                 }
 328             };
 329         }
 330 
 331         @Override
 332         public Type visitWildcardType(WildcardType wt, S s) {
 333             Type t = wt.type;
 334             if (t != null)
 335                 t = visit(t, s);
 336             if (t == wt.type)
 337                 return wt;
 338             else
 339                 return new WildcardType(t, wt.kind, wt.tsym, wt.bound, wt.metadata) {
 340                     @Override
 341                     protected boolean needsStripping() {
 342                         return true;
 343                     }

 999 
1000         @DefinedBy(Api.LANGUAGE_MODEL)
1001         public Type getSuperBound() {
1002             if (kind == SUPER)
1003                 return type;
1004             else
1005                 return null;
1006         }
1007 
1008         @DefinedBy(Api.LANGUAGE_MODEL)
1009         public TypeKind getKind() {
1010             return TypeKind.WILDCARD;
1011         }
1012 
1013         @DefinedBy(Api.LANGUAGE_MODEL)
1014         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1015             return v.visitWildcard(this, p);
1016         }
1017     }
1018 
1019     public static class ConstantPoolQType implements PoolConstant {
1020 
1021         public final Type type;
1022         final Types types;
1023 
1024         public ConstantPoolQType(Type type, Types types) {
1025             this.type = type;
1026             this.types = types;
1027         }
1028 
1029         @Override
1030         public Object poolKey(Types types) {
1031             return this;
1032         }
1033 
1034         @Override
1035         public int poolTag() {
1036             return ClassFile.CONSTANT_Class;
1037         }
1038 
1039         public int hashCode() {
1040             return types.hashCode(type);
1041         }
1042 
1043         public boolean equals(Object obj) {
1044             return (obj instanceof ConstantPoolQType) &&
1045                     types.isSameType(type, ((ConstantPoolQType)obj).type);
1046         }
1047 
1048         public String toString() {
1049             return type.toString();
1050         }
1051     }
1052 
1053     public static class ClassType extends Type implements DeclaredType, LoadableConstant,
1054                                                           javax.lang.model.type.ErrorType {
1055 
1056         /**
1057          * The 'flavor' of a ClassType indicates its reference/primitive projectionness
1058          * viewed against the default nature of the associated class.
1059          */
1060         public enum Flavor {
1061 
1062             /**
1063              * Classic reference type. Also reference projection type of a reference-favoring aka
1064              * reference-default primitive class type
1065              */
1066             L_TypeOf_L,
1067 
1068             /**
1069              * Reference projection type of a primitive-favoring aka primitive-default
1070              * plain vanilla primitive class type,
1071              */
1072             L_TypeOf_Q,
1073 
1074             /**
1075              * Value projection type of a primitive-favoring aka primitive-default
1076              * plain vanilla primitive class type,
1077              */
1078             Q_TypeOf_Q,
1079 
1080             /**
1081              * Value projection type of a reference-favoring aka
1082              * reference-default primitive class type
1083              */
1084             Q_TypeOf_L,
1085 
1086             /**
1087              * Reference projection type of a class type of an as yet unknown default provenance, 'X' will be
1088              * discovered to be 'L' or 'Q' in "due course" and mutated suitably.
1089              */
1090             L_TypeOf_X,
1091 
1092             /**
1093              * Value projection type of a class type of an as yet unknown default provenance, 'X' will be
1094              * discovered to be 'L' or 'Q' in "due course" and mutated suitably.
1095              */
1096             Q_TypeOf_X,
1097 
1098             /**
1099              *  As yet unknown projection type of an as yet unknown default provenance class.
1100              */
1101             X_Typeof_X,
1102 
1103             /**
1104              *  An error type - we don't care to discriminate them any further.
1105              */
1106              E_Typeof_X;
1107 
1108             // We don't seem to need X_Typeof_L or X_Typeof_Q so far.
1109 
1110             // Transform a larval form into a more evolved form
1111             public Flavor metamorphose(boolean isPrimtiveClass) {
1112 
1113                 switch (this) {
1114 
1115                     case E_Typeof_X:  // stunted form
1116                     case L_TypeOf_L:
1117                     case L_TypeOf_Q:
1118                     case Q_TypeOf_L:
1119                     case Q_TypeOf_Q:
1120                             // These are fully evolved sealed forms or stunted - no futher transformation
1121                             return this;
1122                     case L_TypeOf_X:
1123                             return isPrimtiveClass ? L_TypeOf_Q : L_TypeOf_L;
1124                     case Q_TypeOf_X:
1125                             return isPrimtiveClass ? Q_TypeOf_Q : Q_TypeOf_L;
1126                     case X_Typeof_X:
1127                             return isPrimtiveClass ? Q_TypeOf_Q : L_TypeOf_L;
1128                     default:
1129                             throw new AssertionError("Unexpected class type flavor");
1130                 }
1131             }
1132         }
1133 
1134         /** The enclosing type of this type. If this is the type of an inner
1135          *  class, outer_field refers to the type of its enclosing
1136          *  instance class, in all other cases it refers to noType.
1137          */
1138         private Type outer_field;
1139 
1140         /** The type parameters of this type (to be set once class is loaded).
1141          */
1142         public List<Type> typarams_field;
1143 
1144         /** A cache variable for the type parameters of this type,
1145          *  appended to all parameters of its enclosing class.
1146          *  @see #allparams
1147          */
1148         public List<Type> allparams_field;
1149 
1150         /** The supertype of this class (to be set once class is loaded).
1151          */
1152         public Type supertype_field;
1153 
1154         /** The interfaces of this class (to be set once class is loaded).
1155          */
1156         public List<Type> interfaces_field;
1157 
1158         /** All the interfaces of this class, including missing ones.
1159          */
1160         public List<Type> all_interfaces_field;
1161 
1162         /** The 'other' projection: If 'this' is type of a primitive class, then 'projection' is the
1163          *  reference projection type and vice versa. Lazily initialized, not to be accessed directly.
1164         */
1165         public ClassType projection;
1166 
1167         /** Is this L of default {L, Q, X} or Q of default {L, Q, X} ?
1168          */
1169         public Flavor flavor;
1170 
1171         /*
1172          * Use of this constructor is kinda sorta deprecated, use the other constructor
1173          * that forces the call site to consider and include the class type flavor.
1174          */
1175         public ClassType(Type outer, List<Type> typarams, TypeSymbol tsym) {
1176             this(outer, typarams, tsym, TypeMetadata.EMPTY, Flavor.L_TypeOf_L);
1177         }
1178 
1179         public ClassType(Type outer, List<Type> typarams, TypeSymbol tsym,
1180                          TypeMetadata metadata, Flavor flavor) {
1181             super(tsym, metadata);
1182             this.outer_field = outer;
1183             this.typarams_field = typarams;
1184             this.allparams_field = null;
1185             this.supertype_field = null;
1186             this.interfaces_field = null;
1187             this.flavor = flavor;
1188         }
1189 
1190         public int poolTag() {
1191             return ClassFile.CONSTANT_Class;
1192         }
1193 
1194         @Override
1195         public ClassType cloneWithMetadata(TypeMetadata md) {
1196             return new ClassType(outer_field, typarams_field, tsym, md, flavor) {
1197                 @Override
1198                 public Type baseType() { return ClassType.this.baseType(); }
1199             };
1200         }
1201 
1202         @Override
1203         public TypeTag getTag() {
1204             return CLASS;
1205         }
1206 
1207         @Override
1208         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1209             return v.visitClassType(this, s);
1210         }
1211 
1212         public Type constType(Object constValue) {
1213             final Object value = constValue;
1214             return new ClassType(getEnclosingType(), typarams_field, tsym, metadata, flavor) {
1215                     @Override
1216                     public Object constValue() {
1217                         return value;
1218                     }
1219                     @Override
1220                     public Type baseType() {
1221                         return tsym.type;
1222                     }
1223                 };
1224         }
1225 
1226         /** The Java source which this type represents.
1227          */
1228         @DefinedBy(Api.LANGUAGE_MODEL)
1229         public String toString() {
1230             StringBuilder buf = new StringBuilder();
1231             if (getEnclosingType().hasTag(CLASS) && tsym.owner.kind == TYP) {
1232                 buf.append(getEnclosingType().toString());
1233                 buf.append(".");
1234                 appendAnnotationsString(buf);

1237                 if (isAnnotated()) {
1238                     if (!tsym.packge().isUnnamed()) {
1239                         buf.append(tsym.packge());
1240                         buf.append(".");
1241                     }
1242                     ListBuffer<Name> names = new ListBuffer<>();
1243                     for (Symbol sym = tsym.owner; sym != null && sym.kind == TYP; sym = sym.owner) {
1244                         names.prepend(sym.name);
1245                     }
1246                     for (Name name : names) {
1247                         buf.append(name);
1248                         buf.append(".");
1249                     }
1250                     appendAnnotationsString(buf);
1251                     buf.append(tsym.name);
1252                 } else {
1253                     buf.append(className(tsym, true));
1254                 }
1255             }
1256 
1257             boolean isReferenceProjection;
1258             try {
1259                 isReferenceProjection = isReferenceProjection();
1260             } catch (CompletionFailure cf) {
1261                 isReferenceProjection = false; // handle missing types gracefully.
1262             }
1263             if (isReferenceProjection) {
1264                 buf.append('.');
1265                 buf.append(tsym.name.table.names.ref);
1266             }
1267 
1268             if (getTypeArguments().nonEmpty()) {
1269                 buf.append('<');
1270                 buf.append(getTypeArguments().toString());
1271                 buf.append(">");
1272             }
1273             return buf.toString();
1274         }
1275 //where
1276             private String className(Symbol sym, boolean longform) {
1277                 if (sym.name.isEmpty() && (sym.flags() & COMPOUND) != 0) {
1278                     StringBuilder s = new StringBuilder(supertype_field.toString());
1279                     for (List<Type> is=interfaces_field; is.nonEmpty(); is = is.tail) {
1280                         s.append("&");
1281                         s.append(is.head.toString());
1282                     }
1283                     return s.toString();
1284                 } else if (sym.name.isEmpty()) {
1285                     String s;
1286                     ClassType norm = (ClassType) tsym.type;
1287                     if (norm == null) {
1288                         s = Log.getLocalizedString("anonymous.class", (Object)null);
1289                     } else if (norm.interfaces_field != null && norm.interfaces_field.nonEmpty()) {
1290                         s = Log.getLocalizedString("anonymous.class",
1291                                                    norm.interfaces_field.head);
1292                     } else {
1293                         s = Log.getLocalizedString("anonymous.class",
1294                                                    norm.supertype_field);
1295                     }
1296                     if (moreInfo)
1297                         s += String.valueOf(sym.hashCode());
1298                     return s;
1299                 }
1300                 String s;
1301                 if (longform) {
1302                     s =  sym.getQualifiedName().toString();
1303                 } else {
1304                     s = sym.name.toString();
1305                 }
1306                 return s;
1307             }
1308 
1309         public Flavor getFlavor() {
1310             return flavor;
1311         }
1312 
1313         @DefinedBy(Api.LANGUAGE_MODEL)
1314         public List<Type> getTypeArguments() {
1315             if (typarams_field == null) {
1316                 complete();
1317                 if (typarams_field == null)
1318                     typarams_field = List.nil();
1319             }
1320             return typarams_field;
1321         }
1322 
1323         public boolean hasErasedSupertypes() {
1324             return isRaw();
1325         }
1326 
1327         @DefinedBy(Api.LANGUAGE_MODEL)
1328         public Type getEnclosingType() {
1329             if (outer_field != null && outer_field.isReferenceProjection()) {
1330                 outer_field = outer_field.valueProjection();
1331             }
1332             return outer_field;
1333         }
1334 
1335         public void setEnclosingType(Type outer) {
1336             outer_field = outer;
1337         }
1338 
1339         public List<Type> allparams() {
1340             if (allparams_field == null) {
1341                 allparams_field = getTypeArguments().prependList(getEnclosingType().allparams());
1342             }
1343             return allparams_field;
1344         }
1345 
1346         public boolean isErroneous() {
1347             return
1348                 getEnclosingType().isErroneous() ||
1349                 isErroneous(getTypeArguments()) ||
1350                 this != tsym.type && tsym.type.isErroneous();
1351         }
1352 
1353         public boolean isParameterized() {
1354             return allparams().tail != null;
1355             // optimization, was: allparams().nonEmpty();
1356         }
1357 
1358         @Override
1359         public boolean isReference() {
1360             return true;
1361         }
1362 
1363         @Override
1364         public boolean isPrimitiveClass() {
1365             return !isReferenceProjection() && tsym != null && tsym.isPrimitiveClass();
1366         }
1367 
1368         @Override
1369         public boolean isValueClass() {
1370             return !isReferenceProjection() && tsym != null && tsym.isValueClass();
1371         }
1372 
1373         @Override
1374         public boolean isValueInterface() {
1375             return tsym != null && tsym.isValueInterface();
1376         }
1377 
1378         @Override
1379         public boolean isIdentityClass() {
1380             return !isReferenceProjection() && tsym != null && tsym.isIdentityClass();
1381         }
1382 
1383         @Override
1384         public boolean isIdentityInterface() {
1385             return isInterface() && tsym.isIdentityInterface();
1386         }
1387 
1388         @Override
1389         public boolean isReferenceProjection() {
1390             // gaurd against over-eager and/or inopportune completion
1391             if (tsym != null) {
1392                 if (flavor == Flavor.L_TypeOf_X || tsym.isCompleted()) {
1393                     flavor = flavor.metamorphose(tsym.isPrimitiveClass());
1394                 }
1395             }
1396             return flavor == Flavor.L_TypeOf_Q;
1397         }
1398 
1399         @Override
1400         public Type valueProjection() {
1401             if (!isReferenceProjection())
1402                 return null;
1403 
1404             if (projection !=  null)
1405                 return projection;
1406 
1407             projection = new ClassType(outer_field, typarams_field, tsym, getMetadata(), Flavor.Q_TypeOf_Q);
1408             projection.allparams_field = allparams_field;
1409             projection.supertype_field = supertype_field;
1410 
1411             projection.interfaces_field = interfaces_field;
1412             projection.all_interfaces_field = all_interfaces_field;
1413             projection.projection = this;
1414             return projection;
1415         }
1416 
1417         // return the reference projection type preserving parameterizations
1418         @Override
1419         public ClassType referenceProjection() {
1420 
1421             if (!isPrimitiveClass())
1422                 return null;
1423 
1424             if (projection != null)
1425                 return projection;
1426 
1427             projection = new ClassType(outer_field, typarams_field, tsym, getMetadata(), Flavor.L_TypeOf_Q);
1428             projection.allparams_field = allparams_field;
1429             projection.supertype_field = supertype_field;
1430 
1431             projection.interfaces_field = interfaces_field;
1432             projection.all_interfaces_field = all_interfaces_field;
1433             projection.projection = this;
1434             return projection;
1435         }
1436 
1437         @Override
1438         public boolean isNullOrReference() {
1439             return true;
1440         }
1441 
1442         /** A cache for the rank. */
1443         int rank_field = -1;
1444 
1445         /** A class type is raw if it misses some
1446          *  of its type parameter sections.
1447          *  After validation, this is equivalent to:
1448          *  {@code allparams.isEmpty() && tsym.type.allparams.nonEmpty(); }
1449          */
1450         public boolean isRaw() {
1451             return
1452                 this != tsym.type && // necessary, but not sufficient condition
1453                 tsym.type.allparams().nonEmpty() &&
1454                 allparams().isEmpty();
1455         }
1456 

1465 
1466         public void complete() {
1467             tsym.complete();
1468         }
1469 
1470         @DefinedBy(Api.LANGUAGE_MODEL)
1471         public TypeKind getKind() {
1472             tsym.apiComplete();
1473             return tsym.kind == TYP ? TypeKind.DECLARED : TypeKind.ERROR;
1474         }
1475 
1476         @DefinedBy(Api.LANGUAGE_MODEL)
1477         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1478             return v.visitDeclared(this, p);
1479         }
1480     }
1481 
1482     public static class ErasedClassType extends ClassType {
1483         public ErasedClassType(Type outer, TypeSymbol tsym,
1484                                TypeMetadata metadata) {
1485             super(outer, List.nil(), tsym, metadata, tsym.type.getFlavor());
1486         }
1487 
1488         @Override
1489         public boolean hasErasedSupertypes() {
1490             return true;
1491         }
1492     }
1493 
1494     // a clone of a ClassType that knows about the alternatives of a union type.
1495     public static class UnionClassType extends ClassType implements UnionType {
1496         final List<? extends Type> alternatives_field;
1497 
1498         public UnionClassType(ClassType ct, List<? extends Type> alternatives) {
1499             // Presently no way to refer to this type directly, so we
1500             // cannot put annotations directly on it.
1501             super(ct.outer_field, ct.typarams_field, ct.tsym);
1502             allparams_field = ct.allparams_field;
1503             supertype_field = ct.supertype_field;
1504             interfaces_field = ct.interfaces_field;
1505             all_interfaces_field = ct.interfaces_field;

2617         @Override
2618         public boolean isNullOrReference() {
2619             return true;
2620         }
2621 
2622     }
2623 
2624     public static class ErrorType extends ClassType
2625             implements javax.lang.model.type.ErrorType {
2626 
2627         private Type originalType = null;
2628 
2629         public ErrorType(ClassSymbol c, Type originalType) {
2630             this(originalType, c);
2631             c.type = this;
2632             c.kind = ERR;
2633             c.members_field = new Scope.ErrorScope(c);
2634         }
2635 
2636         public ErrorType(Type originalType, TypeSymbol tsym) {
2637             super(noType, List.nil(), tsym, TypeMetadata.EMPTY, Flavor.E_Typeof_X);

2638             this.originalType = (originalType == null ? noType : originalType);
2639         }
2640 
2641         private ErrorType(Type originalType, TypeSymbol tsym,
2642                           TypeMetadata metadata, Flavor flavor) {
2643             super(noType, List.nil(), null, metadata, flavor);
2644             this.tsym = tsym;
2645             this.originalType = (originalType == null ? noType : originalType);
2646         }
2647 
2648         @Override
2649         public ErrorType cloneWithMetadata(TypeMetadata md) {
2650             return new ErrorType(originalType, tsym, md, getFlavor()) {
2651                 @Override
2652                 public Type baseType() { return ErrorType.this.baseType(); }
2653             };
2654         }
2655 
2656         @Override
2657         public TypeTag getTag() {
2658             return ERROR;
2659         }
2660 
2661         @Override
2662         public boolean isPartial() {
2663             return true;
2664         }
2665 
2666         @Override
2667         public boolean isReference() {
2668             return true;
2669         }
2670 
< prev index next >