-
Notifications
You must be signed in to change notification settings - Fork 5.4k
/
Symbol.java
2610 lines (2242 loc) · 89.8 KB
/
Symbol.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.lang.annotation.Inherited;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.function.Supplier;
import java.util.function.Predicate;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ElementVisitor;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.ModuleElement;
import javax.lang.model.element.NestingKind;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.RecordComponentElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.TypeParameterElement;
import javax.lang.model.element.VariableElement;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;
import com.sun.tools.javac.code.Kinds.Kind;
import com.sun.tools.javac.comp.Annotate.AnnotationTypeMetadata;
import com.sun.tools.javac.code.Type.*;
import com.sun.tools.javac.comp.Attr;
import com.sun.tools.javac.comp.AttrContext;
import com.sun.tools.javac.comp.Env;
import com.sun.tools.javac.jvm.*;
import com.sun.tools.javac.jvm.PoolConstant;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCAnnotation;
import com.sun.tools.javac.tree.JCTree.JCFieldAccess;
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
import com.sun.tools.javac.tree.JCTree.Tag;
import com.sun.tools.javac.util.*;
import com.sun.tools.javac.util.DefinedBy.Api;
import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.Name;
import static com.sun.tools.javac.code.Flags.*;
import static com.sun.tools.javac.code.Kinds.*;
import static com.sun.tools.javac.code.Kinds.Kind.*;
import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE;
import com.sun.tools.javac.code.Scope.WriteableScope;
import static com.sun.tools.javac.code.TypeTag.CLASS;
import static com.sun.tools.javac.code.TypeTag.FORALL;
import static com.sun.tools.javac.code.TypeTag.TYPEVAR;
import static com.sun.tools.javac.jvm.ByteCodes.iadd;
import static com.sun.tools.javac.jvm.ByteCodes.ishll;
import static com.sun.tools.javac.jvm.ByteCodes.lushrl;
import static com.sun.tools.javac.jvm.ByteCodes.lxor;
import static com.sun.tools.javac.jvm.ByteCodes.string_add;
/** Root class for Java symbols. It contains subclasses
* for specific sorts of symbols, such as variables, methods and operators,
* types, packages. Each subclass is represented as a static inner class
* inside Symbol.
*
* <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>
*/
public abstract class Symbol extends AnnoConstruct implements PoolConstant, Element {
/** The kind of this symbol.
* @see Kinds
*/
public Kind kind;
/** The flags of this symbol.
*/
public long flags_field;
/** An accessor method for the flags of this symbol.
* Flags of class symbols should be accessed through the accessor
* method to make sure that the class symbol is loaded.
*/
public long flags() { return flags_field; }
/** The name of this symbol in Utf8 representation.
*/
public Name name;
/** The type of this symbol.
*/
public Type type;
/** The owner of this symbol.
*/
public Symbol owner;
/** The completer of this symbol.
* This should never equal null (NULL_COMPLETER should be used instead).
*/
public Completer completer;
/** A cache for the type erasure of this symbol.
*/
public Type erasure_field;
// <editor-fold defaultstate="collapsed" desc="annotations">
/** The attributes of this symbol are contained in this
* SymbolMetadata. The SymbolMetadata instance is NOT immutable.
*/
protected SymbolMetadata metadata;
/** An accessor method for the attributes of this symbol.
* Attributes of class symbols should be accessed through the accessor
* method to make sure that the class symbol is loaded.
*/
public List<Attribute.Compound> getRawAttributes() {
return (metadata == null)
? List.nil()
: metadata.getDeclarationAttributes();
}
/** An accessor method for the type attributes of this symbol.
* Attributes of class symbols should be accessed through the accessor
* method to make sure that the class symbol is loaded.
*/
public List<Attribute.TypeCompound> getRawTypeAttributes() {
return (metadata == null)
? List.nil()
: metadata.getTypeAttributes();
}
/** Fetch a particular annotation from a symbol. */
public Attribute.Compound attribute(Symbol anno) {
for (Attribute.Compound a : getRawAttributes()) {
if (a.type.tsym == anno) return a;
}
return null;
}
public boolean annotationsPendingCompletion() {
return metadata == null ? false : metadata.pendingCompletion();
}
public void appendAttributes(List<Attribute.Compound> l) {
if (l.nonEmpty()) {
initedMetadata().append(l);
}
}
public void appendClassInitTypeAttributes(List<Attribute.TypeCompound> l) {
if (l.nonEmpty()) {
initedMetadata().appendClassInitTypeAttributes(l);
}
}
public void appendInitTypeAttributes(List<Attribute.TypeCompound> l) {
if (l.nonEmpty()) {
initedMetadata().appendInitTypeAttributes(l);
}
}
public void appendUniqueTypeAttributes(List<Attribute.TypeCompound> l) {
if (l.nonEmpty()) {
initedMetadata().appendUniqueTypes(l);
}
}
public List<Attribute.TypeCompound> getClassInitTypeAttributes() {
return (metadata == null)
? List.nil()
: metadata.getClassInitTypeAttributes();
}
public List<Attribute.TypeCompound> getInitTypeAttributes() {
return (metadata == null)
? List.nil()
: metadata.getInitTypeAttributes();
}
public void setInitTypeAttributes(List<Attribute.TypeCompound> l) {
initedMetadata().setInitTypeAttributes(l);
}
public void setClassInitTypeAttributes(List<Attribute.TypeCompound> l) {
initedMetadata().setClassInitTypeAttributes(l);
}
public List<Attribute.Compound> getDeclarationAttributes() {
return (metadata == null)
? List.nil()
: metadata.getDeclarationAttributes();
}
public boolean hasAnnotations() {
return (metadata != null && !metadata.isEmpty());
}
public boolean hasTypeAnnotations() {
return (metadata != null && !metadata.isTypesEmpty());
}
public boolean isCompleted() {
return completer.isTerminal();
}
public void prependAttributes(List<Attribute.Compound> l) {
if (l.nonEmpty()) {
initedMetadata().prepend(l);
}
}
public void resetAnnotations() {
initedMetadata().reset();
}
public void setAttributes(Symbol other) {
if (metadata != null || other.metadata != null) {
initedMetadata().setAttributes(other.metadata);
}
}
public void setDeclarationAttributes(List<Attribute.Compound> a) {
if (metadata != null || a.nonEmpty()) {
initedMetadata().setDeclarationAttributes(a);
}
}
public void setTypeAttributes(List<Attribute.TypeCompound> a) {
if (metadata != null || a.nonEmpty()) {
if (metadata == null)
metadata = new SymbolMetadata(this);
metadata.setTypeAttributes(a);
}
}
private SymbolMetadata initedMetadata() {
if (metadata == null)
metadata = new SymbolMetadata(this);
return metadata;
}
/** This method is intended for debugging only. */
public SymbolMetadata getMetadata() {
return metadata;
}
// </editor-fold>
/** Construct a symbol with given kind, flags, name, type and owner.
*/
public Symbol(Kind kind, long flags, Name name, Type type, Symbol owner) {
this.kind = kind;
this.flags_field = flags;
this.type = type;
this.owner = owner;
this.completer = Completer.NULL_COMPLETER;
this.erasure_field = null;
this.name = name;
}
@Override
public int poolTag() {
throw new AssertionError("Invalid pool entry");
}
/** Clone this symbol with new owner.
* Legal only for fields and methods.
*/
public Symbol clone(Symbol newOwner) {
throw new AssertionError();
}
public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
return v.visitSymbol(this, p);
}
/** The Java source which this symbol represents.
* A description of this symbol; overrides Object.
*/
public String toString() {
return name.toString();
}
/** A Java source description of the location of this symbol; used for
* error reporting.
*
* @return null if the symbol is a package or a toplevel class defined in
* the default package; otherwise, the owner symbol is returned
*/
public Symbol location() {
if (owner.name == null || (owner.name.isEmpty() &&
(owner.flags() & BLOCK) == 0 &&
owner.kind != PCK &&
owner.kind != TYP)) {
return null;
}
return owner;
}
public Symbol location(Type site, Types types) {
if (owner.name == null || owner.name.isEmpty()) {
return location();
}
if (owner.type.hasTag(CLASS)) {
Type ownertype = types.asOuterSuper(site, owner);
if (ownertype != null) return ownertype.tsym;
}
return owner;
}
public Symbol baseSymbol() {
return this;
}
/** The symbol's erased type.
*/
public Type erasure(Types types) {
if (erasure_field == null)
erasure_field = types.erasure(type);
return erasure_field;
}
/** The external type of a symbol. This is the symbol's erased type
* except for constructors of inner classes which get the enclosing
* instance class added as first argument.
*/
public Type externalType(Types types) {
Type t = erasure(types);
if (name == name.table.names.init && owner.hasOuterInstance()) {
Type outerThisType = types.erasure(owner.type.getEnclosingType());
return new MethodType(t.getParameterTypes().prepend(outerThisType),
t.getReturnType(),
t.getThrownTypes(),
t.tsym);
} else {
return t;
}
}
public boolean isDeprecated() {
return (flags_field & DEPRECATED) != 0;
}
public boolean hasDeprecatedAnnotation() {
return (flags_field & DEPRECATED_ANNOTATION) != 0;
}
public boolean isDeprecatedForRemoval() {
return (flags_field & DEPRECATED_REMOVAL) != 0;
}
public boolean isPreviewApi() {
return (flags_field & PREVIEW_API) != 0;
}
public boolean isDeprecatableViaAnnotation() {
switch (getKind()) {
case LOCAL_VARIABLE:
case PACKAGE:
case PARAMETER:
case RESOURCE_VARIABLE:
case EXCEPTION_PARAMETER:
return false;
default:
return true;
}
}
public boolean isStatic() {
return
(flags() & STATIC) != 0 ||
(owner.flags() & INTERFACE) != 0 && kind != MTH &&
name != name.table.names._this;
}
public boolean isInterface() {
return (flags() & INTERFACE) != 0;
}
public boolean isAbstract() {
return (flags_field & ABSTRACT) != 0;
}
public boolean isPrivate() {
return (flags_field & Flags.AccessFlags) == PRIVATE;
}
public boolean isPublic() {
return (flags_field & Flags.AccessFlags) == PUBLIC;
}
public boolean isEnum() {
return (flags() & ENUM) != 0;
}
public boolean isSealed() {
return (flags_field & SEALED) != 0;
}
public boolean isNonSealed() {
return (flags_field & NON_SEALED) != 0;
}
public boolean isFinal() {
return (flags_field & FINAL) != 0;
}
/** Is this symbol declared (directly or indirectly) local
* to a method or variable initializer?
* Also includes fields of inner classes which are in
* turn local to a method or variable initializer.
*/
public boolean isDirectlyOrIndirectlyLocal() {
return
(owner.kind.matches(KindSelector.VAL_MTH) ||
(owner.kind == TYP && owner.isDirectlyOrIndirectlyLocal()));
}
/** Has this symbol an empty name? This includes anonymous
* inner classes.
*/
public boolean isAnonymous() {
return name.isEmpty();
}
/** Is this symbol a constructor?
*/
public boolean isConstructor() {
return name == name.table.names.init;
}
public boolean isDynamic() {
return false;
}
/** The fully qualified name of this symbol.
* This is the same as the symbol's name except for class symbols,
* which are handled separately.
*/
public Name getQualifiedName() {
return name;
}
/** The fully qualified name of this symbol after converting to flat
* representation. This is the same as the symbol's name except for
* class symbols, which are handled separately.
*/
public Name flatName() {
return getQualifiedName();
}
/** If this is a class or package, its members, otherwise null.
*/
public WriteableScope members() {
return null;
}
/** A class is an inner class if it it has an enclosing instance class.
*/
public boolean isInner() {
return kind == TYP && type.getEnclosingType().hasTag(CLASS);
}
/** An inner class has an outer instance if it is not an interface
* it has an enclosing instance class which might be referenced from the class.
* Nested classes can see instance members of their enclosing class.
* Their constructors carry an additional this$n parameter, inserted
* implicitly by the compiler.
*
* @see #isInner
*/
public boolean hasOuterInstance() {
return
type.getEnclosingType().hasTag(CLASS) && (flags() & (INTERFACE | NOOUTERTHIS)) == 0;
}
/** The closest enclosing class of this symbol's declaration.
* Warning: this (misnamed) method returns the receiver itself
* when the receiver is a class (as opposed to its enclosing
* class as one may be misled to believe.)
*/
public ClassSymbol enclClass() {
Symbol c = this;
while (c != null &&
(!c.kind.matches(KindSelector.TYP) || !c.type.hasTag(CLASS))) {
c = c.owner;
}
return (ClassSymbol)c;
}
/** The outermost class which indirectly owns this symbol.
*/
public ClassSymbol outermostClass() {
Symbol sym = this;
Symbol prev = null;
while (sym.kind != PCK) {
prev = sym;
sym = sym.owner;
}
return (ClassSymbol) prev;
}
/** The package which indirectly owns this symbol.
*/
public PackageSymbol packge() {
Symbol sym = this;
while (sym.kind != PCK) {
sym = sym.owner;
}
return (PackageSymbol) sym;
}
/** Is this symbol a subclass of `base'? Only defined for ClassSymbols.
*/
public boolean isSubClass(Symbol base, Types types) {
throw new AssertionError("isSubClass " + this);
}
/** Fully check membership: hierarchy, protection, and hiding.
* Does not exclude methods not inherited due to overriding.
*/
public boolean isMemberOf(TypeSymbol clazz, Types types) {
return
owner == clazz ||
clazz.isSubClass(owner, types) &&
isInheritedIn(clazz, types) &&
!hiddenIn((ClassSymbol)clazz, types);
}
/** Is this symbol the same as or enclosed by the given class? */
public boolean isEnclosedBy(ClassSymbol clazz) {
for (Symbol sym = this; sym.kind != PCK; sym = sym.owner)
if (sym == clazz) return true;
return false;
}
private boolean hiddenIn(ClassSymbol clazz, Types types) {
Symbol sym = hiddenInInternal(clazz, types);
Assert.check(sym != null, "the result of hiddenInInternal() can't be null");
/* If we find the current symbol then there is no symbol hiding it
*/
return sym != this;
}
/** This method looks in the supertypes graph that has the current class as the
* initial node, till it finds the current symbol or another symbol that hides it.
* If the current class has more than one supertype (extends one class and
* implements one or more interfaces) then null can be returned, meaning that
* a wrong path in the supertypes graph was selected. Null can only be returned
* as a temporary value, as a result of the recursive call.
*/
private Symbol hiddenInInternal(ClassSymbol currentClass, Types types) {
if (currentClass == owner) {
return this;
}
for (Symbol sym : currentClass.members().getSymbolsByName(name)) {
if (sym.kind == kind &&
(kind != MTH ||
(sym.flags() & STATIC) != 0 &&
types.isSubSignature(sym.type, type))) {
return sym;
}
}
Symbol hiddenSym = null;
for (Type st : types.interfaces(currentClass.type)
.prepend(types.supertype(currentClass.type))) {
if (st != null && (st.hasTag(CLASS))) {
Symbol sym = hiddenInInternal((ClassSymbol)st.tsym, types);
if (sym == this) {
return this;
} else if (sym != null) {
hiddenSym = sym;
}
}
}
return hiddenSym;
}
/** Is this symbol accessible in a given class?
* PRE: If symbol's owner is a interface,
* it is already assumed that the interface is a superinterface
* the given class.
* @param clazz The class for which we want to establish membership.
* This must be a subclass of the member's owner.
*/
public final boolean isAccessibleIn(Symbol clazz, Types types) {
switch ((int)(flags_field & Flags.AccessFlags)) {
default: // error recovery
case PUBLIC:
return true;
case PRIVATE:
return this.owner == clazz;
case PROTECTED:
// we model interfaces as extending Object
return (clazz.flags() & INTERFACE) == 0;
case 0:
PackageSymbol thisPackage = this.packge();
for (Symbol sup = clazz;
sup != null && sup != this.owner;
sup = types.supertype(sup.type).tsym) {
while (sup.type.hasTag(TYPEVAR))
sup = sup.type.getUpperBound().tsym;
if (sup.type.isErroneous())
return true; // error recovery
if ((sup.flags() & COMPOUND) != 0)
continue;
if (sup.packge() != thisPackage)
return false;
}
return (clazz.flags() & INTERFACE) == 0;
}
}
/** Is this symbol inherited into a given class?
* PRE: If symbol's owner is a interface,
* it is already assumed that the interface is a superinterface
* of the given class.
* @param clazz The class for which we want to establish membership.
* This must be a subclass of the member's owner.
*/
public boolean isInheritedIn(Symbol clazz, Types types) {
return isAccessibleIn(clazz, types);
}
/** The (variable or method) symbol seen as a member of given
* class type`site' (this might change the symbol's type).
* This is used exclusively for producing diagnostics.
*/
public Symbol asMemberOf(Type site, Types types) {
throw new AssertionError();
}
/** Does this method symbol override `other' symbol, when both are seen as
* members of class `origin'? It is assumed that _other is a member
* of origin.
*
* It is assumed that both symbols have the same name. The static
* modifier is ignored for this test.
*
* See JLS 8.4.6.1 (without transitivity) and 8.4.6.4
*/
public boolean overrides(Symbol _other, TypeSymbol origin, Types types, boolean checkResult) {
return false;
}
/** Complete the elaboration of this symbol's definition.
*/
public void complete() throws CompletionFailure {
if (completer != Completer.NULL_COMPLETER) {
Completer c = completer;
completer = Completer.NULL_COMPLETER;
c.complete(this);
}
}
public void apiComplete() throws CompletionFailure {
try {
complete();
} catch (CompletionFailure cf) {
cf.dcfh.handleAPICompletionFailure(cf);
}
}
/** True if the symbol represents an entity that exists.
*/
public boolean exists() {
return true;
}
@DefinedBy(Api.LANGUAGE_MODEL)
public Type asType() {
return type;
}
@DefinedBy(Api.LANGUAGE_MODEL)
public Symbol getEnclosingElement() {
return owner;
}
@DefinedBy(Api.LANGUAGE_MODEL)
public ElementKind getKind() {
return ElementKind.OTHER; // most unkind
}
@DefinedBy(Api.LANGUAGE_MODEL)
public Set<Modifier> getModifiers() {
apiComplete();
return Flags.asModifierSet(flags());
}
@DefinedBy(Api.LANGUAGE_MODEL)
public Name getSimpleName() {
return name;
}
/**
* This is the implementation for {@code
* javax.lang.model.element.Element.getAnnotationMirrors()}.
*/
@Override @DefinedBy(Api.LANGUAGE_MODEL)
public List<Attribute.Compound> getAnnotationMirrors() {
apiComplete();
return getRawAttributes();
}
// TODO: getEnclosedElements should return a javac List, fix in FilteredMemberList
@DefinedBy(Api.LANGUAGE_MODEL)
public java.util.List<Symbol> getEnclosedElements() {
return List.nil();
}
public List<TypeVariableSymbol> getTypeParameters() {
ListBuffer<TypeVariableSymbol> l = new ListBuffer<>();
for (Type t : type.getTypeArguments()) {
Assert.check(t.tsym.getKind() == ElementKind.TYPE_PARAMETER);
l.append((TypeVariableSymbol)t.tsym);
}
return l.toList();
}
public static class DelegatedSymbol<T extends Symbol> extends Symbol {
protected T other;
public DelegatedSymbol(T other) {
super(other.kind, other.flags_field, other.name, other.type, other.owner);
this.other = other;
}
public String toString() { return other.toString(); }
public Symbol location() { return other.location(); }
public Symbol location(Type site, Types types) { return other.location(site, types); }
public Symbol baseSymbol() { return other; }
public Type erasure(Types types) { return other.erasure(types); }
public Type externalType(Types types) { return other.externalType(types); }
public boolean isDirectlyOrIndirectlyLocal() { return other.isDirectlyOrIndirectlyLocal(); }
public boolean isConstructor() { return other.isConstructor(); }
public Name getQualifiedName() { return other.getQualifiedName(); }
public Name flatName() { return other.flatName(); }
public WriteableScope members() { return other.members(); }
public boolean isInner() { return other.isInner(); }
public boolean hasOuterInstance() { return other.hasOuterInstance(); }
public ClassSymbol enclClass() { return other.enclClass(); }
public ClassSymbol outermostClass() { return other.outermostClass(); }
public PackageSymbol packge() { return other.packge(); }
public boolean isSubClass(Symbol base, Types types) { return other.isSubClass(base, types); }
public boolean isMemberOf(TypeSymbol clazz, Types types) { return other.isMemberOf(clazz, types); }
public boolean isEnclosedBy(ClassSymbol clazz) { return other.isEnclosedBy(clazz); }
public boolean isInheritedIn(Symbol clazz, Types types) { return other.isInheritedIn(clazz, types); }
public Symbol asMemberOf(Type site, Types types) { return other.asMemberOf(site, types); }
public void complete() throws CompletionFailure { other.complete(); }
@DefinedBy(Api.LANGUAGE_MODEL)
public <R, P> R accept(ElementVisitor<R, P> v, P p) {
return other.accept(v, p);
}
public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
return v.visitSymbol(other, p);
}
public T getUnderlyingSymbol() {
return other;
}
}
/** A base class for Symbols representing types.
*/
public abstract static class TypeSymbol extends Symbol {
public TypeSymbol(Kind kind, long flags, Name name, Type type, Symbol owner) {
super(kind, flags, name, type, owner);
}
/** form a fully qualified name from a name and an owner
*/
public static Name formFullName(Name name, Symbol owner) {
if (owner == null) return name;
if ((owner.kind != ERR) &&
(owner.kind.matches(KindSelector.VAL_MTH) ||
(owner.kind == TYP && owner.type.hasTag(TYPEVAR))
)) return name;
Name prefix = owner.getQualifiedName();
if (prefix == null || prefix == prefix.table.names.empty)
return name;
else return prefix.append('.', name);
}
/** form a fully qualified name from a name and an owner, after
* converting to flat representation
*/
public static Name formFlatName(Name name, Symbol owner) {
if (owner == null || owner.kind.matches(KindSelector.VAL_MTH) ||
(owner.kind == TYP && owner.type.hasTag(TYPEVAR))
) return name;
char sep = owner.kind == TYP ? '$' : '.';
Name prefix = owner.flatName();
if (prefix == null || prefix == prefix.table.names.empty)
return name;
else return prefix.append(sep, name);
}
/**
* A partial ordering between type symbols that refines the
* class inheritance graph.
*
* Type variables always precede other kinds of symbols.
*/
public final boolean precedes(TypeSymbol that, Types types) {
if (this == that)
return false;
if (type.hasTag(that.type.getTag())) {
if (type.hasTag(CLASS)) {
return
types.rank(that.type) < types.rank(this.type) ||
types.rank(that.type) == types.rank(this.type) &&
that.getQualifiedName().compareTo(this.getQualifiedName()) < 0;
} else if (type.hasTag(TYPEVAR)) {
return types.isSubtype(this.type, that.type);
}
}
return type.hasTag(TYPEVAR);
}
@Override @DefinedBy(Api.LANGUAGE_MODEL)
public List<Symbol> getEnclosedElements() {
List<Symbol> list = List.nil();
if (kind == TYP && type.hasTag(TYPEVAR)) {
return list;
}
apiComplete();
for (Symbol sym : members().getSymbols(NON_RECURSIVE)) {
sym.apiComplete();
if ((sym.flags() & SYNTHETIC) == 0 && sym.owner == this && sym.kind != ERR) {
list = list.prepend(sym);
}
}
return list;
}
public AnnotationTypeMetadata getAnnotationTypeMetadata() {
Assert.error("Only on ClassSymbol");
return null; //unreachable
}
public boolean isAnnotationType() { return false; }
@Override
public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
return v.visitTypeSymbol(this, p);
}
}
/**
* Type variables are represented by instances of this class.
*/
public static class TypeVariableSymbol
extends TypeSymbol implements TypeParameterElement {
public TypeVariableSymbol(long flags, Name name, Type type, Symbol owner) {
super(TYP, flags, name, type, owner);
}
@DefinedBy(Api.LANGUAGE_MODEL)
public ElementKind getKind() {
return ElementKind.TYPE_PARAMETER;
}
@Override @DefinedBy(Api.LANGUAGE_MODEL)
public Symbol getGenericElement() {
return owner;
}
@DefinedBy(Api.LANGUAGE_MODEL)
public List<Type> getBounds() {
TypeVar t = (TypeVar)type;
Type bound = t.getUpperBound();
if (!bound.isCompound())
return List.of(bound);
ClassType ct = (ClassType)bound;
if (!ct.tsym.erasure_field.isInterface()) {
return ct.interfaces_field.prepend(ct.supertype_field);
} else {
// No superclass was given in bounds.
// In this case, supertype is Object, erasure is first interface.
return ct.interfaces_field;
}
}
@Override @DefinedBy(Api.LANGUAGE_MODEL)
public List<Attribute.Compound> getAnnotationMirrors() {
// Declaration annotations on type variables are stored in type attributes
// on the owner of the TypeVariableSymbol
List<Attribute.TypeCompound> candidates = owner.getRawTypeAttributes();
int index = owner.getTypeParameters().indexOf(this);
List<Attribute.Compound> res = List.nil();
for (Attribute.TypeCompound a : candidates) {
if (isCurrentSymbolsAnnotation(a, index))
res = res.prepend(a);
}
return res.reverse();
}
// Helper to getAnnotation[s]
@Override
public <A extends Annotation> Attribute.Compound getAttribute(Class<A> annoType) {
String name = annoType.getName();
// Declaration annotations on type variables are stored in type attributes
// on the owner of the TypeVariableSymbol
List<Attribute.TypeCompound> candidates = owner.getRawTypeAttributes();
int index = owner.getTypeParameters().indexOf(this);
for (Attribute.TypeCompound anno : candidates)
if (isCurrentSymbolsAnnotation(anno, index) &&
name.contentEquals(anno.type.tsym.flatName()))
return anno;
return null;
}
//where:
boolean isCurrentSymbolsAnnotation(Attribute.TypeCompound anno, int index) {
return (anno.position.type == TargetType.CLASS_TYPE_PARAMETER ||
anno.position.type == TargetType.METHOD_TYPE_PARAMETER) &&
anno.position.parameter_index == index;
}
@Override @DefinedBy(Api.LANGUAGE_MODEL)
public <R, P> R accept(ElementVisitor<R, P> v, P p) {
return v.visitTypeParameter(this, p);
}
}
/** A class for module symbols.
*/
public static class ModuleSymbol extends TypeSymbol
implements ModuleElement {
public Name version;
public JavaFileManager.Location sourceLocation;
public JavaFileManager.Location classLocation;
public JavaFileManager.Location patchLocation;
public JavaFileManager.Location patchOutputLocation;
/** All directives, in natural order. */
public List<com.sun.tools.javac.code.Directive> directives;
public List<com.sun.tools.javac.code.Directive.RequiresDirective> requires;
public List<com.sun.tools.javac.code.Directive.ExportsDirective> exports;
public List<com.sun.tools.javac.code.Directive.OpensDirective> opens;
public List<com.sun.tools.javac.code.Directive.ProvidesDirective> provides;
public List<com.sun.tools.javac.code.Directive.UsesDirective> uses;
public ClassSymbol module_info;
public PackageSymbol unnamedPackage;
public Map<Name, PackageSymbol> visiblePackages;
public Set<ModuleSymbol> readModules;
public List<Symbol> enclosedPackages = List.nil();
public Completer usesProvidesCompleter = Completer.NULL_COMPLETER;
public final Set<ModuleFlags> flags = EnumSet.noneOf(ModuleFlags.class);
public final Set<ModuleResolutionFlags> resolutionFlags = EnumSet.noneOf(ModuleResolutionFlags.class);
/**
* Create a ModuleSymbol with an associated module-info ClassSymbol.
*/
public static ModuleSymbol create(Name name, Name module_info) {
ModuleSymbol msym = new ModuleSymbol(name, null);
ClassSymbol info = new ClassSymbol(Flags.MODULE, module_info, msym);
info.fullname = formFullName(module_info, msym);
info.flatname = info.fullname;
info.members_field = WriteableScope.create(info);
msym.module_info = info;
return msym;