1 /*
2 * Copyright (c) 1999, 2024, 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 */
25
26 package com.sun.tools.javac.code;
27
28 import java.lang.annotation.Annotation;
29 import java.util.ArrayDeque;
30 import java.util.Collections;
31 import java.util.EnumMap;
32 import java.util.Map;
33 import java.util.Optional;
34 import java.util.function.Function;
35 import java.util.function.Predicate;
36
37 import javax.lang.model.type.*;
38
39 import com.sun.tools.javac.code.Symbol.*;
40 import com.sun.tools.javac.code.TypeMetadata.Annotations;
41 import com.sun.tools.javac.code.TypeMetadata.ConstantValue;
42 import com.sun.tools.javac.code.Types.TypeMapping;
43 import com.sun.tools.javac.code.Types.UniqueType;
44 import com.sun.tools.javac.comp.Infer.IncorporationAction;
45 import com.sun.tools.javac.jvm.ClassFile;
46 import com.sun.tools.javac.jvm.PoolConstant;
47 import com.sun.tools.javac.util.*;
48 import com.sun.tools.javac.util.DefinedBy.Api;
49
50 import static com.sun.tools.javac.code.BoundKind.*;
51 import static com.sun.tools.javac.code.Flags.*;
52 import static com.sun.tools.javac.code.Kinds.Kind.*;
53 import static com.sun.tools.javac.code.TypeTag.*;
54
55 /** This class represents Java types. The class itself defines the behavior of
56 * the following types:
57 * <pre>
58 * base types (tags: BYTE, CHAR, SHORT, INT, LONG, FLOAT, DOUBLE, BOOLEAN),
59 * type `void' (tag: VOID),
60 * the bottom type (tag: BOT),
61 * the missing type (tag: NONE).
62 * </pre>
63 * <p>The behavior of the following types is defined in subclasses, which are
64 * all static inner classes of this class:
65 * <pre>
66 * class types (tag: CLASS, class: ClassType),
67 * array types (tag: ARRAY, class: ArrayType),
68 * method types (tag: METHOD, class: MethodType),
69 * package types (tag: PACKAGE, class: PackageType),
70 * type variables (tag: TYPEVAR, class: TypeVar),
71 * type arguments (tag: WILDCARD, class: WildcardType),
72 * generic method types (tag: FORALL, class: ForAll),
73 * the error type (tag: ERROR, class: ErrorType).
74 * </pre>
75 *
76 * <p><b>This is NOT part of any supported API.
77 * If you write code that depends on this, you do so at your own risk.
78 * This code and its internal interfaces are subject to change or
79 * deletion without notice.</b>
80 *
81 * @see TypeTag
82 */
83 public abstract class Type extends AnnoConstruct implements TypeMirror, PoolConstant {
84
85 /**
86 * Type metadata, Should be {@code null} for the default value.
87 *
88 * Note: it is an invariant that for any {@code TypeMetadata}
89 * class, a given {@code Type} may have at most one metadata array
90 * entry of that class.
91 */
92 protected final List<TypeMetadata> metadata;
93
94 /** Constant type: no type at all. */
95 public static final JCNoType noType = new JCNoType() {
96 @Override @DefinedBy(Api.LANGUAGE_MODEL)
97 public String toString() {
98 return "none";
99 }
100 };
101
102 /** Constant type: special type to be used during recovery of deferred expressions. */
103 public static final JCNoType recoveryType = new JCNoType(){
104 @Override @DefinedBy(Api.LANGUAGE_MODEL)
105 public String toString() {
106 return "recovery";
107 }
108 };
109
110 /** Constant type: special type to be used for marking stuck trees. */
111 public static final JCNoType stuckType = new JCNoType() {
112 @Override @DefinedBy(Api.LANGUAGE_MODEL)
113 public String toString() {
114 return "stuck";
115 }
116 };
117
118 /** If this switch is turned on, the names of type variables
119 * and anonymous classes are printed with hashcodes appended.
120 */
121 public static boolean moreInfo = false;
122
123 /** The defining class / interface / package / type variable.
124 */
125 public TypeSymbol tsym;
126
127 @Override
128 public int poolTag() {
129 throw new AssertionError("Invalid pool entry");
130 }
131
132 @Override
133 public Object poolKey(Types types) {
134 return new UniqueType(this, types);
135 }
136
137 /**
138 * Checks if the current type tag is equal to the given tag.
139 * @return true if tag is equal to the current type tag.
140 */
141 public boolean hasTag(TypeTag tag) {
142 return tag == getTag();
143 }
144
145 /**
146 * Returns the current type tag.
147 * @return the value of the current type tag.
148 */
149 public abstract TypeTag getTag();
150
151 public boolean isNumeric() {
152 return false;
153 }
154
155 public boolean isIntegral() {
156 return false;
157 }
158
159 public boolean isPrimitive() {
160 return false;
161 }
162
163 public boolean isPrimitiveOrVoid() {
164 return false;
165 }
166
167 public boolean isReference() {
168 return false;
169 }
170
171 public boolean isNullOrReference() {
172 return false;
173 }
174
175 public boolean isPartial() {
176 return false;
177 }
178
179 /**
180 * The constant value of this type, null if this type does not
181 * have a constant value attribute. Only primitive types and
182 * strings (ClassType) can have a constant value attribute.
183 * @return the constant value attribute of this type
184 */
185 public Object constValue() {
186 return getMetadata(TypeMetadata.ConstantValue.class, ConstantValue::value, null);
187 }
188
189 /** Is this a constant type whose value is false?
190 */
191 public boolean isFalse() {
192 return false;
193 }
194
195 /** Is this a constant type whose value is true?
196 */
197 public boolean isTrue() {
198 return false;
199 }
200
201 /**
202 * Get the representation of this type used for modelling purposes.
203 * By default, this is itself. For ErrorType, a different value
204 * may be provided.
205 */
206 public Type getModelType() {
207 return this;
208 }
209
210 public static List<Type> getModelTypes(List<Type> ts) {
211 ListBuffer<Type> lb = new ListBuffer<>();
212 for (Type t: ts)
213 lb.append(t.getModelType());
214 return lb.toList();
215 }
216
217 /**For ErrorType, returns the original type, otherwise returns the type itself.
218 */
219 public Type getOriginalType() {
220 return this;
221 }
222
223 public <R,S> R accept(Type.Visitor<R,S> v, S s) { return v.visitType(this, s); }
224
225 /** Define a type given its tag, type symbol, and type annotations
226 */
227
228 public Type(TypeSymbol tsym, List<TypeMetadata> metadata) {
229 Assert.checkNonNull(metadata);
230 this.tsym = tsym;
231 this.metadata = metadata;
232 }
233
234 /**
235 * A subclass of {@link Types.TypeMapping} which applies a mapping recursively to the subterms
236 * of a given type expression. This mapping returns the original type is no changes occurred
237 * when recursively mapping the original type's subterms.
238 */
239 public abstract static class StructuralTypeMapping<S> extends Types.TypeMapping<S> {
240
241 @Override
242 public Type visitClassType(ClassType t, S s) {
243 Type outer = t.getEnclosingType();
244 Type outer1 = visit(outer, s);
245 List<Type> typarams = t.getTypeArguments();
246 List<Type> typarams1 = visit(typarams, s);
247 if (outer1 == outer && typarams1 == typarams) return t;
248 else return new ClassType(outer1, typarams1, t.tsym, t.metadata) {
249 @Override
250 protected boolean needsStripping() {
251 return true;
252 }
253 };
254 }
255
256 @Override
257 public Type visitWildcardType(WildcardType wt, S s) {
258 Type t = wt.type;
259 if (t != null)
260 t = visit(t, s);
261 if (t == wt.type)
262 return wt;
263 else
264 return new WildcardType(t, wt.kind, wt.tsym, wt.bound, wt.metadata) {
265 @Override
266 protected boolean needsStripping() {
267 return true;
268 }
269 };
270 }
271
272 @Override
273 public Type visitArrayType(ArrayType t, S s) {
274 Type elemtype = t.elemtype;
275 Type elemtype1 = visit(elemtype, s);
276 if (elemtype1 == elemtype) return t;
277 else return new ArrayType(elemtype1, t.tsym, t.metadata) {
278 @Override
279 protected boolean needsStripping() {
280 return true;
281 }
282 };
283 }
284
285 @Override
286 public Type visitMethodType(MethodType t, S s) {
287 List<Type> argtypes = t.argtypes;
288 Type restype = t.restype;
289 List<Type> thrown = t.thrown;
290 List<Type> argtypes1 = visit(argtypes, s);
291 Type restype1 = visit(restype, s);
292 List<Type> thrown1 = visit(thrown, s);
293 if (argtypes1 == argtypes &&
294 restype1 == restype &&
295 thrown1 == thrown) return t;
296 else return new MethodType(argtypes1, restype1, thrown1, t.tsym) {
297 @Override
298 protected boolean needsStripping() {
299 return true;
300 }
301 };
302 }
303
304 @Override
305 public Type visitForAll(ForAll t, S s) {
306 return visit(t.qtype, s);
307 }
308 }
309
310 /** map a type function over all immediate descendants of this type
311 */
312 public <Z> Type map(TypeMapping<Z> mapping, Z arg) {
313 return mapping.visit(this, arg);
314 }
315
316 /** map a type function over all immediate descendants of this type (no arg version)
317 */
318 public <Z> Type map(TypeMapping<Z> mapping) {
319 return mapping.visit(this, null);
320 }
321
322 /** Define a constant type, of the same kind as this type
323 * and with given constant value
324 */
325 public Type constType(Object constValue) {
326 throw new AssertionError();
327 }
328
329 /**
330 * If this is a constant type, return its underlying type.
331 * Otherwise, return the type itself.
332 */
333 public Type baseType() {
334 return this;
335 }
336
337 /**
338 * Returns the original version of this type, before metadata were added. This routine is meant
339 * for internal use only (i.e. {@link Type#equalsIgnoreMetadata(Type)}, {@link Type#stripMetadata});
340 * it should not be used outside this class.
341 */
342 protected Type typeNoMetadata() {
343 return metadata.isEmpty() ? this : stripMetadata();
344 }
345
346 /**
347 * Create a new copy of this type but with the specified TypeMetadata.
348 * Only to be used internally!
349 */
350 protected Type cloneWithMetadata(List<TypeMetadata> metadata) {
351 throw new AssertionError("Cannot add metadata to this type: " + getTag());
352 }
353
354 /**
355 * Get all the type metadata associated with this type.
356 */
357 public List<TypeMetadata> getMetadata() {
358 return metadata;
359 }
360
361 /**
362 * Get the type metadata of the given kind associated with this type (if any).
363 */
364 public <M extends TypeMetadata> M getMetadata(Class<M> metadataClass) {
365 return getMetadata(metadataClass, Function.identity(), null);
366 }
367
368 /**
369 * Get the type metadata of the given kind associated with this type (if any),
370 * and apply the provided mapping function.
371 */
372 @SuppressWarnings("unchecked")
373 public <M extends TypeMetadata, Z> Z getMetadata(Class<M> metadataClass, Function<M, Z> metadataFunc, Z defaultValue) {
374 for (TypeMetadata m : metadata) {
375 if (m.getClass() == metadataClass) {
376 return metadataFunc.apply((M)m);
377 }
378 }
379 return defaultValue;
380 }
381
382 /**
383 * Create a new copy of this type but with the specified type metadata.
384 * If this type is already associated with a type metadata of the same class,
385 * an exception is thrown.
386 */
387 public Type addMetadata(TypeMetadata md) {
388 Assert.check(getMetadata(md.getClass()) == null);
389 return cloneWithMetadata(metadata.prepend(md));
390 }
391
392 /**
393 * Create a new copy of this type but without the specified type metadata.
394 */
395 public Type dropMetadata(Class<? extends TypeMetadata> metadataClass) {
396 List<TypeMetadata> newMetadata = List.nil();
397 for (TypeMetadata m : metadata) {
398 if (m.getClass() != metadataClass) {
399 newMetadata = newMetadata.prepend(m);
400 }
401 }
402 return cloneWithMetadata(newMetadata);
403 }
404
405 /**
406 * Does this type require annotation stripping for API clients?
407 */
408 protected boolean needsStripping() {
409 return false;
410 }
411
412 /**
413 * Strip all metadata associated with this type - this could return a new clone of the type.
414 * This routine is only used to present the correct annotated types back to the users when types
415 * are accessed through compiler APIs; it should not be used anywhere in the compiler internals
416 * as doing so might result in performance penalties.
417 */
418 public Type stripMetadataIfNeeded() {
419 return needsStripping() ?
420 accept(stripMetadata, null) :
421 this;
422 }
423
424 public Type stripMetadata() {
425 return accept(stripMetadata, null);
426 }
427 //where
428 /**
429 * Note: this visitor only needs to handle cases where
430 * 'contained' types can be annotated. These cases are
431 * described in JVMS 4.7.20.2 and are : classes (for type
432 * parameters and enclosing types), wildcards, and arrays.
433 */
434 private static final TypeMapping<Void> stripMetadata = new StructuralTypeMapping<Void>() {
435 @Override
436 public Type visitClassType(ClassType t, Void aVoid) {
437 return super.visitClassType((ClassType) dropMetadata(t), aVoid);
438 }
439
440 @Override
441 public Type visitArrayType(ArrayType t, Void aVoid) {
442 return super.visitArrayType((ArrayType) dropMetadata(t), aVoid);
443 }
444
445 @Override
446 public Type visitWildcardType(WildcardType wt, Void aVoid) {
447 return super.visitWildcardType((WildcardType) dropMetadata(wt), aVoid);
448 }
449
450 @Override
451 public Type visitType(Type t, Void aVoid) {
452 return dropMetadata(t);
453 }
454
455 private static Type dropMetadata(Type t) {
456 if (t.getMetadata().isEmpty()) {
457 return t;
458 }
459 Type baseType = t.baseType();
460 if (baseType.getMetadata().isEmpty()) {
461 return baseType;
462 }
463 return baseType.cloneWithMetadata(List.nil());
464 }
465 };
466
467 public Type preannotatedType() {
468 return addMetadata(new Annotations());
469 }
470
471 public Type annotatedType(final List<Attribute.TypeCompound> annos) {
472 return addMetadata(new Annotations(annos));
473 }
474
475 public boolean isAnnotated() {
476 return getMetadata(TypeMetadata.Annotations.class) != null;
477 }
478
479 @Override @DefinedBy(Api.LANGUAGE_MODEL)
480 public List<Attribute.TypeCompound> getAnnotationMirrors() {
481 return getMetadata(TypeMetadata.Annotations.class, Annotations::annotations, List.nil());
482 }
483
484 /** Return the base types of a list of types.
485 */
486 public static List<Type> baseTypes(List<Type> ts) {
487 if (ts.nonEmpty()) {
488 Type t = ts.head.baseType();
489 List<Type> baseTypes = baseTypes(ts.tail);
490 if (t != ts.head || baseTypes != ts.tail)
491 return baseTypes.prepend(t);
492 }
493 return ts;
494 }
495
496 protected void appendAnnotationsString(StringBuilder sb,
497 boolean prefix) {
498 if (isAnnotated()) {
499 if (prefix) {
500 sb.append(" ");
501 }
502 sb.append(getAnnotationMirrors().toString(" "));
503 sb.append(" ");
504 }
505 }
506
507 protected void appendAnnotationsString(StringBuilder sb) {
508 appendAnnotationsString(sb, false);
509 }
510
511 /** The Java source which this type represents.
512 */
513 @DefinedBy(Api.LANGUAGE_MODEL)
514 public String toString() {
515 StringBuilder sb = new StringBuilder();
516 appendAnnotationsString(sb);
517 if (tsym == null || tsym.name == null) {
518 sb.append("<none>");
519 } else {
520 sb.append(tsym.name.toString());
521 }
522 if (moreInfo && hasTag(TYPEVAR)) {
523 sb.append(hashCode());
524 }
525 return sb.toString();
526 }
527
528 /**
529 * The Java source which this type list represents. A List is
530 * represented as a comma-separated listing of the elements in
531 * that list.
532 */
533 public static String toString(List<Type> ts) {
534 if (ts.isEmpty()) {
535 return "";
536 } else {
537 StringBuilder buf = new StringBuilder();
538 buf.append(ts.head.toString());
539 for (List<Type> l = ts.tail; l.nonEmpty(); l = l.tail)
540 buf.append(",").append(l.head.toString());
541 return buf.toString();
542 }
543 }
544
545 /**
546 * The constant value of this type, converted to String
547 */
548 public String stringValue() {
549 Object cv = Assert.checkNonNull(constValue());
550 return cv.toString();
551 }
552
553 /**
554 * Override this method with care. For most Type instances this should behave as ==.
555 */
556 @Override @DefinedBy(Api.LANGUAGE_MODEL)
557 public boolean equals(Object t) {
558 return this == t;
559 }
560
561 public boolean equalsIgnoreMetadata(Type t) {
562 return typeNoMetadata().equals(t.typeNoMetadata());
563 }
564
565 @Override @DefinedBy(Api.LANGUAGE_MODEL)
566 public int hashCode() {
567 return super.hashCode();
568 }
569
570 public String argtypes(boolean varargs) {
571 List<Type> args = getParameterTypes();
572 if (!varargs) return args.toString();
573 StringBuilder buf = new StringBuilder();
574 while (args.tail.nonEmpty()) {
575 buf.append(args.head);
576 args = args.tail;
577 buf.append(',');
578 }
579 if (args.head.hasTag(ARRAY)) {
580 buf.append(((ArrayType)args.head).elemtype);
581 if (args.head.getAnnotationMirrors().nonEmpty()) {
582 buf.append(args.head.getAnnotationMirrors());
583 }
584 buf.append("...");
585 } else {
586 buf.append(args.head);
587 }
588 return buf.toString();
589 }
590
591 /** Access methods.
592 */
593 public List<Type> getTypeArguments() { return List.nil(); }
594 public Type getEnclosingType() { return null; }
595 public List<Type> getParameterTypes() { return List.nil(); }
596 public Type getReturnType() { return null; }
597 public Type getReceiverType() { return null; }
598 public List<Type> getThrownTypes() { return List.nil(); }
599 public Type getUpperBound() { return null; }
600 public Type getLowerBound() { return null; }
601
602 /* Navigation methods, these will work for classes, type variables,
603 * foralls, but will return null for arrays and methods.
604 */
605
606 /** Return all parameters of this type and all its outer types in order
607 * outer (first) to inner (last).
608 */
609 public List<Type> allparams() { return List.nil(); }
610
611 /** Does this type contain "error" elements?
612 */
613 public boolean isErroneous() {
614 return false;
615 }
616
617 public static boolean isErroneous(List<Type> ts) {
618 for (List<Type> l = ts; l.nonEmpty(); l = l.tail)
619 if (l.head.isErroneous()) return true;
620 return false;
621 }
622
623 /** Is this type parameterized?
624 * A class type is parameterized if it has some parameters.
625 * An array type is parameterized if its element type is parameterized.
626 * All other types are not parameterized.
627 */
628 public boolean isParameterized() {
629 return false;
630 }
631
632 /** Is this type a raw type?
633 * A class type is a raw type if it misses some of its parameters.
634 * An array type is a raw type if its element type is raw.
635 * All other types are not raw.
636 * Type validation will ensure that the only raw types
637 * in a program are types that miss all their type variables.
638 */
639 public boolean isRaw() {
640 return false;
641 }
642
643 /**
644 * A compound type is a special class type whose supertypes are used to store a list
645 * of component types. There are two kinds of compound types: (i) intersection types
646 * {@link IntersectionClassType} and (ii) union types {@link UnionClassType}.
647 */
648 public boolean isCompound() {
649 return false;
650 }
651
652 public boolean isIntersection() {
653 return false;
654 }
655
656 public boolean isUnion() {
657 return false;
658 }
659
660 public boolean isInterface() {
661 return (tsym.flags() & INTERFACE) != 0;
662 }
663
664 public boolean isFinal() {
665 return (tsym.flags() & FINAL) != 0;
666 }
667
668 public boolean isValueBased() {
669 return tsym != null && (tsym.flags_field & VALUE_BASED) != 0;
670 }
671
672 /**
673 * Does this type contain occurrences of type t?
674 */
675 public boolean contains(Type t) {
676 return t.equalsIgnoreMetadata(this);
677 }
678
679 public static boolean contains(List<Type> ts, Type t) {
680 for (List<Type> l = ts;
681 l.tail != null /*inlined: l.nonEmpty()*/;
682 l = l.tail)
683 if (l.head.contains(t)) return true;
684 return false;
685 }
686
687 /** Does this type contain an occurrence of some type in 'ts'?
688 */
689 public boolean containsAny(List<Type> ts) {
690 for (Type t : ts)
691 if (this.contains(t)) return true;
692 return false;
693 }
694
695 public static boolean containsAny(List<Type> ts1, List<Type> ts2) {
696 for (Type t : ts1)
697 if (t.containsAny(ts2)) return true;
698 return false;
699 }
700
701 public static List<Type> filter(List<Type> ts, Predicate<Type> tf) {
702 ListBuffer<Type> buf = new ListBuffer<>();
703 for (Type t : ts) {
704 if (tf.test(t)) {
705 buf.append(t);
706 }
707 }
708 return buf.toList();
709 }
710
711 public boolean isSuperBound() { return false; }
712 public boolean isExtendsBound() { return false; }
713 public boolean isUnbound() { return false; }
714 public Type withTypeVar(Type t) { return this; }
715
716 /** The underlying method type of this type.
717 */
718 public MethodType asMethodType() { throw new AssertionError(); }
719
720 /** Complete loading all classes in this type.
721 */
722 public void complete() {}
723
724 public TypeSymbol asElement() {
725 return tsym;
726 }
727
728 @Override @DefinedBy(Api.LANGUAGE_MODEL)
729 public TypeKind getKind() {
730 return TypeKind.OTHER;
731 }
732
733 @Override @DefinedBy(Api.LANGUAGE_MODEL)
734 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
735 throw new AssertionError();
736 }
737
738 public static class JCPrimitiveType extends Type
739 implements javax.lang.model.type.PrimitiveType {
740
741 TypeTag tag;
742
743 public JCPrimitiveType(TypeTag tag, TypeSymbol tsym) {
744 this(tag, tsym, List.nil());
745 }
746
747 private JCPrimitiveType(TypeTag tag, TypeSymbol tsym, List<TypeMetadata> metadata) {
748 super(tsym, metadata);
749 this.tag = tag;
750 Assert.check(tag.isPrimitive);
751 }
752
753 @Override
754 protected JCPrimitiveType cloneWithMetadata(List<TypeMetadata> md) {
755 return new JCPrimitiveType(tag, tsym, md) {
756 @Override
757 public Type baseType() { return JCPrimitiveType.this.baseType(); }
758 };
759 }
760
761 @Override
762 public boolean isNumeric() {
763 return tag != BOOLEAN;
764 }
765
766 @Override
767 public boolean isIntegral() {
768 switch (tag) {
769 case CHAR:
770 case BYTE:
771 case SHORT:
772 case INT:
773 case LONG:
774 return true;
775 default:
776 return false;
777 }
778 }
779
780 @Override
781 public boolean isPrimitive() {
782 return true;
783 }
784
785 @Override
786 public TypeTag getTag() {
787 return tag;
788 }
789
790 @Override
791 public boolean isPrimitiveOrVoid() {
792 return true;
793 }
794
795 /** Define a constant type, of the same kind as this type
796 * and with given constant value
797 */
798 @Override
799 public Type constType(Object constValue) {
800 return addMetadata(new ConstantValue(constValue));
801 }
802
803 /**
804 * The constant value of this type, converted to String
805 */
806 @Override
807 public String stringValue() {
808 Object cv = Assert.checkNonNull(constValue());
809 if (tag == BOOLEAN) {
810 return ((Integer) cv).intValue() == 0 ? "false" : "true";
811 }
812 else if (tag == CHAR) {
813 return String.valueOf((char) ((Integer) cv).intValue());
814 }
815 else {
816 return cv.toString();
817 }
818 }
819
820 /** Is this a constant type whose value is false?
821 */
822 @Override
823 public boolean isFalse() {
824 return
825 tag == BOOLEAN &&
826 constValue() != null &&
827 ((Integer)constValue()).intValue() == 0;
828 }
829
830 /** Is this a constant type whose value is true?
831 */
832 @Override
833 public boolean isTrue() {
834 return
835 tag == BOOLEAN &&
836 constValue() != null &&
837 ((Integer)constValue()).intValue() != 0;
838 }
839
840 @Override @DefinedBy(Api.LANGUAGE_MODEL)
841 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
842 return v.visitPrimitive(this, p);
843 }
844
845 @Override @DefinedBy(Api.LANGUAGE_MODEL)
846 public TypeKind getKind() {
847 switch (tag) {
848 case BYTE: return TypeKind.BYTE;
849 case CHAR: return TypeKind.CHAR;
850 case SHORT: return TypeKind.SHORT;
851 case INT: return TypeKind.INT;
852 case LONG: return TypeKind.LONG;
853 case FLOAT: return TypeKind.FLOAT;
854 case DOUBLE: return TypeKind.DOUBLE;
855 case BOOLEAN: return TypeKind.BOOLEAN;
856 }
857 throw new AssertionError();
858 }
859
860 }
861
862 public static class WildcardType extends Type
863 implements javax.lang.model.type.WildcardType {
864
865 public Type type;
866 public BoundKind kind;
867 public TypeVar bound;
868
869 @Override
870 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
871 return v.visitWildcardType(this, s);
872 }
873
874 public WildcardType(Type type, BoundKind kind, TypeSymbol tsym) {
875 this(type, kind, tsym, null, List.nil());
876 }
877
878 public WildcardType(Type type, BoundKind kind, TypeSymbol tsym,
879 List<TypeMetadata> metadata) {
880 this(type, kind, tsym, null, metadata);
881 }
882
883 public WildcardType(Type type, BoundKind kind, TypeSymbol tsym,
884 TypeVar bound) {
885 this(type, kind, tsym, bound, List.nil());
886 }
887
888 public WildcardType(Type type, BoundKind kind, TypeSymbol tsym,
889 TypeVar bound, List<TypeMetadata> metadata) {
890 super(tsym, metadata);
891 this.type = Assert.checkNonNull(type);
892 this.kind = kind;
893 this.bound = bound;
894 }
895
896 @Override
897 protected WildcardType cloneWithMetadata(List<TypeMetadata> md) {
898 return new WildcardType(type, kind, tsym, bound, md) {
899 @Override
900 public Type baseType() { return WildcardType.this.baseType(); }
901 };
902 }
903
904 @Override
905 public TypeTag getTag() {
906 return WILDCARD;
907 }
908
909 @Override
910 public boolean contains(Type t) {
911 return kind != UNBOUND && type.contains(t);
912 }
913
914 public boolean isSuperBound() {
915 return kind == SUPER ||
916 kind == UNBOUND;
917 }
918 public boolean isExtendsBound() {
919 return kind == EXTENDS ||
920 kind == UNBOUND;
921 }
922 public boolean isUnbound() {
923 // is it `?` or `? extends Object`?
924 return kind == UNBOUND ||
925 (kind == EXTENDS && type.tsym.flatName() == type.tsym.name.table.names.java_lang_Object);
926 }
927
928 @Override
929 public boolean isReference() {
930 return true;
931 }
932
933 @Override
934 public boolean isNullOrReference() {
935 return true;
936 }
937
938 @Override
939 public Type withTypeVar(Type t) {
940 //-System.err.println(this+".withTypeVar("+t+");");//DEBUG
941 if (bound == t)
942 return this;
943 bound = (TypeVar)t;
944 return this;
945 }
946
947 boolean isPrintingBound = false;
948 @DefinedBy(Api.LANGUAGE_MODEL)
949 public String toString() {
950 StringBuilder s = new StringBuilder();
951 appendAnnotationsString(s);
952 s.append(kind.toString());
953 if (kind != UNBOUND)
954 s.append(type);
955 if (moreInfo && bound != null && !isPrintingBound)
956 try {
957 isPrintingBound = true;
958 s.append("{:").append(bound.getUpperBound()).append(":}");
959 } finally {
960 isPrintingBound = false;
961 }
962 return s.toString();
963 }
964
965 @DefinedBy(Api.LANGUAGE_MODEL)
966 public Type getExtendsBound() {
967 if (kind == EXTENDS)
968 return type;
969 else
970 return null;
971 }
972
973 @DefinedBy(Api.LANGUAGE_MODEL)
974 public Type getSuperBound() {
975 if (kind == SUPER)
976 return type;
977 else
978 return null;
979 }
980
981 @DefinedBy(Api.LANGUAGE_MODEL)
982 public TypeKind getKind() {
983 return TypeKind.WILDCARD;
984 }
985
986 @DefinedBy(Api.LANGUAGE_MODEL)
987 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
988 return v.visitWildcard(this, p);
989 }
990 }
991
992 public static class ClassType extends Type implements DeclaredType, LoadableConstant,
993 javax.lang.model.type.ErrorType {
994
995 /** The enclosing type of this type. If this is the type of an inner
996 * class, outer_field refers to the type of its enclosing
997 * instance class, in all other cases it refers to noType.
998 */
999 private Type outer_field;
1000
1001 /** The type parameters of this type (to be set once class is loaded).
1002 */
1003 public List<Type> typarams_field;
1004
1005 /** A cache variable for the type parameters of this type,
1006 * appended to all parameters of its enclosing class.
1007 * @see #allparams
1008 */
1009 public List<Type> allparams_field;
1010
1011 /** The supertype of this class (to be set once class is loaded).
1012 */
1013 public Type supertype_field;
1014
1015 /** The interfaces of this class (to be set once class is loaded).
1016 */
1017 public List<Type> interfaces_field;
1018
1019 /** All the interfaces of this class, including missing ones.
1020 */
1021 public List<Type> all_interfaces_field;
1022
1023 public ClassType(Type outer, List<Type> typarams, TypeSymbol tsym) {
1024 this(outer, typarams, tsym, List.nil());
1025 }
1026
1027 public ClassType(Type outer, List<Type> typarams, TypeSymbol tsym,
1028 List<TypeMetadata> metadata) {
1029 super(tsym, metadata);
1030 this.outer_field = outer;
1031 this.typarams_field = typarams;
1032 this.allparams_field = null;
1033 this.supertype_field = null;
1034 this.interfaces_field = null;
1035 }
1036
1037 public int poolTag() {
1038 return ClassFile.CONSTANT_Class;
1039 }
1040
1041 @Override
1042 protected ClassType cloneWithMetadata(List<TypeMetadata> md) {
1043 return new ClassType(outer_field, typarams_field, tsym, md) {
1044 @Override
1045 public Type baseType() { return ClassType.this.baseType(); }
1046 };
1047 }
1048
1049 @Override
1050 public TypeTag getTag() {
1051 return CLASS;
1052 }
1053
1054 @Override
1055 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1056 return v.visitClassType(this, s);
1057 }
1058
1059 public Type constType(Object constValue) {
1060 return addMetadata(new ConstantValue(constValue));
1061 }
1062
1063 /** The Java source which this type represents.
1064 */
1065 @DefinedBy(Api.LANGUAGE_MODEL)
1066 public String toString() {
1067 StringBuilder buf = new StringBuilder();
1068 if (getEnclosingType().hasTag(CLASS) && tsym.owner.kind == TYP) {
1069 buf.append(getEnclosingType().toString());
1070 buf.append(".");
1071 appendAnnotationsString(buf);
1072 buf.append(className(tsym, false));
1073 } else {
1074 if (isAnnotated()) {
1075 if (!tsym.packge().isUnnamed()) {
1076 buf.append(tsym.packge());
1077 buf.append(".");
1078 }
1079 ListBuffer<Name> names = new ListBuffer<>();
1080 for (Symbol sym = tsym.owner; sym != null && sym.kind == TYP; sym = sym.owner) {
1081 names.prepend(sym.name);
1082 }
1083 for (Name name : names) {
1084 buf.append(name);
1085 buf.append(".");
1086 }
1087 appendAnnotationsString(buf);
1088 buf.append(tsym.name);
1089 } else {
1090 buf.append(className(tsym, true));
1091 }
1092 }
1093
1094 if (getTypeArguments().nonEmpty()) {
1095 buf.append('<');
1096 buf.append(getTypeArguments().toString());
1097 buf.append(">");
1098 }
1099 return buf.toString();
1100 }
1101 //where
1102 private String className(Symbol sym, boolean longform) {
1103 if (sym.name.isEmpty() && (sym.flags() & COMPOUND) != 0) {
1104 StringBuilder s = new StringBuilder(supertype_field.toString());
1105 for (List<Type> is=interfaces_field; is.nonEmpty(); is = is.tail) {
1106 s.append("&");
1107 s.append(is.head.toString());
1108 }
1109 return s.toString();
1110 } else if (sym.name.isEmpty()) {
1111 String s;
1112 ClassType norm = (ClassType) tsym.type;
1113 if (norm == null) {
1114 s = Log.getLocalizedString("anonymous.class", (Object)null);
1115 } else if (norm.interfaces_field != null && norm.interfaces_field.nonEmpty()) {
1116 s = Log.getLocalizedString("anonymous.class",
1117 norm.interfaces_field.head);
1118 } else {
1119 s = Log.getLocalizedString("anonymous.class",
1120 norm.supertype_field);
1121 }
1122 if (moreInfo)
1123 s += String.valueOf(sym.hashCode());
1124 return s;
1125 } else if (longform) {
1126 sym.apiComplete();
1127 return sym.getQualifiedName().toString();
1128 } else {
1129 return sym.name.toString();
1130 }
1131 }
1132
1133 @DefinedBy(Api.LANGUAGE_MODEL)
1134 public List<Type> getTypeArguments() {
1135 if (typarams_field == null) {
1136 complete();
1137 if (typarams_field == null)
1138 typarams_field = List.nil();
1139 }
1140 return typarams_field;
1141 }
1142
1143 public boolean hasErasedSupertypes() {
1144 return isRaw();
1145 }
1146
1147 @DefinedBy(Api.LANGUAGE_MODEL)
1148 public Type getEnclosingType() {
1149 return outer_field;
1150 }
1151
1152 public void setEnclosingType(Type outer) {
1153 outer_field = outer;
1154 }
1155
1156 public List<Type> allparams() {
1157 if (allparams_field == null) {
1158 allparams_field = getTypeArguments().prependList(getEnclosingType().allparams());
1159 }
1160 return allparams_field;
1161 }
1162
1163 public boolean isErroneous() {
1164 return
1165 getEnclosingType().isErroneous() ||
1166 isErroneous(getTypeArguments()) ||
1167 this != tsym.type && tsym.type.isErroneous();
1168 }
1169
1170 public boolean isParameterized() {
1171 return allparams().tail != null;
1172 // optimization, was: allparams().nonEmpty();
1173 }
1174
1175 @Override
1176 public boolean isReference() {
1177 return true;
1178 }
1179
1180 @Override
1181 public boolean isNullOrReference() {
1182 return true;
1183 }
1184
1185 /** A cache for the rank. */
1186 int rank_field = -1;
1187
1188 /** A class type is raw if it misses some
1189 * of its type parameter sections.
1190 * After validation, this is equivalent to:
1191 * {@code allparams.isEmpty() && tsym.type.allparams.nonEmpty(); }
1192 */
1193 public boolean isRaw() {
1194 return
1195 this != tsym.type && // necessary, but not sufficient condition
1196 tsym.type.allparams().nonEmpty() &&
1197 allparams().isEmpty();
1198 }
1199
1200 public boolean contains(Type elem) {
1201 return
1202 elem.equalsIgnoreMetadata(this)
1203 || (isParameterized()
1204 && (getEnclosingType().contains(elem) || contains(getTypeArguments(), elem)))
1205 || (isCompound()
1206 && (supertype_field.contains(elem) || contains(interfaces_field, elem)));
1207 }
1208
1209 public void complete() {
1210 tsym.complete();
1211 }
1212
1213 @DefinedBy(Api.LANGUAGE_MODEL)
1214 public TypeKind getKind() {
1215 tsym.apiComplete();
1216 return tsym.kind == TYP ? TypeKind.DECLARED : TypeKind.ERROR;
1217 }
1218
1219 @DefinedBy(Api.LANGUAGE_MODEL)
1220 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1221 return v.visitDeclared(this, p);
1222 }
1223 }
1224
1225 public static class ErasedClassType extends ClassType {
1226 public ErasedClassType(Type outer, TypeSymbol tsym,
1227 List<TypeMetadata> metadata) {
1228 super(outer, List.nil(), tsym, metadata);
1229 }
1230
1231 @Override
1232 public boolean hasErasedSupertypes() {
1233 return true;
1234 }
1235 }
1236
1237 // a clone of a ClassType that knows about the alternatives of a union type.
1238 public static class UnionClassType extends ClassType implements UnionType {
1239 final List<? extends Type> alternatives_field;
1240
1241 public UnionClassType(ClassType ct, List<? extends Type> alternatives) {
1242 // Presently no way to refer to this type directly, so we
1243 // cannot put annotations directly on it.
1244 super(ct.outer_field, ct.typarams_field, ct.tsym);
1245 allparams_field = ct.allparams_field;
1246 supertype_field = ct.supertype_field;
1247 interfaces_field = ct.interfaces_field;
1248 all_interfaces_field = ct.interfaces_field;
1249 alternatives_field = alternatives;
1250 }
1251
1252 public Type getLub() {
1253 return tsym.type;
1254 }
1255
1256 @DefinedBy(Api.LANGUAGE_MODEL)
1257 public java.util.List<? extends TypeMirror> getAlternatives() {
1258 return Collections.unmodifiableList(alternatives_field);
1259 }
1260
1261 @Override
1262 public boolean isUnion() {
1263 return true;
1264 }
1265
1266 @Override
1267 public boolean isCompound() {
1268 return getLub().isCompound();
1269 }
1270
1271 @Override @DefinedBy(Api.LANGUAGE_MODEL)
1272 public TypeKind getKind() {
1273 return TypeKind.UNION;
1274 }
1275
1276 @Override @DefinedBy(Api.LANGUAGE_MODEL)
1277 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1278 return v.visitUnion(this, p);
1279 }
1280
1281 public Iterable<? extends Type> getAlternativeTypes() {
1282 return alternatives_field;
1283 }
1284 }
1285
1286 // a clone of a ClassType that knows about the bounds of an intersection type.
1287 public static class IntersectionClassType extends ClassType implements IntersectionType {
1288
1289 public boolean allInterfaces;
1290
1291 public IntersectionClassType(List<Type> bounds, ClassSymbol csym, boolean allInterfaces) {
1292 // Presently no way to refer to this type directly, so we
1293 // cannot put annotations directly on it.
1294 super(Type.noType, List.nil(), csym);
1295 this.allInterfaces = allInterfaces;
1296 Assert.check((csym.flags() & COMPOUND) != 0);
1297 supertype_field = bounds.head;
1298 interfaces_field = bounds.tail;
1299 Assert.check(!supertype_field.tsym.isCompleted() ||
1300 !supertype_field.isInterface(), supertype_field);
1301 }
1302
1303 @DefinedBy(Api.LANGUAGE_MODEL)
1304 public java.util.List<? extends TypeMirror> getBounds() {
1305 return Collections.unmodifiableList(getExplicitComponents());
1306 }
1307
1308 @Override
1309 public boolean isCompound() {
1310 return true;
1311 }
1312
1313 public List<Type> getComponents() {
1314 return interfaces_field.prepend(supertype_field);
1315 }
1316
1317 @Override
1318 public boolean isIntersection() {
1319 return true;
1320 }
1321
1322 public List<Type> getExplicitComponents() {
1323 return allInterfaces ?
1324 interfaces_field :
1325 getComponents();
1326 }
1327
1328 @Override @DefinedBy(Api.LANGUAGE_MODEL)
1329 public TypeKind getKind() {
1330 return TypeKind.INTERSECTION;
1331 }
1332
1333 @Override @DefinedBy(Api.LANGUAGE_MODEL)
1334 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1335 return v.visitIntersection(this, p);
1336 }
1337 }
1338
1339 public static class ArrayType extends Type
1340 implements LoadableConstant, javax.lang.model.type.ArrayType {
1341
1342 public Type elemtype;
1343
1344 public ArrayType(Type elemtype, TypeSymbol arrayClass) {
1345 this(elemtype, arrayClass, List.nil());
1346 }
1347
1348 public ArrayType(Type elemtype, TypeSymbol arrayClass,
1349 List<TypeMetadata> metadata) {
1350 super(arrayClass, metadata);
1351 this.elemtype = elemtype;
1352 }
1353
1354 public ArrayType(ArrayType that) {
1355 //note: type metadata is deliberately shared here, as we want side-effects from annotation
1356 //processing to flow from original array to the cloned array.
1357 this(that.elemtype, that.tsym, that.getMetadata());
1358 }
1359
1360 public int poolTag() {
1361 return ClassFile.CONSTANT_Class;
1362 }
1363
1364 @Override
1365 protected ArrayType cloneWithMetadata(List<TypeMetadata> md) {
1366 return new ArrayType(elemtype, tsym, md) {
1367 @Override
1368 public Type baseType() { return ArrayType.this.baseType(); }
1369 };
1370 }
1371
1372 @Override
1373 public TypeTag getTag() {
1374 return ARRAY;
1375 }
1376
1377 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1378 return v.visitArrayType(this, s);
1379 }
1380
1381 @DefinedBy(Api.LANGUAGE_MODEL)
1382 public String toString() {
1383 StringBuilder sb = new StringBuilder();
1384
1385 // First append root component type
1386 Type t = elemtype;
1387 while (t.getKind() == TypeKind.ARRAY)
1388 t = ((ArrayType) t).getComponentType();
1389 sb.append(t);
1390
1391 // then append @Anno[] @Anno[] ... @Anno[]
1392 t = this;
1393 do {
1394 t.appendAnnotationsString(sb, true);
1395 sb.append("[]");
1396 t = ((ArrayType) t).getComponentType();
1397 } while (t.getKind() == TypeKind.ARRAY);
1398
1399 return sb.toString();
1400 }
1401
1402 @Override @DefinedBy(Api.LANGUAGE_MODEL)
1403 public boolean equals(Object obj) {
1404 return (obj instanceof ArrayType arrayType)
1405 && (this == arrayType || elemtype.equals(arrayType.elemtype));
1406 }
1407
1408 @DefinedBy(Api.LANGUAGE_MODEL)
1409 public int hashCode() {
1410 return (ARRAY.ordinal() << 5) + elemtype.hashCode();
1411 }
1412
1413 public boolean isVarargs() {
1414 return false;
1415 }
1416
1417 public List<Type> allparams() { return elemtype.allparams(); }
1418
1419 public boolean isErroneous() {
1420 return elemtype.isErroneous();
1421 }
1422
1423 public boolean isParameterized() {
1424 return elemtype.isParameterized();
1425 }
1426
1427 @Override
1428 public boolean isReference() {
1429 return true;
1430 }
1431
1432 @Override
1433 public boolean isNullOrReference() {
1434 return true;
1435 }
1436
1437 public boolean isRaw() {
1438 return elemtype.isRaw();
1439 }
1440
1441 public ArrayType makeVarargs() {
1442 return new ArrayType(elemtype, tsym, metadata) {
1443 @Override
1444 public boolean isVarargs() {
1445 return true;
1446 }
1447 };
1448 }
1449
1450 public boolean contains(Type elem) {
1451 return elem.equalsIgnoreMetadata(this) || elemtype.contains(elem);
1452 }
1453
1454 public void complete() {
1455 elemtype.complete();
1456 }
1457
1458 @DefinedBy(Api.LANGUAGE_MODEL)
1459 public Type getComponentType() {
1460 return elemtype;
1461 }
1462
1463 @DefinedBy(Api.LANGUAGE_MODEL)
1464 public TypeKind getKind() {
1465 return TypeKind.ARRAY;
1466 }
1467
1468 @DefinedBy(Api.LANGUAGE_MODEL)
1469 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1470 return v.visitArray(this, p);
1471 }
1472 }
1473
1474 public static class MethodType extends Type implements ExecutableType, LoadableConstant {
1475
1476 public List<Type> argtypes;
1477 public Type restype;
1478 public List<Type> thrown;
1479
1480 /** The type annotations on the method receiver.
1481 */
1482 public Type recvtype;
1483
1484 public MethodType(List<Type> argtypes,
1485 Type restype,
1486 List<Type> thrown,
1487 TypeSymbol methodClass) {
1488 // Presently no way to refer to a method type directly, so
1489 // we cannot put type annotations on it.
1490 super(methodClass, List.nil());
1491 this.argtypes = argtypes;
1492 this.restype = restype;
1493 this.thrown = thrown;
1494 }
1495
1496 @Override
1497 public TypeTag getTag() {
1498 return METHOD;
1499 }
1500
1501 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1502 return v.visitMethodType(this, s);
1503 }
1504
1505 /** The Java source which this type represents.
1506 *
1507 * XXX 06/09/99 iris This isn't correct Java syntax, but it probably
1508 * should be.
1509 */
1510 @DefinedBy(Api.LANGUAGE_MODEL)
1511 public String toString() {
1512 StringBuilder sb = new StringBuilder();
1513 appendAnnotationsString(sb);
1514 sb.append('(');
1515 sb.append(argtypes);
1516 sb.append(')');
1517 sb.append(restype);
1518 return sb.toString();
1519 }
1520
1521 @DefinedBy(Api.LANGUAGE_MODEL)
1522 public List<Type> getParameterTypes() { return argtypes; }
1523 @DefinedBy(Api.LANGUAGE_MODEL)
1524 public Type getReturnType() { return restype; }
1525 @DefinedBy(Api.LANGUAGE_MODEL)
1526 public Type getReceiverType() {
1527 return (recvtype == null) ? Type.noType : recvtype;
1528 }
1529 @DefinedBy(Api.LANGUAGE_MODEL)
1530 public List<Type> getThrownTypes() { return thrown; }
1531
1532 public boolean isErroneous() {
1533 return
1534 isErroneous(argtypes) ||
1535 restype != null && restype.isErroneous();
1536 }
1537
1538 @Override
1539 public int poolTag() {
1540 return ClassFile.CONSTANT_MethodType;
1541 }
1542
1543 public boolean contains(Type elem) {
1544 return elem.equalsIgnoreMetadata(this) || contains(argtypes, elem) || restype.contains(elem) || contains(thrown, elem);
1545 }
1546
1547 public MethodType asMethodType() { return this; }
1548
1549 public void complete() {
1550 for (List<Type> l = argtypes; l.nonEmpty(); l = l.tail)
1551 l.head.complete();
1552 restype.complete();
1553 recvtype.complete();
1554 for (List<Type> l = thrown; l.nonEmpty(); l = l.tail)
1555 l.head.complete();
1556 }
1557
1558 @DefinedBy(Api.LANGUAGE_MODEL)
1559 public List<TypeVar> getTypeVariables() {
1560 return List.nil();
1561 }
1562
1563 public TypeSymbol asElement() {
1564 return null;
1565 }
1566
1567 @DefinedBy(Api.LANGUAGE_MODEL)
1568 public TypeKind getKind() {
1569 return TypeKind.EXECUTABLE;
1570 }
1571
1572 @DefinedBy(Api.LANGUAGE_MODEL)
1573 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1574 return v.visitExecutable(this, p);
1575 }
1576 }
1577
1578 public static class PackageType extends Type implements NoType {
1579
1580 PackageType(PackageSymbol tsym) {
1581 // Package types cannot be annotated
1582 super(tsym, List.nil());
1583 }
1584
1585 @Override
1586 public TypeTag getTag() {
1587 return PACKAGE;
1588 }
1589
1590 @Override
1591 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1592 return v.visitPackageType(this, s);
1593 }
1594
1595 @DefinedBy(Api.LANGUAGE_MODEL)
1596 public String toString() {
1597 return tsym.getQualifiedName().toString();
1598 }
1599
1600 @DefinedBy(Api.LANGUAGE_MODEL)
1601 public TypeKind getKind() {
1602 return TypeKind.PACKAGE;
1603 }
1604
1605 @DefinedBy(Api.LANGUAGE_MODEL)
1606 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1607 return v.visitNoType(this, p);
1608 }
1609 }
1610
1611 public static class ModuleType extends Type implements NoType {
1612
1613 ModuleType(ModuleSymbol tsym) {
1614 // Module types cannot be annotated
1615 super(tsym, List.nil());
1616 }
1617
1618 @Override
1619 public ModuleType annotatedType(List<Attribute.TypeCompound> annos) {
1620 throw new AssertionError("Cannot annotate a module type");
1621 }
1622
1623 @Override
1624 public TypeTag getTag() {
1625 return TypeTag.MODULE;
1626 }
1627
1628 @Override
1629 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1630 return v.visitModuleType(this, s);
1631 }
1632
1633 @Override @DefinedBy(Api.LANGUAGE_MODEL)
1634 public String toString() {
1635 return tsym.getQualifiedName().toString();
1636 }
1637
1638 @Override @DefinedBy(Api.LANGUAGE_MODEL)
1639 public TypeKind getKind() {
1640 return TypeKind.MODULE;
1641 }
1642
1643 @Override @DefinedBy(Api.LANGUAGE_MODEL)
1644 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1645 return v.visitNoType(this, p);
1646 }
1647 }
1648
1649 public static class TypeVar extends Type implements TypeVariable {
1650
1651 /** The upper bound of this type variable; set from outside.
1652 * Must be nonempty once it is set.
1653 * For a bound, `bound' is the bound type itself.
1654 * Multiple bounds are expressed as a single class type which has the
1655 * individual bounds as superclass, respectively interfaces.
1656 * The class type then has as `tsym' a compiler generated class `c',
1657 * which has a flag COMPOUND and whose owner is the type variable
1658 * itself. Furthermore, the erasure_field of the class
1659 * points to the first class or interface bound.
1660 */
1661 private Type _bound = null;
1662
1663 /** The lower bound of this type variable.
1664 * TypeVars don't normally have a lower bound, so it is normally set
1665 * to syms.botType.
1666 * Subtypes, such as CapturedType, may provide a different value.
1667 */
1668 public Type lower;
1669
1670 @SuppressWarnings("this-escape")
1671 public TypeVar(Name name, Symbol owner, Type lower) {
1672 super(null, List.nil());
1673 Assert.checkNonNull(lower);
1674 tsym = new TypeVariableSymbol(0, name, this, owner);
1675 this.setUpperBound(null);
1676 this.lower = lower;
1677 }
1678
1679 public TypeVar(TypeSymbol tsym, Type bound, Type lower) {
1680 this(tsym, bound, lower, List.nil());
1681 }
1682
1683 @SuppressWarnings("this-escape")
1684 public TypeVar(TypeSymbol tsym, Type bound, Type lower,
1685 List<TypeMetadata> metadata) {
1686 super(tsym, metadata);
1687 Assert.checkNonNull(lower);
1688 this.setUpperBound(bound);
1689 this.lower = lower;
1690 }
1691
1692 @Override
1693 protected TypeVar cloneWithMetadata(List<TypeMetadata> md) {
1694 return new TypeVar(tsym, getUpperBound(), lower, md) {
1695 @Override
1696 public Type baseType() { return TypeVar.this.baseType(); }
1697
1698 @Override @DefinedBy(Api.LANGUAGE_MODEL)
1699 public Type getUpperBound() { return TypeVar.this.getUpperBound(); }
1700
1701 public void setUpperBound(Type bound) { TypeVar.this.setUpperBound(bound); }
1702 };
1703 }
1704
1705 @Override
1706 public TypeTag getTag() {
1707 return TYPEVAR;
1708 }
1709
1710 @Override
1711 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1712 return v.visitTypeVar(this, s);
1713 }
1714
1715 @Override @DefinedBy(Api.LANGUAGE_MODEL)
1716 public Type getUpperBound() { return _bound; }
1717
1718 public void setUpperBound(Type bound) { this._bound = bound; }
1719
1720 int rank_field = -1;
1721
1722 @Override @DefinedBy(Api.LANGUAGE_MODEL)
1723 public Type getLowerBound() {
1724 return lower;
1725 }
1726
1727 @DefinedBy(Api.LANGUAGE_MODEL)
1728 public TypeKind getKind() {
1729 return TypeKind.TYPEVAR;
1730 }
1731
1732 public boolean isCaptured() {
1733 return false;
1734 }
1735
1736 @Override
1737 public boolean isReference() {
1738 return true;
1739 }
1740
1741 @Override
1742 public boolean isNullOrReference() {
1743 return true;
1744 }
1745
1746 @Override @DefinedBy(Api.LANGUAGE_MODEL)
1747 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1748 return v.visitTypeVariable(this, p);
1749 }
1750 }
1751
1752 /** A captured type variable comes from wildcards which can have
1753 * both upper and lower bound. CapturedType extends TypeVar with
1754 * a lower bound.
1755 */
1756 public static class CapturedType extends TypeVar {
1757
1758 public WildcardType wildcard;
1759
1760 @SuppressWarnings("this-escape")
1761 public CapturedType(Name name,
1762 Symbol owner,
1763 Type upper,
1764 Type lower,
1765 WildcardType wildcard) {
1766 super(name, owner, lower);
1767 this.lower = Assert.checkNonNull(lower);
1768 this.setUpperBound(upper);
1769 this.wildcard = wildcard;
1770 }
1771
1772 public CapturedType(TypeSymbol tsym,
1773 Type bound,
1774 Type upper,
1775 Type lower,
1776 WildcardType wildcard,
1777 List<TypeMetadata> metadata) {
1778 super(tsym, bound, lower, metadata);
1779 this.wildcard = wildcard;
1780 }
1781
1782 @Override
1783 protected CapturedType cloneWithMetadata(List<TypeMetadata> md) {
1784 return new CapturedType(tsym, getUpperBound(), getUpperBound(), lower, wildcard, md) {
1785 @Override
1786 public Type baseType() { return CapturedType.this.baseType(); }
1787
1788 @Override @DefinedBy(Api.LANGUAGE_MODEL)
1789 public Type getUpperBound() { return CapturedType.this.getUpperBound(); }
1790
1791 public void setUpperBound(Type bound) { CapturedType.this.setUpperBound(bound); }
1792 };
1793 }
1794
1795 @Override
1796 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1797 return v.visitCapturedType(this, s);
1798 }
1799
1800 @Override
1801 public boolean isCaptured() {
1802 return true;
1803 }
1804
1805 @Override @DefinedBy(Api.LANGUAGE_MODEL)
1806 public String toString() {
1807 StringBuilder sb = new StringBuilder();
1808 appendAnnotationsString(sb);
1809 sb.append("capture#");
1810 sb.append((hashCode() & 0xFFFFFFFFL) % Printer.PRIME);
1811 sb.append(" of ");
1812 sb.append(wildcard);
1813 return sb.toString();
1814 }
1815 }
1816
1817 public abstract static class DelegatedType extends Type {
1818 public Type qtype;
1819 public TypeTag tag;
1820
1821 public DelegatedType(TypeTag tag, Type qtype) {
1822 this(tag, qtype, List.nil());
1823 }
1824
1825 public DelegatedType(TypeTag tag, Type qtype,
1826 List<TypeMetadata> metadata) {
1827 super(qtype.tsym, metadata);
1828 this.tag = tag;
1829 this.qtype = qtype;
1830 }
1831
1832 public TypeTag getTag() { return tag; }
1833 @DefinedBy(Api.LANGUAGE_MODEL)
1834 public String toString() { return qtype.toString(); }
1835 public List<Type> getTypeArguments() { return qtype.getTypeArguments(); }
1836 public Type getEnclosingType() { return qtype.getEnclosingType(); }
1837 public List<Type> getParameterTypes() { return qtype.getParameterTypes(); }
1838 public Type getReturnType() { return qtype.getReturnType(); }
1839 public Type getReceiverType() { return qtype.getReceiverType(); }
1840 public List<Type> getThrownTypes() { return qtype.getThrownTypes(); }
1841 public List<Type> allparams() { return qtype.allparams(); }
1842 public Type getUpperBound() { return qtype.getUpperBound(); }
1843 public boolean isErroneous() { return qtype.isErroneous(); }
1844 }
1845
1846 /**
1847 * The type of a generic method type. It consists of a method type and
1848 * a list of method type-parameters that are used within the method
1849 * type.
1850 */
1851 public static class ForAll extends DelegatedType implements ExecutableType {
1852 public List<Type> tvars;
1853
1854 public ForAll(List<Type> tvars, Type qtype) {
1855 super(FORALL, (MethodType)qtype);
1856 this.tvars = tvars;
1857 }
1858
1859 @Override
1860 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1861 return v.visitForAll(this, s);
1862 }
1863
1864 @DefinedBy(Api.LANGUAGE_MODEL)
1865 public String toString() {
1866 StringBuilder sb = new StringBuilder();
1867 appendAnnotationsString(sb);
1868 sb.append('<');
1869 sb.append(tvars);
1870 sb.append('>');
1871 sb.append(qtype);
1872 return sb.toString();
1873 }
1874
1875 public List<Type> getTypeArguments() { return tvars; }
1876
1877 public boolean isErroneous() {
1878 return qtype.isErroneous();
1879 }
1880
1881 public boolean contains(Type elem) {
1882 return qtype.contains(elem);
1883 }
1884
1885 public MethodType asMethodType() {
1886 return (MethodType)qtype;
1887 }
1888
1889 public void complete() {
1890 for (List<Type> l = tvars; l.nonEmpty(); l = l.tail) {
1891 ((TypeVar)l.head).getUpperBound().complete();
1892 }
1893 qtype.complete();
1894 }
1895
1896 @DefinedBy(Api.LANGUAGE_MODEL)
1897 public List<TypeVar> getTypeVariables() {
1898 return List.convert(TypeVar.class, getTypeArguments());
1899 }
1900
1901 @DefinedBy(Api.LANGUAGE_MODEL)
1902 public TypeKind getKind() {
1903 return TypeKind.EXECUTABLE;
1904 }
1905
1906 @DefinedBy(Api.LANGUAGE_MODEL)
1907 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1908 return v.visitExecutable(this, p);
1909 }
1910 }
1911
1912 /** A class for inference variables, for use during method/diamond type
1913 * inference. An inference variable has upper/lower bounds and a set
1914 * of equality constraints. Such bounds are set during subtyping, type-containment,
1915 * type-equality checks, when the types being tested contain inference variables.
1916 * A change listener can be attached to an inference variable, to receive notifications
1917 * whenever the bounds of an inference variable change.
1918 */
1919 public static class UndetVar extends DelegatedType {
1920
1921 enum Kind {
1922 NORMAL,
1923 CAPTURED,
1924 THROWS;
1925 }
1926
1927 /** Inference variable change listener. The listener method is called
1928 * whenever a change to the inference variable's bounds occurs
1929 */
1930 public interface UndetVarListener {
1931 /** called when some inference variable bounds (of given kinds ibs) change */
1932 void varBoundChanged(UndetVar uv, InferenceBound ib, Type bound, boolean update);
1933 /** called when the inferred type is set on some inference variable */
1934 default void varInstantiated(UndetVar uv) { Assert.error(); }
1935 }
1936
1937 /**
1938 * Inference variable bound kinds
1939 */
1940 public enum InferenceBound {
1941 /** lower bounds */
1942 LOWER {
1943 public InferenceBound complement() { return UPPER; }
1944 },
1945 /** equality constraints */
1946 EQ {
1947 public InferenceBound complement() { return EQ; }
1948 },
1949 /** upper bounds */
1950 UPPER {
1951 public InferenceBound complement() { return LOWER; }
1952 };
1953
1954 public abstract InferenceBound complement();
1955
1956 public boolean lessThan(InferenceBound that) {
1957 if (that == this) {
1958 return false;
1959 } else {
1960 switch (that) {
1961 case UPPER: return true;
1962 case LOWER: return false;
1963 case EQ: return (this != UPPER);
1964 default:
1965 Assert.error("Cannot get here!");
1966 return false;
1967 }
1968 }
1969 }
1970 }
1971
1972 /** list of incorporation actions (used by the incorporation engine). */
1973 public ArrayDeque<IncorporationAction> incorporationActions = new ArrayDeque<>();
1974
1975 /** inference variable bounds */
1976 protected Map<InferenceBound, List<Type>> bounds;
1977
1978 /** inference variable's inferred type (set from Infer.java) */
1979 private Type inst = null;
1980
1981 /** number of declared (upper) bounds */
1982 public int declaredCount;
1983
1984 /** inference variable's change listener */
1985 public UndetVarListener listener = null;
1986
1987 Kind kind;
1988
1989 @Override
1990 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1991 return v.visitUndetVar(this, s);
1992 }
1993
1994 @SuppressWarnings("this-escape")
1995 public UndetVar(TypeVar origin, UndetVarListener listener, Types types) {
1996 // This is a synthesized internal type, so we cannot annotate it.
1997 super(UNDETVAR, origin);
1998 this.kind = origin.isCaptured() ?
1999 Kind.CAPTURED :
2000 Kind.NORMAL;
2001 this.listener = listener;
2002 bounds = new EnumMap<>(InferenceBound.class);
2003 List<Type> declaredBounds = types.getBounds(origin);
2004 declaredCount = declaredBounds.length();
2005 bounds.put(InferenceBound.UPPER, List.nil());
2006 bounds.put(InferenceBound.LOWER, List.nil());
2007 bounds.put(InferenceBound.EQ, List.nil());
2008 for (Type t : declaredBounds.reverse()) {
2009 //add bound works in reverse order
2010 addBound(InferenceBound.UPPER, t, types, true);
2011 }
2012 if (origin.isCaptured() && !origin.lower.hasTag(BOT)) {
2013 //add lower bound if needed
2014 addBound(InferenceBound.LOWER, origin.lower, types, true);
2015 }
2016 }
2017
2018 @DefinedBy(Api.LANGUAGE_MODEL)
2019 public String toString() {
2020 StringBuilder sb = new StringBuilder();
2021 appendAnnotationsString(sb);
2022 if (inst == null) {
2023 sb.append(qtype);
2024 sb.append('?');
2025 } else {
2026 sb.append(inst);
2027 }
2028 return sb.toString();
2029 }
2030
2031 public String debugString() {
2032 String result = "inference var = " + qtype + "\n";
2033 if (inst != null) {
2034 result += "inst = " + inst + '\n';
2035 }
2036 for (InferenceBound bound: InferenceBound.values()) {
2037 List<Type> aboundList = bounds.get(bound);
2038 if (aboundList != null && aboundList.size() > 0) {
2039 result += bound + " = " + aboundList + '\n';
2040 }
2041 }
2042 return result;
2043 }
2044
2045 public void setThrow() {
2046 if (this.kind == Kind.CAPTURED) {
2047 //invalid state transition
2048 throw new IllegalStateException();
2049 }
2050 this.kind = Kind.THROWS;
2051 }
2052
2053 public void setNormal() {
2054 Assert.check(this.kind == Kind.CAPTURED);
2055 this.kind = Kind.NORMAL;
2056 }
2057
2058 /**
2059 * Returns a new copy of this undet var.
2060 */
2061 public UndetVar dup(Types types) {
2062 UndetVar uv2 = new UndetVar((TypeVar)qtype, listener, types);
2063 dupTo(uv2, types);
2064 return uv2;
2065 }
2066
2067 /**
2068 * Dumps the contents of this undet var on another undet var.
2069 */
2070 public void dupTo(UndetVar uv2, Types types) {
2071 uv2.listener = null;
2072 uv2.bounds.clear();
2073 for (InferenceBound ib : InferenceBound.values()) {
2074 uv2.bounds.put(ib, List.nil());
2075 for (Type t : getBounds(ib)) {
2076 uv2.addBound(ib, t, types, true);
2077 }
2078 }
2079 uv2.inst = inst;
2080 uv2.listener = listener;
2081 uv2.incorporationActions = new ArrayDeque<>();
2082 for (IncorporationAction action : incorporationActions) {
2083 uv2.incorporationActions.add(action.dup(uv2));
2084 }
2085 uv2.kind = kind;
2086 }
2087
2088 @Override
2089 public boolean isPartial() {
2090 return true;
2091 }
2092
2093 @Override
2094 public Type baseType() {
2095 return (inst == null) ? this : inst.baseType();
2096 }
2097
2098 public Type getInst() {
2099 return inst;
2100 }
2101
2102 public void setInst(Type inst) {
2103 this.inst = inst;
2104 if (listener != null) {
2105 listener.varInstantiated(this);
2106 }
2107 }
2108
2109 /** get all bounds of a given kind */
2110 public List<Type> getBounds(InferenceBound... ibs) {
2111 ListBuffer<Type> buf = new ListBuffer<>();
2112 for (InferenceBound ib : ibs) {
2113 buf.appendList(bounds.get(ib));
2114 }
2115 return buf.toList();
2116 }
2117
2118 /** get the list of declared (upper) bounds */
2119 public List<Type> getDeclaredBounds() {
2120 ListBuffer<Type> buf = new ListBuffer<>();
2121 int count = 0;
2122 for (Type b : getBounds(InferenceBound.UPPER)) {
2123 if (count++ == declaredCount) break;
2124 buf.append(b);
2125 }
2126 return buf.toList();
2127 }
2128
2129 /** internal method used to override an undetvar bounds */
2130 public void setBounds(InferenceBound ib, List<Type> newBounds) {
2131 bounds.put(ib, newBounds);
2132 }
2133
2134 /** add a bound of a given kind - this might trigger listener notification */
2135 public final void addBound(InferenceBound ib, Type bound, Types types) {
2136 addBound(ib, bound, types, false);
2137 }
2138
2139 private void addBound(InferenceBound ib, Type bound, Types types, boolean update) {
2140 if (kind == Kind.CAPTURED && !update) {
2141 //Captured inference variables bounds must not be updated during incorporation,
2142 //except when some inference variable (beta) has been instantiated in the
2143 //right-hand-side of a 'C<alpha> = capture(C<? extends/super beta>) constraint.
2144 if (bound.hasTag(UNDETVAR) && !((UndetVar)bound).isCaptured()) {
2145 //If the new incoming bound is itself a (regular) inference variable,
2146 //then we are allowed to propagate this inference variable bounds to it.
2147 ((UndetVar)bound).addBound(ib.complement(), this, types, false);
2148 }
2149 } else {
2150 Type bound2 = bound.map(toTypeVarMap).baseType();
2151 List<Type> prevBounds = bounds.get(ib);
2152 if (bound == qtype) return;
2153 for (Type b : prevBounds) {
2154 //check for redundancy - do not add same bound twice
2155 if (types.isSameType(b, bound2)) return;
2156 }
2157 bounds.put(ib, prevBounds.prepend(bound2));
2158 notifyBoundChange(ib, bound2, false);
2159 }
2160 }
2161 //where
2162 TypeMapping<Void> toTypeVarMap = new StructuralTypeMapping<Void>() {
2163 @Override
2164 public Type visitUndetVar(UndetVar uv, Void _unused) {
2165 return uv.inst != null ? uv.inst : uv.qtype;
2166 }
2167 };
2168
2169 /** replace types in all bounds - this might trigger listener notification */
2170 public void substBounds(List<Type> from, List<Type> to, Types types) {
2171 final ListBuffer<Pair<InferenceBound, Type>> boundsChanged = new ListBuffer<>();
2172 UndetVarListener prevListener = listener;
2173 try {
2174 //setup new listener for keeping track of changed bounds
2175 listener = (uv, ib, t, _ignored) -> {
2176 Assert.check(uv == UndetVar.this);
2177 boundsChanged.add(new Pair<>(ib, t));
2178 };
2179 for (Map.Entry<InferenceBound, List<Type>> _entry : bounds.entrySet()) {
2180 InferenceBound ib = _entry.getKey();
2181 List<Type> prevBounds = _entry.getValue();
2182 ListBuffer<Type> newBounds = new ListBuffer<>();
2183 ListBuffer<Type> deps = new ListBuffer<>();
2184 //step 1 - re-add bounds that are not dependent on ivars
2185 for (Type t : prevBounds) {
2186 if (!t.containsAny(from)) {
2187 newBounds.append(t);
2188 } else {
2189 deps.append(t);
2190 }
2191 }
2192 //step 2 - replace bounds
2193 bounds.put(ib, newBounds.toList());
2194 //step 3 - for each dependency, add new replaced bound
2195 for (Type dep : deps) {
2196 addBound(ib, types.subst(dep, from, to), types, true);
2197 }
2198 }
2199 } finally {
2200 listener = prevListener;
2201 for (Pair<InferenceBound, Type> boundUpdate : boundsChanged) {
2202 notifyBoundChange(boundUpdate.fst, boundUpdate.snd, true);
2203 }
2204 }
2205 }
2206
2207 private void notifyBoundChange(InferenceBound ib, Type bound, boolean update) {
2208 if (listener != null) {
2209 listener.varBoundChanged(this, ib, bound, update);
2210 }
2211 }
2212
2213 public final boolean isCaptured() {
2214 return kind == Kind.CAPTURED;
2215 }
2216
2217 public final boolean isThrows() {
2218 return kind == Kind.THROWS;
2219 }
2220 }
2221
2222 /** Represents NONE.
2223 */
2224 public static class JCNoType extends Type implements NoType {
2225 public JCNoType() {
2226 // Need to use List.nil(), because JCNoType constructor
2227 // gets called in static initializers in Type, where
2228 // noAnnotations is also defined.
2229 super(null, List.nil());
2230 }
2231
2232 @Override
2233 public TypeTag getTag() {
2234 return NONE;
2235 }
2236
2237 @Override @DefinedBy(Api.LANGUAGE_MODEL)
2238 public TypeKind getKind() {
2239 return TypeKind.NONE;
2240 }
2241
2242 @Override @DefinedBy(Api.LANGUAGE_MODEL)
2243 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
2244 return v.visitNoType(this, p);
2245 }
2246
2247 @Override
2248 public boolean isCompound() { return false; }
2249 }
2250
2251 /** Represents VOID.
2252 */
2253 public static class JCVoidType extends Type implements NoType {
2254
2255 public JCVoidType() {
2256 // Void cannot be annotated
2257 super(null, List.nil());
2258 }
2259
2260 @Override
2261 public TypeTag getTag() {
2262 return VOID;
2263 }
2264
2265 @Override @DefinedBy(Api.LANGUAGE_MODEL)
2266 public TypeKind getKind() {
2267 return TypeKind.VOID;
2268 }
2269
2270 @Override
2271 public boolean isCompound() { return false; }
2272
2273 @Override @DefinedBy(Api.LANGUAGE_MODEL)
2274 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
2275 return v.visitNoType(this, p);
2276 }
2277
2278 @Override
2279 public boolean isPrimitiveOrVoid() {
2280 return true;
2281 }
2282 }
2283
2284 static class BottomType extends Type implements NullType {
2285 public BottomType() {
2286 // Bottom is a synthesized internal type, so it cannot be annotated
2287 super(null, List.nil());
2288 }
2289
2290 @Override
2291 public TypeTag getTag() {
2292 return BOT;
2293 }
2294
2295 @Override @DefinedBy(Api.LANGUAGE_MODEL)
2296 public TypeKind getKind() {
2297 return TypeKind.NULL;
2298 }
2299
2300 @Override
2301 public boolean isCompound() { return false; }
2302
2303 @Override @DefinedBy(Api.LANGUAGE_MODEL)
2304 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
2305 return v.visitNull(this, p);
2306 }
2307
2308 @Override
2309 public Type constType(Object value) {
2310 return this;
2311 }
2312
2313 @Override
2314 public String stringValue() {
2315 return "null";
2316 }
2317
2318 @Override
2319 public boolean isNullOrReference() {
2320 return true;
2321 }
2322
2323 }
2324
2325 public static class ErrorType extends ClassType
2326 implements javax.lang.model.type.ErrorType {
2327
2328 private Type originalType = null;
2329
2330 public ErrorType(ClassSymbol c, Type originalType) {
2331 this(originalType, c);
2332 c.type = this;
2333 c.kind = ERR;
2334 c.members_field = new Scope.ErrorScope(c);
2335 }
2336
2337 public ErrorType(Type originalType, TypeSymbol tsym) {
2338 super(noType, List.nil(), null);
2339 this.tsym = tsym;
2340 this.originalType = (originalType == null ? noType : originalType);
2341 }
2342
2343 public ErrorType(Type originalType, TypeSymbol tsym,
2344 List<TypeMetadata> metadata) {
2345 super(noType, List.nil(), null, metadata);
2346 this.tsym = tsym;
2347 this.originalType = (originalType == null ? noType : originalType);
2348 }
2349
2350 @Override
2351 protected ErrorType cloneWithMetadata(List<TypeMetadata> md) {
2352 return new ErrorType(originalType, tsym, md) {
2353 @Override
2354 public Type baseType() { return ErrorType.this.baseType(); }
2355 };
2356 }
2357
2358 @Override
2359 public TypeTag getTag() {
2360 return ERROR;
2361 }
2362
2363 @Override
2364 public boolean isPartial() {
2365 return true;
2366 }
2367
2368 @Override
2369 public boolean isReference() {
2370 return true;
2371 }
2372
2373 @Override
2374 public boolean isNullOrReference() {
2375 return true;
2376 }
2377
2378 public ErrorType(Name name, TypeSymbol container, Type originalType) {
2379 this(new ClassSymbol(PUBLIC|STATIC|ACYCLIC, name, null, container), originalType);
2380 }
2381
2382 @Override
2383 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
2384 return v.visitErrorType(this, s);
2385 }
2386
2387 public Type constType(Object constValue) { return this; }
2388 @DefinedBy(Api.LANGUAGE_MODEL)
2389 public Type getEnclosingType() { return Type.noType; }
2390 public Type getReturnType() { return this; }
2391 public Type asSub(Symbol sym) { return this; }
2392
2393 public boolean isGenType(Type t) { return true; }
2394 public boolean isErroneous() { return true; }
2395 public boolean isCompound() { return false; }
2396 public boolean isInterface() { return false; }
2397
2398 @DefinedBy(Api.LANGUAGE_MODEL)
2399 public TypeKind getKind() {
2400 return TypeKind.ERROR;
2401 }
2402
2403 public Type getOriginalType() {
2404 return originalType;
2405 }
2406
2407 @DefinedBy(Api.LANGUAGE_MODEL)
2408 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
2409 return v.visitError(this, p);
2410 }
2411 }
2412
2413 /**
2414 * A visitor for types. A visitor is used to implement operations
2415 * (or relations) on types. Most common operations on types are
2416 * binary relations and this interface is designed for binary
2417 * relations, that is, operations of the form
2418 * Type × S → R.
2419 * <!-- In plain text: Type x S -> R -->
2420 *
2421 * @param <R> the return type of the operation implemented by this
2422 * visitor; use Void if no return type is needed.
2423 * @param <S> the type of the second argument (the first being the
2424 * type itself) of the operation implemented by this visitor; use
2425 * Void if a second argument is not needed.
2426 */
2427 public interface Visitor<R,S> {
2428 R visitClassType(ClassType t, S s);
2429 R visitWildcardType(WildcardType t, S s);
2430 R visitArrayType(ArrayType t, S s);
2431 R visitMethodType(MethodType t, S s);
2432 R visitPackageType(PackageType t, S s);
2433 R visitModuleType(ModuleType t, S s);
2434 R visitTypeVar(TypeVar t, S s);
2435 R visitCapturedType(CapturedType t, S s);
2436 R visitForAll(ForAll t, S s);
2437 R visitUndetVar(UndetVar t, S s);
2438 R visitErrorType(ErrorType t, S s);
2439 R visitType(Type t, S s);
2440 }
2441 }