Wed, 21 Sep 2011 21:56:53 -0700
7092965: javac should not close processorClassLoader before end of compilation
Reviewed-by: darcy
1 /*
2 * Copyright (c) 1999, 2011, 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.util.Collections;
30 import com.sun.tools.javac.util.*;
31 import com.sun.tools.javac.code.Symbol.*;
33 import javax.lang.model.type.*;
35 import static com.sun.tools.javac.code.Flags.*;
36 import static com.sun.tools.javac.code.Kinds.*;
37 import static com.sun.tools.javac.code.BoundKind.*;
38 import static com.sun.tools.javac.code.TypeTags.*;
40 /** This class represents Java types. The class itself defines the behavior of
41 * the following types:
42 * <pre>
43 * base types (tags: BYTE, CHAR, SHORT, INT, LONG, FLOAT, DOUBLE, BOOLEAN),
44 * type `void' (tag: VOID),
45 * the bottom type (tag: BOT),
46 * the missing type (tag: NONE).
47 * </pre>
48 * <p>The behavior of the following types is defined in subclasses, which are
49 * all static inner classes of this class:
50 * <pre>
51 * class types (tag: CLASS, class: ClassType),
52 * array types (tag: ARRAY, class: ArrayType),
53 * method types (tag: METHOD, class: MethodType),
54 * package types (tag: PACKAGE, class: PackageType),
55 * type variables (tag: TYPEVAR, class: TypeVar),
56 * type arguments (tag: WILDCARD, class: WildcardType),
57 * polymorphic types (tag: FORALL, class: ForAll),
58 * the error type (tag: ERROR, class: ErrorType).
59 * </pre>
60 *
61 * <p><b>This is NOT part of any supported API.
62 * If you write code that depends on this, you do so at your own risk.
63 * This code and its internal interfaces are subject to change or
64 * deletion without notice.</b>
65 *
66 * @see TypeTags
67 */
68 public class Type implements PrimitiveType {
70 /** Constant type: no type at all. */
71 public static final JCNoType noType = new JCNoType(NONE);
73 /** If this switch is turned on, the names of type variables
74 * and anonymous classes are printed with hashcodes appended.
75 */
76 public static boolean moreInfo = false;
78 /** The tag of this type.
79 *
80 * @see TypeTags
81 */
82 public int tag;
84 /** The defining class / interface / package / type variable
85 */
86 public TypeSymbol tsym;
88 /**
89 * The constant value of this type, null if this type does not
90 * have a constant value attribute. Only primitive types and
91 * strings (ClassType) can have a constant value attribute.
92 * @return the constant value attribute of this type
93 */
94 public Object constValue() {
95 return null;
96 }
98 /**
99 * Get the representation of this type used for modelling purposes.
100 * By default, this is itself. For ErrorType, a different value
101 * may be provided,
102 */
103 public Type getModelType() {
104 return this;
105 }
107 public static List<Type> getModelTypes(List<Type> ts) {
108 ListBuffer<Type> lb = new ListBuffer<Type>();
109 for (Type t: ts)
110 lb.append(t.getModelType());
111 return lb.toList();
112 }
114 public <R,S> R accept(Type.Visitor<R,S> v, S s) { return v.visitType(this, s); }
116 /** Define a type given its tag and type symbol
117 */
118 public Type(int tag, TypeSymbol tsym) {
119 this.tag = tag;
120 this.tsym = tsym;
121 }
123 /** An abstract class for mappings from types to types
124 */
125 public static abstract class Mapping {
126 private String name;
127 public Mapping(String name) {
128 this.name = name;
129 }
130 public abstract Type apply(Type t);
131 public String toString() {
132 return name;
133 }
134 }
136 /** map a type function over all immediate descendants of this type
137 */
138 public Type map(Mapping f) {
139 return this;
140 }
142 /** map a type function over a list of types
143 */
144 public static List<Type> map(List<Type> ts, Mapping f) {
145 if (ts.nonEmpty()) {
146 List<Type> tail1 = map(ts.tail, f);
147 Type t = f.apply(ts.head);
148 if (tail1 != ts.tail || t != ts.head)
149 return tail1.prepend(t);
150 }
151 return ts;
152 }
154 /** Define a constant type, of the same kind as this type
155 * and with given constant value
156 */
157 public Type constType(Object constValue) {
158 final Object value = constValue;
159 Assert.check(tag <= BOOLEAN);
160 return new Type(tag, tsym) {
161 @Override
162 public Object constValue() {
163 return value;
164 }
165 @Override
166 public Type baseType() {
167 return tsym.type;
168 }
169 };
170 }
172 /**
173 * If this is a constant type, return its underlying type.
174 * Otherwise, return the type itself.
175 */
176 public Type baseType() {
177 return this;
178 }
180 /** Return the base types of a list of types.
181 */
182 public static List<Type> baseTypes(List<Type> ts) {
183 if (ts.nonEmpty()) {
184 Type t = ts.head.baseType();
185 List<Type> baseTypes = baseTypes(ts.tail);
186 if (t != ts.head || baseTypes != ts.tail)
187 return baseTypes.prepend(t);
188 }
189 return ts;
190 }
192 /** The Java source which this type represents.
193 */
194 public String toString() {
195 String s = (tsym == null || tsym.name == null)
196 ? "<none>"
197 : tsym.name.toString();
198 if (moreInfo && tag == TYPEVAR) s = s + hashCode();
199 return s;
200 }
202 /**
203 * The Java source which this type list represents. A List is
204 * represented as a comma-spearated listing of the elements in
205 * that list.
206 */
207 public static String toString(List<Type> ts) {
208 if (ts.isEmpty()) {
209 return "";
210 } else {
211 StringBuilder buf = new StringBuilder();
212 buf.append(ts.head.toString());
213 for (List<Type> l = ts.tail; l.nonEmpty(); l = l.tail)
214 buf.append(",").append(l.head.toString());
215 return buf.toString();
216 }
217 }
219 /**
220 * The constant value of this type, converted to String
221 */
222 public String stringValue() {
223 Object cv = Assert.checkNonNull(constValue());
224 if (tag == BOOLEAN)
225 return ((Integer) cv).intValue() == 0 ? "false" : "true";
226 else if (tag == CHAR)
227 return String.valueOf((char) ((Integer) cv).intValue());
228 else
229 return cv.toString();
230 }
232 /**
233 * This method is analogous to isSameType, but weaker, since we
234 * never complete classes. Where isSameType would complete a
235 * class, equals assumes that the two types are different.
236 */
237 public boolean equals(Object t) {
238 return super.equals(t);
239 }
241 public int hashCode() {
242 return super.hashCode();
243 }
245 /** Is this a constant type whose value is false?
246 */
247 public boolean isFalse() {
248 return
249 tag == BOOLEAN &&
250 constValue() != null &&
251 ((Integer)constValue()).intValue() == 0;
252 }
254 /** Is this a constant type whose value is true?
255 */
256 public boolean isTrue() {
257 return
258 tag == BOOLEAN &&
259 constValue() != null &&
260 ((Integer)constValue()).intValue() != 0;
261 }
263 public String argtypes(boolean varargs) {
264 List<Type> args = getParameterTypes();
265 if (!varargs) return args.toString();
266 StringBuilder buf = new StringBuilder();
267 while (args.tail.nonEmpty()) {
268 buf.append(args.head);
269 args = args.tail;
270 buf.append(',');
271 }
272 if (args.head.tag == ARRAY) {
273 buf.append(((ArrayType)args.head).elemtype);
274 buf.append("...");
275 } else {
276 buf.append(args.head);
277 }
278 return buf.toString();
279 }
281 /** Access methods.
282 */
283 public List<Type> getTypeArguments() { return List.nil(); }
284 public Type getEnclosingType() { return null; }
285 public List<Type> getParameterTypes() { return List.nil(); }
286 public Type getReturnType() { return null; }
287 public List<Type> getThrownTypes() { return List.nil(); }
288 public Type getUpperBound() { return null; }
289 public Type getLowerBound() { return null; }
291 /** Navigation methods, these will work for classes, type variables,
292 * foralls, but will return null for arrays and methods.
293 */
295 /** Return all parameters of this type and all its outer types in order
296 * outer (first) to inner (last).
297 */
298 public List<Type> allparams() { return List.nil(); }
300 /** Does this type contain "error" elements?
301 */
302 public boolean isErroneous() {
303 return false;
304 }
306 public static boolean isErroneous(List<Type> ts) {
307 for (List<Type> l = ts; l.nonEmpty(); l = l.tail)
308 if (l.head.isErroneous()) return true;
309 return false;
310 }
312 /** Is this type parameterized?
313 * A class type is parameterized if it has some parameters.
314 * An array type is parameterized if its element type is parameterized.
315 * All other types are not parameterized.
316 */
317 public boolean isParameterized() {
318 return false;
319 }
321 /** Is this type a raw type?
322 * A class type is a raw type if it misses some of its parameters.
323 * An array type is a raw type if its element type is raw.
324 * All other types are not raw.
325 * Type validation will ensure that the only raw types
326 * in a program are types that miss all their type variables.
327 */
328 public boolean isRaw() {
329 return false;
330 }
332 public boolean isCompound() {
333 return tsym.completer == null
334 // Compound types can't have a completer. Calling
335 // flags() will complete the symbol causing the
336 // compiler to load classes unnecessarily. This led
337 // to regression 6180021.
338 && (tsym.flags() & COMPOUND) != 0;
339 }
341 public boolean isInterface() {
342 return (tsym.flags() & INTERFACE) != 0;
343 }
345 public boolean isFinal() {
346 return (tsym.flags() & FINAL) != 0;
347 }
349 public boolean isPrimitive() {
350 return tag < VOID;
351 }
353 /**
354 * Does this type contain occurrences of type t?
355 */
356 public boolean contains(Type t) {
357 return t == this;
358 }
360 public static boolean contains(List<Type> ts, Type t) {
361 for (List<Type> l = ts;
362 l.tail != null /*inlined: l.nonEmpty()*/;
363 l = l.tail)
364 if (l.head.contains(t)) return true;
365 return false;
366 }
368 /** Does this type contain an occurrence of some type in 'ts'?
369 */
370 public boolean containsAny(List<Type> ts) {
371 for (Type t : ts)
372 if (this.contains(t)) return true;
373 return false;
374 }
376 public static boolean containsAny(List<Type> ts1, List<Type> ts2) {
377 for (Type t : ts1)
378 if (t.containsAny(ts2)) return true;
379 return false;
380 }
382 public static List<Type> filter(List<Type> ts, Filter<Type> tf) {
383 ListBuffer<Type> buf = ListBuffer.lb();
384 for (Type t : ts) {
385 if (tf.accepts(t)) {
386 buf.append(t);
387 }
388 }
389 return buf.toList();
390 }
392 public boolean isSuperBound() { return false; }
393 public boolean isExtendsBound() { return false; }
394 public boolean isUnbound() { return false; }
395 public Type withTypeVar(Type t) { return this; }
397 /** The underlying method type of this type.
398 */
399 public MethodType asMethodType() { throw new AssertionError(); }
401 /** Complete loading all classes in this type.
402 */
403 public void complete() {}
405 public TypeSymbol asElement() {
406 return tsym;
407 }
409 public TypeKind getKind() {
410 switch (tag) {
411 case BYTE: return TypeKind.BYTE;
412 case CHAR: return TypeKind.CHAR;
413 case SHORT: return TypeKind.SHORT;
414 case INT: return TypeKind.INT;
415 case LONG: return TypeKind.LONG;
416 case FLOAT: return TypeKind.FLOAT;
417 case DOUBLE: return TypeKind.DOUBLE;
418 case BOOLEAN: return TypeKind.BOOLEAN;
419 case VOID: return TypeKind.VOID;
420 case BOT: return TypeKind.NULL;
421 case NONE: return TypeKind.NONE;
422 default: return TypeKind.OTHER;
423 }
424 }
426 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
427 if (isPrimitive())
428 return v.visitPrimitive(this, p);
429 else
430 throw new AssertionError();
431 }
433 public static class WildcardType extends Type
434 implements javax.lang.model.type.WildcardType {
436 public Type type;
437 public BoundKind kind;
438 public TypeVar bound;
440 @Override
441 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
442 return v.visitWildcardType(this, s);
443 }
445 public WildcardType(Type type, BoundKind kind, TypeSymbol tsym) {
446 super(WILDCARD, tsym);
447 this.type = Assert.checkNonNull(type);
448 this.kind = kind;
449 }
450 public WildcardType(WildcardType t, TypeVar bound) {
451 this(t.type, t.kind, t.tsym, bound);
452 }
454 public WildcardType(Type type, BoundKind kind, TypeSymbol tsym, TypeVar bound) {
455 this(type, kind, tsym);
456 this.bound = bound;
457 }
459 public boolean contains(Type t) {
460 return kind != UNBOUND && type.contains(t);
461 }
463 public boolean isSuperBound() {
464 return kind == SUPER ||
465 kind == UNBOUND;
466 }
467 public boolean isExtendsBound() {
468 return kind == EXTENDS ||
469 kind == UNBOUND;
470 }
471 public boolean isUnbound() {
472 return kind == UNBOUND;
473 }
475 public Type withTypeVar(Type t) {
476 //-System.err.println(this+".withTypeVar("+t+");");//DEBUG
477 if (bound == t)
478 return this;
479 bound = (TypeVar)t;
480 return this;
481 }
483 boolean isPrintingBound = false;
484 public String toString() {
485 StringBuilder s = new StringBuilder();
486 s.append(kind.toString());
487 if (kind != UNBOUND)
488 s.append(type);
489 if (moreInfo && bound != null && !isPrintingBound)
490 try {
491 isPrintingBound = true;
492 s.append("{:").append(bound.bound).append(":}");
493 } finally {
494 isPrintingBound = false;
495 }
496 return s.toString();
497 }
499 public Type map(Mapping f) {
500 //- System.err.println(" (" + this + ").map(" + f + ")");//DEBUG
501 Type t = type;
502 if (t != null)
503 t = f.apply(t);
504 if (t == type)
505 return this;
506 else
507 return new WildcardType(t, kind, tsym, bound);
508 }
510 public Type getExtendsBound() {
511 if (kind == EXTENDS)
512 return type;
513 else
514 return null;
515 }
517 public Type getSuperBound() {
518 if (kind == SUPER)
519 return type;
520 else
521 return null;
522 }
524 public TypeKind getKind() {
525 return TypeKind.WILDCARD;
526 }
528 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
529 return v.visitWildcard(this, p);
530 }
531 }
533 public static class ClassType extends Type implements DeclaredType {
535 /** The enclosing type of this type. If this is the type of an inner
536 * class, outer_field refers to the type of its enclosing
537 * instance class, in all other cases it referes to noType.
538 */
539 private Type outer_field;
541 /** The type parameters of this type (to be set once class is loaded).
542 */
543 public List<Type> typarams_field;
545 /** A cache variable for the type parameters of this type,
546 * appended to all parameters of its enclosing class.
547 * @see #allparams
548 */
549 public List<Type> allparams_field;
551 /** The supertype of this class (to be set once class is loaded).
552 */
553 public Type supertype_field;
555 /** The interfaces of this class (to be set once class is loaded).
556 */
557 public List<Type> interfaces_field;
559 /** All the interfaces of this class, including missing ones.
560 */
561 public List<Type> all_interfaces_field;
563 public ClassType(Type outer, List<Type> typarams, TypeSymbol tsym) {
564 super(CLASS, tsym);
565 this.outer_field = outer;
566 this.typarams_field = typarams;
567 this.allparams_field = null;
568 this.supertype_field = null;
569 this.interfaces_field = null;
570 /*
571 // this can happen during error recovery
572 assert
573 outer.isParameterized() ?
574 typarams.length() == tsym.type.typarams().length() :
575 outer.isRaw() ?
576 typarams.length() == 0 :
577 true;
578 */
579 }
581 @Override
582 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
583 return v.visitClassType(this, s);
584 }
586 public Type constType(Object constValue) {
587 final Object value = constValue;
588 return new ClassType(getEnclosingType(), typarams_field, tsym) {
589 @Override
590 public Object constValue() {
591 return value;
592 }
593 @Override
594 public Type baseType() {
595 return tsym.type;
596 }
597 };
598 }
600 /** The Java source which this type represents.
601 */
602 public String toString() {
603 StringBuilder buf = new StringBuilder();
604 if (getEnclosingType().tag == CLASS && tsym.owner.kind == TYP) {
605 buf.append(getEnclosingType().toString());
606 buf.append(".");
607 buf.append(className(tsym, false));
608 } else {
609 buf.append(className(tsym, true));
610 }
611 if (getTypeArguments().nonEmpty()) {
612 buf.append('<');
613 buf.append(getTypeArguments().toString());
614 buf.append(">");
615 }
616 return buf.toString();
617 }
618 //where
619 private String className(Symbol sym, boolean longform) {
620 if (sym.name.isEmpty() && (sym.flags() & COMPOUND) != 0) {
621 StringBuilder s = new StringBuilder(supertype_field.toString());
622 for (List<Type> is=interfaces_field; is.nonEmpty(); is = is.tail) {
623 s.append("&");
624 s.append(is.head.toString());
625 }
626 return s.toString();
627 } else if (sym.name.isEmpty()) {
628 String s;
629 ClassType norm = (ClassType) tsym.type;
630 if (norm == null) {
631 s = Log.getLocalizedString("anonymous.class", (Object)null);
632 } else if (norm.interfaces_field != null && norm.interfaces_field.nonEmpty()) {
633 s = Log.getLocalizedString("anonymous.class",
634 norm.interfaces_field.head);
635 } else {
636 s = Log.getLocalizedString("anonymous.class",
637 norm.supertype_field);
638 }
639 if (moreInfo)
640 s += String.valueOf(sym.hashCode());
641 return s;
642 } else if (longform) {
643 return sym.getQualifiedName().toString();
644 } else {
645 return sym.name.toString();
646 }
647 }
649 public List<Type> getTypeArguments() {
650 if (typarams_field == null) {
651 complete();
652 if (typarams_field == null)
653 typarams_field = List.nil();
654 }
655 return typarams_field;
656 }
658 public boolean hasErasedSupertypes() {
659 return isRaw();
660 }
662 public Type getEnclosingType() {
663 return outer_field;
664 }
666 public void setEnclosingType(Type outer) {
667 outer_field = outer;
668 }
670 public List<Type> allparams() {
671 if (allparams_field == null) {
672 allparams_field = getTypeArguments().prependList(getEnclosingType().allparams());
673 }
674 return allparams_field;
675 }
677 public boolean isErroneous() {
678 return
679 getEnclosingType().isErroneous() ||
680 isErroneous(getTypeArguments()) ||
681 this != tsym.type && tsym.type.isErroneous();
682 }
684 public boolean isParameterized() {
685 return allparams().tail != null;
686 // optimization, was: allparams().nonEmpty();
687 }
689 /** A cache for the rank. */
690 int rank_field = -1;
692 /** A class type is raw if it misses some
693 * of its type parameter sections.
694 * After validation, this is equivalent to:
695 * allparams.isEmpty() && tsym.type.allparams.nonEmpty();
696 */
697 public boolean isRaw() {
698 return
699 this != tsym.type && // necessary, but not sufficient condition
700 tsym.type.allparams().nonEmpty() &&
701 allparams().isEmpty();
702 }
704 public Type map(Mapping f) {
705 Type outer = getEnclosingType();
706 Type outer1 = f.apply(outer);
707 List<Type> typarams = getTypeArguments();
708 List<Type> typarams1 = map(typarams, f);
709 if (outer1 == outer && typarams1 == typarams) return this;
710 else return new ClassType(outer1, typarams1, tsym);
711 }
713 public boolean contains(Type elem) {
714 return
715 elem == this
716 || (isParameterized()
717 && (getEnclosingType().contains(elem) || contains(getTypeArguments(), elem)))
718 || (isCompound()
719 && (supertype_field.contains(elem) || contains(interfaces_field, elem)));
720 }
722 public void complete() {
723 if (tsym.completer != null) tsym.complete();
724 }
726 public TypeKind getKind() {
727 return TypeKind.DECLARED;
728 }
730 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
731 return v.visitDeclared(this, p);
732 }
733 }
735 public static class ErasedClassType extends ClassType {
736 public ErasedClassType(Type outer, TypeSymbol tsym) {
737 super(outer, List.<Type>nil(), tsym);
738 }
740 @Override
741 public boolean hasErasedSupertypes() {
742 return true;
743 }
744 }
746 // a clone of a ClassType that knows about the alternatives of a union type.
747 public static class UnionClassType extends ClassType implements UnionType {
748 final List<? extends Type> alternatives_field;
750 public UnionClassType(ClassType ct, List<? extends Type> alternatives) {
751 super(ct.outer_field, ct.typarams_field, ct.tsym);
752 allparams_field = ct.allparams_field;
753 supertype_field = ct.supertype_field;
754 interfaces_field = ct.interfaces_field;
755 all_interfaces_field = ct.interfaces_field;
756 alternatives_field = alternatives;
757 }
759 public Type getLub() {
760 return tsym.type;
761 }
763 public java.util.List<? extends TypeMirror> getAlternatives() {
764 return Collections.unmodifiableList(alternatives_field);
765 }
767 @Override
768 public TypeKind getKind() {
769 return TypeKind.UNION;
770 }
772 @Override
773 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
774 return v.visitUnion(this, p);
775 }
776 }
778 public static class ArrayType extends Type
779 implements javax.lang.model.type.ArrayType {
781 public Type elemtype;
783 public ArrayType(Type elemtype, TypeSymbol arrayClass) {
784 super(ARRAY, arrayClass);
785 this.elemtype = elemtype;
786 }
788 @Override
789 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
790 return v.visitArrayType(this, s);
791 }
793 public String toString() {
794 return elemtype + "[]";
795 }
797 public boolean equals(Object obj) {
798 return
799 this == obj ||
800 (obj instanceof ArrayType &&
801 this.elemtype.equals(((ArrayType)obj).elemtype));
802 }
804 public int hashCode() {
805 return (ARRAY << 5) + elemtype.hashCode();
806 }
808 public boolean isVarargs() {
809 return false;
810 }
812 public List<Type> allparams() { return elemtype.allparams(); }
814 public boolean isErroneous() {
815 return elemtype.isErroneous();
816 }
818 public boolean isParameterized() {
819 return elemtype.isParameterized();
820 }
822 public boolean isRaw() {
823 return elemtype.isRaw();
824 }
826 public ArrayType makeVarargs() {
827 return new ArrayType(elemtype, tsym) {
828 @Override
829 public boolean isVarargs() {
830 return true;
831 }
832 };
833 }
835 public Type map(Mapping f) {
836 Type elemtype1 = f.apply(elemtype);
837 if (elemtype1 == elemtype) return this;
838 else return new ArrayType(elemtype1, tsym);
839 }
841 public boolean contains(Type elem) {
842 return elem == this || elemtype.contains(elem);
843 }
845 public void complete() {
846 elemtype.complete();
847 }
849 public Type getComponentType() {
850 return elemtype;
851 }
853 public TypeKind getKind() {
854 return TypeKind.ARRAY;
855 }
857 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
858 return v.visitArray(this, p);
859 }
860 }
862 public static class MethodType extends Type implements ExecutableType {
864 public List<Type> argtypes;
865 public Type restype;
866 public List<Type> thrown;
868 public MethodType(List<Type> argtypes,
869 Type restype,
870 List<Type> thrown,
871 TypeSymbol methodClass) {
872 super(METHOD, methodClass);
873 this.argtypes = argtypes;
874 this.restype = restype;
875 this.thrown = thrown;
876 }
878 @Override
879 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
880 return v.visitMethodType(this, s);
881 }
883 /** The Java source which this type represents.
884 *
885 * XXX 06/09/99 iris This isn't correct Java syntax, but it probably
886 * should be.
887 */
888 public String toString() {
889 return "(" + argtypes + ")" + restype;
890 }
892 public boolean equals(Object obj) {
893 if (this == obj)
894 return true;
895 if (!(obj instanceof MethodType))
896 return false;
897 MethodType m = (MethodType)obj;
898 List<Type> args1 = argtypes;
899 List<Type> args2 = m.argtypes;
900 while (!args1.isEmpty() && !args2.isEmpty()) {
901 if (!args1.head.equals(args2.head))
902 return false;
903 args1 = args1.tail;
904 args2 = args2.tail;
905 }
906 if (!args1.isEmpty() || !args2.isEmpty())
907 return false;
908 return restype.equals(m.restype);
909 }
911 public int hashCode() {
912 int h = METHOD;
913 for (List<Type> thisargs = this.argtypes;
914 thisargs.tail != null; /*inlined: thisargs.nonEmpty()*/
915 thisargs = thisargs.tail)
916 h = (h << 5) + thisargs.head.hashCode();
917 return (h << 5) + this.restype.hashCode();
918 }
920 public List<Type> getParameterTypes() { return argtypes; }
921 public Type getReturnType() { return restype; }
922 public List<Type> getThrownTypes() { return thrown; }
924 public boolean isErroneous() {
925 return
926 isErroneous(argtypes) ||
927 restype != null && restype.isErroneous();
928 }
930 public Type map(Mapping f) {
931 List<Type> argtypes1 = map(argtypes, f);
932 Type restype1 = f.apply(restype);
933 List<Type> thrown1 = map(thrown, f);
934 if (argtypes1 == argtypes &&
935 restype1 == restype &&
936 thrown1 == thrown) return this;
937 else return new MethodType(argtypes1, restype1, thrown1, tsym);
938 }
940 public boolean contains(Type elem) {
941 return elem == this || contains(argtypes, elem) || restype.contains(elem);
942 }
944 public MethodType asMethodType() { return this; }
946 public void complete() {
947 for (List<Type> l = argtypes; l.nonEmpty(); l = l.tail)
948 l.head.complete();
949 restype.complete();
950 for (List<Type> l = thrown; l.nonEmpty(); l = l.tail)
951 l.head.complete();
952 }
954 public List<TypeVar> getTypeVariables() {
955 return List.nil();
956 }
958 public TypeSymbol asElement() {
959 return null;
960 }
962 public TypeKind getKind() {
963 return TypeKind.EXECUTABLE;
964 }
966 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
967 return v.visitExecutable(this, p);
968 }
969 }
971 public static class PackageType extends Type implements NoType {
973 PackageType(TypeSymbol tsym) {
974 super(PACKAGE, tsym);
975 }
977 @Override
978 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
979 return v.visitPackageType(this, s);
980 }
982 public String toString() {
983 return tsym.getQualifiedName().toString();
984 }
986 public TypeKind getKind() {
987 return TypeKind.PACKAGE;
988 }
990 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
991 return v.visitNoType(this, p);
992 }
993 }
995 public static class TypeVar extends Type implements TypeVariable {
997 /** The upper bound of this type variable; set from outside.
998 * Must be nonempty once it is set.
999 * For a bound, `bound' is the bound type itself.
1000 * Multiple bounds are expressed as a single class type which has the
1001 * individual bounds as superclass, respectively interfaces.
1002 * The class type then has as `tsym' a compiler generated class `c',
1003 * which has a flag COMPOUND and whose owner is the type variable
1004 * itself. Furthermore, the erasure_field of the class
1005 * points to the first class or interface bound.
1006 */
1007 public Type bound = null;
1009 /** The lower bound of this type variable.
1010 * TypeVars don't normally have a lower bound, so it is normally set
1011 * to syms.botType.
1012 * Subtypes, such as CapturedType, may provide a different value.
1013 */
1014 public Type lower;
1016 public TypeVar(Name name, Symbol owner, Type lower) {
1017 super(TYPEVAR, null);
1018 tsym = new TypeSymbol(0, name, this, owner);
1019 this.lower = lower;
1020 }
1022 public TypeVar(TypeSymbol tsym, Type bound, Type lower) {
1023 super(TYPEVAR, tsym);
1024 this.bound = bound;
1025 this.lower = lower;
1026 }
1028 @Override
1029 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1030 return v.visitTypeVar(this, s);
1031 }
1033 @Override
1034 public Type getUpperBound() { return bound; }
1036 int rank_field = -1;
1038 @Override
1039 public Type getLowerBound() {
1040 return lower;
1041 }
1043 public TypeKind getKind() {
1044 return TypeKind.TYPEVAR;
1045 }
1047 public boolean isCaptured() {
1048 return false;
1049 }
1051 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1052 return v.visitTypeVariable(this, p);
1053 }
1054 }
1056 /** A captured type variable comes from wildcards which can have
1057 * both upper and lower bound. CapturedType extends TypeVar with
1058 * a lower bound.
1059 */
1060 public static class CapturedType extends TypeVar {
1062 public WildcardType wildcard;
1064 public CapturedType(Name name,
1065 Symbol owner,
1066 Type upper,
1067 Type lower,
1068 WildcardType wildcard) {
1069 super(name, owner, lower);
1070 this.lower = Assert.checkNonNull(lower);
1071 this.bound = upper;
1072 this.wildcard = wildcard;
1073 }
1075 @Override
1076 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1077 return v.visitCapturedType(this, s);
1078 }
1080 @Override
1081 public boolean isCaptured() {
1082 return true;
1083 }
1085 @Override
1086 public String toString() {
1087 return "capture#"
1088 + (hashCode() & 0xFFFFFFFFL) % Printer.PRIME
1089 + " of "
1090 + wildcard;
1091 }
1092 }
1094 public static abstract class DelegatedType extends Type {
1095 public Type qtype;
1096 public DelegatedType(int tag, Type qtype) {
1097 super(tag, qtype.tsym);
1098 this.qtype = qtype;
1099 }
1100 public String toString() { return qtype.toString(); }
1101 public List<Type> getTypeArguments() { return qtype.getTypeArguments(); }
1102 public Type getEnclosingType() { return qtype.getEnclosingType(); }
1103 public List<Type> getParameterTypes() { return qtype.getParameterTypes(); }
1104 public Type getReturnType() { return qtype.getReturnType(); }
1105 public List<Type> getThrownTypes() { return qtype.getThrownTypes(); }
1106 public List<Type> allparams() { return qtype.allparams(); }
1107 public Type getUpperBound() { return qtype.getUpperBound(); }
1108 public boolean isErroneous() { return qtype.isErroneous(); }
1109 }
1111 public static class ForAll extends DelegatedType implements ExecutableType {
1112 public List<Type> tvars;
1114 public ForAll(List<Type> tvars, Type qtype) {
1115 super(FORALL, qtype);
1116 this.tvars = tvars;
1117 }
1119 @Override
1120 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1121 return v.visitForAll(this, s);
1122 }
1124 public String toString() {
1125 return "<" + tvars + ">" + qtype;
1126 }
1128 public List<Type> getTypeArguments() { return tvars; }
1130 public boolean isErroneous() {
1131 return qtype.isErroneous();
1132 }
1134 /**
1135 * Replaces this ForAll's typevars with a set of concrete Java types
1136 * and returns the instantiated generic type. Subclasses should override
1137 * in order to check that the list of types is a valid instantiation
1138 * of the ForAll's typevars.
1139 *
1140 * @param actuals list of actual types
1141 * @param types types instance
1142 * @return qtype where all occurrences of tvars are replaced
1143 * by types in actuals
1144 */
1145 public Type inst(List<Type> actuals, Types types) {
1146 return types.subst(qtype, tvars, actuals);
1147 }
1149 /**
1150 * Kind of type-constraint derived during type inference
1151 */
1152 public enum ConstraintKind {
1153 /**
1154 * upper bound constraint (a type variable must be instantiated
1155 * with a type T, where T is a subtype of all the types specified by
1156 * its EXTENDS constraints).
1157 */
1158 EXTENDS,
1159 /**
1160 * lower bound constraint (a type variable must be instantiated
1161 * with a type T, where T is a supertype of all the types specified by
1162 * its SUPER constraints).
1163 */
1164 SUPER,
1165 /**
1166 * equality constraint (a type variable must be instantiated to the type
1167 * specified by its EQUAL constraint.
1168 */
1169 EQUAL;
1170 }
1172 /**
1173 * Get the type-constraints of a given kind for a given type-variable of
1174 * this ForAll type. Subclasses should override in order to return more
1175 * accurate sets of constraints.
1176 *
1177 * @param tv the type-variable for which the constraint is to be retrieved
1178 * @param ck the constraint kind to be retrieved
1179 * @return the list of types specified by the selected constraint
1180 */
1181 public List<Type> getConstraints(TypeVar tv, ConstraintKind ck) {
1182 return List.nil();
1183 }
1185 public Type map(Mapping f) {
1186 return f.apply(qtype);
1187 }
1189 public boolean contains(Type elem) {
1190 return qtype.contains(elem);
1191 }
1193 public MethodType asMethodType() {
1194 return qtype.asMethodType();
1195 }
1197 public void complete() {
1198 for (List<Type> l = tvars; l.nonEmpty(); l = l.tail) {
1199 ((TypeVar)l.head).bound.complete();
1200 }
1201 qtype.complete();
1202 }
1204 public List<TypeVar> getTypeVariables() {
1205 return List.convert(TypeVar.class, getTypeArguments());
1206 }
1208 public TypeKind getKind() {
1209 return TypeKind.EXECUTABLE;
1210 }
1212 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1213 return v.visitExecutable(this, p);
1214 }
1215 }
1217 /** A class for instantiatable variables, for use during type
1218 * inference.
1219 */
1220 public static class UndetVar extends DelegatedType {
1221 public List<Type> lobounds = List.nil();
1222 public List<Type> hibounds = List.nil();
1223 public Type inst = null;
1225 @Override
1226 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1227 return v.visitUndetVar(this, s);
1228 }
1230 public UndetVar(Type origin) {
1231 super(UNDETVAR, origin);
1232 }
1234 public String toString() {
1235 if (inst != null) return inst.toString();
1236 else return qtype + "?";
1237 }
1239 public Type baseType() {
1240 if (inst != null) return inst.baseType();
1241 else return this;
1242 }
1243 }
1245 /** Represents VOID or NONE.
1246 */
1247 static class JCNoType extends Type implements NoType {
1248 public JCNoType(int tag) {
1249 super(tag, null);
1250 }
1252 @Override
1253 public TypeKind getKind() {
1254 switch (tag) {
1255 case VOID: return TypeKind.VOID;
1256 case NONE: return TypeKind.NONE;
1257 default:
1258 throw new AssertionError("Unexpected tag: " + tag);
1259 }
1260 }
1262 @Override
1263 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1264 return v.visitNoType(this, p);
1265 }
1266 }
1268 static class BottomType extends Type implements NullType {
1269 public BottomType() {
1270 super(TypeTags.BOT, null);
1271 }
1273 @Override
1274 public TypeKind getKind() {
1275 return TypeKind.NULL;
1276 }
1278 @Override
1279 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1280 return v.visitNull(this, p);
1281 }
1283 @Override
1284 public Type constType(Object value) {
1285 return this;
1286 }
1288 @Override
1289 public String stringValue() {
1290 return "null";
1291 }
1292 }
1294 public static class ErrorType extends ClassType
1295 implements javax.lang.model.type.ErrorType {
1297 private Type originalType = null;
1299 public ErrorType(Type originalType, TypeSymbol tsym) {
1300 super(noType, List.<Type>nil(), null);
1301 tag = ERROR;
1302 this.tsym = tsym;
1303 this.originalType = (originalType == null ? noType : originalType);
1304 }
1306 public ErrorType(ClassSymbol c, Type originalType) {
1307 this(originalType, c);
1308 c.type = this;
1309 c.kind = ERR;
1310 c.members_field = new Scope.ErrorScope(c);
1311 }
1313 public ErrorType(Name name, TypeSymbol container, Type originalType) {
1314 this(new ClassSymbol(PUBLIC|STATIC|ACYCLIC, name, null, container), originalType);
1315 }
1317 @Override
1318 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1319 return v.visitErrorType(this, s);
1320 }
1322 public Type constType(Object constValue) { return this; }
1323 public Type getEnclosingType() { return this; }
1324 public Type getReturnType() { return this; }
1325 public Type asSub(Symbol sym) { return this; }
1326 public Type map(Mapping f) { return this; }
1328 public boolean isGenType(Type t) { return true; }
1329 public boolean isErroneous() { return true; }
1330 public boolean isCompound() { return false; }
1331 public boolean isInterface() { return false; }
1333 public List<Type> allparams() { return List.nil(); }
1334 public List<Type> getTypeArguments() { return List.nil(); }
1336 public TypeKind getKind() {
1337 return TypeKind.ERROR;
1338 }
1340 public Type getOriginalType() {
1341 return originalType;
1342 }
1344 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1345 return v.visitError(this, p);
1346 }
1347 }
1349 /**
1350 * A visitor for types. A visitor is used to implement operations
1351 * (or relations) on types. Most common operations on types are
1352 * binary relations and this interface is designed for binary
1353 * relations, that is, operations on the form
1354 * Type × S → R.
1355 * <!-- In plain text: Type x S -> R -->
1356 *
1357 * @param <R> the return type of the operation implemented by this
1358 * visitor; use Void if no return type is needed.
1359 * @param <S> the type of the second argument (the first being the
1360 * type itself) of the operation implemented by this visitor; use
1361 * Void if a second argument is not needed.
1362 */
1363 public interface Visitor<R,S> {
1364 R visitClassType(ClassType t, S s);
1365 R visitWildcardType(WildcardType t, S s);
1366 R visitArrayType(ArrayType t, S s);
1367 R visitMethodType(MethodType t, S s);
1368 R visitPackageType(PackageType t, S s);
1369 R visitTypeVar(TypeVar t, S s);
1370 R visitCapturedType(CapturedType t, S s);
1371 R visitForAll(ForAll t, S s);
1372 R visitUndetVar(UndetVar t, S s);
1373 R visitErrorType(ErrorType t, S s);
1374 R visitType(Type t, S s);
1375 }
1376 }