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