View Javadoc
1   /*
2    * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  
26  package com.sun.tools.javac.code;
27  
28  import java.lang.annotation.Annotation;
29  import java.util.Collections;
30  import java.util.EnumMap;
31  import java.util.EnumSet;
32  import java.util.Map;
33  import java.util.Set;
34  
35  import javax.lang.model.type.*;
36  
37  import com.sun.tools.javac.code.Symbol.*;
38  import com.sun.tools.javac.util.*;
39  import static com.sun.tools.javac.code.BoundKind.*;
40  import static com.sun.tools.javac.code.Flags.*;
41  import static com.sun.tools.javac.code.Kinds.*;
42  import static com.sun.tools.javac.code.TypeTag.*;
43  
44  /** This class represents Java types. The class itself defines the behavior of
45   *  the following types:
46   *  <pre>
47   *  base types (tags: BYTE, CHAR, SHORT, INT, LONG, FLOAT, DOUBLE, BOOLEAN),
48   *  type `void' (tag: VOID),
49   *  the bottom type (tag: BOT),
50   *  the missing type (tag: NONE).
51   *  </pre>
52   *  <p>The behavior of the following types is defined in subclasses, which are
53   *  all static inner classes of this class:
54   *  <pre>
55   *  class types (tag: CLASS, class: ClassType),
56   *  array types (tag: ARRAY, class: ArrayType),
57   *  method types (tag: METHOD, class: MethodType),
58   *  package types (tag: PACKAGE, class: PackageType),
59   *  type variables (tag: TYPEVAR, class: TypeVar),
60   *  type arguments (tag: WILDCARD, class: WildcardType),
61   *  generic method types (tag: FORALL, class: ForAll),
62   *  the error type (tag: ERROR, class: ErrorType).
63   *  </pre>
64   *
65   *  <p><b>This is NOT part of any supported API.
66   *  If you write code that depends on this, you do so at your own risk.
67   *  This code and its internal interfaces are subject to change or
68   *  deletion without notice.</b>
69   *
70   *  @see TypeTag
71   */
72  public abstract class Type extends AnnoConstruct implements TypeMirror {
73  
74      /** Constant type: no type at all. */
75      public static final JCNoType noType = new JCNoType();
76  
77      /** Constant type: special type to be used during recovery of deferred expressions. */
78      public static final JCNoType recoveryType = new JCNoType();
79  
80      /** Constant type: special type to be used for marking stuck trees. */
81      public static final JCNoType stuckType = new JCNoType();
82  
83      /** If this switch is turned on, the names of type variables
84       *  and anonymous classes are printed with hashcodes appended.
85       */
86      public static boolean moreInfo = false;
87  
88      /** The defining class / interface / package / type variable.
89       */
90      public TypeSymbol tsym;
91  
92      /**
93       * Checks if the current type tag is equal to the given tag.
94       * @return true if tag is equal to the current type tag.
95       */
96      public boolean hasTag(TypeTag tag) {
97          return tag == getTag();
98      }
99  
100     /**
101      * Returns the current type tag.
102      * @return the value of the current type tag.
103      */
104     public abstract TypeTag getTag();
105 
106     public boolean isNumeric() {
107         return false;
108     }
109 
110     public boolean isPrimitive() {
111         return false;
112     }
113 
114     public boolean isPrimitiveOrVoid() {
115         return false;
116     }
117 
118     public boolean isReference() {
119         return false;
120     }
121 
122     public boolean isNullOrReference() {
123         return false;
124     }
125 
126     public boolean isPartial() {
127         return false;
128     }
129 
130     /**
131      * The constant value of this type, null if this type does not
132      * have a constant value attribute. Only primitive types and
133      * strings (ClassType) can have a constant value attribute.
134      * @return the constant value attribute of this type
135      */
136     public Object constValue() {
137         return null;
138     }
139 
140     /** Is this a constant type whose value is false?
141      */
142     public boolean isFalse() {
143         return false;
144     }
145 
146     /** Is this a constant type whose value is true?
147      */
148     public boolean isTrue() {
149         return false;
150     }
151 
152     /**
153      * Get the representation of this type used for modelling purposes.
154      * By default, this is itself. For ErrorType, a different value
155      * may be provided.
156      */
157     public Type getModelType() {
158         return this;
159     }
160 
161     public static List<Type> getModelTypes(List<Type> ts) {
162         ListBuffer<Type> lb = new ListBuffer<>();
163         for (Type t: ts)
164             lb.append(t.getModelType());
165         return lb.toList();
166     }
167 
168     /**For ErrorType, returns the original type, otherwise returns the type itself.
169      */
170     public Type getOriginalType() {
171         return this;
172     }
173 
174     public <R,S> R accept(Type.Visitor<R,S> v, S s) { return v.visitType(this, s); }
175 
176     /** Define a type given its tag and type symbol
177      */
178     public Type(TypeSymbol tsym) {
179         this.tsym = tsym;
180     }
181 
182     /** An abstract class for mappings from types to types
183      */
184     public static abstract class Mapping {
185         private String name;
186         public Mapping(String name) {
187             this.name = name;
188         }
189         public abstract Type apply(Type t);
190         public String toString() {
191             return name;
192         }
193     }
194 
195     /** map a type function over all immediate descendants of this type
196      */
197     public Type map(Mapping f) {
198         return this;
199     }
200 
201     /** map a type function over a list of types
202      */
203     public static List<Type> map(List<Type> ts, Mapping f) {
204         if (ts.nonEmpty()) {
205             List<Type> tail1 = map(ts.tail, f);
206             Type t = f.apply(ts.head);
207             if (tail1 != ts.tail || t != ts.head)
208                 return tail1.prepend(t);
209         }
210         return ts;
211     }
212 
213     /** Define a constant type, of the same kind as this type
214      *  and with given constant value
215      */
216     public Type constType(Object constValue) {
217         throw new AssertionError();
218     }
219 
220     /**
221      * If this is a constant type, return its underlying type.
222      * Otherwise, return the type itself.
223      */
224     public Type baseType() {
225         return this;
226     }
227 
228     public Type annotatedType(List<Attribute.TypeCompound> annos) {
229         return new AnnotatedType(annos, this);
230     }
231 
232     public boolean isAnnotated() {
233         return false;
234     }
235 
236     /**
237      * If this is an annotated type, return the underlying type.
238      * Otherwise, return the type itself.
239      */
240     public Type unannotatedType() {
241         return this;
242     }
243 
244     @Override
245     public List<Attribute.TypeCompound> getAnnotationMirrors() {
246         return List.nil();
247     }
248 
249 
250     @Override
251     public <A extends Annotation> A getAnnotation(Class<A> annotationType) {
252         return null;
253     }
254 
255 
256     @Override
257     public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationType) {
258         @SuppressWarnings("unchecked")
259         A[] tmp = (A[]) java.lang.reflect.Array.newInstance(annotationType, 0);
260         return tmp;
261     }
262 
263     /** Return the base types of a list of types.
264      */
265     public static List<Type> baseTypes(List<Type> ts) {
266         if (ts.nonEmpty()) {
267             Type t = ts.head.baseType();
268             List<Type> baseTypes = baseTypes(ts.tail);
269             if (t != ts.head || baseTypes != ts.tail)
270                 return baseTypes.prepend(t);
271         }
272         return ts;
273     }
274 
275     /** The Java source which this type represents.
276      */
277     public String toString() {
278         String s = (tsym == null || tsym.name == null)
279             ? "<none>"
280             : tsym.name.toString();
281         if (moreInfo && hasTag(TYPEVAR)) {
282             s = s + hashCode();
283         }
284         return s;
285     }
286 
287     /**
288      * The Java source which this type list represents.  A List is
289      * represented as a comma-spearated listing of the elements in
290      * that list.
291      */
292     public static String toString(List<Type> ts) {
293         if (ts.isEmpty()) {
294             return "";
295         } else {
296             StringBuilder buf = new StringBuilder();
297             buf.append(ts.head.toString());
298             for (List<Type> l = ts.tail; l.nonEmpty(); l = l.tail)
299                 buf.append(",").append(l.head.toString());
300             return buf.toString();
301         }
302     }
303 
304     /**
305      * The constant value of this type, converted to String
306      */
307     public String stringValue() {
308         Object cv = Assert.checkNonNull(constValue());
309         return cv.toString();
310     }
311 
312     /**
313      * This method is analogous to isSameType, but weaker, since we
314      * never complete classes. Where isSameType would complete a
315      * class, equals assumes that the two types are different.
316      */
317     @Override
318     public boolean equals(Object t) {
319         return super.equals(t);
320     }
321 
322     @Override
323     public int hashCode() {
324         return super.hashCode();
325     }
326 
327     public String argtypes(boolean varargs) {
328         List<Type> args = getParameterTypes();
329         if (!varargs) return args.toString();
330         StringBuilder buf = new StringBuilder();
331         while (args.tail.nonEmpty()) {
332             buf.append(args.head);
333             args = args.tail;
334             buf.append(',');
335         }
336         if (args.head.unannotatedType().hasTag(ARRAY)) {
337             buf.append(((ArrayType)args.head.unannotatedType()).elemtype);
338             if (args.head.getAnnotationMirrors().nonEmpty()) {
339                 buf.append(args.head.getAnnotationMirrors());
340             }
341             buf.append("...");
342         } else {
343             buf.append(args.head);
344         }
345         return buf.toString();
346     }
347 
348     /** Access methods.
349      */
350     public List<Type>        getTypeArguments()  { return List.nil(); }
351     public Type              getEnclosingType()  { return null; }
352     public List<Type>        getParameterTypes() { return List.nil(); }
353     public Type              getReturnType()     { return null; }
354     public Type              getReceiverType()   { return null; }
355     public List<Type>        getThrownTypes()    { return List.nil(); }
356     public Type              getUpperBound()     { return null; }
357     public Type              getLowerBound()     { return null; }
358 
359     /** Navigation methods, these will work for classes, type variables,
360      *  foralls, but will return null for arrays and methods.
361      */
362 
363    /** Return all parameters of this type and all its outer types in order
364     *  outer (first) to inner (last).
365     */
366     public List<Type> allparams() { return List.nil(); }
367 
368     /** Does this type contain "error" elements?
369      */
370     public boolean isErroneous() {
371         return false;
372     }
373 
374     public static boolean isErroneous(List<Type> ts) {
375         for (List<Type> l = ts; l.nonEmpty(); l = l.tail)
376             if (l.head.isErroneous()) return true;
377         return false;
378     }
379 
380     /** Is this type parameterized?
381      *  A class type is parameterized if it has some parameters.
382      *  An array type is parameterized if its element type is parameterized.
383      *  All other types are not parameterized.
384      */
385     public boolean isParameterized() {
386         return false;
387     }
388 
389     /** Is this type a raw type?
390      *  A class type is a raw type if it misses some of its parameters.
391      *  An array type is a raw type if its element type is raw.
392      *  All other types are not raw.
393      *  Type validation will ensure that the only raw types
394      *  in a program are types that miss all their type variables.
395      */
396     public boolean isRaw() {
397         return false;
398     }
399 
400     public boolean isCompound() {
401         return tsym.completer == null
402             // Compound types can't have a completer.  Calling
403             // flags() will complete the symbol causing the
404             // compiler to load classes unnecessarily.  This led
405             // to regression 6180021.
406             && (tsym.flags() & COMPOUND) != 0;
407     }
408 
409     public boolean isInterface() {
410         return (tsym.flags() & INTERFACE) != 0;
411     }
412 
413     public boolean isFinal() {
414         return (tsym.flags() & FINAL) != 0;
415     }
416 
417     /**
418      * Does this type contain occurrences of type t?
419      */
420     public boolean contains(Type t) {
421         return t == this;
422     }
423 
424     public static boolean contains(List<Type> ts, Type t) {
425         for (List<Type> l = ts;
426              l.tail != null /*inlined: l.nonEmpty()*/;
427              l = l.tail)
428             if (l.head.contains(t)) return true;
429         return false;
430     }
431 
432     /** Does this type contain an occurrence of some type in 'ts'?
433      */
434     public boolean containsAny(List<Type> ts) {
435         for (Type t : ts)
436             if (this.contains(t)) return true;
437         return false;
438     }
439 
440     public static boolean containsAny(List<Type> ts1, List<Type> ts2) {
441         for (Type t : ts1)
442             if (t.containsAny(ts2)) return true;
443         return false;
444     }
445 
446     public static List<Type> filter(List<Type> ts, Filter<Type> tf) {
447         ListBuffer<Type> buf = new ListBuffer<>();
448         for (Type t : ts) {
449             if (tf.accepts(t)) {
450                 buf.append(t);
451             }
452         }
453         return buf.toList();
454     }
455 
456     public boolean isSuperBound() { return false; }
457     public boolean isExtendsBound() { return false; }
458     public boolean isUnbound() { return false; }
459     public Type withTypeVar(Type t) { return this; }
460 
461     /** The underlying method type of this type.
462      */
463     public MethodType asMethodType() { throw new AssertionError(); }
464 
465     /** Complete loading all classes in this type.
466      */
467     public void complete() {}
468 
469     public TypeSymbol asElement() {
470         return tsym;
471     }
472 
473     @Override
474     public TypeKind getKind() {
475         return TypeKind.OTHER;
476     }
477 
478     @Override
479     public <R, P> R accept(TypeVisitor<R, P> v, P p) {
480         throw new AssertionError();
481     }
482 
483     public static class JCPrimitiveType extends Type
484             implements javax.lang.model.type.PrimitiveType {
485 
486         TypeTag tag;
487 
488         public JCPrimitiveType(TypeTag tag, TypeSymbol tsym) {
489             super(tsym);
490             this.tag = tag;
491             Assert.check(tag.isPrimitive);
492         }
493 
494         @Override
495         public boolean isNumeric() {
496             return tag != BOOLEAN;
497         }
498 
499         @Override
500         public boolean isPrimitive() {
501             return true;
502         }
503 
504         @Override
505         public TypeTag getTag() {
506             return tag;
507         }
508 
509         @Override
510         public boolean isPrimitiveOrVoid() {
511             return true;
512         }
513 
514         /** Define a constant type, of the same kind as this type
515          *  and with given constant value
516          */
517         @Override
518         public Type constType(Object constValue) {
519             final Object value = constValue;
520             return new JCPrimitiveType(tag, tsym) {
521                     @Override
522                     public Object constValue() {
523                         return value;
524                     }
525                     @Override
526                     public Type baseType() {
527                         return tsym.type;
528                     }
529                 };
530         }
531 
532         /**
533          * The constant value of this type, converted to String
534          */
535         @Override
536         public String stringValue() {
537             Object cv = Assert.checkNonNull(constValue());
538             if (tag == BOOLEAN) {
539                 return ((Integer) cv).intValue() == 0 ? "false" : "true";
540             }
541             else if (tag == CHAR) {
542                 return String.valueOf((char) ((Integer) cv).intValue());
543             }
544             else {
545                 return cv.toString();
546             }
547         }
548 
549         /** Is this a constant type whose value is false?
550          */
551         @Override
552         public boolean isFalse() {
553             return
554                 tag == BOOLEAN &&
555                 constValue() != null &&
556                 ((Integer)constValue()).intValue() == 0;
557         }
558 
559         /** Is this a constant type whose value is true?
560          */
561         @Override
562         public boolean isTrue() {
563             return
564                 tag == BOOLEAN &&
565                 constValue() != null &&
566                 ((Integer)constValue()).intValue() != 0;
567         }
568 
569         @Override
570         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
571             return v.visitPrimitive(this, p);
572         }
573 
574         @Override
575         public TypeKind getKind() {
576             switch (tag) {
577                 case BYTE:      return TypeKind.BYTE;
578                 case CHAR:      return TypeKind.CHAR;
579                 case SHORT:     return TypeKind.SHORT;
580                 case INT:       return TypeKind.INT;
581                 case LONG:      return TypeKind.LONG;
582                 case FLOAT:     return TypeKind.FLOAT;
583                 case DOUBLE:    return TypeKind.DOUBLE;
584                 case BOOLEAN:   return TypeKind.BOOLEAN;
585             }
586             throw new AssertionError();
587         }
588 
589     }
590 
591     public static class WildcardType extends Type
592             implements javax.lang.model.type.WildcardType {
593 
594         public Type type;
595         public BoundKind kind;
596         public TypeVar bound;
597 
598         @Override
599         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
600             return v.visitWildcardType(this, s);
601         }
602 
603         public WildcardType(Type type, BoundKind kind, TypeSymbol tsym) {
604             super(tsym);
605             this.type = Assert.checkNonNull(type);
606             this.kind = kind;
607         }
608         public WildcardType(WildcardType t, TypeVar bound) {
609             this(t.type, t.kind, t.tsym, bound);
610         }
611 
612         public WildcardType(Type type, BoundKind kind, TypeSymbol tsym, TypeVar bound) {
613             this(type, kind, tsym);
614             this.bound = bound;
615         }
616 
617         @Override
618         public TypeTag getTag() {
619             return WILDCARD;
620         }
621 
622         @Override
623         public boolean contains(Type t) {
624             return kind != UNBOUND && type.contains(t);
625         }
626 
627         public boolean isSuperBound() {
628             return kind == SUPER ||
629                 kind == UNBOUND;
630         }
631         public boolean isExtendsBound() {
632             return kind == EXTENDS ||
633                 kind == UNBOUND;
634         }
635         public boolean isUnbound() {
636             return kind == UNBOUND;
637         }
638 
639         @Override
640         public boolean isReference() {
641             return true;
642         }
643 
644         @Override
645         public boolean isNullOrReference() {
646             return true;
647         }
648 
649         @Override
650         public Type withTypeVar(Type t) {
651             //-System.err.println(this+".withTypeVar("+t+");");//DEBUG
652             if (bound == t)
653                 return this;
654             bound = (TypeVar)t;
655             return this;
656         }
657 
658         boolean isPrintingBound = false;
659         public String toString() {
660             StringBuilder s = new StringBuilder();
661             s.append(kind.toString());
662             if (kind != UNBOUND)
663                 s.append(type);
664             if (moreInfo && bound != null && !isPrintingBound)
665                 try {
666                     isPrintingBound = true;
667                     s.append("{:").append(bound.bound).append(":}");
668                 } finally {
669                     isPrintingBound = false;
670                 }
671             return s.toString();
672         }
673 
674         public Type map(Mapping f) {
675             //- System.err.println("   (" + this + ").map(" + f + ")");//DEBUG
676             Type t = type;
677             if (t != null)
678                 t = f.apply(t);
679             if (t == type)
680                 return this;
681             else
682                 return new WildcardType(t, kind, tsym, bound);
683         }
684 
685         public Type getExtendsBound() {
686             if (kind == EXTENDS)
687                 return type;
688             else
689                 return null;
690         }
691 
692         public Type getSuperBound() {
693             if (kind == SUPER)
694                 return type;
695             else
696                 return null;
697         }
698 
699         public TypeKind getKind() {
700             return TypeKind.WILDCARD;
701         }
702 
703         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
704             return v.visitWildcard(this, p);
705         }
706     }
707 
708     public static class ClassType extends Type implements DeclaredType {
709 
710         /** The enclosing type of this type. If this is the type of an inner
711          *  class, outer_field refers to the type of its enclosing
712          *  instance class, in all other cases it refers to noType.
713          */
714         private Type outer_field;
715 
716         /** The type parameters of this type (to be set once class is loaded).
717          */
718         public List<Type> typarams_field;
719 
720         /** A cache variable for the type parameters of this type,
721          *  appended to all parameters of its enclosing class.
722          *  @see #allparams
723          */
724         public List<Type> allparams_field;
725 
726         /** The supertype of this class (to be set once class is loaded).
727          */
728         public Type supertype_field;
729 
730         /** The interfaces of this class (to be set once class is loaded).
731          */
732         public List<Type> interfaces_field;
733 
734         /** All the interfaces of this class, including missing ones.
735          */
736         public List<Type> all_interfaces_field;
737 
738         public ClassType(Type outer, List<Type> typarams, TypeSymbol tsym) {
739             super(tsym);
740             this.outer_field = outer;
741             this.typarams_field = typarams;
742             this.allparams_field = null;
743             this.supertype_field = null;
744             this.interfaces_field = null;
745             /*
746             // this can happen during error recovery
747             assert
748                 outer.isParameterized() ?
749                 typarams.length() == tsym.type.typarams().length() :
750                 outer.isRaw() ?
751                 typarams.length() == 0 :
752                 true;
753             */
754         }
755 
756         @Override
757         public TypeTag getTag() {
758             return CLASS;
759         }
760 
761         @Override
762         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
763             return v.visitClassType(this, s);
764         }
765 
766         public Type constType(Object constValue) {
767             final Object value = constValue;
768             return new ClassType(getEnclosingType(), typarams_field, tsym) {
769                     @Override
770                     public Object constValue() {
771                         return value;
772                     }
773                     @Override
774                     public Type baseType() {
775                         return tsym.type;
776                     }
777                 };
778         }
779 
780         /** The Java source which this type represents.
781          */
782         public String toString() {
783             StringBuilder buf = new StringBuilder();
784             if (getEnclosingType().hasTag(CLASS) && tsym.owner.kind == TYP) {
785                 buf.append(getEnclosingType().toString());
786                 buf.append(".");
787                 buf.append(className(tsym, false));
788             } else {
789                 buf.append(className(tsym, true));
790             }
791             if (getTypeArguments().nonEmpty()) {
792                 buf.append('<');
793                 buf.append(getTypeArguments().toString());
794                 buf.append(">");
795             }
796             return buf.toString();
797         }
798 //where
799             private String className(Symbol sym, boolean longform) {
800                 if (sym.name.isEmpty() && (sym.flags() & COMPOUND) != 0) {
801                     StringBuilder s = new StringBuilder(supertype_field.toString());
802                     for (List<Type> is=interfaces_field; is.nonEmpty(); is = is.tail) {
803                         s.append("&");
804                         s.append(is.head.toString());
805                     }
806                     return s.toString();
807                 } else if (sym.name.isEmpty()) {
808                     String s;
809                     ClassType norm = (ClassType) tsym.type.unannotatedType();
810                     if (norm == null) {
811                         s = Log.getLocalizedString("anonymous.class", (Object)null);
812                     } else if (norm.interfaces_field != null && norm.interfaces_field.nonEmpty()) {
813                         s = Log.getLocalizedString("anonymous.class",
814                                                    norm.interfaces_field.head);
815                     } else {
816                         s = Log.getLocalizedString("anonymous.class",
817                                                    norm.supertype_field);
818                     }
819                     if (moreInfo)
820                         s += String.valueOf(sym.hashCode());
821                     return s;
822                 } else if (longform) {
823                     return sym.getQualifiedName().toString();
824                 } else {
825                     return sym.name.toString();
826                 }
827             }
828 
829         public List<Type> getTypeArguments() {
830             if (typarams_field == null) {
831                 complete();
832                 if (typarams_field == null)
833                     typarams_field = List.nil();
834             }
835             return typarams_field;
836         }
837 
838         public boolean hasErasedSupertypes() {
839             return isRaw();
840         }
841 
842         public Type getEnclosingType() {
843             return outer_field;
844         }
845 
846         public void setEnclosingType(Type outer) {
847             outer_field = outer;
848         }
849 
850         public List<Type> allparams() {
851             if (allparams_field == null) {
852                 allparams_field = getTypeArguments().prependList(getEnclosingType().allparams());
853             }
854             return allparams_field;
855         }
856 
857         public boolean isErroneous() {
858             return
859                 getEnclosingType().isErroneous() ||
860                 isErroneous(getTypeArguments()) ||
861                 this != tsym.type.unannotatedType() && tsym.type.isErroneous();
862         }
863 
864         public boolean isParameterized() {
865             return allparams().tail != null;
866             // optimization, was: allparams().nonEmpty();
867         }
868 
869         @Override
870         public boolean isReference() {
871             return true;
872         }
873 
874         @Override
875         public boolean isNullOrReference() {
876             return true;
877         }
878 
879         /** A cache for the rank. */
880         int rank_field = -1;
881 
882         /** A class type is raw if it misses some
883          *  of its type parameter sections.
884          *  After validation, this is equivalent to:
885          *  {@code allparams.isEmpty() && tsym.type.allparams.nonEmpty(); }
886          */
887         public boolean isRaw() {
888             return
889                 this != tsym.type && // necessary, but not sufficient condition
890                 tsym.type.allparams().nonEmpty() &&
891                 allparams().isEmpty();
892         }
893 
894         public Type map(Mapping f) {
895             Type outer = getEnclosingType();
896             Type outer1 = f.apply(outer);
897             List<Type> typarams = getTypeArguments();
898             List<Type> typarams1 = map(typarams, f);
899             if (outer1 == outer && typarams1 == typarams) return this;
900             else return new ClassType(outer1, typarams1, tsym);
901         }
902 
903         public boolean contains(Type elem) {
904             return
905                 elem == this
906                 || (isParameterized()
907                     && (getEnclosingType().contains(elem) || contains(getTypeArguments(), elem)))
908                 || (isCompound()
909                     && (supertype_field.contains(elem) || contains(interfaces_field, elem)));
910         }
911 
912         public void complete() {
913             if (tsym.completer != null) tsym.complete();
914         }
915 
916         public TypeKind getKind() {
917             return TypeKind.DECLARED;
918         }
919 
920         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
921             return v.visitDeclared(this, p);
922         }
923     }
924 
925     public static class ErasedClassType extends ClassType {
926         public ErasedClassType(Type outer, TypeSymbol tsym) {
927             super(outer, List.<Type>nil(), tsym);
928         }
929 
930         @Override
931         public boolean hasErasedSupertypes() {
932             return true;
933         }
934     }
935 
936     // a clone of a ClassType that knows about the alternatives of a union type.
937     public static class UnionClassType extends ClassType implements UnionType {
938         final List<? extends Type> alternatives_field;
939 
940         public UnionClassType(ClassType ct, List<? extends Type> alternatives) {
941             super(ct.outer_field, ct.typarams_field, ct.tsym);
942             allparams_field = ct.allparams_field;
943             supertype_field = ct.supertype_field;
944             interfaces_field = ct.interfaces_field;
945             all_interfaces_field = ct.interfaces_field;
946             alternatives_field = alternatives;
947         }
948 
949         public Type getLub() {
950             return tsym.type;
951         }
952 
953         public java.util.List<? extends TypeMirror> getAlternatives() {
954             return Collections.unmodifiableList(alternatives_field);
955         }
956 
957         @Override
958         public TypeKind getKind() {
959             return TypeKind.UNION;
960         }
961 
962         @Override
963         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
964             return v.visitUnion(this, p);
965         }
966     }
967 
968     // a clone of a ClassType that knows about the bounds of an intersection type.
969     public static class IntersectionClassType extends ClassType implements IntersectionType {
970 
971         public boolean allInterfaces;
972 
973         public IntersectionClassType(List<Type> bounds, ClassSymbol csym, boolean allInterfaces) {
974             super(Type.noType, List.<Type>nil(), csym);
975             this.allInterfaces = allInterfaces;
976             Assert.check((csym.flags() & COMPOUND) != 0);
977             supertype_field = bounds.head;
978             interfaces_field = bounds.tail;
979             Assert.check(supertype_field.tsym.completer != null ||
980                     !supertype_field.isInterface(), supertype_field);
981         }
982 
983         public java.util.List<? extends TypeMirror> getBounds() {
984             return Collections.unmodifiableList(getExplicitComponents());
985         }
986 
987         public List<Type> getComponents() {
988             return interfaces_field.prepend(supertype_field);
989         }
990 
991         public List<Type> getExplicitComponents() {
992             return allInterfaces ?
993                     interfaces_field :
994                     getComponents();
995         }
996 
997         @Override
998         public TypeKind getKind() {
999             return TypeKind.INTERSECTION;
1000         }
1001 
1002         @Override
1003         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1004             return v.visitIntersection(this, p);
1005         }
1006     }
1007 
1008     public static class ArrayType extends Type
1009             implements javax.lang.model.type.ArrayType {
1010 
1011         public Type elemtype;
1012 
1013         public ArrayType(Type elemtype, TypeSymbol arrayClass) {
1014             super(arrayClass);
1015             this.elemtype = elemtype;
1016         }
1017 
1018         @Override
1019         public TypeTag getTag() {
1020             return ARRAY;
1021         }
1022 
1023         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1024             return v.visitArrayType(this, s);
1025         }
1026 
1027         public String toString() {
1028             return elemtype + "[]";
1029         }
1030 
1031         public boolean equals(Object obj) {
1032             return
1033                 this == obj ||
1034                 (obj instanceof ArrayType &&
1035                  this.elemtype.equals(((ArrayType)obj).elemtype));
1036         }
1037 
1038         public int hashCode() {
1039             return (ARRAY.ordinal() << 5) + elemtype.hashCode();
1040         }
1041 
1042         public boolean isVarargs() {
1043             return false;
1044         }
1045 
1046         public List<Type> allparams() { return elemtype.allparams(); }
1047 
1048         public boolean isErroneous() {
1049             return elemtype.isErroneous();
1050         }
1051 
1052         public boolean isParameterized() {
1053             return elemtype.isParameterized();
1054         }
1055 
1056         @Override
1057         public boolean isReference() {
1058             return true;
1059         }
1060 
1061         @Override
1062         public boolean isNullOrReference() {
1063             return true;
1064         }
1065 
1066         public boolean isRaw() {
1067             return elemtype.isRaw();
1068         }
1069 
1070         public ArrayType makeVarargs() {
1071             return new ArrayType(elemtype, tsym) {
1072                 @Override
1073                 public boolean isVarargs() {
1074                     return true;
1075                 }
1076             };
1077         }
1078 
1079         public Type map(Mapping f) {
1080             Type elemtype1 = f.apply(elemtype);
1081             if (elemtype1 == elemtype) return this;
1082             else return new ArrayType(elemtype1, tsym);
1083         }
1084 
1085         public boolean contains(Type elem) {
1086             return elem == this || elemtype.contains(elem);
1087         }
1088 
1089         public void complete() {
1090             elemtype.complete();
1091         }
1092 
1093         public Type getComponentType() {
1094             return elemtype;
1095         }
1096 
1097         public TypeKind getKind() {
1098             return TypeKind.ARRAY;
1099         }
1100 
1101         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1102             return v.visitArray(this, p);
1103         }
1104     }
1105 
1106     public static class MethodType extends Type implements ExecutableType {
1107 
1108         public List<Type> argtypes;
1109         public Type restype;
1110         public List<Type> thrown;
1111 
1112         /** The type annotations on the method receiver.
1113          */
1114         public Type recvtype;
1115 
1116         public MethodType(List<Type> argtypes,
1117                           Type restype,
1118                           List<Type> thrown,
1119                           TypeSymbol methodClass) {
1120             super(methodClass);
1121             this.argtypes = argtypes;
1122             this.restype = restype;
1123             this.thrown = thrown;
1124         }
1125 
1126         @Override
1127         public TypeTag getTag() {
1128             return METHOD;
1129         }
1130 
1131         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1132             return v.visitMethodType(this, s);
1133         }
1134 
1135         /** The Java source which this type represents.
1136          *
1137          *  XXX 06/09/99 iris This isn't correct Java syntax, but it probably
1138          *  should be.
1139          */
1140         public String toString() {
1141             return "(" + argtypes + ")" + restype;
1142         }
1143 
1144         public List<Type>        getParameterTypes() { return argtypes; }
1145         public Type              getReturnType()     { return restype; }
1146         public Type              getReceiverType()   { return recvtype; }
1147         public List<Type>        getThrownTypes()    { return thrown; }
1148 
1149         public boolean isErroneous() {
1150             return
1151                 isErroneous(argtypes) ||
1152                 restype != null && restype.isErroneous();
1153         }
1154 
1155         public Type map(Mapping f) {
1156             List<Type> argtypes1 = map(argtypes, f);
1157             Type restype1 = f.apply(restype);
1158             List<Type> thrown1 = map(thrown, f);
1159             if (argtypes1 == argtypes &&
1160                 restype1 == restype &&
1161                 thrown1 == thrown) return this;
1162             else return new MethodType(argtypes1, restype1, thrown1, tsym);
1163         }
1164 
1165         public boolean contains(Type elem) {
1166             return elem == this || contains(argtypes, elem) || restype.contains(elem) || contains(thrown, elem);
1167         }
1168 
1169         public MethodType asMethodType() { return this; }
1170 
1171         public void complete() {
1172             for (List<Type> l = argtypes; l.nonEmpty(); l = l.tail)
1173                 l.head.complete();
1174             restype.complete();
1175             recvtype.complete();
1176             for (List<Type> l = thrown; l.nonEmpty(); l = l.tail)
1177                 l.head.complete();
1178         }
1179 
1180         public List<TypeVar> getTypeVariables() {
1181             return List.nil();
1182         }
1183 
1184         public TypeSymbol asElement() {
1185             return null;
1186         }
1187 
1188         public TypeKind getKind() {
1189             return TypeKind.EXECUTABLE;
1190         }
1191 
1192         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1193             return v.visitExecutable(this, p);
1194         }
1195     }
1196 
1197     public static class PackageType extends Type implements NoType {
1198 
1199         PackageType(TypeSymbol tsym) {
1200             super(tsym);
1201         }
1202 
1203         @Override
1204         public TypeTag getTag() {
1205             return PACKAGE;
1206         }
1207 
1208         @Override
1209         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1210             return v.visitPackageType(this, s);
1211         }
1212 
1213         public String toString() {
1214             return tsym.getQualifiedName().toString();
1215         }
1216 
1217         public TypeKind getKind() {
1218             return TypeKind.PACKAGE;
1219         }
1220 
1221         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1222             return v.visitNoType(this, p);
1223         }
1224     }
1225 
1226     public static class TypeVar extends Type implements TypeVariable {
1227 
1228         /** The upper bound of this type variable; set from outside.
1229          *  Must be nonempty once it is set.
1230          *  For a bound, `bound' is the bound type itself.
1231          *  Multiple bounds are expressed as a single class type which has the
1232          *  individual bounds as superclass, respectively interfaces.
1233          *  The class type then has as `tsym' a compiler generated class `c',
1234          *  which has a flag COMPOUND and whose owner is the type variable
1235          *  itself. Furthermore, the erasure_field of the class
1236          *  points to the first class or interface bound.
1237          */
1238         public Type bound = null;
1239 
1240         /** The lower bound of this type variable.
1241          *  TypeVars don't normally have a lower bound, so it is normally set
1242          *  to syms.botType.
1243          *  Subtypes, such as CapturedType, may provide a different value.
1244          */
1245         public Type lower;
1246 
1247         public TypeVar(Name name, Symbol owner, Type lower) {
1248             super(null);
1249             tsym = new TypeVariableSymbol(0, name, this, owner);
1250             this.lower = lower;
1251         }
1252 
1253         public TypeVar(TypeSymbol tsym, Type bound, Type lower) {
1254             super(tsym);
1255             this.bound = bound;
1256             this.lower = lower;
1257         }
1258 
1259         @Override
1260         public TypeTag getTag() {
1261             return TYPEVAR;
1262         }
1263 
1264         @Override
1265         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1266             return v.visitTypeVar(this, s);
1267         }
1268 
1269         @Override
1270         public Type getUpperBound() {
1271             if ((bound == null || bound.hasTag(NONE)) && this != tsym.type) {
1272                 bound = tsym.type.getUpperBound();
1273             }
1274             return bound;
1275         }
1276 
1277         int rank_field = -1;
1278 
1279         @Override
1280         public Type getLowerBound() {
1281             return lower;
1282         }
1283 
1284         public TypeKind getKind() {
1285             return TypeKind.TYPEVAR;
1286         }
1287 
1288         public boolean isCaptured() {
1289             return false;
1290         }
1291 
1292         @Override
1293         public boolean isReference() {
1294             return true;
1295         }
1296 
1297         @Override
1298         public boolean isNullOrReference() {
1299             return true;
1300         }
1301 
1302         @Override
1303         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1304             return v.visitTypeVariable(this, p);
1305         }
1306     }
1307 
1308     /** A captured type variable comes from wildcards which can have
1309      *  both upper and lower bound.  CapturedType extends TypeVar with
1310      *  a lower bound.
1311      */
1312     public static class CapturedType extends TypeVar {
1313 
1314         public WildcardType wildcard;
1315 
1316         public CapturedType(Name name,
1317                             Symbol owner,
1318                             Type upper,
1319                             Type lower,
1320                             WildcardType wildcard) {
1321             super(name, owner, lower);
1322             this.lower = Assert.checkNonNull(lower);
1323             this.bound = upper;
1324             this.wildcard = wildcard;
1325         }
1326 
1327         @Override
1328         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1329             return v.visitCapturedType(this, s);
1330         }
1331 
1332         @Override
1333         public boolean isCaptured() {
1334             return true;
1335         }
1336 
1337         @Override
1338         public String toString() {
1339             return "capture#"
1340                 + (hashCode() & 0xFFFFFFFFL) % Printer.PRIME
1341                 + " of "
1342                 + wildcard;
1343         }
1344     }
1345 
1346     public static abstract class DelegatedType extends Type {
1347         public Type qtype;
1348         public TypeTag tag;
1349         public DelegatedType(TypeTag tag, Type qtype) {
1350             super(qtype.tsym);
1351             this.tag = tag;
1352             this.qtype = qtype;
1353         }
1354         public TypeTag getTag() { return tag; }
1355         public String toString() { return qtype.toString(); }
1356         public List<Type> getTypeArguments() { return qtype.getTypeArguments(); }
1357         public Type getEnclosingType() { return qtype.getEnclosingType(); }
1358         public List<Type> getParameterTypes() { return qtype.getParameterTypes(); }
1359         public Type getReturnType() { return qtype.getReturnType(); }
1360         public Type getReceiverType() { return qtype.getReceiverType(); }
1361         public List<Type> getThrownTypes() { return qtype.getThrownTypes(); }
1362         public List<Type> allparams() { return qtype.allparams(); }
1363         public Type getUpperBound() { return qtype.getUpperBound(); }
1364         public boolean isErroneous() { return qtype.isErroneous(); }
1365     }
1366 
1367     /**
1368      * The type of a generic method type. It consists of a method type and
1369      * a list of method type-parameters that are used within the method
1370      * type.
1371      */
1372     public static class ForAll extends DelegatedType implements ExecutableType {
1373         public List<Type> tvars;
1374 
1375         public ForAll(List<Type> tvars, Type qtype) {
1376             super(FORALL, (MethodType)qtype);
1377             this.tvars = tvars;
1378         }
1379 
1380         @Override
1381         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1382             return v.visitForAll(this, s);
1383         }
1384 
1385         public String toString() {
1386             return "<" + tvars + ">" + qtype;
1387         }
1388 
1389         public List<Type> getTypeArguments()   { return tvars; }
1390 
1391         public boolean isErroneous()  {
1392             return qtype.isErroneous();
1393         }
1394 
1395         public Type map(Mapping f) {
1396             return f.apply(qtype);
1397         }
1398 
1399         public boolean contains(Type elem) {
1400             return qtype.contains(elem);
1401         }
1402 
1403         public MethodType asMethodType() {
1404             return (MethodType)qtype;
1405         }
1406 
1407         public void complete() {
1408             for (List<Type> l = tvars; l.nonEmpty(); l = l.tail) {
1409                 ((TypeVar)l.head).bound.complete();
1410             }
1411             qtype.complete();
1412         }
1413 
1414         public List<TypeVar> getTypeVariables() {
1415             return List.convert(TypeVar.class, getTypeArguments());
1416         }
1417 
1418         public TypeKind getKind() {
1419             return TypeKind.EXECUTABLE;
1420         }
1421 
1422         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1423             return v.visitExecutable(this, p);
1424         }
1425     }
1426 
1427     /** A class for inference variables, for use during method/diamond type
1428      *  inference. An inference variable has upper/lower bounds and a set
1429      *  of equality constraints. Such bounds are set during subtyping, type-containment,
1430      *  type-equality checks, when the types being tested contain inference variables.
1431      *  A change listener can be attached to an inference variable, to receive notifications
1432      *  whenever the bounds of an inference variable change.
1433      */
1434     public static class UndetVar extends DelegatedType {
1435 
1436         /** Inference variable change listener. The listener method is called
1437          *  whenever a change to the inference variable's bounds occurs
1438          */
1439         public interface UndetVarListener {
1440             /** called when some inference variable bounds (of given kinds ibs) change */
1441             void varChanged(UndetVar uv, Set<InferenceBound> ibs);
1442         }
1443 
1444         /**
1445          * Inference variable bound kinds
1446          */
1447         public enum InferenceBound {
1448             /** upper bounds */
1449             UPPER,
1450             /** lower bounds */
1451             LOWER,
1452             /** equality constraints */
1453             EQ;
1454         }
1455 
1456         /** inference variable bounds */
1457         protected Map<InferenceBound, List<Type>> bounds;
1458 
1459         /** inference variable's inferred type (set from Infer.java) */
1460         public Type inst = null;
1461 
1462         /** number of declared (upper) bounds */
1463         public int declaredCount;
1464 
1465         /** inference variable's change listener */
1466         public UndetVarListener listener = null;
1467 
1468         @Override
1469         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1470             return v.visitUndetVar(this, s);
1471         }
1472 
1473         public UndetVar(TypeVar origin, Types types) {
1474             super(UNDETVAR, origin);
1475             bounds = new EnumMap<InferenceBound, List<Type>>(InferenceBound.class);
1476             List<Type> declaredBounds = types.getBounds(origin);
1477             declaredCount = declaredBounds.length();
1478             bounds.put(InferenceBound.UPPER, declaredBounds);
1479             bounds.put(InferenceBound.LOWER, List.<Type>nil());
1480             bounds.put(InferenceBound.EQ, List.<Type>nil());
1481         }
1482 
1483         public String toString() {
1484             if (inst != null) return inst.toString();
1485             else return qtype + "?";
1486         }
1487 
1488         @Override
1489         public boolean isPartial() {
1490             return true;
1491         }
1492 
1493         @Override
1494         public Type baseType() {
1495             if (inst != null) return inst.baseType();
1496             else return this;
1497         }
1498 
1499         /** get all bounds of a given kind */
1500         public List<Type> getBounds(InferenceBound... ibs) {
1501             ListBuffer<Type> buf = new ListBuffer<>();
1502             for (InferenceBound ib : ibs) {
1503                 buf.appendList(bounds.get(ib));
1504             }
1505             return buf.toList();
1506         }
1507 
1508         /** get the list of declared (upper) bounds */
1509         public List<Type> getDeclaredBounds() {
1510             ListBuffer<Type> buf = new ListBuffer<>();
1511             int count = 0;
1512             for (Type b : getBounds(InferenceBound.UPPER)) {
1513                 if (count++ == declaredCount) break;
1514                 buf.append(b);
1515             }
1516             return buf.toList();
1517         }
1518 
1519         /** internal method used to override an undetvar bounds */
1520         public void setBounds(InferenceBound ib, List<Type> newBounds) {
1521             bounds.put(ib, newBounds);
1522         }
1523 
1524         /** add a bound of a given kind - this might trigger listener notification */
1525         public final void addBound(InferenceBound ib, Type bound, Types types) {
1526             addBound(ib, bound, types, false);
1527         }
1528 
1529         protected void addBound(InferenceBound ib, Type bound, Types types, boolean update) {
1530             Type bound2 = toTypeVarMap.apply(bound).baseType();
1531             List<Type> prevBounds = bounds.get(ib);
1532             for (Type b : prevBounds) {
1533                 //check for redundancy - use strict version of isSameType on tvars
1534                 //(as the standard version will lead to false positives w.r.t. clones ivars)
1535                 if (types.isSameType(b, bound2, true) || bound == qtype) return;
1536             }
1537             bounds.put(ib, prevBounds.prepend(bound2));
1538             notifyChange(EnumSet.of(ib));
1539         }
1540         //where
1541             Type.Mapping toTypeVarMap = new Mapping("toTypeVarMap") {
1542                 @Override
1543                 public Type apply(Type t) {
1544                     if (t.hasTag(UNDETVAR)) {
1545                         UndetVar uv = (UndetVar)t;
1546                         return uv.inst != null ? uv.inst : uv.qtype;
1547                     } else {
1548                         return t.map(this);
1549                     }
1550                 }
1551             };
1552 
1553         /** replace types in all bounds - this might trigger listener notification */
1554         public void substBounds(List<Type> from, List<Type> to, Types types) {
1555             List<Type> instVars = from.diff(to);
1556             //if set of instantiated ivars is empty, there's nothing to do!
1557             if (instVars.isEmpty()) return;
1558             final EnumSet<InferenceBound> boundsChanged = EnumSet.noneOf(InferenceBound.class);
1559             UndetVarListener prevListener = listener;
1560             try {
1561                 //setup new listener for keeping track of changed bounds
1562                 listener = new UndetVarListener() {
1563                     public void varChanged(UndetVar uv, Set<InferenceBound> ibs) {
1564                         boundsChanged.addAll(ibs);
1565                     }
1566                 };
1567                 for (Map.Entry<InferenceBound, List<Type>> _entry : bounds.entrySet()) {
1568                     InferenceBound ib = _entry.getKey();
1569                     List<Type> prevBounds = _entry.getValue();
1570                     ListBuffer<Type> newBounds = new ListBuffer<>();
1571                     ListBuffer<Type> deps = new ListBuffer<>();
1572                     //step 1 - re-add bounds that are not dependent on ivars
1573                     for (Type t : prevBounds) {
1574                         if (!t.containsAny(instVars)) {
1575                             newBounds.append(t);
1576                         } else {
1577                             deps.append(t);
1578                         }
1579                     }
1580                     //step 2 - replace bounds
1581                     bounds.put(ib, newBounds.toList());
1582                     //step 3 - for each dependency, add new replaced bound
1583                     for (Type dep : deps) {
1584                         addBound(ib, types.subst(dep, from, to), types, true);
1585                     }
1586                 }
1587             } finally {
1588                 listener = prevListener;
1589                 if (!boundsChanged.isEmpty()) {
1590                     notifyChange(boundsChanged);
1591                 }
1592             }
1593         }
1594 
1595         private void notifyChange(EnumSet<InferenceBound> ibs) {
1596             if (listener != null) {
1597                 listener.varChanged(this, ibs);
1598             }
1599         }
1600 
1601         public boolean isCaptured() {
1602             return false;
1603         }
1604     }
1605 
1606     /**
1607      * This class is used to represent synthetic captured inference variables
1608      * that can be generated during nested generic method calls. The only difference
1609      * between these inference variables and ordinary ones is that captured inference
1610      * variables cannot get new bounds through incorporation.
1611      */
1612     public static class CapturedUndetVar extends UndetVar {
1613 
1614         public CapturedUndetVar(CapturedType origin, Types types) {
1615             super(origin, types);
1616             if (!origin.lower.hasTag(BOT)) {
1617                 bounds.put(InferenceBound.LOWER, List.of(origin.lower));
1618             }
1619         }
1620 
1621         @Override
1622         public void addBound(InferenceBound ib, Type bound, Types types, boolean update) {
1623             if (update) {
1624                 //only change bounds if request comes from substBounds
1625                 super.addBound(ib, bound, types, update);
1626             }
1627         }
1628 
1629         @Override
1630         public boolean isCaptured() {
1631             return true;
1632         }
1633     }
1634 
1635     /** Represents NONE.
1636      */
1637     public static class JCNoType extends Type implements NoType {
1638         public JCNoType() {
1639             super(null);
1640         }
1641 
1642         @Override
1643         public TypeTag getTag() {
1644             return NONE;
1645         }
1646 
1647         @Override
1648         public TypeKind getKind() {
1649             return TypeKind.NONE;
1650         }
1651 
1652         @Override
1653         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1654             return v.visitNoType(this, p);
1655         }
1656 
1657         @Override
1658         public boolean isCompound() { return false; }
1659     }
1660 
1661     /** Represents VOID.
1662      */
1663     public static class JCVoidType extends Type implements NoType {
1664 
1665         public JCVoidType() {
1666             super(null);
1667         }
1668 
1669         @Override
1670         public TypeTag getTag() {
1671             return VOID;
1672         }
1673 
1674         @Override
1675         public TypeKind getKind() {
1676             return TypeKind.VOID;
1677         }
1678 
1679         @Override
1680         public boolean isCompound() { return false; }
1681 
1682         @Override
1683         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1684             return v.visitNoType(this, p);
1685         }
1686 
1687         @Override
1688         public boolean isPrimitiveOrVoid() {
1689             return true;
1690         }
1691     }
1692 
1693     static class BottomType extends Type implements NullType {
1694         public BottomType() {
1695             super(null);
1696         }
1697 
1698         @Override
1699         public TypeTag getTag() {
1700             return BOT;
1701         }
1702 
1703         @Override
1704         public TypeKind getKind() {
1705             return TypeKind.NULL;
1706         }
1707 
1708         @Override
1709         public boolean isCompound() { return false; }
1710 
1711         @Override
1712         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1713             return v.visitNull(this, p);
1714         }
1715 
1716         @Override
1717         public Type constType(Object value) {
1718             return this;
1719         }
1720 
1721         @Override
1722         public String stringValue() {
1723             return "null";
1724         }
1725 
1726         @Override
1727         public boolean isNullOrReference() {
1728             return true;
1729         }
1730 
1731     }
1732 
1733     public static class ErrorType extends ClassType
1734             implements javax.lang.model.type.ErrorType {
1735 
1736         private Type originalType = null;
1737 
1738         public ErrorType(Type originalType, TypeSymbol tsym) {
1739             super(noType, List.<Type>nil(), null);
1740             this.tsym = tsym;
1741             this.originalType = (originalType == null ? noType : originalType);
1742         }
1743 
1744         public ErrorType(ClassSymbol c, Type originalType) {
1745             this(originalType, c);
1746             c.type = this;
1747             c.kind = ERR;
1748             c.members_field = new Scope.ErrorScope(c);
1749         }
1750 
1751         @Override
1752         public TypeTag getTag() {
1753             return ERROR;
1754         }
1755 
1756         @Override
1757         public boolean isPartial() {
1758             return true;
1759         }
1760 
1761         @Override
1762         public boolean isReference() {
1763             return true;
1764         }
1765 
1766         @Override
1767         public boolean isNullOrReference() {
1768             return true;
1769         }
1770 
1771         public ErrorType(Name name, TypeSymbol container, Type originalType) {
1772             this(new ClassSymbol(PUBLIC|STATIC|ACYCLIC, name, null, container), originalType);
1773         }
1774 
1775         @Override
1776         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1777             return v.visitErrorType(this, s);
1778         }
1779 
1780         public Type constType(Object constValue) { return this; }
1781         public Type getEnclosingType()           { return this; }
1782         public Type getReturnType()              { return this; }
1783         public Type asSub(Symbol sym)            { return this; }
1784         public Type map(Mapping f)               { return this; }
1785 
1786         public boolean isGenType(Type t)         { return true; }
1787         public boolean isErroneous()             { return true; }
1788         public boolean isCompound()              { return false; }
1789         public boolean isInterface()             { return false; }
1790 
1791         public List<Type> allparams()            { return List.nil(); }
1792         public List<Type> getTypeArguments()     { return List.nil(); }
1793 
1794         public TypeKind getKind() {
1795             return TypeKind.ERROR;
1796         }
1797 
1798         public Type getOriginalType() {
1799             return originalType;
1800         }
1801 
1802         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1803             return v.visitError(this, p);
1804         }
1805     }
1806 
1807     public static class AnnotatedType extends Type
1808             implements
1809                 javax.lang.model.type.ArrayType,
1810                 javax.lang.model.type.DeclaredType,
1811                 javax.lang.model.type.PrimitiveType,
1812                 javax.lang.model.type.TypeVariable,
1813                 javax.lang.model.type.WildcardType {
1814         /** The type annotations on this type.
1815          */
1816         private List<Attribute.TypeCompound> typeAnnotations;
1817 
1818         /** The underlying type that is annotated.
1819          */
1820         private Type underlyingType;
1821 
1822         protected AnnotatedType(List<Attribute.TypeCompound> typeAnnotations,
1823                 Type underlyingType) {
1824             super(underlyingType.tsym);
1825             this.typeAnnotations = typeAnnotations;
1826             this.underlyingType = underlyingType;
1827             Assert.check(typeAnnotations != null && typeAnnotations.nonEmpty(),
1828                     "Can't create AnnotatedType without annotations: " + underlyingType);
1829             Assert.check(!underlyingType.isAnnotated(),
1830                     "Can't annotate already annotated type: " + underlyingType +
1831                     "; adding: " + typeAnnotations);
1832         }
1833 
1834         @Override
1835         public TypeTag getTag() {
1836             return underlyingType.getTag();
1837         }
1838 
1839         @Override
1840         public boolean isAnnotated() {
1841             return true;
1842         }
1843 
1844         @Override
1845         public List<Attribute.TypeCompound> getAnnotationMirrors() {
1846             return typeAnnotations;
1847         }
1848 
1849 
1850         @Override
1851         public TypeKind getKind() {
1852             return underlyingType.getKind();
1853         }
1854 
1855         @Override
1856         public Type unannotatedType() {
1857             return underlyingType;
1858         }
1859 
1860         @Override
1861         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1862             return v.visitAnnotatedType(this, s);
1863         }
1864 
1865         @Override
1866         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1867             return underlyingType.accept(v, p);
1868         }
1869 
1870         @Override
1871         public Type map(Mapping f) {
1872             underlyingType.map(f);
1873             return this;
1874         }
1875 
1876         @Override
1877         public Type constType(Object constValue) { return underlyingType.constType(constValue); }
1878         @Override
1879         public Type getEnclosingType()           { return underlyingType.getEnclosingType(); }
1880 
1881         @Override
1882         public Type getReturnType()              { return underlyingType.getReturnType(); }
1883         @Override
1884         public List<Type> getTypeArguments()     { return underlyingType.getTypeArguments(); }
1885         @Override
1886         public List<Type> getParameterTypes()    { return underlyingType.getParameterTypes(); }
1887         @Override
1888         public Type getReceiverType()            { return underlyingType.getReceiverType(); }
1889         @Override
1890         public List<Type> getThrownTypes()       { return underlyingType.getThrownTypes(); }
1891         @Override
1892         public Type getUpperBound()              { return underlyingType.getUpperBound(); }
1893         @Override
1894         public Type getLowerBound()              { return underlyingType.getLowerBound(); }
1895 
1896         @Override
1897         public boolean isErroneous()             { return underlyingType.isErroneous(); }
1898         @Override
1899         public boolean isCompound()              { return underlyingType.isCompound(); }
1900         @Override
1901         public boolean isInterface()             { return underlyingType.isInterface(); }
1902         @Override
1903         public List<Type> allparams()            { return underlyingType.allparams(); }
1904         @Override
1905         public boolean isPrimitive()             { return underlyingType.isPrimitive(); }
1906         @Override
1907         public boolean isPrimitiveOrVoid()       { return underlyingType.isPrimitiveOrVoid(); }
1908         @Override
1909         public boolean isNumeric()               { return underlyingType.isNumeric(); }
1910         @Override
1911         public boolean isReference()             { return underlyingType.isReference(); }
1912         @Override
1913         public boolean isNullOrReference()       { return underlyingType.isNullOrReference(); }
1914         @Override
1915         public boolean isPartial()               { return underlyingType.isPartial(); }
1916         @Override
1917         public boolean isParameterized()         { return underlyingType.isParameterized(); }
1918         @Override
1919         public boolean isRaw()                   { return underlyingType.isRaw(); }
1920         @Override
1921         public boolean isFinal()                 { return underlyingType.isFinal(); }
1922         @Override
1923         public boolean isSuperBound()            { return underlyingType.isSuperBound(); }
1924         @Override
1925         public boolean isExtendsBound()          { return underlyingType.isExtendsBound(); }
1926         @Override
1927         public boolean isUnbound()               { return underlyingType.isUnbound(); }
1928 
1929         @Override
1930         public String toString() {
1931             // This method is only used for internal debugging output.
1932             // See
1933             // com.sun.tools.javac.code.Printer.visitAnnotatedType(AnnotatedType, Locale)
1934             // for the user-visible logic.
1935             if (typeAnnotations != null &&
1936                     !typeAnnotations.isEmpty()) {
1937                 return "(" + typeAnnotations.toString() + " :: " + underlyingType.toString() + ")";
1938             } else {
1939                 return "({} :: " + underlyingType.toString() +")";
1940             }
1941         }
1942 
1943         @Override
1944         public boolean contains(Type t)          { return underlyingType.contains(t); }
1945 
1946         @Override
1947         public Type withTypeVar(Type t) {
1948             // Don't create a new AnnotatedType, as 'this' will
1949             // get its annotations set later.
1950             underlyingType = underlyingType.withTypeVar(t);
1951             return this;
1952         }
1953 
1954         // TODO: attach annotations?
1955         @Override
1956         public TypeSymbol asElement()            { return underlyingType.asElement(); }
1957 
1958         // TODO: attach annotations?
1959         @Override
1960         public MethodType asMethodType()         { return underlyingType.asMethodType(); }
1961 
1962         @Override
1963         public void complete()                   { underlyingType.complete(); }
1964 
1965         @Override
1966         public TypeMirror getComponentType()     { return ((ArrayType)underlyingType).getComponentType(); }
1967 
1968         // The result is an ArrayType, but only in the model sense, not the Type sense.
1969         public Type makeVarargs() {
1970             return ((ArrayType) underlyingType).makeVarargs().annotatedType(typeAnnotations);
1971         }
1972 
1973         @Override
1974         public TypeMirror getExtendsBound()      { return ((WildcardType)underlyingType).getExtendsBound(); }
1975         @Override
1976         public TypeMirror getSuperBound()        { return ((WildcardType)underlyingType).getSuperBound(); }
1977     }
1978 
1979     public static class UnknownType extends Type {
1980 
1981         public UnknownType() {
1982             super(null);
1983         }
1984 
1985         @Override
1986         public TypeTag getTag() {
1987             return UNKNOWN;
1988         }
1989 
1990         @Override
1991         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1992             return v.visitUnknown(this, p);
1993         }
1994 
1995         @Override
1996         public boolean isPartial() {
1997             return true;
1998         }
1999     }
2000 
2001     /**
2002      * A visitor for types.  A visitor is used to implement operations
2003      * (or relations) on types.  Most common operations on types are
2004      * binary relations and this interface is designed for binary
2005      * relations, that is, operations of the form
2006      * Type&nbsp;&times;&nbsp;S&nbsp;&rarr;&nbsp;R.
2007      * <!-- In plain text: Type x S -> R -->
2008      *
2009      * @param <R> the return type of the operation implemented by this
2010      * visitor; use Void if no return type is needed.
2011      * @param <S> the type of the second argument (the first being the
2012      * type itself) of the operation implemented by this visitor; use
2013      * Void if a second argument is not needed.
2014      */
2015     public interface Visitor<R,S> {
2016         R visitClassType(ClassType t, S s);
2017         R visitWildcardType(WildcardType t, S s);
2018         R visitArrayType(ArrayType t, S s);
2019         R visitMethodType(MethodType t, S s);
2020         R visitPackageType(PackageType t, S s);
2021         R visitTypeVar(TypeVar t, S s);
2022         R visitCapturedType(CapturedType t, S s);
2023         R visitForAll(ForAll t, S s);
2024         R visitUndetVar(UndetVar t, S s);
2025         R visitErrorType(ErrorType t, S s);
2026         R visitAnnotatedType(AnnotatedType t, S s);
2027         R visitType(Type t, S s);
2028     }
2029 }