Tue, 07 Sep 2010 17:31:54 +0100
6337171: javac should create bridge methods when type variable bounds restricted
Summary: javac should add synthetic overrides for inherited abstract methods in order to preserve binary compatibility
Reviewed-by: jjg
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 StringBuffer buf = new StringBuffer();
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 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;
949 public Type lower;
951 public TypeVar(Name name, Symbol owner, Type lower) {
952 super(TYPEVAR, null);
953 tsym = new TypeSymbol(0, name, this, owner);
954 this.lower = lower;
955 }
957 public TypeVar(TypeSymbol tsym, Type bound, Type lower) {
958 super(TYPEVAR, tsym);
959 this.bound = bound;
960 this.lower = lower;
961 }
963 @Override
964 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
965 return v.visitTypeVar(this, s);
966 }
968 public Type getUpperBound() { return bound; }
970 int rank_field = -1;
972 public Type getLowerBound() {
973 return lower;
974 }
976 public TypeKind getKind() {
977 return TypeKind.TYPEVAR;
978 }
980 public boolean isCaptured() {
981 return false;
982 }
984 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
985 return v.visitTypeVariable(this, p);
986 }
987 }
989 /** A captured type variable comes from wildcards which can have
990 * both upper and lower bound. CapturedType extends TypeVar with
991 * a lower bound.
992 */
993 public static class CapturedType extends TypeVar {
995 public Type lower;
996 public WildcardType wildcard;
998 public CapturedType(Name name,
999 Symbol owner,
1000 Type upper,
1001 Type lower,
1002 WildcardType wildcard) {
1003 super(name, owner, lower);
1004 assert lower != null;
1005 this.bound = upper;
1006 this.lower = lower;
1007 this.wildcard = wildcard;
1008 }
1010 @Override
1011 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1012 return v.visitCapturedType(this, s);
1013 }
1015 public Type getLowerBound() {
1016 return lower;
1017 }
1019 @Override
1020 public boolean isCaptured() {
1021 return true;
1022 }
1024 @Override
1025 public String toString() {
1026 return "capture#"
1027 + (hashCode() & 0xFFFFFFFFL) % Printer.PRIME
1028 + " of "
1029 + wildcard;
1030 }
1031 }
1033 public static abstract class DelegatedType extends Type {
1034 public Type qtype;
1035 public DelegatedType(int tag, Type qtype) {
1036 super(tag, qtype.tsym);
1037 this.qtype = qtype;
1038 }
1039 public String toString() { return qtype.toString(); }
1040 public List<Type> getTypeArguments() { return qtype.getTypeArguments(); }
1041 public Type getEnclosingType() { return qtype.getEnclosingType(); }
1042 public List<Type> getParameterTypes() { return qtype.getParameterTypes(); }
1043 public Type getReturnType() { return qtype.getReturnType(); }
1044 public List<Type> getThrownTypes() { return qtype.getThrownTypes(); }
1045 public List<Type> allparams() { return qtype.allparams(); }
1046 public Type getUpperBound() { return qtype.getUpperBound(); }
1047 public Object clone() { DelegatedType t = (DelegatedType)super.clone(); t.qtype = (Type)qtype.clone(); return t; }
1048 public boolean isErroneous() { return qtype.isErroneous(); }
1049 }
1051 public static class ForAll extends DelegatedType
1052 implements Cloneable, ExecutableType {
1053 public List<Type> tvars;
1055 public ForAll(List<Type> tvars, Type qtype) {
1056 super(FORALL, qtype);
1057 this.tvars = tvars;
1058 }
1060 @Override
1061 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1062 return v.visitForAll(this, s);
1063 }
1065 public String toString() {
1066 return "<" + tvars + ">" + qtype;
1067 }
1069 public List<Type> getTypeArguments() { return tvars; }
1071 public void setThrown(List<Type> t) {
1072 qtype.setThrown(t);
1073 }
1075 public Object clone() {
1076 ForAll result = (ForAll)super.clone();
1077 result.qtype = (Type)result.qtype.clone();
1078 return result;
1079 }
1081 public boolean isErroneous() {
1082 return qtype.isErroneous();
1083 }
1085 /**
1086 * Replaces this ForAll's typevars with a set of concrete Java types
1087 * and returns the instantiated generic type. Subclasses should override
1088 * in order to check that the list of types is a valid instantiation
1089 * of the ForAll's typevars.
1090 *
1091 * @param actuals list of actual types
1092 * @param types types instance
1093 * @return qtype where all occurrences of tvars are replaced
1094 * by types in actuals
1095 */
1096 public Type inst(List<Type> actuals, Types types) {
1097 return types.subst(qtype, tvars, actuals);
1098 }
1100 /**
1101 * Kind of type-constraint derived during type inference
1102 */
1103 public enum ConstraintKind {
1104 /**
1105 * upper bound constraint (a type variable must be instantiated
1106 * with a type T, where T is a subtype of all the types specified by
1107 * its EXTENDS constraints).
1108 */
1109 EXTENDS,
1110 /**
1111 * lower bound constraint (a type variable must be instantiated
1112 * with a type T, where T is a supertype of all the types specified by
1113 * its SUPER constraints).
1114 */
1115 SUPER,
1116 /**
1117 * equality constraint (a type variable must be instantiated to the type
1118 * specified by its EQUAL constraint.
1119 */
1120 EQUAL;
1121 }
1123 /**
1124 * Get the type-constraints of a given kind for a given type-variable of
1125 * this ForAll type. Subclasses should override in order to return more
1126 * accurate sets of constraints.
1127 *
1128 * @param tv the type-variable for which the constraint is to be retrieved
1129 * @param ck the constraint kind to be retrieved
1130 * @return the list of types specified by the selected constraint
1131 */
1132 public List<Type> getConstraints(TypeVar tv, ConstraintKind ck) {
1133 return List.nil();
1134 }
1136 public Type map(Mapping f) {
1137 return f.apply(qtype);
1138 }
1140 public boolean contains(Type elem) {
1141 return qtype.contains(elem);
1142 }
1144 public MethodType asMethodType() {
1145 return qtype.asMethodType();
1146 }
1148 public void complete() {
1149 for (List<Type> l = tvars; l.nonEmpty(); l = l.tail) {
1150 ((TypeVar)l.head).bound.complete();
1151 }
1152 qtype.complete();
1153 }
1155 public List<TypeVar> getTypeVariables() {
1156 return List.convert(TypeVar.class, getTypeArguments());
1157 }
1159 public TypeKind getKind() {
1160 return TypeKind.EXECUTABLE;
1161 }
1163 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1164 return v.visitExecutable(this, p);
1165 }
1166 }
1168 /** A class for instantiatable variables, for use during type
1169 * inference.
1170 */
1171 public static class UndetVar extends DelegatedType {
1172 public List<Type> lobounds = List.nil();
1173 public List<Type> hibounds = List.nil();
1174 public Type inst = null;
1176 @Override
1177 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1178 return v.visitUndetVar(this, s);
1179 }
1181 public UndetVar(Type origin) {
1182 super(UNDETVAR, origin);
1183 }
1185 public String toString() {
1186 if (inst != null) return inst.toString();
1187 else return qtype + "?";
1188 }
1190 public Type baseType() {
1191 if (inst != null) return inst.baseType();
1192 else return this;
1193 }
1194 }
1196 /** Represents VOID or NONE.
1197 */
1198 static class JCNoType extends Type implements NoType {
1199 public JCNoType(int tag) {
1200 super(tag, null);
1201 }
1203 @Override
1204 public TypeKind getKind() {
1205 switch (tag) {
1206 case VOID: return TypeKind.VOID;
1207 case NONE: return TypeKind.NONE;
1208 default:
1209 throw new AssertionError("Unexpected tag: " + tag);
1210 }
1211 }
1213 @Override
1214 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1215 return v.visitNoType(this, p);
1216 }
1217 }
1219 static class BottomType extends Type implements NullType {
1220 public BottomType() {
1221 super(TypeTags.BOT, null);
1222 }
1224 @Override
1225 public TypeKind getKind() {
1226 return TypeKind.NULL;
1227 }
1229 @Override
1230 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1231 return v.visitNull(this, p);
1232 }
1234 @Override
1235 public Type constType(Object value) {
1236 return this;
1237 }
1239 @Override
1240 public String stringValue() {
1241 return "null";
1242 }
1243 }
1245 public static class ErrorType extends ClassType
1246 implements javax.lang.model.type.ErrorType {
1248 private Type originalType = null;
1250 public ErrorType(Type originalType, TypeSymbol tsym) {
1251 super(noType, List.<Type>nil(), null);
1252 tag = ERROR;
1253 this.tsym = tsym;
1254 this.originalType = (originalType == null ? noType : originalType);
1255 }
1257 public ErrorType(ClassSymbol c, Type originalType) {
1258 this(originalType, c);
1259 c.type = this;
1260 c.kind = ERR;
1261 c.members_field = new Scope.ErrorScope(c);
1262 }
1264 public ErrorType(Name name, TypeSymbol container, Type originalType) {
1265 this(new ClassSymbol(PUBLIC|STATIC|ACYCLIC, name, null, container), originalType);
1266 }
1268 @Override
1269 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1270 return v.visitErrorType(this, s);
1271 }
1273 public Type constType(Object constValue) { return this; }
1274 public Type getEnclosingType() { return this; }
1275 public Type getReturnType() { return this; }
1276 public Type asSub(Symbol sym) { return this; }
1277 public Type map(Mapping f) { return this; }
1279 public boolean isGenType(Type t) { return true; }
1280 public boolean isErroneous() { return true; }
1281 public boolean isCompound() { return false; }
1282 public boolean isInterface() { return false; }
1284 public List<Type> allparams() { return List.nil(); }
1285 public List<Type> getTypeArguments() { return List.nil(); }
1287 public TypeKind getKind() {
1288 return TypeKind.ERROR;
1289 }
1291 public Type getOriginalType() {
1292 return originalType;
1293 }
1295 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1296 return v.visitError(this, p);
1297 }
1298 }
1300 /**
1301 * A visitor for types. A visitor is used to implement operations
1302 * (or relations) on types. Most common operations on types are
1303 * binary relations and this interface is designed for binary
1304 * relations, that is, operations on the form
1305 * Type × S → R.
1306 * <!-- In plain text: Type x S -> R -->
1307 *
1308 * @param <R> the return type of the operation implemented by this
1309 * visitor; use Void if no return type is needed.
1310 * @param <S> the type of the second argument (the first being the
1311 * type itself) of the operation implemented by this visitor; use
1312 * Void if a second argument is not needed.
1313 */
1314 public interface Visitor<R,S> {
1315 R visitClassType(ClassType t, S s);
1316 R visitWildcardType(WildcardType t, S s);
1317 R visitArrayType(ArrayType t, S s);
1318 R visitMethodType(MethodType t, S s);
1319 R visitPackageType(PackageType t, S s);
1320 R visitTypeVar(TypeVar t, S s);
1321 R visitCapturedType(CapturedType t, S s);
1322 R visitForAll(ForAll t, S s);
1323 R visitUndetVar(UndetVar t, S s);
1324 R visitErrorType(ErrorType t, S s);
1325 R visitType(Type t, S s);
1326 }
1327 }