Sun, 12 Dec 2010 10:05:40 -0800
6990134: minor (but red) findbugs warnings
Reviewed-by: mcimadamore
1 /*
2 * Copyright (c) 1999, 2009, 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 com.sun.tools.javac.util.*;
29 import com.sun.tools.javac.code.Symbol.*;
31 import javax.lang.model.type.*;
33 import static com.sun.tools.javac.code.Flags.*;
34 import static com.sun.tools.javac.code.Kinds.*;
35 import static com.sun.tools.javac.code.BoundKind.*;
36 import static com.sun.tools.javac.code.TypeTags.*;
38 /** This class represents Java types. The class itself defines the behavior of
39 * the following types:
40 * <pre>
41 * base types (tags: BYTE, CHAR, SHORT, INT, LONG, FLOAT, DOUBLE, BOOLEAN),
42 * type `void' (tag: VOID),
43 * the bottom type (tag: BOT),
44 * the missing type (tag: NONE).
45 * </pre>
46 * <p>The behavior of the following types is defined in subclasses, which are
47 * all static inner classes of this class:
48 * <pre>
49 * class types (tag: CLASS, class: ClassType),
50 * array types (tag: ARRAY, class: ArrayType),
51 * method types (tag: METHOD, class: MethodType),
52 * package types (tag: PACKAGE, class: PackageType),
53 * type variables (tag: TYPEVAR, class: TypeVar),
54 * type arguments (tag: WILDCARD, class: WildcardType),
55 * polymorphic types (tag: FORALL, class: ForAll),
56 * the error type (tag: ERROR, class: ErrorType).
57 * </pre>
58 *
59 * <p><b>This is NOT part of any supported API.
60 * If you write code that depends on this, you do so at your own risk.
61 * This code and its internal interfaces are subject to change or
62 * deletion without notice.</b>
63 *
64 * @see TypeTags
65 */
66 public class Type implements PrimitiveType {
68 /** Constant type: no type at all. */
69 public static final JCNoType noType = new JCNoType(NONE);
71 /** If this switch is turned on, the names of type variables
72 * and anonymous classes are printed with hashcodes appended.
73 */
74 public static boolean moreInfo = false;
76 /** The tag of this type.
77 *
78 * @see TypeTags
79 */
80 public int tag;
82 /** The defining class / interface / package / type variable
83 */
84 public TypeSymbol tsym;
86 /**
87 * The constant value of this type, null if this type does not
88 * have a constant value attribute. Only primitive types and
89 * strings (ClassType) can have a constant value attribute.
90 * @return the constant value attribute of this type
91 */
92 public Object constValue() {
93 return null;
94 }
96 public <R,S> R accept(Type.Visitor<R,S> v, S s) { return v.visitType(this, s); }
98 /** Define a type given its tag and type symbol
99 */
100 public Type(int tag, TypeSymbol tsym) {
101 this.tag = tag;
102 this.tsym = tsym;
103 }
105 /** An abstract class for mappings from types to types
106 */
107 public static abstract class Mapping {
108 private String name;
109 public Mapping(String name) {
110 this.name = name;
111 }
112 public abstract Type apply(Type t);
113 public String toString() {
114 return name;
115 }
116 }
118 /** map a type function over all immediate descendants of this type
119 */
120 public Type map(Mapping f) {
121 return this;
122 }
124 /** map a type function over a list of types
125 */
126 public static List<Type> map(List<Type> ts, Mapping f) {
127 if (ts.nonEmpty()) {
128 List<Type> tail1 = map(ts.tail, f);
129 Type t = f.apply(ts.head);
130 if (tail1 != ts.tail || t != ts.head)
131 return tail1.prepend(t);
132 }
133 return ts;
134 }
136 /** Define a constant type, of the same kind as this type
137 * and with given constant value
138 */
139 public Type constType(Object constValue) {
140 final Object value = constValue;
141 assert tag <= BOOLEAN;
142 return new Type(tag, tsym) {
143 @Override
144 public Object constValue() {
145 return value;
146 }
147 @Override
148 public Type baseType() {
149 return tsym.type;
150 }
151 };
152 }
154 /**
155 * If this is a constant type, return its underlying type.
156 * Otherwise, return the type itself.
157 */
158 public Type baseType() {
159 return this;
160 }
162 /** Return the base types of a list of types.
163 */
164 public static List<Type> baseTypes(List<Type> ts) {
165 if (ts.nonEmpty()) {
166 Type t = ts.head.baseType();
167 List<Type> baseTypes = baseTypes(ts.tail);
168 if (t != ts.head || baseTypes != ts.tail)
169 return baseTypes.prepend(t);
170 }
171 return ts;
172 }
174 /** The Java source which this type represents.
175 */
176 public String toString() {
177 String s = (tsym == null || tsym.name == null)
178 ? "<none>"
179 : tsym.name.toString();
180 if (moreInfo && tag == TYPEVAR) s = s + hashCode();
181 return s;
182 }
184 /**
185 * The Java source which this type list represents. A List is
186 * represented as a comma-spearated listing of the elements in
187 * that list.
188 */
189 public static String toString(List<Type> ts) {
190 if (ts.isEmpty()) {
191 return "";
192 } else {
193 StringBuffer buf = new StringBuffer();
194 buf.append(ts.head.toString());
195 for (List<Type> l = ts.tail; l.nonEmpty(); l = l.tail)
196 buf.append(",").append(l.head.toString());
197 return buf.toString();
198 }
199 }
201 /**
202 * The constant value of this type, converted to String
203 */
204 public String stringValue() {
205 assert constValue() != null;
206 if (tag == BOOLEAN)
207 return ((Integer) constValue()).intValue() == 0 ? "false" : "true";
208 else if (tag == CHAR)
209 return String.valueOf((char) ((Integer) constValue()).intValue());
210 else
211 return constValue().toString();
212 }
214 /**
215 * This method is analogous to isSameType, but weaker, since we
216 * never complete classes. Where isSameType would complete a
217 * class, equals assumes that the two types are different.
218 */
219 public boolean equals(Object t) {
220 return super.equals(t);
221 }
223 public int hashCode() {
224 return super.hashCode();
225 }
227 /** Is this a constant type whose value is false?
228 */
229 public boolean isFalse() {
230 return
231 tag == BOOLEAN &&
232 constValue() != null &&
233 ((Integer)constValue()).intValue() == 0;
234 }
236 /** Is this a constant type whose value is true?
237 */
238 public boolean isTrue() {
239 return
240 tag == BOOLEAN &&
241 constValue() != null &&
242 ((Integer)constValue()).intValue() != 0;
243 }
245 public String argtypes(boolean varargs) {
246 List<Type> args = getParameterTypes();
247 if (!varargs) return args.toString();
248 StringBuilder buf = new StringBuilder();
249 while (args.tail.nonEmpty()) {
250 buf.append(args.head);
251 args = args.tail;
252 buf.append(',');
253 }
254 if (args.head.tag == ARRAY) {
255 buf.append(((ArrayType)args.head).elemtype);
256 buf.append("...");
257 } else {
258 buf.append(args.head);
259 }
260 return buf.toString();
261 }
263 /** Access methods.
264 */
265 public List<Type> getTypeArguments() { return List.nil(); }
266 public Type getEnclosingType() { return null; }
267 public List<Type> getParameterTypes() { return List.nil(); }
268 public Type getReturnType() { return null; }
269 public List<Type> getThrownTypes() { return List.nil(); }
270 public Type getUpperBound() { return null; }
271 public Type getLowerBound() { return null; }
273 public void setThrown(List<Type> ts) {
274 throw new AssertionError();
275 }
277 /** Navigation methods, these will work for classes, type variables,
278 * foralls, but will return null for arrays and methods.
279 */
281 /** Return all parameters of this type and all its outer types in order
282 * outer (first) to inner (last).
283 */
284 public List<Type> allparams() { return List.nil(); }
286 /** Does this type contain "error" elements?
287 */
288 public boolean isErroneous() {
289 return false;
290 }
292 public static boolean isErroneous(List<Type> ts) {
293 for (List<Type> l = ts; l.nonEmpty(); l = l.tail)
294 if (l.head.isErroneous()) return true;
295 return false;
296 }
298 /** Is this type parameterized?
299 * A class type is parameterized if it has some parameters.
300 * An array type is parameterized if its element type is parameterized.
301 * All other types are not parameterized.
302 */
303 public boolean isParameterized() {
304 return false;
305 }
307 /** Is this type a raw type?
308 * A class type is a raw type if it misses some of its parameters.
309 * An array type is a raw type if its element type is raw.
310 * All other types are not raw.
311 * Type validation will ensure that the only raw types
312 * in a program are types that miss all their type variables.
313 */
314 public boolean isRaw() {
315 return false;
316 }
318 public boolean isCompound() {
319 return tsym.completer == null
320 // Compound types can't have a completer. Calling
321 // flags() will complete the symbol causing the
322 // compiler to load classes unnecessarily. This led
323 // to regression 6180021.
324 && (tsym.flags() & COMPOUND) != 0;
325 }
327 public boolean isInterface() {
328 return (tsym.flags() & INTERFACE) != 0;
329 }
331 public boolean isFinal() {
332 return (tsym.flags() & FINAL) != 0;
333 }
335 public boolean isPrimitive() {
336 return tag < VOID;
337 }
339 /**
340 * Does this type contain occurrences of type t?
341 */
342 public boolean contains(Type t) {
343 return t == this;
344 }
346 public static boolean contains(List<Type> ts, Type t) {
347 for (List<Type> l = ts;
348 l.tail != null /*inlined: l.nonEmpty()*/;
349 l = l.tail)
350 if (l.head.contains(t)) return true;
351 return false;
352 }
354 /** Does this type contain an occurrence of some type in 'ts'?
355 */
356 public boolean containsAny(List<Type> ts) {
357 for (Type t : ts)
358 if (this.contains(t)) return true;
359 return false;
360 }
362 public static boolean containsAny(List<Type> ts1, List<Type> ts2) {
363 for (Type t : ts1)
364 if (t.containsAny(ts2)) return true;
365 return false;
366 }
368 public boolean isSuperBound() { return false; }
369 public boolean isExtendsBound() { return false; }
370 public boolean isUnbound() { return false; }
371 public Type withTypeVar(Type t) { return this; }
373 /** The underlying method type of this type.
374 */
375 public MethodType asMethodType() { throw new AssertionError(); }
377 /** Complete loading all classes in this type.
378 */
379 public void complete() {}
381 public Object clone() {
382 try {
383 return super.clone();
384 } catch (CloneNotSupportedException e) {
385 throw new AssertionError(e);
386 }
387 }
389 public TypeSymbol asElement() {
390 return tsym;
391 }
393 public TypeKind getKind() {
394 switch (tag) {
395 case BYTE: return TypeKind.BYTE;
396 case CHAR: return TypeKind.CHAR;
397 case SHORT: return TypeKind.SHORT;
398 case INT: return TypeKind.INT;
399 case LONG: return TypeKind.LONG;
400 case FLOAT: return TypeKind.FLOAT;
401 case DOUBLE: return TypeKind.DOUBLE;
402 case BOOLEAN: return TypeKind.BOOLEAN;
403 case VOID: return TypeKind.VOID;
404 case BOT: return TypeKind.NULL;
405 case NONE: return TypeKind.NONE;
406 default: return TypeKind.OTHER;
407 }
408 }
410 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
411 if (isPrimitive())
412 return v.visitPrimitive(this, p);
413 else
414 throw new AssertionError();
415 }
417 public static class WildcardType extends Type
418 implements javax.lang.model.type.WildcardType {
420 public Type type;
421 public BoundKind kind;
422 public TypeVar bound;
424 @Override
425 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
426 return v.visitWildcardType(this, s);
427 }
429 public WildcardType(Type type, BoundKind kind, TypeSymbol tsym) {
430 super(WILDCARD, tsym);
431 assert(type != null);
432 this.kind = kind;
433 this.type = type;
434 }
435 public WildcardType(WildcardType t, TypeVar bound) {
436 this(t.type, t.kind, t.tsym, bound);
437 }
439 public WildcardType(Type type, BoundKind kind, TypeSymbol tsym, TypeVar bound) {
440 this(type, kind, tsym);
441 this.bound = bound;
442 }
444 public boolean contains(Type t) {
445 return kind != UNBOUND && type.contains(t);
446 }
448 public boolean isSuperBound() {
449 return kind == SUPER ||
450 kind == UNBOUND;
451 }
452 public boolean isExtendsBound() {
453 return kind == EXTENDS ||
454 kind == UNBOUND;
455 }
456 public boolean isUnbound() {
457 return kind == UNBOUND;
458 }
460 public Type withTypeVar(Type t) {
461 //-System.err.println(this+".withTypeVar("+t+");");//DEBUG
462 if (bound == t)
463 return this;
464 bound = (TypeVar)t;
465 return this;
466 }
468 boolean isPrintingBound = false;
469 public String toString() {
470 StringBuffer s = new StringBuffer();
471 s.append(kind.toString());
472 if (kind != UNBOUND)
473 s.append(type);
474 if (moreInfo && bound != null && !isPrintingBound)
475 try {
476 isPrintingBound = true;
477 s.append("{:").append(bound.bound).append(":}");
478 } finally {
479 isPrintingBound = false;
480 }
481 return s.toString();
482 }
484 public Type map(Mapping f) {
485 //- System.err.println(" (" + this + ").map(" + f + ")");//DEBUG
486 Type t = type;
487 if (t != null)
488 t = f.apply(t);
489 if (t == type)
490 return this;
491 else
492 return new WildcardType(t, kind, tsym, bound);
493 }
495 public Type getExtendsBound() {
496 if (kind == EXTENDS)
497 return type;
498 else
499 return null;
500 }
502 public Type getSuperBound() {
503 if (kind == SUPER)
504 return type;
505 else
506 return null;
507 }
509 public TypeKind getKind() {
510 return TypeKind.WILDCARD;
511 }
513 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
514 return v.visitWildcard(this, p);
515 }
516 }
518 public static class ClassType extends Type implements DeclaredType {
520 /** The enclosing type of this type. If this is the type of an inner
521 * class, outer_field refers to the type of its enclosing
522 * instance class, in all other cases it referes to noType.
523 */
524 private Type outer_field;
526 /** The type parameters of this type (to be set once class is loaded).
527 */
528 public List<Type> typarams_field;
530 /** A cache variable for the type parameters of this type,
531 * appended to all parameters of its enclosing class.
532 * @see #allparams
533 */
534 public List<Type> allparams_field;
536 /** The supertype of this class (to be set once class is loaded).
537 */
538 public Type supertype_field;
540 /** The interfaces of this class (to be set once class is loaded).
541 */
542 public List<Type> interfaces_field;
544 public ClassType(Type outer, List<Type> typarams, TypeSymbol tsym) {
545 super(CLASS, tsym);
546 this.outer_field = outer;
547 this.typarams_field = typarams;
548 this.allparams_field = null;
549 this.supertype_field = null;
550 this.interfaces_field = null;
551 /*
552 // this can happen during error recovery
553 assert
554 outer.isParameterized() ?
555 typarams.length() == tsym.type.typarams().length() :
556 outer.isRaw() ?
557 typarams.length() == 0 :
558 true;
559 */
560 }
562 @Override
563 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
564 return v.visitClassType(this, s);
565 }
567 public Type constType(Object constValue) {
568 final Object value = constValue;
569 return new ClassType(getEnclosingType(), typarams_field, tsym) {
570 @Override
571 public Object constValue() {
572 return value;
573 }
574 @Override
575 public Type baseType() {
576 return tsym.type;
577 }
578 };
579 }
581 /** The Java source which this type represents.
582 */
583 public String toString() {
584 StringBuffer buf = new StringBuffer();
585 if (getEnclosingType().tag == CLASS && tsym.owner.kind == TYP) {
586 buf.append(getEnclosingType().toString());
587 buf.append(".");
588 buf.append(className(tsym, false));
589 } else {
590 buf.append(className(tsym, true));
591 }
592 if (getTypeArguments().nonEmpty()) {
593 buf.append('<');
594 buf.append(getTypeArguments().toString());
595 buf.append(">");
596 }
597 return buf.toString();
598 }
599 //where
600 private String className(Symbol sym, boolean longform) {
601 if (sym.name.isEmpty() && (sym.flags() & COMPOUND) != 0) {
602 StringBuffer s = new StringBuffer(supertype_field.toString());
603 for (List<Type> is=interfaces_field; is.nonEmpty(); is = is.tail) {
604 s.append("&");
605 s.append(is.head.toString());
606 }
607 return s.toString();
608 } else if (sym.name.isEmpty()) {
609 String s;
610 ClassType norm = (ClassType) tsym.type;
611 if (norm == null) {
612 s = Log.getLocalizedString("anonymous.class", (Object)null);
613 } else if (norm.interfaces_field != null && norm.interfaces_field.nonEmpty()) {
614 s = Log.getLocalizedString("anonymous.class",
615 norm.interfaces_field.head);
616 } else {
617 s = Log.getLocalizedString("anonymous.class",
618 norm.supertype_field);
619 }
620 if (moreInfo)
621 s += String.valueOf(sym.hashCode());
622 return s;
623 } else if (longform) {
624 return sym.getQualifiedName().toString();
625 } else {
626 return sym.name.toString();
627 }
628 }
630 public List<Type> getTypeArguments() {
631 if (typarams_field == null) {
632 complete();
633 if (typarams_field == null)
634 typarams_field = List.nil();
635 }
636 return typarams_field;
637 }
639 public boolean hasErasedSupertypes() {
640 return isRaw();
641 }
643 public Type getEnclosingType() {
644 return outer_field;
645 }
647 public void setEnclosingType(Type outer) {
648 outer_field = outer;
649 }
651 public List<Type> allparams() {
652 if (allparams_field == null) {
653 allparams_field = getTypeArguments().prependList(getEnclosingType().allparams());
654 }
655 return allparams_field;
656 }
658 public boolean isErroneous() {
659 return
660 getEnclosingType().isErroneous() ||
661 isErroneous(getTypeArguments()) ||
662 this != tsym.type && tsym.type.isErroneous();
663 }
665 public boolean isParameterized() {
666 return allparams().tail != null;
667 // optimization, was: allparams().nonEmpty();
668 }
670 /** A cache for the rank. */
671 int rank_field = -1;
673 /** A class type is raw if it misses some
674 * of its type parameter sections.
675 * After validation, this is equivalent to:
676 * allparams.isEmpty() && tsym.type.allparams.nonEmpty();
677 */
678 public boolean isRaw() {
679 return
680 this != tsym.type && // necessary, but not sufficient condition
681 tsym.type.allparams().nonEmpty() &&
682 allparams().isEmpty();
683 }
685 public Type map(Mapping f) {
686 Type outer = getEnclosingType();
687 Type outer1 = f.apply(outer);
688 List<Type> typarams = getTypeArguments();
689 List<Type> typarams1 = map(typarams, f);
690 if (outer1 == outer && typarams1 == typarams) return this;
691 else return new ClassType(outer1, typarams1, tsym);
692 }
694 public boolean contains(Type elem) {
695 return
696 elem == this
697 || (isParameterized()
698 && (getEnclosingType().contains(elem) || contains(getTypeArguments(), elem)))
699 || (isCompound()
700 && (supertype_field.contains(elem) || contains(interfaces_field, elem)));
701 }
703 public void complete() {
704 if (tsym.completer != null) tsym.complete();
705 }
707 public TypeKind getKind() {
708 return TypeKind.DECLARED;
709 }
711 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
712 return v.visitDeclared(this, p);
713 }
714 }
716 public static class ErasedClassType extends ClassType {
717 public ErasedClassType(Type outer, TypeSymbol tsym) {
718 super(outer, List.<Type>nil(), tsym);
719 }
721 @Override
722 public boolean hasErasedSupertypes() {
723 return true;
724 }
725 }
727 public static class ArrayType extends Type
728 implements javax.lang.model.type.ArrayType {
730 public Type elemtype;
732 public ArrayType(Type elemtype, TypeSymbol arrayClass) {
733 super(ARRAY, arrayClass);
734 this.elemtype = elemtype;
735 }
737 @Override
738 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
739 return v.visitArrayType(this, s);
740 }
742 public String toString() {
743 return elemtype + "[]";
744 }
746 public boolean equals(Object obj) {
747 return
748 this == obj ||
749 (obj instanceof ArrayType &&
750 this.elemtype.equals(((ArrayType)obj).elemtype));
751 }
753 public int hashCode() {
754 return (ARRAY << 5) + elemtype.hashCode();
755 }
757 public List<Type> allparams() { return elemtype.allparams(); }
759 public boolean isErroneous() {
760 return elemtype.isErroneous();
761 }
763 public boolean isParameterized() {
764 return elemtype.isParameterized();
765 }
767 public boolean isRaw() {
768 return elemtype.isRaw();
769 }
771 public Type map(Mapping f) {
772 Type elemtype1 = f.apply(elemtype);
773 if (elemtype1 == elemtype) return this;
774 else return new ArrayType(elemtype1, tsym);
775 }
777 public boolean contains(Type elem) {
778 return elem == this || elemtype.contains(elem);
779 }
781 public void complete() {
782 elemtype.complete();
783 }
785 public Type getComponentType() {
786 return elemtype;
787 }
789 public TypeKind getKind() {
790 return TypeKind.ARRAY;
791 }
793 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
794 return v.visitArray(this, p);
795 }
796 }
798 public static class MethodType extends Type
799 implements Cloneable, ExecutableType {
801 public List<Type> argtypes;
802 public Type restype;
803 public List<Type> thrown;
805 public MethodType(List<Type> argtypes,
806 Type restype,
807 List<Type> thrown,
808 TypeSymbol methodClass) {
809 super(METHOD, methodClass);
810 this.argtypes = argtypes;
811 this.restype = restype;
812 this.thrown = thrown;
813 }
815 @Override
816 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
817 return v.visitMethodType(this, s);
818 }
820 /** The Java source which this type represents.
821 *
822 * XXX 06/09/99 iris This isn't correct Java syntax, but it probably
823 * should be.
824 */
825 public String toString() {
826 return "(" + argtypes + ")" + restype;
827 }
829 public boolean equals(Object obj) {
830 if (this == obj)
831 return true;
832 if (!(obj instanceof MethodType))
833 return false;
834 MethodType m = (MethodType)obj;
835 List<Type> args1 = argtypes;
836 List<Type> args2 = m.argtypes;
837 while (!args1.isEmpty() && !args2.isEmpty()) {
838 if (!args1.head.equals(args2.head))
839 return false;
840 args1 = args1.tail;
841 args2 = args2.tail;
842 }
843 if (!args1.isEmpty() || !args2.isEmpty())
844 return false;
845 return restype.equals(m.restype);
846 }
848 public int hashCode() {
849 int h = METHOD;
850 for (List<Type> thisargs = this.argtypes;
851 thisargs.tail != null; /*inlined: thisargs.nonEmpty()*/
852 thisargs = thisargs.tail)
853 h = (h << 5) + thisargs.head.hashCode();
854 return (h << 5) + this.restype.hashCode();
855 }
857 public List<Type> getParameterTypes() { return argtypes; }
858 public Type getReturnType() { return restype; }
859 public List<Type> getThrownTypes() { return thrown; }
861 public void setThrown(List<Type> t) {
862 thrown = t;
863 }
865 public boolean isErroneous() {
866 return
867 isErroneous(argtypes) ||
868 restype != null && restype.isErroneous();
869 }
871 public Type map(Mapping f) {
872 List<Type> argtypes1 = map(argtypes, f);
873 Type restype1 = f.apply(restype);
874 List<Type> thrown1 = map(thrown, f);
875 if (argtypes1 == argtypes &&
876 restype1 == restype &&
877 thrown1 == thrown) return this;
878 else return new MethodType(argtypes1, restype1, thrown1, tsym);
879 }
881 public boolean contains(Type elem) {
882 return elem == this || contains(argtypes, elem) || restype.contains(elem);
883 }
885 public MethodType asMethodType() { return this; }
887 public void complete() {
888 for (List<Type> l = argtypes; l.nonEmpty(); l = l.tail)
889 l.head.complete();
890 restype.complete();
891 for (List<Type> l = thrown; l.nonEmpty(); l = l.tail)
892 l.head.complete();
893 }
895 public List<TypeVar> getTypeVariables() {
896 return List.nil();
897 }
899 public TypeSymbol asElement() {
900 return null;
901 }
903 public TypeKind getKind() {
904 return TypeKind.EXECUTABLE;
905 }
907 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
908 return v.visitExecutable(this, p);
909 }
910 }
912 public static class PackageType extends Type implements NoType {
914 PackageType(TypeSymbol tsym) {
915 super(PACKAGE, tsym);
916 }
918 @Override
919 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
920 return v.visitPackageType(this, s);
921 }
923 public String toString() {
924 return tsym.getQualifiedName().toString();
925 }
927 public TypeKind getKind() {
928 return TypeKind.PACKAGE;
929 }
931 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
932 return v.visitNoType(this, p);
933 }
934 }
936 public static class TypeVar extends Type implements TypeVariable {
938 /** The upper bound of this type variable; set from outside.
939 * Must be nonempty once it is set.
940 * For a bound, `bound' is the bound type itself.
941 * Multiple bounds are expressed as a single class type which has the
942 * individual bounds as superclass, respectively interfaces.
943 * The class type then has as `tsym' a compiler generated class `c',
944 * which has a flag COMPOUND and whose owner is the type variable
945 * itself. Furthermore, the erasure_field of the class
946 * points to the first class or interface bound.
947 */
948 public Type bound = null;
950 /** The lower bound of this type variable.
951 * TypeVars don't normally have a lower bound, so it is normally set
952 * to syms.botType.
953 * Subtypes, such as CapturedType, may provide a different value.
954 */
955 public Type lower;
957 public TypeVar(Name name, Symbol owner, Type lower) {
958 super(TYPEVAR, null);
959 tsym = new TypeSymbol(0, name, this, owner);
960 this.lower = lower;
961 }
963 public TypeVar(TypeSymbol tsym, Type bound, Type lower) {
964 super(TYPEVAR, tsym);
965 this.bound = bound;
966 this.lower = lower;
967 }
969 @Override
970 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
971 return v.visitTypeVar(this, s);
972 }
974 @Override
975 public Type getUpperBound() { return bound; }
977 int rank_field = -1;
979 @Override
980 public Type getLowerBound() {
981 return lower;
982 }
984 public TypeKind getKind() {
985 return TypeKind.TYPEVAR;
986 }
988 public boolean isCaptured() {
989 return false;
990 }
992 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
993 return v.visitTypeVariable(this, p);
994 }
995 }
997 /** A captured type variable comes from wildcards which can have
998 * both upper and lower bound. CapturedType extends TypeVar with
999 * a lower bound.
1000 */
1001 public static class CapturedType extends TypeVar {
1003 public WildcardType wildcard;
1005 public CapturedType(Name name,
1006 Symbol owner,
1007 Type upper,
1008 Type lower,
1009 WildcardType wildcard) {
1010 super(name, owner, lower);
1011 assert lower != null;
1012 this.bound = upper;
1013 this.lower = lower;
1014 this.wildcard = wildcard;
1015 }
1017 @Override
1018 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1019 return v.visitCapturedType(this, s);
1020 }
1022 @Override
1023 public boolean isCaptured() {
1024 return true;
1025 }
1027 @Override
1028 public String toString() {
1029 return "capture#"
1030 + (hashCode() & 0xFFFFFFFFL) % Printer.PRIME
1031 + " of "
1032 + wildcard;
1033 }
1034 }
1036 public static abstract class DelegatedType extends Type {
1037 public Type qtype;
1038 public DelegatedType(int tag, Type qtype) {
1039 super(tag, qtype.tsym);
1040 this.qtype = qtype;
1041 }
1042 public String toString() { return qtype.toString(); }
1043 public List<Type> getTypeArguments() { return qtype.getTypeArguments(); }
1044 public Type getEnclosingType() { return qtype.getEnclosingType(); }
1045 public List<Type> getParameterTypes() { return qtype.getParameterTypes(); }
1046 public Type getReturnType() { return qtype.getReturnType(); }
1047 public List<Type> getThrownTypes() { return qtype.getThrownTypes(); }
1048 public List<Type> allparams() { return qtype.allparams(); }
1049 public Type getUpperBound() { return qtype.getUpperBound(); }
1050 public Object clone() { DelegatedType t = (DelegatedType)super.clone(); t.qtype = (Type)qtype.clone(); return t; }
1051 public boolean isErroneous() { return qtype.isErroneous(); }
1052 }
1054 public static class ForAll extends DelegatedType
1055 implements Cloneable, ExecutableType {
1056 public List<Type> tvars;
1058 public ForAll(List<Type> tvars, Type qtype) {
1059 super(FORALL, qtype);
1060 this.tvars = tvars;
1061 }
1063 @Override
1064 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1065 return v.visitForAll(this, s);
1066 }
1068 public String toString() {
1069 return "<" + tvars + ">" + qtype;
1070 }
1072 public List<Type> getTypeArguments() { return tvars; }
1074 public void setThrown(List<Type> t) {
1075 qtype.setThrown(t);
1076 }
1078 public Object clone() {
1079 ForAll result = (ForAll)super.clone();
1080 result.qtype = (Type)result.qtype.clone();
1081 return result;
1082 }
1084 public boolean isErroneous() {
1085 return qtype.isErroneous();
1086 }
1088 /**
1089 * Replaces this ForAll's typevars with a set of concrete Java types
1090 * and returns the instantiated generic type. Subclasses should override
1091 * in order to check that the list of types is a valid instantiation
1092 * of the ForAll's typevars.
1093 *
1094 * @param actuals list of actual types
1095 * @param types types instance
1096 * @return qtype where all occurrences of tvars are replaced
1097 * by types in actuals
1098 */
1099 public Type inst(List<Type> actuals, Types types) {
1100 return types.subst(qtype, tvars, actuals);
1101 }
1103 /**
1104 * Kind of type-constraint derived during type inference
1105 */
1106 public enum ConstraintKind {
1107 /**
1108 * upper bound constraint (a type variable must be instantiated
1109 * with a type T, where T is a subtype of all the types specified by
1110 * its EXTENDS constraints).
1111 */
1112 EXTENDS,
1113 /**
1114 * lower bound constraint (a type variable must be instantiated
1115 * with a type T, where T is a supertype of all the types specified by
1116 * its SUPER constraints).
1117 */
1118 SUPER,
1119 /**
1120 * equality constraint (a type variable must be instantiated to the type
1121 * specified by its EQUAL constraint.
1122 */
1123 EQUAL;
1124 }
1126 /**
1127 * Get the type-constraints of a given kind for a given type-variable of
1128 * this ForAll type. Subclasses should override in order to return more
1129 * accurate sets of constraints.
1130 *
1131 * @param tv the type-variable for which the constraint is to be retrieved
1132 * @param ck the constraint kind to be retrieved
1133 * @return the list of types specified by the selected constraint
1134 */
1135 public List<Type> getConstraints(TypeVar tv, ConstraintKind ck) {
1136 return List.nil();
1137 }
1139 public Type map(Mapping f) {
1140 return f.apply(qtype);
1141 }
1143 public boolean contains(Type elem) {
1144 return qtype.contains(elem);
1145 }
1147 public MethodType asMethodType() {
1148 return qtype.asMethodType();
1149 }
1151 public void complete() {
1152 for (List<Type> l = tvars; l.nonEmpty(); l = l.tail) {
1153 ((TypeVar)l.head).bound.complete();
1154 }
1155 qtype.complete();
1156 }
1158 public List<TypeVar> getTypeVariables() {
1159 return List.convert(TypeVar.class, getTypeArguments());
1160 }
1162 public TypeKind getKind() {
1163 return TypeKind.EXECUTABLE;
1164 }
1166 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1167 return v.visitExecutable(this, p);
1168 }
1169 }
1171 /** A class for instantiatable variables, for use during type
1172 * inference.
1173 */
1174 public static class UndetVar extends DelegatedType {
1175 public List<Type> lobounds = List.nil();
1176 public List<Type> hibounds = List.nil();
1177 public Type inst = null;
1179 @Override
1180 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1181 return v.visitUndetVar(this, s);
1182 }
1184 public UndetVar(Type origin) {
1185 super(UNDETVAR, origin);
1186 }
1188 public String toString() {
1189 if (inst != null) return inst.toString();
1190 else return qtype + "?";
1191 }
1193 public Type baseType() {
1194 if (inst != null) return inst.baseType();
1195 else return this;
1196 }
1197 }
1199 /** Represents VOID or NONE.
1200 */
1201 static class JCNoType extends Type implements NoType {
1202 public JCNoType(int tag) {
1203 super(tag, null);
1204 }
1206 @Override
1207 public TypeKind getKind() {
1208 switch (tag) {
1209 case VOID: return TypeKind.VOID;
1210 case NONE: return TypeKind.NONE;
1211 default:
1212 throw new AssertionError("Unexpected tag: " + tag);
1213 }
1214 }
1216 @Override
1217 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1218 return v.visitNoType(this, p);
1219 }
1220 }
1222 static class BottomType extends Type implements NullType {
1223 public BottomType() {
1224 super(TypeTags.BOT, null);
1225 }
1227 @Override
1228 public TypeKind getKind() {
1229 return TypeKind.NULL;
1230 }
1232 @Override
1233 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1234 return v.visitNull(this, p);
1235 }
1237 @Override
1238 public Type constType(Object value) {
1239 return this;
1240 }
1242 @Override
1243 public String stringValue() {
1244 return "null";
1245 }
1246 }
1248 public static class ErrorType extends ClassType
1249 implements javax.lang.model.type.ErrorType {
1251 private Type originalType = null;
1253 public ErrorType(Type originalType, TypeSymbol tsym) {
1254 super(noType, List.<Type>nil(), null);
1255 tag = ERROR;
1256 this.tsym = tsym;
1257 this.originalType = (originalType == null ? noType : originalType);
1258 }
1260 public ErrorType(ClassSymbol c, Type originalType) {
1261 this(originalType, c);
1262 c.type = this;
1263 c.kind = ERR;
1264 c.members_field = new Scope.ErrorScope(c);
1265 }
1267 public ErrorType(Name name, TypeSymbol container, Type originalType) {
1268 this(new ClassSymbol(PUBLIC|STATIC|ACYCLIC, name, null, container), originalType);
1269 }
1271 @Override
1272 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1273 return v.visitErrorType(this, s);
1274 }
1276 public Type constType(Object constValue) { return this; }
1277 public Type getEnclosingType() { return this; }
1278 public Type getReturnType() { return this; }
1279 public Type asSub(Symbol sym) { return this; }
1280 public Type map(Mapping f) { return this; }
1282 public boolean isGenType(Type t) { return true; }
1283 public boolean isErroneous() { return true; }
1284 public boolean isCompound() { return false; }
1285 public boolean isInterface() { return false; }
1287 public List<Type> allparams() { return List.nil(); }
1288 public List<Type> getTypeArguments() { return List.nil(); }
1290 public TypeKind getKind() {
1291 return TypeKind.ERROR;
1292 }
1294 public Type getOriginalType() {
1295 return originalType;
1296 }
1298 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1299 return v.visitError(this, p);
1300 }
1301 }
1303 /**
1304 * A visitor for types. A visitor is used to implement operations
1305 * (or relations) on types. Most common operations on types are
1306 * binary relations and this interface is designed for binary
1307 * relations, that is, operations on the form
1308 * Type × S → R.
1309 * <!-- In plain text: Type x S -> R -->
1310 *
1311 * @param <R> the return type of the operation implemented by this
1312 * visitor; use Void if no return type is needed.
1313 * @param <S> the type of the second argument (the first being the
1314 * type itself) of the operation implemented by this visitor; use
1315 * Void if a second argument is not needed.
1316 */
1317 public interface Visitor<R,S> {
1318 R visitClassType(ClassType t, S s);
1319 R visitWildcardType(WildcardType t, S s);
1320 R visitArrayType(ArrayType t, S s);
1321 R visitMethodType(MethodType t, S s);
1322 R visitPackageType(PackageType t, S s);
1323 R visitTypeVar(TypeVar t, S s);
1324 R visitCapturedType(CapturedType t, S s);
1325 R visitForAll(ForAll t, S s);
1326 R visitUndetVar(UndetVar t, S s);
1327 R visitErrorType(ErrorType t, S s);
1328 R visitType(Type t, S s);
1329 }
1330 }