Tue, 25 Sep 2012 11:56:46 +0100
7177306: Regression: unchecked method call does not erase return type
Summary: Spurious extra call to Attr.checkMethod when method call is unchecked
Reviewed-by: jjg, dlsmith
1 /*
2 * Copyright (c) 1999, 2012, 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 java.util.EnumMap;
34 import java.util.EnumSet;
35 import java.util.Map;
36 import java.util.Set;
37 import javax.lang.model.type.*;
39 import static com.sun.tools.javac.code.Flags.*;
40 import static com.sun.tools.javac.code.Kinds.*;
41 import static com.sun.tools.javac.code.BoundKind.*;
42 import static com.sun.tools.javac.code.TypeTags.*;
44 /** This class represents Java types. The class itself defines the behavior of
45 * the following types:
46 * <pre>
47 * base types (tags: BYTE, CHAR, SHORT, INT, LONG, FLOAT, DOUBLE, BOOLEAN),
48 * type `void' (tag: VOID),
49 * the bottom type (tag: BOT),
50 * the missing type (tag: NONE).
51 * </pre>
52 * <p>The behavior of the following types is defined in subclasses, which are
53 * all static inner classes of this class:
54 * <pre>
55 * class types (tag: CLASS, class: ClassType),
56 * array types (tag: ARRAY, class: ArrayType),
57 * method types (tag: METHOD, class: MethodType),
58 * package types (tag: PACKAGE, class: PackageType),
59 * type variables (tag: TYPEVAR, class: TypeVar),
60 * type arguments (tag: WILDCARD, class: WildcardType),
61 * generic method types (tag: FORALL, class: ForAll),
62 * the error type (tag: ERROR, class: ErrorType).
63 * </pre>
64 *
65 * <p><b>This is NOT part of any supported API.
66 * If you write code that depends on this, you do so at your own risk.
67 * This code and its internal interfaces are subject to change or
68 * deletion without notice.</b>
69 *
70 * @see TypeTags
71 */
72 public class Type implements PrimitiveType {
74 /** Constant type: no type at all. */
75 public static final JCNoType noType = new JCNoType(NONE);
77 /** If this switch is turned on, the names of type variables
78 * and anonymous classes are printed with hashcodes appended.
79 */
80 public static boolean moreInfo = false;
82 /** The tag of this type.
83 *
84 * @see TypeTags
85 */
86 public int tag;
88 /** The defining class / interface / package / type variable
89 */
90 public TypeSymbol tsym;
92 /**
93 * The constant value of this type, null if this type does not
94 * have a constant value attribute. Only primitive types and
95 * strings (ClassType) can have a constant value attribute.
96 * @return the constant value attribute of this type
97 */
98 public Object constValue() {
99 return null;
100 }
102 /**
103 * Get the representation of this type used for modelling purposes.
104 * By default, this is itself. For ErrorType, a different value
105 * may be provided,
106 */
107 public Type getModelType() {
108 return this;
109 }
111 public static List<Type> getModelTypes(List<Type> ts) {
112 ListBuffer<Type> lb = new ListBuffer<Type>();
113 for (Type t: ts)
114 lb.append(t.getModelType());
115 return lb.toList();
116 }
118 public <R,S> R accept(Type.Visitor<R,S> v, S s) { return v.visitType(this, s); }
120 /** Define a type given its tag and type symbol
121 */
122 public Type(int tag, TypeSymbol tsym) {
123 this.tag = tag;
124 this.tsym = tsym;
125 }
127 /** An abstract class for mappings from types to types
128 */
129 public static abstract class Mapping {
130 private String name;
131 public Mapping(String name) {
132 this.name = name;
133 }
134 public abstract Type apply(Type t);
135 public String toString() {
136 return name;
137 }
138 }
140 /** map a type function over all immediate descendants of this type
141 */
142 public Type map(Mapping f) {
143 return this;
144 }
146 /** map a type function over a list of types
147 */
148 public static List<Type> map(List<Type> ts, Mapping f) {
149 if (ts.nonEmpty()) {
150 List<Type> tail1 = map(ts.tail, f);
151 Type t = f.apply(ts.head);
152 if (tail1 != ts.tail || t != ts.head)
153 return tail1.prepend(t);
154 }
155 return ts;
156 }
158 /** Define a constant type, of the same kind as this type
159 * and with given constant value
160 */
161 public Type constType(Object constValue) {
162 final Object value = constValue;
163 Assert.check(tag <= BOOLEAN);
164 return new Type(tag, tsym) {
165 @Override
166 public Object constValue() {
167 return value;
168 }
169 @Override
170 public Type baseType() {
171 return tsym.type;
172 }
173 };
174 }
176 /**
177 * If this is a constant type, return its underlying type.
178 * Otherwise, return the type itself.
179 */
180 public Type baseType() {
181 return this;
182 }
184 /** Return the base types of a list of types.
185 */
186 public static List<Type> baseTypes(List<Type> ts) {
187 if (ts.nonEmpty()) {
188 Type t = ts.head.baseType();
189 List<Type> baseTypes = baseTypes(ts.tail);
190 if (t != ts.head || baseTypes != ts.tail)
191 return baseTypes.prepend(t);
192 }
193 return ts;
194 }
196 /** The Java source which this type represents.
197 */
198 public String toString() {
199 String s = (tsym == null || tsym.name == null)
200 ? "<none>"
201 : tsym.name.toString();
202 if (moreInfo && tag == TYPEVAR) s = s + hashCode();
203 return s;
204 }
206 /**
207 * The Java source which this type list represents. A List is
208 * represented as a comma-spearated listing of the elements in
209 * that list.
210 */
211 public static String toString(List<Type> ts) {
212 if (ts.isEmpty()) {
213 return "";
214 } else {
215 StringBuilder buf = new StringBuilder();
216 buf.append(ts.head.toString());
217 for (List<Type> l = ts.tail; l.nonEmpty(); l = l.tail)
218 buf.append(",").append(l.head.toString());
219 return buf.toString();
220 }
221 }
223 /**
224 * The constant value of this type, converted to String
225 */
226 public String stringValue() {
227 Object cv = Assert.checkNonNull(constValue());
228 if (tag == BOOLEAN)
229 return ((Integer) cv).intValue() == 0 ? "false" : "true";
230 else if (tag == CHAR)
231 return String.valueOf((char) ((Integer) cv).intValue());
232 else
233 return cv.toString();
234 }
236 /**
237 * This method is analogous to isSameType, but weaker, since we
238 * never complete classes. Where isSameType would complete a
239 * class, equals assumes that the two types are different.
240 */
241 public boolean equals(Object t) {
242 return super.equals(t);
243 }
245 public int hashCode() {
246 return super.hashCode();
247 }
249 /** Is this a constant type whose value is false?
250 */
251 public boolean isFalse() {
252 return
253 tag == BOOLEAN &&
254 constValue() != null &&
255 ((Integer)constValue()).intValue() == 0;
256 }
258 /** Is this a constant type whose value is true?
259 */
260 public boolean isTrue() {
261 return
262 tag == BOOLEAN &&
263 constValue() != null &&
264 ((Integer)constValue()).intValue() != 0;
265 }
267 public String argtypes(boolean varargs) {
268 List<Type> args = getParameterTypes();
269 if (!varargs) return args.toString();
270 StringBuilder buf = new StringBuilder();
271 while (args.tail.nonEmpty()) {
272 buf.append(args.head);
273 args = args.tail;
274 buf.append(',');
275 }
276 if (args.head.tag == ARRAY) {
277 buf.append(((ArrayType)args.head).elemtype);
278 buf.append("...");
279 } else {
280 buf.append(args.head);
281 }
282 return buf.toString();
283 }
285 /** Access methods.
286 */
287 public List<Type> getTypeArguments() { return List.nil(); }
288 public Type getEnclosingType() { return null; }
289 public List<Type> getParameterTypes() { return List.nil(); }
290 public Type getReturnType() { return null; }
291 public List<Type> getThrownTypes() { return List.nil(); }
292 public Type getUpperBound() { return null; }
293 public Type getLowerBound() { return null; }
295 /** Navigation methods, these will work for classes, type variables,
296 * foralls, but will return null for arrays and methods.
297 */
299 /** Return all parameters of this type and all its outer types in order
300 * outer (first) to inner (last).
301 */
302 public List<Type> allparams() { return List.nil(); }
304 /** Does this type contain "error" elements?
305 */
306 public boolean isErroneous() {
307 return false;
308 }
310 public static boolean isErroneous(List<Type> ts) {
311 for (List<Type> l = ts; l.nonEmpty(); l = l.tail)
312 if (l.head.isErroneous()) return true;
313 return false;
314 }
316 /** Is this type parameterized?
317 * A class type is parameterized if it has some parameters.
318 * An array type is parameterized if its element type is parameterized.
319 * All other types are not parameterized.
320 */
321 public boolean isParameterized() {
322 return false;
323 }
325 /** Is this type a raw type?
326 * A class type is a raw type if it misses some of its parameters.
327 * An array type is a raw type if its element type is raw.
328 * All other types are not raw.
329 * Type validation will ensure that the only raw types
330 * in a program are types that miss all their type variables.
331 */
332 public boolean isRaw() {
333 return false;
334 }
336 public boolean isCompound() {
337 return tsym.completer == null
338 // Compound types can't have a completer. Calling
339 // flags() will complete the symbol causing the
340 // compiler to load classes unnecessarily. This led
341 // to regression 6180021.
342 && (tsym.flags() & COMPOUND) != 0;
343 }
345 public boolean isInterface() {
346 return (tsym.flags() & INTERFACE) != 0;
347 }
349 public boolean isFinal() {
350 return (tsym.flags() & FINAL) != 0;
351 }
353 public boolean isPrimitive() {
354 return tag < VOID;
355 }
357 /**
358 * Does this type contain occurrences of type t?
359 */
360 public boolean contains(Type t) {
361 return t == this;
362 }
364 public static boolean contains(List<Type> ts, Type t) {
365 for (List<Type> l = ts;
366 l.tail != null /*inlined: l.nonEmpty()*/;
367 l = l.tail)
368 if (l.head.contains(t)) return true;
369 return false;
370 }
372 /** Does this type contain an occurrence of some type in 'ts'?
373 */
374 public boolean containsAny(List<Type> ts) {
375 for (Type t : ts)
376 if (this.contains(t)) return true;
377 return false;
378 }
380 public static boolean containsAny(List<Type> ts1, List<Type> ts2) {
381 for (Type t : ts1)
382 if (t.containsAny(ts2)) return true;
383 return false;
384 }
386 public static List<Type> filter(List<Type> ts, Filter<Type> tf) {
387 ListBuffer<Type> buf = ListBuffer.lb();
388 for (Type t : ts) {
389 if (tf.accepts(t)) {
390 buf.append(t);
391 }
392 }
393 return buf.toList();
394 }
396 public boolean isSuperBound() { return false; }
397 public boolean isExtendsBound() { return false; }
398 public boolean isUnbound() { return false; }
399 public Type withTypeVar(Type t) { return this; }
401 /** The underlying method type of this type.
402 */
403 public MethodType asMethodType() { throw new AssertionError(); }
405 /** Complete loading all classes in this type.
406 */
407 public void complete() {}
409 public TypeSymbol asElement() {
410 return tsym;
411 }
413 public TypeKind getKind() {
414 switch (tag) {
415 case BYTE: return TypeKind.BYTE;
416 case CHAR: return TypeKind.CHAR;
417 case SHORT: return TypeKind.SHORT;
418 case INT: return TypeKind.INT;
419 case LONG: return TypeKind.LONG;
420 case FLOAT: return TypeKind.FLOAT;
421 case DOUBLE: return TypeKind.DOUBLE;
422 case BOOLEAN: return TypeKind.BOOLEAN;
423 case VOID: return TypeKind.VOID;
424 case BOT: return TypeKind.NULL;
425 case NONE: return TypeKind.NONE;
426 default: return TypeKind.OTHER;
427 }
428 }
430 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
431 if (isPrimitive())
432 return v.visitPrimitive(this, p);
433 else
434 throw new AssertionError();
435 }
437 public static class WildcardType extends Type
438 implements javax.lang.model.type.WildcardType {
440 public Type type;
441 public BoundKind kind;
442 public TypeVar bound;
444 @Override
445 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
446 return v.visitWildcardType(this, s);
447 }
449 public WildcardType(Type type, BoundKind kind, TypeSymbol tsym) {
450 super(WILDCARD, tsym);
451 this.type = Assert.checkNonNull(type);
452 this.kind = kind;
453 }
454 public WildcardType(WildcardType t, TypeVar bound) {
455 this(t.type, t.kind, t.tsym, bound);
456 }
458 public WildcardType(Type type, BoundKind kind, TypeSymbol tsym, TypeVar bound) {
459 this(type, kind, tsym);
460 this.bound = bound;
461 }
463 public boolean contains(Type t) {
464 return kind != UNBOUND && type.contains(t);
465 }
467 public boolean isSuperBound() {
468 return kind == SUPER ||
469 kind == UNBOUND;
470 }
471 public boolean isExtendsBound() {
472 return kind == EXTENDS ||
473 kind == UNBOUND;
474 }
475 public boolean isUnbound() {
476 return kind == UNBOUND;
477 }
479 public Type withTypeVar(Type t) {
480 //-System.err.println(this+".withTypeVar("+t+");");//DEBUG
481 if (bound == t)
482 return this;
483 bound = (TypeVar)t;
484 return this;
485 }
487 boolean isPrintingBound = false;
488 public String toString() {
489 StringBuilder s = new StringBuilder();
490 s.append(kind.toString());
491 if (kind != UNBOUND)
492 s.append(type);
493 if (moreInfo && bound != null && !isPrintingBound)
494 try {
495 isPrintingBound = true;
496 s.append("{:").append(bound.bound).append(":}");
497 } finally {
498 isPrintingBound = false;
499 }
500 return s.toString();
501 }
503 public Type map(Mapping f) {
504 //- System.err.println(" (" + this + ").map(" + f + ")");//DEBUG
505 Type t = type;
506 if (t != null)
507 t = f.apply(t);
508 if (t == type)
509 return this;
510 else
511 return new WildcardType(t, kind, tsym, bound);
512 }
514 public Type getExtendsBound() {
515 if (kind == EXTENDS)
516 return type;
517 else
518 return null;
519 }
521 public Type getSuperBound() {
522 if (kind == SUPER)
523 return type;
524 else
525 return null;
526 }
528 public TypeKind getKind() {
529 return TypeKind.WILDCARD;
530 }
532 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
533 return v.visitWildcard(this, p);
534 }
535 }
537 public static class ClassType extends Type implements DeclaredType {
539 /** The enclosing type of this type. If this is the type of an inner
540 * class, outer_field refers to the type of its enclosing
541 * instance class, in all other cases it referes to noType.
542 */
543 private Type outer_field;
545 /** The type parameters of this type (to be set once class is loaded).
546 */
547 public List<Type> typarams_field;
549 /** A cache variable for the type parameters of this type,
550 * appended to all parameters of its enclosing class.
551 * @see #allparams
552 */
553 public List<Type> allparams_field;
555 /** The supertype of this class (to be set once class is loaded).
556 */
557 public Type supertype_field;
559 /** The interfaces of this class (to be set once class is loaded).
560 */
561 public List<Type> interfaces_field;
563 /** All the interfaces of this class, including missing ones.
564 */
565 public List<Type> all_interfaces_field;
567 public ClassType(Type outer, List<Type> typarams, TypeSymbol tsym) {
568 super(CLASS, tsym);
569 this.outer_field = outer;
570 this.typarams_field = typarams;
571 this.allparams_field = null;
572 this.supertype_field = null;
573 this.interfaces_field = null;
574 /*
575 // this can happen during error recovery
576 assert
577 outer.isParameterized() ?
578 typarams.length() == tsym.type.typarams().length() :
579 outer.isRaw() ?
580 typarams.length() == 0 :
581 true;
582 */
583 }
585 @Override
586 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
587 return v.visitClassType(this, s);
588 }
590 public Type constType(Object constValue) {
591 final Object value = constValue;
592 return new ClassType(getEnclosingType(), typarams_field, tsym) {
593 @Override
594 public Object constValue() {
595 return value;
596 }
597 @Override
598 public Type baseType() {
599 return tsym.type;
600 }
601 };
602 }
604 /** The Java source which this type represents.
605 */
606 public String toString() {
607 StringBuilder buf = new StringBuilder();
608 if (getEnclosingType().tag == CLASS && tsym.owner.kind == TYP) {
609 buf.append(getEnclosingType().toString());
610 buf.append(".");
611 buf.append(className(tsym, false));
612 } else {
613 buf.append(className(tsym, true));
614 }
615 if (getTypeArguments().nonEmpty()) {
616 buf.append('<');
617 buf.append(getTypeArguments().toString());
618 buf.append(">");
619 }
620 return buf.toString();
621 }
622 //where
623 private String className(Symbol sym, boolean longform) {
624 if (sym.name.isEmpty() && (sym.flags() & COMPOUND) != 0) {
625 StringBuilder s = new StringBuilder(supertype_field.toString());
626 for (List<Type> is=interfaces_field; is.nonEmpty(); is = is.tail) {
627 s.append("&");
628 s.append(is.head.toString());
629 }
630 return s.toString();
631 } else if (sym.name.isEmpty()) {
632 String s;
633 ClassType norm = (ClassType) tsym.type;
634 if (norm == null) {
635 s = Log.getLocalizedString("anonymous.class", (Object)null);
636 } else if (norm.interfaces_field != null && norm.interfaces_field.nonEmpty()) {
637 s = Log.getLocalizedString("anonymous.class",
638 norm.interfaces_field.head);
639 } else {
640 s = Log.getLocalizedString("anonymous.class",
641 norm.supertype_field);
642 }
643 if (moreInfo)
644 s += String.valueOf(sym.hashCode());
645 return s;
646 } else if (longform) {
647 return sym.getQualifiedName().toString();
648 } else {
649 return sym.name.toString();
650 }
651 }
653 public List<Type> getTypeArguments() {
654 if (typarams_field == null) {
655 complete();
656 if (typarams_field == null)
657 typarams_field = List.nil();
658 }
659 return typarams_field;
660 }
662 public boolean hasErasedSupertypes() {
663 return isRaw();
664 }
666 public Type getEnclosingType() {
667 return outer_field;
668 }
670 public void setEnclosingType(Type outer) {
671 outer_field = outer;
672 }
674 public List<Type> allparams() {
675 if (allparams_field == null) {
676 allparams_field = getTypeArguments().prependList(getEnclosingType().allparams());
677 }
678 return allparams_field;
679 }
681 public boolean isErroneous() {
682 return
683 getEnclosingType().isErroneous() ||
684 isErroneous(getTypeArguments()) ||
685 this != tsym.type && tsym.type.isErroneous();
686 }
688 public boolean isParameterized() {
689 return allparams().tail != null;
690 // optimization, was: allparams().nonEmpty();
691 }
693 /** A cache for the rank. */
694 int rank_field = -1;
696 /** A class type is raw if it misses some
697 * of its type parameter sections.
698 * After validation, this is equivalent to:
699 * {@code allparams.isEmpty() && tsym.type.allparams.nonEmpty(); }
700 */
701 public boolean isRaw() {
702 return
703 this != tsym.type && // necessary, but not sufficient condition
704 tsym.type.allparams().nonEmpty() &&
705 allparams().isEmpty();
706 }
708 public Type map(Mapping f) {
709 Type outer = getEnclosingType();
710 Type outer1 = f.apply(outer);
711 List<Type> typarams = getTypeArguments();
712 List<Type> typarams1 = map(typarams, f);
713 if (outer1 == outer && typarams1 == typarams) return this;
714 else return new ClassType(outer1, typarams1, tsym);
715 }
717 public boolean contains(Type elem) {
718 return
719 elem == this
720 || (isParameterized()
721 && (getEnclosingType().contains(elem) || contains(getTypeArguments(), elem)))
722 || (isCompound()
723 && (supertype_field.contains(elem) || contains(interfaces_field, elem)));
724 }
726 public void complete() {
727 if (tsym.completer != null) tsym.complete();
728 }
730 public TypeKind getKind() {
731 return TypeKind.DECLARED;
732 }
734 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
735 return v.visitDeclared(this, p);
736 }
737 }
739 public static class ErasedClassType extends ClassType {
740 public ErasedClassType(Type outer, TypeSymbol tsym) {
741 super(outer, List.<Type>nil(), tsym);
742 }
744 @Override
745 public boolean hasErasedSupertypes() {
746 return true;
747 }
748 }
750 // a clone of a ClassType that knows about the alternatives of a union type.
751 public static class UnionClassType extends ClassType implements UnionType {
752 final List<? extends Type> alternatives_field;
754 public UnionClassType(ClassType ct, List<? extends Type> alternatives) {
755 super(ct.outer_field, ct.typarams_field, ct.tsym);
756 allparams_field = ct.allparams_field;
757 supertype_field = ct.supertype_field;
758 interfaces_field = ct.interfaces_field;
759 all_interfaces_field = ct.interfaces_field;
760 alternatives_field = alternatives;
761 }
763 public Type getLub() {
764 return tsym.type;
765 }
767 public java.util.List<? extends TypeMirror> getAlternatives() {
768 return Collections.unmodifiableList(alternatives_field);
769 }
771 @Override
772 public TypeKind getKind() {
773 return TypeKind.UNION;
774 }
776 @Override
777 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
778 return v.visitUnion(this, p);
779 }
780 }
782 public static class ArrayType extends Type
783 implements javax.lang.model.type.ArrayType {
785 public Type elemtype;
787 public ArrayType(Type elemtype, TypeSymbol arrayClass) {
788 super(ARRAY, arrayClass);
789 this.elemtype = elemtype;
790 }
792 @Override
793 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
794 return v.visitArrayType(this, s);
795 }
797 public String toString() {
798 return elemtype + "[]";
799 }
801 public boolean equals(Object obj) {
802 return
803 this == obj ||
804 (obj instanceof ArrayType &&
805 this.elemtype.equals(((ArrayType)obj).elemtype));
806 }
808 public int hashCode() {
809 return (ARRAY << 5) + elemtype.hashCode();
810 }
812 public boolean isVarargs() {
813 return false;
814 }
816 public List<Type> allparams() { return elemtype.allparams(); }
818 public boolean isErroneous() {
819 return elemtype.isErroneous();
820 }
822 public boolean isParameterized() {
823 return elemtype.isParameterized();
824 }
826 public boolean isRaw() {
827 return elemtype.isRaw();
828 }
830 public ArrayType makeVarargs() {
831 return new ArrayType(elemtype, tsym) {
832 @Override
833 public boolean isVarargs() {
834 return true;
835 }
836 };
837 }
839 public Type map(Mapping f) {
840 Type elemtype1 = f.apply(elemtype);
841 if (elemtype1 == elemtype) return this;
842 else return new ArrayType(elemtype1, tsym);
843 }
845 public boolean contains(Type elem) {
846 return elem == this || elemtype.contains(elem);
847 }
849 public void complete() {
850 elemtype.complete();
851 }
853 public Type getComponentType() {
854 return elemtype;
855 }
857 public TypeKind getKind() {
858 return TypeKind.ARRAY;
859 }
861 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
862 return v.visitArray(this, p);
863 }
864 }
866 public static class MethodType extends Type implements ExecutableType {
868 public List<Type> argtypes;
869 public Type restype;
870 public List<Type> thrown;
872 public MethodType(List<Type> argtypes,
873 Type restype,
874 List<Type> thrown,
875 TypeSymbol methodClass) {
876 super(METHOD, methodClass);
877 this.argtypes = argtypes;
878 this.restype = restype;
879 this.thrown = thrown;
880 }
882 @Override
883 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
884 return v.visitMethodType(this, s);
885 }
887 /** The Java source which this type represents.
888 *
889 * XXX 06/09/99 iris This isn't correct Java syntax, but it probably
890 * should be.
891 */
892 public String toString() {
893 return "(" + argtypes + ")" + restype;
894 }
896 public boolean equals(Object obj) {
897 if (this == obj)
898 return true;
899 if (!(obj instanceof MethodType))
900 return false;
901 MethodType m = (MethodType)obj;
902 List<Type> args1 = argtypes;
903 List<Type> args2 = m.argtypes;
904 while (!args1.isEmpty() && !args2.isEmpty()) {
905 if (!args1.head.equals(args2.head))
906 return false;
907 args1 = args1.tail;
908 args2 = args2.tail;
909 }
910 if (!args1.isEmpty() || !args2.isEmpty())
911 return false;
912 return restype.equals(m.restype);
913 }
915 public int hashCode() {
916 int h = METHOD;
917 for (List<Type> thisargs = this.argtypes;
918 thisargs.tail != null; /*inlined: thisargs.nonEmpty()*/
919 thisargs = thisargs.tail)
920 h = (h << 5) + thisargs.head.hashCode();
921 return (h << 5) + this.restype.hashCode();
922 }
924 public List<Type> getParameterTypes() { return argtypes; }
925 public Type getReturnType() { return restype; }
926 public List<Type> getThrownTypes() { return thrown; }
928 public boolean isErroneous() {
929 return
930 isErroneous(argtypes) ||
931 restype != null && restype.isErroneous();
932 }
934 public Type map(Mapping f) {
935 List<Type> argtypes1 = map(argtypes, f);
936 Type restype1 = f.apply(restype);
937 List<Type> thrown1 = map(thrown, f);
938 if (argtypes1 == argtypes &&
939 restype1 == restype &&
940 thrown1 == thrown) return this;
941 else return new MethodType(argtypes1, restype1, thrown1, tsym);
942 }
944 public boolean contains(Type elem) {
945 return elem == this || contains(argtypes, elem) || restype.contains(elem);
946 }
948 public MethodType asMethodType() { return this; }
950 public void complete() {
951 for (List<Type> l = argtypes; l.nonEmpty(); l = l.tail)
952 l.head.complete();
953 restype.complete();
954 for (List<Type> l = thrown; l.nonEmpty(); l = l.tail)
955 l.head.complete();
956 }
958 public List<TypeVar> getTypeVariables() {
959 return List.nil();
960 }
962 public TypeSymbol asElement() {
963 return null;
964 }
966 public TypeKind getKind() {
967 return TypeKind.EXECUTABLE;
968 }
970 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
971 return v.visitExecutable(this, p);
972 }
973 }
975 public static class PackageType extends Type implements NoType {
977 PackageType(TypeSymbol tsym) {
978 super(PACKAGE, tsym);
979 }
981 @Override
982 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
983 return v.visitPackageType(this, s);
984 }
986 public String toString() {
987 return tsym.getQualifiedName().toString();
988 }
990 public TypeKind getKind() {
991 return TypeKind.PACKAGE;
992 }
994 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
995 return v.visitNoType(this, p);
996 }
997 }
999 public static class TypeVar extends Type implements TypeVariable {
1001 /** The upper bound of this type variable; set from outside.
1002 * Must be nonempty once it is set.
1003 * For a bound, `bound' is the bound type itself.
1004 * Multiple bounds are expressed as a single class type which has the
1005 * individual bounds as superclass, respectively interfaces.
1006 * The class type then has as `tsym' a compiler generated class `c',
1007 * which has a flag COMPOUND and whose owner is the type variable
1008 * itself. Furthermore, the erasure_field of the class
1009 * points to the first class or interface bound.
1010 */
1011 public Type bound = null;
1013 /** The lower bound of this type variable.
1014 * TypeVars don't normally have a lower bound, so it is normally set
1015 * to syms.botType.
1016 * Subtypes, such as CapturedType, may provide a different value.
1017 */
1018 public Type lower;
1020 public TypeVar(Name name, Symbol owner, Type lower) {
1021 super(TYPEVAR, null);
1022 tsym = new TypeSymbol(0, name, this, owner);
1023 this.lower = lower;
1024 }
1026 public TypeVar(TypeSymbol tsym, Type bound, Type lower) {
1027 super(TYPEVAR, tsym);
1028 this.bound = bound;
1029 this.lower = lower;
1030 }
1032 @Override
1033 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1034 return v.visitTypeVar(this, s);
1035 }
1037 @Override
1038 public Type getUpperBound() { return bound; }
1040 int rank_field = -1;
1042 @Override
1043 public Type getLowerBound() {
1044 return lower;
1045 }
1047 public TypeKind getKind() {
1048 return TypeKind.TYPEVAR;
1049 }
1051 public boolean isCaptured() {
1052 return false;
1053 }
1055 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1056 return v.visitTypeVariable(this, p);
1057 }
1058 }
1060 /** A captured type variable comes from wildcards which can have
1061 * both upper and lower bound. CapturedType extends TypeVar with
1062 * a lower bound.
1063 */
1064 public static class CapturedType extends TypeVar {
1066 public WildcardType wildcard;
1068 public CapturedType(Name name,
1069 Symbol owner,
1070 Type upper,
1071 Type lower,
1072 WildcardType wildcard) {
1073 super(name, owner, lower);
1074 this.lower = Assert.checkNonNull(lower);
1075 this.bound = upper;
1076 this.wildcard = wildcard;
1077 }
1079 @Override
1080 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1081 return v.visitCapturedType(this, s);
1082 }
1084 @Override
1085 public boolean isCaptured() {
1086 return true;
1087 }
1089 @Override
1090 public String toString() {
1091 return "capture#"
1092 + (hashCode() & 0xFFFFFFFFL) % Printer.PRIME
1093 + " of "
1094 + wildcard;
1095 }
1096 }
1098 public static abstract class DelegatedType extends Type {
1099 public Type qtype;
1100 public DelegatedType(int tag, Type qtype) {
1101 super(tag, qtype.tsym);
1102 this.qtype = qtype;
1103 }
1104 public String toString() { return qtype.toString(); }
1105 public List<Type> getTypeArguments() { return qtype.getTypeArguments(); }
1106 public Type getEnclosingType() { return qtype.getEnclosingType(); }
1107 public List<Type> getParameterTypes() { return qtype.getParameterTypes(); }
1108 public Type getReturnType() { return qtype.getReturnType(); }
1109 public List<Type> getThrownTypes() { return qtype.getThrownTypes(); }
1110 public List<Type> allparams() { return qtype.allparams(); }
1111 public Type getUpperBound() { return qtype.getUpperBound(); }
1112 public boolean isErroneous() { return qtype.isErroneous(); }
1113 }
1115 /**
1116 * The type of a generic method type. It consists of a method type and
1117 * a list of method type-parameters that are used within the method
1118 * type.
1119 */
1120 public static class ForAll extends DelegatedType implements ExecutableType {
1121 public List<Type> tvars;
1123 public ForAll(List<Type> tvars, Type qtype) {
1124 super(FORALL, (MethodType)qtype);
1125 this.tvars = tvars;
1126 }
1128 @Override
1129 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1130 return v.visitForAll(this, s);
1131 }
1133 public String toString() {
1134 return "<" + tvars + ">" + qtype;
1135 }
1137 public List<Type> getTypeArguments() { return tvars; }
1139 public boolean isErroneous() {
1140 return qtype.isErroneous();
1141 }
1143 public Type map(Mapping f) {
1144 return f.apply(qtype);
1145 }
1147 public boolean contains(Type elem) {
1148 return qtype.contains(elem);
1149 }
1151 public MethodType asMethodType() {
1152 return (MethodType)qtype;
1153 }
1155 public void complete() {
1156 for (List<Type> l = tvars; l.nonEmpty(); l = l.tail) {
1157 ((TypeVar)l.head).bound.complete();
1158 }
1159 qtype.complete();
1160 }
1162 public List<TypeVar> getTypeVariables() {
1163 return List.convert(TypeVar.class, getTypeArguments());
1164 }
1166 public TypeKind getKind() {
1167 return TypeKind.EXECUTABLE;
1168 }
1170 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1171 return v.visitExecutable(this, p);
1172 }
1173 }
1175 /** A class for inference variables, for use during method/diamond type
1176 * inference. An inference variable has upper/lower bounds and a set
1177 * of equality constraints. Such bounds are set during subtyping, type-containment,
1178 * type-equality checks, when the types being tested contain inference variables.
1179 * A change listener can be attached to an inference variable, to receive notifications
1180 * whenever the bounds of an inference variable change.
1181 */
1182 public static class UndetVar extends DelegatedType {
1184 /** Inference variable change listener. The listener method is called
1185 * whenever a change to the inference variable's bounds occurs
1186 */
1187 public interface UndetVarListener {
1188 /** called when some inference variable bounds (of given kinds ibs) change */
1189 void varChanged(UndetVar uv, Set<InferenceBound> ibs);
1190 }
1192 /**
1193 * Inference variable bound kinds
1194 */
1195 public enum InferenceBound {
1196 /** upper bounds */
1197 UPPER,
1198 /** lower bounds */
1199 LOWER,
1200 /** equality constraints */
1201 EQ;
1202 }
1204 /** inference variable bounds */
1205 private Map<InferenceBound, List<Type>> bounds;
1207 /** inference variable's inferred type (set from Infer.java) */
1208 public Type inst = null;
1210 /** inference variable's change listener */
1211 public UndetVarListener listener = null;
1213 @Override
1214 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1215 return v.visitUndetVar(this, s);
1216 }
1218 public UndetVar(TypeVar origin, Types types) {
1219 super(UNDETVAR, origin);
1220 bounds = new EnumMap<InferenceBound, List<Type>>(InferenceBound.class);
1221 bounds.put(InferenceBound.UPPER, types.getBounds(origin));
1222 bounds.put(InferenceBound.LOWER, List.<Type>nil());
1223 bounds.put(InferenceBound.EQ, List.<Type>nil());
1224 }
1226 public String toString() {
1227 if (inst != null) return inst.toString();
1228 else return qtype + "?";
1229 }
1231 public Type baseType() {
1232 if (inst != null) return inst.baseType();
1233 else return this;
1234 }
1236 /** get all bounds of a given kind */
1237 public List<Type> getBounds(InferenceBound ib) {
1238 return bounds.get(ib);
1239 }
1241 /** add a bound of a given kind - this might trigger listener notification */
1242 public void addBound(InferenceBound ib, Type bound, Types types) {
1243 List<Type> prevBounds = bounds.get(ib);
1244 for (Type b : prevBounds) {
1245 if (types.isSameType(b, bound)) {
1246 return;
1247 }
1248 }
1249 bounds.put(ib, prevBounds.prepend(bound));
1250 notifyChange(EnumSet.of(ib));
1251 }
1253 /** replace types in all bounds - this might trigger listener notification */
1254 public void substBounds(List<Type> from, List<Type> to, Types types) {
1255 EnumSet<InferenceBound> changed = EnumSet.noneOf(InferenceBound.class);
1256 Map<InferenceBound, List<Type>> bounds2 = new EnumMap<InferenceBound, List<Type>>(InferenceBound.class);
1257 for (Map.Entry<InferenceBound, List<Type>> _entry : bounds.entrySet()) {
1258 InferenceBound ib = _entry.getKey();
1259 List<Type> prevBounds = _entry.getValue();
1260 List<Type> newBounds = types.subst(prevBounds, from, to);
1261 bounds2.put(ib, newBounds);
1262 if (prevBounds != newBounds) {
1263 changed.add(ib);
1264 }
1265 }
1266 if (!changed.isEmpty()) {
1267 bounds = bounds2;
1268 notifyChange(changed);
1269 }
1270 }
1272 private void notifyChange(EnumSet<InferenceBound> ibs) {
1273 if (listener != null) {
1274 listener.varChanged(this, ibs);
1275 }
1276 }
1277 }
1279 /** Represents VOID or NONE.
1280 */
1281 static class JCNoType extends Type implements NoType {
1282 public JCNoType(int tag) {
1283 super(tag, null);
1284 }
1286 @Override
1287 public TypeKind getKind() {
1288 switch (tag) {
1289 case VOID: return TypeKind.VOID;
1290 case NONE: return TypeKind.NONE;
1291 default:
1292 throw new AssertionError("Unexpected tag: " + tag);
1293 }
1294 }
1296 @Override
1297 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1298 return v.visitNoType(this, p);
1299 }
1300 }
1302 static class BottomType extends Type implements NullType {
1303 public BottomType() {
1304 super(TypeTags.BOT, null);
1305 }
1307 @Override
1308 public TypeKind getKind() {
1309 return TypeKind.NULL;
1310 }
1312 @Override
1313 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1314 return v.visitNull(this, p);
1315 }
1317 @Override
1318 public Type constType(Object value) {
1319 return this;
1320 }
1322 @Override
1323 public String stringValue() {
1324 return "null";
1325 }
1326 }
1328 public static class ErrorType extends ClassType
1329 implements javax.lang.model.type.ErrorType {
1331 private Type originalType = null;
1333 public ErrorType(Type originalType, TypeSymbol tsym) {
1334 super(noType, List.<Type>nil(), null);
1335 tag = ERROR;
1336 this.tsym = tsym;
1337 this.originalType = (originalType == null ? noType : originalType);
1338 }
1340 public ErrorType(ClassSymbol c, Type originalType) {
1341 this(originalType, c);
1342 c.type = this;
1343 c.kind = ERR;
1344 c.members_field = new Scope.ErrorScope(c);
1345 }
1347 public ErrorType(Name name, TypeSymbol container, Type originalType) {
1348 this(new ClassSymbol(PUBLIC|STATIC|ACYCLIC, name, null, container), originalType);
1349 }
1351 @Override
1352 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1353 return v.visitErrorType(this, s);
1354 }
1356 public Type constType(Object constValue) { return this; }
1357 public Type getEnclosingType() { return this; }
1358 public Type getReturnType() { return this; }
1359 public Type asSub(Symbol sym) { return this; }
1360 public Type map(Mapping f) { return this; }
1362 public boolean isGenType(Type t) { return true; }
1363 public boolean isErroneous() { return true; }
1364 public boolean isCompound() { return false; }
1365 public boolean isInterface() { return false; }
1367 public List<Type> allparams() { return List.nil(); }
1368 public List<Type> getTypeArguments() { return List.nil(); }
1370 public TypeKind getKind() {
1371 return TypeKind.ERROR;
1372 }
1374 public Type getOriginalType() {
1375 return originalType;
1376 }
1378 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1379 return v.visitError(this, p);
1380 }
1381 }
1383 /**
1384 * A visitor for types. A visitor is used to implement operations
1385 * (or relations) on types. Most common operations on types are
1386 * binary relations and this interface is designed for binary
1387 * relations, that is, operations on the form
1388 * Type × S → R.
1389 * <!-- In plain text: Type x S -> R -->
1390 *
1391 * @param <R> the return type of the operation implemented by this
1392 * visitor; use Void if no return type is needed.
1393 * @param <S> the type of the second argument (the first being the
1394 * type itself) of the operation implemented by this visitor; use
1395 * Void if a second argument is not needed.
1396 */
1397 public interface Visitor<R,S> {
1398 R visitClassType(ClassType t, S s);
1399 R visitWildcardType(WildcardType t, S s);
1400 R visitArrayType(ArrayType t, S s);
1401 R visitMethodType(MethodType t, S s);
1402 R visitPackageType(PackageType t, S s);
1403 R visitTypeVar(TypeVar t, S s);
1404 R visitCapturedType(CapturedType t, S s);
1405 R visitForAll(ForAll t, S s);
1406 R visitUndetVar(UndetVar t, S s);
1407 R visitErrorType(ErrorType t, S s);
1408 R visitType(Type t, S s);
1409 }
1410 }