Wed, 17 Jul 2013 14:04:01 +0100
8012242: Lambda compatibility and checked exceptions
Summary: Inference variables in 'throws' clause with no constraints should be inferred as RuntimeException
Reviewed-by: jjg, vromero
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 = ListBuffer.lb();
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(getComponents());
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);
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 private 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 = ListBuffer.lb();
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 = ListBuffer.lb();
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 void addBound(InferenceBound ib, Type bound, Types types) {
1524 Type bound2 = boundMap.apply(bound);
1525 List<Type> prevBounds = bounds.get(ib);
1526 for (Type b : prevBounds) {
1527 //check for redundancy - use strict version of isSameType on tvars
1528 //(as the standard version will lead to false positives w.r.t. clones ivars)
1529 if (types.isSameType(b, bound2, true) || bound == qtype) return;
1530 }
1531 bounds.put(ib, prevBounds.prepend(bound2));
1532 notifyChange(EnumSet.of(ib));
1533 }
1534 //where
1535 Type.Mapping boundMap = new Mapping("boundMap") {
1536 @Override
1537 public Type apply(Type t) {
1538 if (t.hasTag(UNDETVAR)) {
1539 UndetVar uv = (UndetVar)t;
1540 return uv.inst != null ? uv.inst : uv.qtype;
1541 } else {
1542 return t.map(this);
1543 }
1544 }
1545 };
1547 /** replace types in all bounds - this might trigger listener notification */
1548 public void substBounds(List<Type> from, List<Type> to, Types types) {
1549 List<Type> instVars = from.diff(to);
1550 //if set of instantiated ivars is empty, there's nothing to do!
1551 if (instVars.isEmpty()) return;
1552 final EnumSet<InferenceBound> boundsChanged = EnumSet.noneOf(InferenceBound.class);
1553 UndetVarListener prevListener = listener;
1554 try {
1555 //setup new listener for keeping track of changed bounds
1556 listener = new UndetVarListener() {
1557 public void varChanged(UndetVar uv, Set<InferenceBound> ibs) {
1558 boundsChanged.addAll(ibs);
1559 }
1560 };
1561 for (Map.Entry<InferenceBound, List<Type>> _entry : bounds.entrySet()) {
1562 InferenceBound ib = _entry.getKey();
1563 List<Type> prevBounds = _entry.getValue();
1564 ListBuffer<Type> newBounds = ListBuffer.lb();
1565 ListBuffer<Type> deps = ListBuffer.lb();
1566 //step 1 - re-add bounds that are not dependent on ivars
1567 for (Type t : prevBounds) {
1568 if (!t.containsAny(instVars)) {
1569 newBounds.append(t);
1570 } else {
1571 deps.append(t);
1572 }
1573 }
1574 //step 2 - replace bounds
1575 bounds.put(ib, newBounds.toList());
1576 //step 3 - for each dependency, add new replaced bound
1577 for (Type dep : deps) {
1578 addBound(ib, types.subst(dep, from, to), types);
1579 }
1580 }
1581 } finally {
1582 listener = prevListener;
1583 if (!boundsChanged.isEmpty()) {
1584 notifyChange(boundsChanged);
1585 }
1586 }
1587 }
1589 private void notifyChange(EnumSet<InferenceBound> ibs) {
1590 if (listener != null) {
1591 listener.varChanged(this, ibs);
1592 }
1593 }
1594 }
1596 /** Represents NONE.
1597 */
1598 public static class JCNoType extends Type implements NoType {
1599 public JCNoType() {
1600 super(null);
1601 }
1603 @Override
1604 public TypeTag getTag() {
1605 return NONE;
1606 }
1608 @Override
1609 public TypeKind getKind() {
1610 return TypeKind.NONE;
1611 }
1613 @Override
1614 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1615 return v.visitNoType(this, p);
1616 }
1617 }
1619 /** Represents VOID.
1620 */
1621 public static class JCVoidType extends Type implements NoType {
1623 public JCVoidType() {
1624 super(null);
1625 }
1627 @Override
1628 public TypeTag getTag() {
1629 return VOID;
1630 }
1632 @Override
1633 public TypeKind getKind() {
1634 return TypeKind.VOID;
1635 }
1637 @Override
1638 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1639 return v.visitNoType(this, p);
1640 }
1642 @Override
1643 public boolean isPrimitiveOrVoid() {
1644 return true;
1645 }
1646 }
1648 static class BottomType extends Type implements NullType {
1649 public BottomType() {
1650 super(null);
1651 }
1653 @Override
1654 public TypeTag getTag() {
1655 return BOT;
1656 }
1658 @Override
1659 public TypeKind getKind() {
1660 return TypeKind.NULL;
1661 }
1663 @Override
1664 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1665 return v.visitNull(this, p);
1666 }
1668 @Override
1669 public Type constType(Object value) {
1670 return this;
1671 }
1673 @Override
1674 public String stringValue() {
1675 return "null";
1676 }
1678 @Override
1679 public boolean isNullOrReference() {
1680 return true;
1681 }
1683 }
1685 public static class ErrorType extends ClassType
1686 implements javax.lang.model.type.ErrorType {
1688 private Type originalType = null;
1690 public ErrorType(Type originalType, TypeSymbol tsym) {
1691 super(noType, List.<Type>nil(), null);
1692 this.tsym = tsym;
1693 this.originalType = (originalType == null ? noType : originalType);
1694 }
1696 public ErrorType(ClassSymbol c, Type originalType) {
1697 this(originalType, c);
1698 c.type = this;
1699 c.kind = ERR;
1700 c.members_field = new Scope.ErrorScope(c);
1701 }
1703 @Override
1704 public TypeTag getTag() {
1705 return ERROR;
1706 }
1708 @Override
1709 public boolean isPartial() {
1710 return true;
1711 }
1713 @Override
1714 public boolean isReference() {
1715 return true;
1716 }
1718 @Override
1719 public boolean isNullOrReference() {
1720 return true;
1721 }
1723 public ErrorType(Name name, TypeSymbol container, Type originalType) {
1724 this(new ClassSymbol(PUBLIC|STATIC|ACYCLIC, name, null, container), originalType);
1725 }
1727 @Override
1728 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1729 return v.visitErrorType(this, s);
1730 }
1732 public Type constType(Object constValue) { return this; }
1733 public Type getEnclosingType() { return this; }
1734 public Type getReturnType() { return this; }
1735 public Type asSub(Symbol sym) { return this; }
1736 public Type map(Mapping f) { return this; }
1738 public boolean isGenType(Type t) { return true; }
1739 public boolean isErroneous() { return true; }
1740 public boolean isCompound() { return false; }
1741 public boolean isInterface() { return false; }
1743 public List<Type> allparams() { return List.nil(); }
1744 public List<Type> getTypeArguments() { return List.nil(); }
1746 public TypeKind getKind() {
1747 return TypeKind.ERROR;
1748 }
1750 public Type getOriginalType() {
1751 return originalType;
1752 }
1754 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1755 return v.visitError(this, p);
1756 }
1757 }
1759 public static class AnnotatedType extends Type
1760 implements
1761 javax.lang.model.type.ArrayType,
1762 javax.lang.model.type.DeclaredType,
1763 javax.lang.model.type.PrimitiveType,
1764 javax.lang.model.type.TypeVariable,
1765 javax.lang.model.type.WildcardType {
1766 /** The type annotations on this type.
1767 */
1768 public List<Attribute.TypeCompound> typeAnnotations;
1770 /** The underlying type that is annotated.
1771 */
1772 public Type underlyingType;
1774 public AnnotatedType(Type underlyingType) {
1775 super(underlyingType.tsym);
1776 this.typeAnnotations = List.nil();
1777 this.underlyingType = underlyingType;
1778 Assert.check(!underlyingType.isAnnotated(),
1779 "Can't annotate already annotated type: " + underlyingType);
1780 }
1782 public AnnotatedType(List<Attribute.TypeCompound> typeAnnotations,
1783 Type underlyingType) {
1784 super(underlyingType.tsym);
1785 this.typeAnnotations = typeAnnotations;
1786 this.underlyingType = underlyingType;
1787 Assert.check(!underlyingType.isAnnotated(),
1788 "Can't annotate already annotated type: " + underlyingType +
1789 "; adding: " + typeAnnotations);
1790 }
1792 @Override
1793 public TypeTag getTag() {
1794 return underlyingType.getTag();
1795 }
1797 @Override
1798 public boolean isAnnotated() {
1799 return true;
1800 }
1802 @Override
1803 public List<? extends Attribute.TypeCompound> getAnnotationMirrors() {
1804 return typeAnnotations;
1805 }
1807 @Override
1808 public <A extends Annotation> A getAnnotation(Class<A> annotationType) {
1809 return JavacAnnoConstructs.getAnnotation(this, annotationType);
1810 }
1812 @Override
1813 public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationType) {
1814 return JavacAnnoConstructs.getAnnotationsByType(this, annotationType);
1815 }
1817 @Override
1818 public TypeKind getKind() {
1819 return underlyingType.getKind();
1820 }
1822 @Override
1823 public Type unannotatedType() {
1824 return underlyingType;
1825 }
1827 @Override
1828 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1829 return v.visitAnnotatedType(this, s);
1830 }
1832 @Override
1833 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1834 return underlyingType.accept(v, p);
1835 }
1837 @Override
1838 public Type map(Mapping f) {
1839 underlyingType.map(f);
1840 return this;
1841 }
1843 @Override
1844 public Type constType(Object constValue) { return underlyingType.constType(constValue); }
1845 @Override
1846 public Type getEnclosingType() { return underlyingType.getEnclosingType(); }
1848 @Override
1849 public Type getReturnType() { return underlyingType.getReturnType(); }
1850 @Override
1851 public List<Type> getTypeArguments() { return underlyingType.getTypeArguments(); }
1852 @Override
1853 public List<Type> getParameterTypes() { return underlyingType.getParameterTypes(); }
1854 @Override
1855 public Type getReceiverType() { return underlyingType.getReceiverType(); }
1856 @Override
1857 public List<Type> getThrownTypes() { return underlyingType.getThrownTypes(); }
1858 @Override
1859 public Type getUpperBound() { return underlyingType.getUpperBound(); }
1860 @Override
1861 public Type getLowerBound() { return underlyingType.getLowerBound(); }
1863 @Override
1864 public boolean isErroneous() { return underlyingType.isErroneous(); }
1865 @Override
1866 public boolean isCompound() { return underlyingType.isCompound(); }
1867 @Override
1868 public boolean isInterface() { return underlyingType.isInterface(); }
1869 @Override
1870 public List<Type> allparams() { return underlyingType.allparams(); }
1871 @Override
1872 public boolean isPrimitive() { return underlyingType.isPrimitive(); }
1873 @Override
1874 public boolean isPrimitiveOrVoid() { return underlyingType.isPrimitiveOrVoid(); }
1875 @Override
1876 public boolean isNumeric() { return underlyingType.isNumeric(); }
1877 @Override
1878 public boolean isReference() { return underlyingType.isReference(); }
1879 @Override
1880 public boolean isNullOrReference() { return underlyingType.isNullOrReference(); }
1881 @Override
1882 public boolean isPartial() { return underlyingType.isPartial(); }
1883 @Override
1884 public boolean isParameterized() { return underlyingType.isParameterized(); }
1885 @Override
1886 public boolean isRaw() { return underlyingType.isRaw(); }
1887 @Override
1888 public boolean isFinal() { return underlyingType.isFinal(); }
1889 @Override
1890 public boolean isSuperBound() { return underlyingType.isSuperBound(); }
1891 @Override
1892 public boolean isExtendsBound() { return underlyingType.isExtendsBound(); }
1893 @Override
1894 public boolean isUnbound() { return underlyingType.isUnbound(); }
1896 @Override
1897 public String toString() {
1898 // This method is only used for internal debugging output.
1899 // See
1900 // com.sun.tools.javac.code.Printer.visitAnnotatedType(AnnotatedType, Locale)
1901 // for the user-visible logic.
1902 if (typeAnnotations != null &&
1903 !typeAnnotations.isEmpty()) {
1904 return "(" + typeAnnotations.toString() + " :: " + underlyingType.toString() + ")";
1905 } else {
1906 return "({} :: " + underlyingType.toString() +")";
1907 }
1908 }
1910 @Override
1911 public boolean contains(Type t) { return underlyingType.contains(t); }
1913 @Override
1914 public Type withTypeVar(Type t) {
1915 // Don't create a new AnnotatedType, as 'this' will
1916 // get its annotations set later.
1917 underlyingType = underlyingType.withTypeVar(t);
1918 return this;
1919 }
1921 // TODO: attach annotations?
1922 @Override
1923 public TypeSymbol asElement() { return underlyingType.asElement(); }
1925 // TODO: attach annotations?
1926 @Override
1927 public MethodType asMethodType() { return underlyingType.asMethodType(); }
1929 @Override
1930 public void complete() { underlyingType.complete(); }
1932 @Override
1933 public TypeMirror getComponentType() { return ((ArrayType)underlyingType).getComponentType(); }
1935 // The result is an ArrayType, but only in the model sense, not the Type sense.
1936 public AnnotatedType makeVarargs() {
1937 AnnotatedType atype = new AnnotatedType(((ArrayType)underlyingType).makeVarargs());
1938 atype.typeAnnotations = this.typeAnnotations;
1939 return atype;
1940 }
1942 @Override
1943 public TypeMirror getExtendsBound() { return ((WildcardType)underlyingType).getExtendsBound(); }
1944 @Override
1945 public TypeMirror getSuperBound() { return ((WildcardType)underlyingType).getSuperBound(); }
1946 }
1948 public static class UnknownType extends Type {
1950 public UnknownType() {
1951 super(null);
1952 }
1954 @Override
1955 public TypeTag getTag() {
1956 return UNKNOWN;
1957 }
1959 @Override
1960 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1961 return v.visitUnknown(this, p);
1962 }
1964 @Override
1965 public boolean isPartial() {
1966 return true;
1967 }
1968 }
1970 /**
1971 * A visitor for types. A visitor is used to implement operations
1972 * (or relations) on types. Most common operations on types are
1973 * binary relations and this interface is designed for binary
1974 * relations, that is, operations of the form
1975 * Type × S → R.
1976 * <!-- In plain text: Type x S -> R -->
1977 *
1978 * @param <R> the return type of the operation implemented by this
1979 * visitor; use Void if no return type is needed.
1980 * @param <S> the type of the second argument (the first being the
1981 * type itself) of the operation implemented by this visitor; use
1982 * Void if a second argument is not needed.
1983 */
1984 public interface Visitor<R,S> {
1985 R visitClassType(ClassType t, S s);
1986 R visitWildcardType(WildcardType t, S s);
1987 R visitArrayType(ArrayType t, S s);
1988 R visitMethodType(MethodType t, S s);
1989 R visitPackageType(PackageType t, S s);
1990 R visitTypeVar(TypeVar t, S s);
1991 R visitCapturedType(CapturedType t, S s);
1992 R visitForAll(ForAll t, S s);
1993 R visitUndetVar(UndetVar t, S s);
1994 R visitErrorType(ErrorType t, S s);
1995 R visitAnnotatedType(AnnotatedType t, S s);
1996 R visitType(Type t, S s);
1997 }
1998 }