/
Type.java
2482 lines (2105 loc) · 79.1 KB
/
Type.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.javac.code;
import java.lang.annotation.Annotation;
import java.util.ArrayDeque;
import java.util.Collections;
import java.util.EnumMap;
import java.util.Map;
import java.util.function.Predicate;
import javax.lang.model.type.*;
import com.sun.tools.javac.code.Symbol.*;
import com.sun.tools.javac.code.TypeMetadata.Entry;
import com.sun.tools.javac.code.Types.TypeMapping;
import com.sun.tools.javac.code.Types.UniqueType;
import com.sun.tools.javac.comp.Infer.IncorporationAction;
import com.sun.tools.javac.jvm.ClassFile;
import com.sun.tools.javac.jvm.PoolConstant;
import com.sun.tools.javac.util.*;
import com.sun.tools.javac.util.DefinedBy.Api;
import static com.sun.tools.javac.code.BoundKind.*;
import static com.sun.tools.javac.code.Flags.*;
import static com.sun.tools.javac.code.Kinds.Kind.*;
import static com.sun.tools.javac.code.TypeTag.*;
/** This class represents Java types. The class itself defines the behavior of
* the following types:
* <pre>
* base types (tags: BYTE, CHAR, SHORT, INT, LONG, FLOAT, DOUBLE, BOOLEAN),
* type `void' (tag: VOID),
* the bottom type (tag: BOT),
* the missing type (tag: NONE).
* </pre>
* <p>The behavior of the following types is defined in subclasses, which are
* all static inner classes of this class:
* <pre>
* class types (tag: CLASS, class: ClassType),
* array types (tag: ARRAY, class: ArrayType),
* method types (tag: METHOD, class: MethodType),
* package types (tag: PACKAGE, class: PackageType),
* type variables (tag: TYPEVAR, class: TypeVar),
* type arguments (tag: WILDCARD, class: WildcardType),
* generic method types (tag: FORALL, class: ForAll),
* the error type (tag: ERROR, class: ErrorType).
* </pre>
*
* <p><b>This is NOT part of any supported API.
* If you write code that depends on this, you do so at your own risk.
* This code and its internal interfaces are subject to change or
* deletion without notice.</b>
*
* @see TypeTag
*/
public abstract class Type extends AnnoConstruct implements TypeMirror, PoolConstant {
/**
* Type metadata, Should be {@code null} for the default value.
*
* Note: it is an invariant that for any {@code TypeMetadata}
* class, a given {@code Type} may have at most one metadata array
* entry of that class.
*/
protected final TypeMetadata metadata;
public TypeMetadata getMetadata() {
return metadata;
}
public Entry getMetadataOfKind(final Entry.Kind kind) {
return metadata != null ? metadata.get(kind) : null;
}
/** Constant type: no type at all. */
public static final JCNoType noType = new JCNoType() {
@Override @DefinedBy(Api.LANGUAGE_MODEL)
public String toString() {
return "none";
}
};
/** Constant type: special type to be used during recovery of deferred expressions. */
public static final JCNoType recoveryType = new JCNoType(){
@Override @DefinedBy(Api.LANGUAGE_MODEL)
public String toString() {
return "recovery";
}
};
/** Constant type: special type to be used for marking stuck trees. */
public static final JCNoType stuckType = new JCNoType() {
@Override @DefinedBy(Api.LANGUAGE_MODEL)
public String toString() {
return "stuck";
}
};
/** If this switch is turned on, the names of type variables
* and anonymous classes are printed with hashcodes appended.
*/
public static boolean moreInfo = false;
/** The defining class / interface / package / type variable.
*/
public TypeSymbol tsym;
@Override
public int poolTag() {
throw new AssertionError("Invalid pool entry");
}
@Override
public Object poolKey(Types types) {
return new UniqueType(this, types);
}
/**
* Checks if the current type tag is equal to the given tag.
* @return true if tag is equal to the current type tag.
*/
public boolean hasTag(TypeTag tag) {
return tag == getTag();
}
/**
* Returns the current type tag.
* @return the value of the current type tag.
*/
public abstract TypeTag getTag();
public boolean isNumeric() {
return false;
}
public boolean isIntegral() {
return false;
}
public boolean isPrimitive() {
return false;
}
public boolean isPrimitiveOrVoid() {
return false;
}
public boolean isReference() {
return false;
}
public boolean isNullOrReference() {
return false;
}
public boolean isPartial() {
return false;
}
/**
* The constant value of this type, null if this type does not
* have a constant value attribute. Only primitive types and
* strings (ClassType) can have a constant value attribute.
* @return the constant value attribute of this type
*/
public Object constValue() {
return null;
}
/** Is this a constant type whose value is false?
*/
public boolean isFalse() {
return false;
}
/** Is this a constant type whose value is true?
*/
public boolean isTrue() {
return false;
}
/**
* Get the representation of this type used for modelling purposes.
* By default, this is itself. For ErrorType, a different value
* may be provided.
*/
public Type getModelType() {
return this;
}
public static List<Type> getModelTypes(List<Type> ts) {
ListBuffer<Type> lb = new ListBuffer<>();
for (Type t: ts)
lb.append(t.getModelType());
return lb.toList();
}
/**For ErrorType, returns the original type, otherwise returns the type itself.
*/
public Type getOriginalType() {
return this;
}
public <R,S> R accept(Type.Visitor<R,S> v, S s) { return v.visitType(this, s); }
/** Define a type given its tag, type symbol, and type annotations
*/
public Type(TypeSymbol tsym, TypeMetadata metadata) {
Assert.checkNonNull(metadata);
this.tsym = tsym;
this.metadata = metadata;
}
/**
* A subclass of {@link Types.TypeMapping} which applies a mapping recursively to the subterms
* of a given type expression. This mapping returns the original type is no changes occurred
* when recursively mapping the original type's subterms.
*/
public static abstract class StructuralTypeMapping<S> extends Types.TypeMapping<S> {
@Override
public Type visitClassType(ClassType t, S s) {
Type outer = t.getEnclosingType();
Type outer1 = visit(outer, s);
List<Type> typarams = t.getTypeArguments();
List<Type> typarams1 = visit(typarams, s);
if (outer1 == outer && typarams1 == typarams) return t;
else return new ClassType(outer1, typarams1, t.tsym, t.metadata) {
@Override
protected boolean needsStripping() {
return true;
}
};
}
@Override
public Type visitWildcardType(WildcardType wt, S s) {
Type t = wt.type;
if (t != null)
t = visit(t, s);
if (t == wt.type)
return wt;
else
return new WildcardType(t, wt.kind, wt.tsym, wt.bound, wt.metadata) {
@Override
protected boolean needsStripping() {
return true;
}
};
}
@Override
public Type visitArrayType(ArrayType t, S s) {
Type elemtype = t.elemtype;
Type elemtype1 = visit(elemtype, s);
if (elemtype1 == elemtype) return t;
else return new ArrayType(elemtype1, t.tsym, t.metadata) {
@Override
protected boolean needsStripping() {
return true;
}
};
}
@Override
public Type visitMethodType(MethodType t, S s) {
List<Type> argtypes = t.argtypes;
Type restype = t.restype;
List<Type> thrown = t.thrown;
List<Type> argtypes1 = visit(argtypes, s);
Type restype1 = visit(restype, s);
List<Type> thrown1 = visit(thrown, s);
if (argtypes1 == argtypes &&
restype1 == restype &&
thrown1 == thrown) return t;
else return new MethodType(argtypes1, restype1, thrown1, t.tsym) {
@Override
protected boolean needsStripping() {
return true;
}
};
}
@Override
public Type visitForAll(ForAll t, S s) {
return visit(t.qtype, s);
}
}
/** map a type function over all immediate descendants of this type
*/
public <Z> Type map(TypeMapping<Z> mapping, Z arg) {
return mapping.visit(this, arg);
}
/** map a type function over all immediate descendants of this type (no arg version)
*/
public <Z> Type map(TypeMapping<Z> mapping) {
return mapping.visit(this, null);
}
/** Define a constant type, of the same kind as this type
* and with given constant value
*/
public Type constType(Object constValue) {
throw new AssertionError();
}
/**
* If this is a constant type, return its underlying type.
* Otherwise, return the type itself.
*/
public Type baseType() {
return this;
}
/**
* Returns the original version of this type, before metadata were added. This routine is meant
* for internal use only (i.e. {@link Type#equalsIgnoreMetadata(Type)}, {@link Type#stripMetadata});
* it should not be used outside this class.
*/
protected Type typeNoMetadata() {
return metadata == TypeMetadata.EMPTY ? this : baseType();
}
/**
* Create a new copy of this type but with the specified TypeMetadata.
*/
public abstract Type cloneWithMetadata(TypeMetadata metadata);
/**
* Does this type require annotation stripping for API clients?
*/
protected boolean needsStripping() {
return false;
}
/**
* Strip all metadata associated with this type - this could return a new clone of the type.
* This routine is only used to present the correct annotated types back to the users when types
* are accessed through compiler APIs; it should not be used anywhere in the compiler internals
* as doing so might result in performance penalties.
*/
public Type stripMetadataIfNeeded() {
return needsStripping() ?
accept(stripMetadata, null) :
this;
}
public Type stripMetadata() {
return accept(stripMetadata, null);
}
//where
private static final TypeMapping<Void> stripMetadata = new StructuralTypeMapping<Void>() {
@Override
public Type visitClassType(ClassType t, Void aVoid) {
return super.visitClassType((ClassType)t.typeNoMetadata(), aVoid);
}
@Override
public Type visitArrayType(ArrayType t, Void aVoid) {
return super.visitArrayType((ArrayType)t.typeNoMetadata(), aVoid);
}
@Override
public Type visitTypeVar(TypeVar t, Void aVoid) {
return super.visitTypeVar((TypeVar)t.typeNoMetadata(), aVoid);
}
@Override
public Type visitWildcardType(WildcardType wt, Void aVoid) {
return super.visitWildcardType((WildcardType)wt.typeNoMetadata(), aVoid);
}
};
public Type annotatedType(final List<Attribute.TypeCompound> annos) {
final Entry annoMetadata = new TypeMetadata.Annotations(annos);
return cloneWithMetadata(metadata.combine(annoMetadata));
}
public boolean isAnnotated() {
final TypeMetadata.Annotations metadata =
(TypeMetadata.Annotations)getMetadataOfKind(Entry.Kind.ANNOTATIONS);
return null != metadata && !metadata.getAnnotations().isEmpty();
}
@Override @DefinedBy(Api.LANGUAGE_MODEL)
public List<Attribute.TypeCompound> getAnnotationMirrors() {
final TypeMetadata.Annotations metadata =
(TypeMetadata.Annotations)getMetadataOfKind(Entry.Kind.ANNOTATIONS);
return metadata == null ? List.nil() : metadata.getAnnotations();
}
@Override @DefinedBy(Api.LANGUAGE_MODEL)
public <A extends Annotation> A getAnnotation(Class<A> annotationType) {
return null;
}
@Override @DefinedBy(Api.LANGUAGE_MODEL)
public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationType) {
@SuppressWarnings("unchecked")
A[] tmp = (A[]) java.lang.reflect.Array.newInstance(annotationType, 0);
return tmp;
}
/** Return the base types of a list of types.
*/
public static List<Type> baseTypes(List<Type> ts) {
if (ts.nonEmpty()) {
Type t = ts.head.baseType();
List<Type> baseTypes = baseTypes(ts.tail);
if (t != ts.head || baseTypes != ts.tail)
return baseTypes.prepend(t);
}
return ts;
}
protected void appendAnnotationsString(StringBuilder sb,
boolean prefix) {
if (isAnnotated()) {
if (prefix) {
sb.append(" ");
}
sb.append(getAnnotationMirrors());
sb.append(" ");
}
}
protected void appendAnnotationsString(StringBuilder sb) {
appendAnnotationsString(sb, false);
}
/** The Java source which this type represents.
*/
@DefinedBy(Api.LANGUAGE_MODEL)
public String toString() {
StringBuilder sb = new StringBuilder();
appendAnnotationsString(sb);
if (tsym == null || tsym.name == null) {
sb.append("<none>");
} else {
sb.append(tsym.name);
}
if (moreInfo && hasTag(TYPEVAR)) {
sb.append(hashCode());
}
return sb.toString();
}
/**
* The Java source which this type list represents. A List is
* represented as a comma-separated listing of the elements in
* that list.
*/
public static String toString(List<Type> ts) {
if (ts.isEmpty()) {
return "";
} else {
StringBuilder buf = new StringBuilder();
buf.append(ts.head.toString());
for (List<Type> l = ts.tail; l.nonEmpty(); l = l.tail)
buf.append(",").append(l.head.toString());
return buf.toString();
}
}
/**
* The constant value of this type, converted to String
*/
public String stringValue() {
Object cv = Assert.checkNonNull(constValue());
return cv.toString();
}
/**
* Override this method with care. For most Type instances this should behave as ==.
*/
@Override @DefinedBy(Api.LANGUAGE_MODEL)
public boolean equals(Object t) {
return this == t;
}
public boolean equalsIgnoreMetadata(Type t) {
return typeNoMetadata().equals(t.typeNoMetadata());
}
@Override @DefinedBy(Api.LANGUAGE_MODEL)
public int hashCode() {
return super.hashCode();
}
public String argtypes(boolean varargs) {
List<Type> args = getParameterTypes();
if (!varargs) return args.toString();
StringBuilder buf = new StringBuilder();
while (args.tail.nonEmpty()) {
buf.append(args.head);
args = args.tail;
buf.append(',');
}
if (args.head.hasTag(ARRAY)) {
buf.append(((ArrayType)args.head).elemtype);
if (args.head.getAnnotationMirrors().nonEmpty()) {
buf.append(args.head.getAnnotationMirrors());
}
buf.append("...");
} else {
buf.append(args.head);
}
return buf.toString();
}
/** Access methods.
*/
public List<Type> getTypeArguments() { return List.nil(); }
public Type getEnclosingType() { return null; }
public List<Type> getParameterTypes() { return List.nil(); }
public Type getReturnType() { return null; }
public Type getReceiverType() { return null; }
public List<Type> getThrownTypes() { return List.nil(); }
public Type getUpperBound() { return null; }
public Type getLowerBound() { return null; }
/** Navigation methods, these will work for classes, type variables,
* foralls, but will return null for arrays and methods.
*/
/** Return all parameters of this type and all its outer types in order
* outer (first) to inner (last).
*/
public List<Type> allparams() { return List.nil(); }
/** Does this type contain "error" elements?
*/
public boolean isErroneous() {
return false;
}
public static boolean isErroneous(List<Type> ts) {
for (List<Type> l = ts; l.nonEmpty(); l = l.tail)
if (l.head.isErroneous()) return true;
return false;
}
/** Is this type parameterized?
* A class type is parameterized if it has some parameters.
* An array type is parameterized if its element type is parameterized.
* All other types are not parameterized.
*/
public boolean isParameterized() {
return false;
}
/** Is this type a raw type?
* A class type is a raw type if it misses some of its parameters.
* An array type is a raw type if its element type is raw.
* All other types are not raw.
* Type validation will ensure that the only raw types
* in a program are types that miss all their type variables.
*/
public boolean isRaw() {
return false;
}
/**
* A compound type is a special class type whose supertypes are used to store a list
* of component types. There are two kinds of compound types: (i) intersection types
* {@see IntersectionClassType} and (ii) union types {@see UnionClassType}.
*/
public boolean isCompound() {
return false;
}
public boolean isIntersection() {
return false;
}
public boolean isUnion() {
return false;
}
public boolean isInterface() {
return (tsym.flags() & INTERFACE) != 0;
}
public boolean isFinal() {
return (tsym.flags() & FINAL) != 0;
}
/**
* Does this type contain occurrences of type t?
*/
public boolean contains(Type t) {
return t.equalsIgnoreMetadata(this);
}
public static boolean contains(List<Type> ts, Type t) {
for (List<Type> l = ts;
l.tail != null /*inlined: l.nonEmpty()*/;
l = l.tail)
if (l.head.contains(t)) return true;
return false;
}
/** Does this type contain an occurrence of some type in 'ts'?
*/
public boolean containsAny(List<Type> ts) {
for (Type t : ts)
if (this.contains(t)) return true;
return false;
}
public static boolean containsAny(List<Type> ts1, List<Type> ts2) {
for (Type t : ts1)
if (t.containsAny(ts2)) return true;
return false;
}
public static List<Type> filter(List<Type> ts, Predicate<Type> tf) {
ListBuffer<Type> buf = new ListBuffer<>();
for (Type t : ts) {
if (tf.test(t)) {
buf.append(t);
}
}
return buf.toList();
}
public boolean isSuperBound() { return false; }
public boolean isExtendsBound() { return false; }
public boolean isUnbound() { return false; }
public Type withTypeVar(Type t) { return this; }
/** The underlying method type of this type.
*/
public MethodType asMethodType() { throw new AssertionError(); }
/** Complete loading all classes in this type.
*/
public void complete() {}
public TypeSymbol asElement() {
return tsym;
}
@Override @DefinedBy(Api.LANGUAGE_MODEL)
public TypeKind getKind() {
return TypeKind.OTHER;
}
@Override @DefinedBy(Api.LANGUAGE_MODEL)
public <R, P> R accept(TypeVisitor<R, P> v, P p) {
throw new AssertionError();
}
public static class JCPrimitiveType extends Type
implements javax.lang.model.type.PrimitiveType {
TypeTag tag;
public JCPrimitiveType(TypeTag tag, TypeSymbol tsym) {
this(tag, tsym, TypeMetadata.EMPTY);
}
private JCPrimitiveType(TypeTag tag, TypeSymbol tsym, TypeMetadata metadata) {
super(tsym, metadata);
this.tag = tag;
Assert.check(tag.isPrimitive);
}
@Override
public JCPrimitiveType cloneWithMetadata(TypeMetadata md) {
return new JCPrimitiveType(tag, tsym, md) {
@Override
public Type baseType() { return JCPrimitiveType.this.baseType(); }
};
}
@Override
public boolean isNumeric() {
return tag != BOOLEAN;
}
@Override
public boolean isIntegral() {
switch (tag) {
case CHAR:
case BYTE:
case SHORT:
case INT:
case LONG:
return true;
default:
return false;
}
}
@Override
public boolean isPrimitive() {
return true;
}
@Override
public TypeTag getTag() {
return tag;
}
@Override
public boolean isPrimitiveOrVoid() {
return true;
}
/** Define a constant type, of the same kind as this type
* and with given constant value
*/
@Override
public Type constType(Object constValue) {
final Object value = constValue;
return new JCPrimitiveType(tag, tsym, metadata) {
@Override
public Object constValue() {
return value;
}
@Override
public Type baseType() {
return tsym.type;
}
};
}
/**
* The constant value of this type, converted to String
*/
@Override
public String stringValue() {
Object cv = Assert.checkNonNull(constValue());
if (tag == BOOLEAN) {
return ((Integer) cv).intValue() == 0 ? "false" : "true";
}
else if (tag == CHAR) {
return String.valueOf((char) ((Integer) cv).intValue());
}
else {
return cv.toString();
}
}
/** Is this a constant type whose value is false?
*/
@Override
public boolean isFalse() {
return
tag == BOOLEAN &&
constValue() != null &&
((Integer)constValue()).intValue() == 0;
}
/** Is this a constant type whose value is true?
*/
@Override
public boolean isTrue() {
return
tag == BOOLEAN &&
constValue() != null &&
((Integer)constValue()).intValue() != 0;
}
@Override @DefinedBy(Api.LANGUAGE_MODEL)
public <R, P> R accept(TypeVisitor<R, P> v, P p) {
return v.visitPrimitive(this, p);
}
@Override @DefinedBy(Api.LANGUAGE_MODEL)
public TypeKind getKind() {
switch (tag) {
case BYTE: return TypeKind.BYTE;
case CHAR: return TypeKind.CHAR;
case SHORT: return TypeKind.SHORT;
case INT: return TypeKind.INT;
case LONG: return TypeKind.LONG;
case FLOAT: return TypeKind.FLOAT;
case DOUBLE: return TypeKind.DOUBLE;
case BOOLEAN: return TypeKind.BOOLEAN;
}
throw new AssertionError();
}
}
public static class WildcardType extends Type
implements javax.lang.model.type.WildcardType {
public Type type;
public BoundKind kind;
public TypeVar bound;
@Override
public <R,S> R accept(Type.Visitor<R,S> v, S s) {
return v.visitWildcardType(this, s);
}
public WildcardType(Type type, BoundKind kind, TypeSymbol tsym) {
this(type, kind, tsym, null, TypeMetadata.EMPTY);
}
public WildcardType(Type type, BoundKind kind, TypeSymbol tsym,
TypeMetadata metadata) {
this(type, kind, tsym, null, metadata);
}
public WildcardType(Type type, BoundKind kind, TypeSymbol tsym,
TypeVar bound) {
this(type, kind, tsym, bound, TypeMetadata.EMPTY);
}
public WildcardType(Type type, BoundKind kind, TypeSymbol tsym,
TypeVar bound, TypeMetadata metadata) {
super(tsym, metadata);
this.type = Assert.checkNonNull(type);
this.kind = kind;
this.bound = bound;
}
@Override
public WildcardType cloneWithMetadata(TypeMetadata md) {
return new WildcardType(type, kind, tsym, bound, md) {
@Override
public Type baseType() { return WildcardType.this.baseType(); }
};
}
@Override
public TypeTag getTag() {
return WILDCARD;
}
@Override
public boolean contains(Type t) {
return kind != UNBOUND && type.contains(t);
}
public boolean isSuperBound() {
return kind == SUPER ||
kind == UNBOUND;
}
public boolean isExtendsBound() {
return kind == EXTENDS ||
kind == UNBOUND;
}
public boolean isUnbound() {
return kind == UNBOUND;
}
@Override
public boolean isReference() {
return true;
}
@Override
public boolean isNullOrReference() {
return true;
}
@Override
public Type withTypeVar(Type t) {
//-System.err.println(this+".withTypeVar("+t+");");//DEBUG
if (bound == t)
return this;
bound = (TypeVar)t;
return this;
}
boolean isPrintingBound = false;
@DefinedBy(Api.LANGUAGE_MODEL)
public String toString() {
StringBuilder s = new StringBuilder();
appendAnnotationsString(s);
s.append(kind.toString());
if (kind != UNBOUND)
s.append(type);
if (moreInfo && bound != null && !isPrintingBound)
try {
isPrintingBound = true;
s.append("{:").append(bound.getUpperBound()).append(":}");
} finally {
isPrintingBound = false;
}
return s.toString();
}
@DefinedBy(Api.LANGUAGE_MODEL)
public Type getExtendsBound() {
if (kind == EXTENDS)
return type;
else
return null;
}
@DefinedBy(Api.LANGUAGE_MODEL)
public Type getSuperBound() {
if (kind == SUPER)
return type;
else
return null;
}
@DefinedBy(Api.LANGUAGE_MODEL)
public TypeKind getKind() {
return TypeKind.WILDCARD;
}
@DefinedBy(Api.LANGUAGE_MODEL)
public <R, P> R accept(TypeVisitor<R, P> v, P p) {
return v.visitWildcard(this, p);
}
}
public static class ClassType extends Type implements DeclaredType, LoadableConstant,
javax.lang.model.type.ErrorType {
/** The enclosing type of this type. If this is the type of an inner
* class, outer_field refers to the type of its enclosing
* instance class, in all other cases it refers to noType.
*/
private Type outer_field;
/** The type parameters of this type (to be set once class is loaded).
*/
public List<Type> typarams_field;
/** A cache variable for the type parameters of this type,
* appended to all parameters of its enclosing class.
* @see #allparams
*/
public List<Type> allparams_field;
/** The supertype of this class (to be set once class is loaded).
*/
public Type supertype_field;
/** The interfaces of this class (to be set once class is loaded).
*/
public List<Type> interfaces_field;
/** All the interfaces of this class, including missing ones.
*/
public List<Type> all_interfaces_field;
public ClassType(Type outer, List<Type> typarams, TypeSymbol tsym) {
this(outer, typarams, tsym, TypeMetadata.EMPTY);
}
public ClassType(Type outer, List<Type> typarams, TypeSymbol tsym,
TypeMetadata metadata) {
super(tsym, metadata);
this.outer_field = outer;
this.typarams_field = typarams;
this.allparams_field = null;
this.supertype_field = null;
this.interfaces_field = null;
}
public int poolTag() {
return ClassFile.CONSTANT_Class;
}
@Override
public ClassType cloneWithMetadata(TypeMetadata md) {
return new ClassType(outer_field, typarams_field, tsym, md) {
@Override
public Type baseType() { return ClassType.this.baseType(); }