forked from microbean/microbean-construct
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDomain.java
More file actions
1666 lines (1595 loc) · 72.3 KB
/
Domain.java
File metadata and controls
1666 lines (1595 loc) · 72.3 KB
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
/* -*- mode: Java; c-basic-offset: 2; indent-tabs-mode: nil; coding: utf-8-unix -*-
*
* Copyright © 2024–2026 microBean™.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.microbean.construct;
import java.lang.reflect.Constructor;
import java.lang.reflect.Executable;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.GenericDeclaration;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import javax.lang.model.AnnotatedConstruct;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.ModuleElement;
import javax.lang.model.element.Name;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.Parameterizable;
import javax.lang.model.element.QualifiedNameable;
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.lang.model.type.ArrayType;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.ExecutableType;
import javax.lang.model.type.NoType;
import javax.lang.model.type.NullType;
import javax.lang.model.type.PrimitiveType;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.type.TypeVariable;
import javax.lang.model.type.WildcardType;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Elements.Origin;
import org.microbean.construct.element.AnnotationMirrors;
import org.microbean.construct.element.UniversalElement;
import org.microbean.construct.type.UniversalType;
import static java.util.Collections.unmodifiableList;
import static java.util.Objects.requireNonNull;
import static javax.lang.model.type.TypeKind.DECLARED;
/**
* A {@link PrimordialDomain} that represents a domain of valid Java language constructs.
*
* <p>A <dfn id="domain">domain</dfn> is a set of valid Java language <a href="#construct">constructs</a>. A {@link
* Domain} provides access to a domain and its members.</p>
*
* <p>A Java <dfn id="construct">construct</dfn> is either a <a href="#type">type</a> or an <a
* href="#element">element</a>.</p>
*
* <p>A <dfn id="type">type</dfn> is a usage of a Java type, most commonly represented as a {@link TypeMirror}.</p>
*
* <p>An <dfn id="element">element</dfn> ia a declaration of a Java program element, most commonly represented as an
* {@link Element}.</p>
*
* <p>Domains impose constraints on the <a href="#type">types</a> and <a href="#element">elements</a> they contain, and
* on the kinds and semantics of operations that can be performed on them.</p>
*
* <p>This interface is modeled on a deliberately restricted combination of the {@link javax.lang.model.util.Elements}
* and {@link javax.lang.model.util.Types} interfaces.</p>
*
* <p>{@link Domain} implementations must be thread-safe.</p>
*
* @author <a href="https://about.me/lairdnelson" target="_top">Laird Nelson</a>
*
* @see PrimordialDomain
*
* @see DefaultDomain
*
* @see <a href="https://bugs.openjdk.org/browse/JDK-8055219">JDK-8055219</a>
*/
@SuppressWarnings("try")
public interface Domain extends PrimordialDomain {
/**
* Returns an immutable, determinate, non-{@code null} {@link List} of {@link AnnotationMirror} instances representing
* all annotations <dfn>present</dfn> on an element, whether <dfn>directly present</dfn> or present via inheritance.
*
* <p>The default implementation of this method calls {@link UniversalElement#of(Element, Domain)} with the supplied
* {@code element} and {@code this} as arguments, calls the {@link
* AnnotationMirrors#allAnnotationMirrors(Element)} with the result, and returns the result.</p>
*
* @param element the {@link Element} whose present annotations should be returned; must not be {@code null}
*
* @return an immutable, determinate, non-{@code null} {@link List} of {@link AnnotationMirror} instances representing
* all annotations <dfn>present</dfn> on an element, whether <dfn>directly present</dfn> or present via inheritance
*
* @exception NullPointerException if {@code element} is {@code null}
*
* @see AnnotationMirrors#allAnnotationMirrors(Element)
*
* @see javax.lang.model.util.Elements#getAllAnnotationMirrors(Element)
*/
public default List<? extends AnnotationMirror> allAnnotationMirrors(final Element element) {
return AnnotationMirrors.allAnnotationMirrors(UniversalElement.of(element, this)); // handles locking, symbol completion
}
/**
* Returns a non-{@code null}, determinate, immutable {@link List} whose elements comprise the <dfn>members</dfn> of
* the supplied {@link TypeElement}, whether they are inherited or declared directly.
*
* <p>If the supplied {@link TypeElement} is a {@linkplain ElementKind#CLASS class}, the returned {@link List} will
* also include the class' constructors, but not any local or anonymous classes.</p>
*
* @param e a {@link TypeElement}; must not be {@code null}
*
* @return a non-{@code null}, determinate, immutable {@link List} whose elements comprise the <dfn>members</dfn> of
* the supplied {@link TypeElement}, whether they are inherited or declared directly
*
* @exception NullPointerException if {@code e} is {@code null}
*
* @see Elements#getAllMembers(TypeElement)
*/
public abstract List<? extends Element> allMembers(TypeElement e);
/**
* An <strong>experimental</strong> method that returns an {@link Element} that is {@linkplain Element#equals(Object)
* equal to} the supplied {@link Element} but {@linkplain Element#getAnnotationMirrors() with} a {@link List} of
* annotations {@linkplain List#equals(Object) equal to} the supplied {@link List} of {@link AnnotationMirror}s.
*
* <p>The supplied {@link Element} may be modified in place if the return value of its {@link
* Element#getAnnotationMirrors() getAnnotationMirrors()} method is mutable.</p>
*
* <p>No validation of any kind is performed on either argument.</p>
*
* @param annotations a {@link List} of {@link AnnotationMirror}s; must not be {@code null}
*
* @param e an {@link Element}; must not be {@code null}
*
* @return an {@link Element} that is {@linkplain Element#equals(Object) equal to} the supplied {@link Element} but
* {@linkplain Element#getAnnotationMirrors() with} a {@link List} of annotations {@linkplain List#equals(Object)
* equal to} the supplied {@link List} of {@link AnnotationMirror}s
*
* @exception NullPointerException if any argument is {@code null}
*
* @see UniversalElement#getAnnotationMirrors()
*/
public default Element annotate(final List<? extends AnnotationMirror> annotations, final Element e) {
final UniversalElement ue = UniversalElement.of(e, this);
final List<AnnotationMirror> l = ue.getAnnotationMirrors();
l.clear();
l.addAll(annotations);
return ue;
}
/**
* An <strong>experimental</strong> method that returns a {@link TypeMirror} that is {@linkplain
* TypeMirror#equals(Object) equal to} the supplied {@link TypeMirror} but {@linkplain
* TypeMirror#getAnnotationMirrors() with} a {@link List} of annotations {@linkplain List#equals(Object) equal to} the
* supplied {@link List} of {@link AnnotationMirror}s.
*
* <p>The supplied {@link TypeMirror} may be modified in place if the return value of its {@link
* TypeMirror#getAnnotationMirrors() getAnnotationMirrors()} method is mutable.</p>
*
* <p>No validation of any kind is performed on either argument.</p>
*
* @param annotations a {@link List} of {@link AnnotationMirror}s; must not be {@code null}
*
* @param t a {@link TypeMirror}; must not be {@code null}
*
* @return a {@link TypeMirror} that is {@linkplain TypeMirror#equals(Object) equal to} the supplied {@link
* TypeMirror} but {@linkplain TypeMirror#getAnnotationMirrors() with} a {@link List} of annotations {@linkplain
* List#equals(Object) equal to} the supplied {@link List} of {@link AnnotationMirror}s
*
* @exception NullPointerException if any argument is {@code null}
*
* @see UniversalType#getAnnotationMirrors()
*/
public default TypeMirror annotate(final List<? extends AnnotationMirror> annotations, final TypeMirror t) {
final UniversalType ut = UniversalType.of(t, this);
final List<AnnotationMirror> l = ut.getAnnotationMirrors();
l.clear();
l.addAll(annotations);
return ut;
}
/**
* Returns an {@link ArrayType} whose {@linkplain ArrayType#getComponentType() component type} is {@linkplain
* #sameType(TypeMirror, TypeMirror) the same as} the supplied {@code componentType}.
*
* @param componentType the component type; must not be {@code null}
*
* @return a non-{@code null} {@link ArrayType} whose {@linkplain ArrayType#getComponentType() component type} is
* {@linkplain #sameType(TypeMirror, TypeMirror) the same as} the supplied {@code componentType}
*
* @exception NullPointerException if {@code componentType} is {@code null}
*
* @exception IllegalArgumentException if {@code componentType} is not a valid component type
*
* @see javax.lang.model.util.Types#getArrayType(TypeMirror)
*/
// Type factory method
public ArrayType arrayTypeOf(final TypeMirror componentType);
/**
* Returns a non-{@code null} {@link TypeMirror} representing the type of the supplied {@link Element} when that
* {@link Element} is viewed as a member of, or otherwise directly contained by, the supplied {@code containingType}.
*
* <p>For example, when viewed as a member of the parameterized type {@link java.util.Set Set<String>}, the
* {@link java.util.Set#add(Object)} method (represented as an {@link ExecutableElement}) {@linkplain
* ExecutableElement#asType() has} a {@linkplain ExecutableType type} whose {@linkplain
* ExecutableType#getParameterTypes() method parameter is of type} {@link String} (not {@link String}'s erasure).</p>
*
* @param containingType the containing {@link DeclaredType}; must not be {@code null}
*
* @param e the member {@link Element}; must not be {@code null}
*
* @return a non-{@code null} {@linkplain TypeMirror type} representing the {@linkplain Element#asType() type of} the
* supplied {@link Element} when viewed as a member of the supplied {@code containingType}; never {@code null}
*
* @exception NullPointerException if either {@code containingType} or {@code e} is {@code null}
*
* @exception IllegalArgumentException if {@code e} cannot be viewed as a member of the supplied {@code
* containingType} (because it is the wrong {@linkplain Element#getKind() kind}, for example)
*
* @see javax.lang.model.util.Types#asMemberOf(DeclaredType, Element)
*/
public TypeMirror asMemberOf(final DeclaredType containingType, final Element e);
/**
* Returns {@code true} if and only if the supplied {@code payload} (the first argument) is considered assignable to
* the supplied {@code receiver} (the second argument) according to <a
* href="https://docs.oracle.com/javase/specs/jls/se21/html/jls-5.html#jls-5.2">the rules of the Java Language
* Specification</a>; <strong>note the counterintuitive order of the parameters</strong>.
*
* <p>Perhaps surprisingly, the "left hand side" of the putative assignment is represented by the second parameter
* ({@code receiver}). The "right hand side" of the putative assignment is represented by the first parameter
* ({@code payload}). This follows the contract of the {@link javax.lang.model.util.Types#isAssignable(TypeMirror,
* TypeMirror)} method, on which this method is modeled.</p>
*
* @param payload the {@link TypeMirror} being assigned; must not be {@code null}
*
* @param receiver the {@link TypeMirror} receiving the assignment; must not be {@code null}
*
* @return {@code true} if and only if {@code payload} is assignable to {@code receiver} according to <a
* href="https://docs.oracle.com/javase/specs/jls/se21/html/jls-5.html#jls-5.2">the rules of the Java Language
* Specification</a>
*
* @exception NullPointerException if either {@code payload} or {@code receiver} is {@code null}
*
* @exception IllegalArgumentException if either {@link TypeMirror} is not one that can take part in an assignment
*
* @see javax.lang.model.util.Types#isAssignable(TypeMirror, TypeMirror)
*
* @spec https://docs.oracle.com/javase/specs/jls/se21/html/jls-5.html#jls-5.2 Java Language Specification, section
* 5.2
*/
// Note the strange positioning of payload and receiver.
public boolean assignable(final TypeMirror payload, final TypeMirror receiver);
/**
* Returns the (non-{@code null}) <a
* href="https://docs.oracle.com/javase/specs/jls/se21/html/jls-13.html#jls-13.1"><dfn>binary name</dfn></a> of the
* supplied {@link TypeElement}.
*
* @param e a {@link TypeElement}; must not be {@code null}
*
* @return a non-{@code null} {@link Name}
*
* @exception NullPointerException if {@code e} is {@code null}
*
* @see javax.lang.model.util.Elements#getBinaryName(TypeElement)
*
* @spec https://docs.oracle.com/javase/specs/jls/se21/html/jls-13.html#jls-13.1 Java Language Specification, section
* 13.1
*/
public Name binaryName(final TypeElement e);
/**
* Returns {@code true} if and only if the supplied {@link ExecutableElement} represents a <dfn>bridge method</dfn>.
*
* @param e an {@link ExecutableElement}; must not be {@code null}
*
* @return {@code true} if and only if the supplied {@link ExecutableElement} represents a bridge method
*
* @exception NullPointerException if {@code e} is {@code null}
*
* @see javax.lang.model.util.Elements#isBridge(ExecutableElement)
*
* @spec https://docs.oracle.com/javase/specs/jls/se23/html/jls-8.html#jls-8.4.8.3 Java Language Specification,
* section 8.4.8.3
*
* @spec https://docs.oracle.com/javase/specs/jls/se23/html/jls-15.html#jls-15.12.4.5 Java Language Specification,
* section 15.12.4.5
*/
public boolean bridge(final ExecutableElement e);
/**
* Applies <a href="https://docs.oracle.com/javase/specs/jls/se23/html/jls-5.html#jls-5.1.10"><dfn>capture
* conversion</dfn></a> to the supplied {@link TypeMirror}, which is normally a {@linkplain TypeKind#WILDCARD wildcard
* type}.
*
* @param wildcard a {@link TypeMirror}; must not be {@code null}; if not a {@linkplain TypeKind#WILDCARD wildcard
* type}, then it will be returned unchanged
*
* @return a non-{@code null} {@link TypeMirror} representing the result of <a
* href="https://docs.oracle.com/javase/specs/jls/se23/html/jls-5.html#jls-5.1.10">capture conversion</a> applied to
* the supplied {@link TypeMirror}
*
* @exception NullPointerException if {@code wildcard} is {@code null}
*
* @see javax.lang.model.util.Types#capture(TypeMirror)
*
* @spec https://docs.oracle.com/javase/specs/jls/se23/html/jls-5.html#jls-5.1.10 Java Language Specification, section
* 5.1.10
*/
public TypeMirror capture(final TypeMirror wildcard);
/**
* Returns {@code true} if and only if {@code candidateContainer} <dfn>contains</dfn> {@code candidate}, according to
* the <a href="https://docs.oracle.com/javase/specs/jls/se23/html/jls-4.html#jls-4.5.1">Java Language Specification,
* section 4.5.1</a>.
*
* @param candidateContainer the putative containing type; normally a {@linkplain TypeKind#WILDCARD wildcard type};
* must not be {@code null}
*
* @param candidate the putative contained type; must not be {@code null}
*
* @return {@code true} if and only if {@code candidateContainer} <dfn>contains</dfn> {@code candidate}, according to
* the <a href="https://docs.oracle.com/javase/specs/jls/se23/html/jls-4.html#jls-4.5.1">Java Language Specification,
* section 4.5.1</a>; {@code false} otherwise
*
* @exception NullPointerException if either argument is {@code null}
*
* @exception IllegalArgumentException if either argument is either an {@linkplain TypeKind#EXECUTABLE executable
* type}, a {@linkplain TypeKind#MODULE module type}, or a {@linkplain TypeKind#PACKAGE package type}
*
* @spec https://docs.oracle.com/javase/specs/jls/se23/html/jls-4.html#jls-4.5.1 Java Language Specification, section
* 4.5.1
*/
public boolean contains(final TypeMirror candidateContainer, final TypeMirror candidate);
/**
* A convenience method that returns the {@link DeclaredType} {@linkplain TypeElement#asType() of} a {@link
* TypeElement} that bears the supplied {@code canonicalName}, <strong>or {@code null} if there is no such {@link
* TypeElement} (and therefore no such {@link DeclaredType})</strong>.
*
* @param canonicalName a valid canonical name; must not be {@code null}
*
* @return a {@link DeclaredType} with a {@link TypeKind} of {@link TypeKind#DECLARED}, or {@code null}
*
* @see #typeElement(CharSequence)
*
* @see #declaredType(TypeElement, TypeMirror...)
*
* @spec https://docs.oracle.com/javase/specs/jls/se23/html/jls-6.html#jls-6.7 Java Language Specification, section
* 6.7
*/
// (Convenience.)
public default DeclaredType declaredType(final CharSequence canonicalName) {
final TypeElement e = this.typeElement(canonicalName);
return e == null ? null : this.declaredType(e);
}
/**
* Returns the {@link DeclaredType} {@linkplain TypeElement#asType() of} the supplied {@link TypeElement} with the
* supplied {@link TypeMirror} arguments (if any), yielding a parameterized type.
*
* <p>Given a {@link TypeElement} representing the class named {@link java.util.Set java.util.Set} and a {@link
* TypeMirror} representing the type declared by {@link String java.lang.String}, for example, this method will return
* a {@link DeclaredType} representing the parameterized type corresponding to {@link java.util.Set
* java.util.Set<java.lang.String>}.</p>
*
* <p>The number of supplied type arguments must either equal the number of the supplied {@link TypeElement}'s
* {@linkplain TypeElement#getTypeParameters() formal type parameters}, or must be zero. If it is zero, and if the
* supplied {@link TypeElement} {@linkplain #generic(Element) is generic}, then the supplied {@link TypeElement}'s raw
* type is returned.</p>
*
* <p>If a parameterized type is returned, {@linkplain DeclaredType#asElement() its <code>TypeElement</code>} must not
* be contained within a {@linkplain #generic(Element) generic} outer class. The parameterized type {@code
* Outer<String>.Inner<Number>}, for example, may be constructed by first using this method to get the type {@code
* Outer<String>}, and then invoking {@link #declaredType(DeclaredType, TypeElement, TypeMirror...)}.</p>
*
* @param typeElement a {@link TypeElement}; must not be {@code null}
*
* @param typeArguments any type arguments (represented by {@link TypeMirror}s); must not be {@code null}
*
* @return a non-{@code null} {@link DeclaredType} with a {@link TypeKind} of {@link TypeKind#DECLARED}
*
* @exception NullPointerException if {@code typeElement} or {@code typeArguments} is {@code null}
*
* @see javax.lang.model.util.Types#getDeclaredType(TypeElement, TypeMirror...)
*
* @spec https://docs.oracle.com/javase/specs/jls/se23/html/jls-4.html#jls-4.5 Java Language Specification, section 4.5
*/
// Type factory method.
public DeclaredType declaredType(final TypeElement typeElement,
final TypeMirror... typeArguments);
/**
* Returns the {@link DeclaredType} {@linkplain TypeElement#asType() of} the supplied {@link TypeElement} with the
* supplied {@link TypeMirror} arguments (if any), given a containing {@link DeclaredType} of which it is a member,
* yielding a parameterized type.
*
* <p>Given a {@link DeclaredType} representing the parameterized type corresponding to {@code Outer<}{@link
* String}{@code >} (see the {@link #declaredType(TypeElement, TypeMirror...)} method), a {@link TypeElement}
* representing the class named {@code Outer.Inner} and a {@link DeclaredType} representing the non-generic class
* corresponding to {@link Number}, for example, this method will return a {@link DeclaredType} representing the
* parameterized type corresponding to {@code Outer<}{@link String}{@code >}{@code .Inner<}{@link Number}{@code >}.</p>
*
* <p>The number of supplied type arguments must either equal the number of the supplied {@link TypeElement}'s
* {@linkplain TypeElement#getTypeParameters() formal type parameters}, or must be zero. If it is zero, and if the
* supplied {@link TypeElement} {@link #generic(Element) is generic}, then the supplied {@link TypeElement}'s raw type
* is returned.</p>
*
* @param enclosingType a {@link DeclaredType} representing the containing type; must not be {@code null}
*
* @param typeElement a {@link TypeElement}; must not be {@code null}
*
* @param typeArguments any type arguments (represented by {@link TypeMirror}s); must not be {@code null}
*
* @return a non-{@code null} {@link DeclaredType} with a {@link TypeKind} of {@link TypeKind#DECLARED}
*
* @exception NullPointerException if {@code enclosingType}, {@code typeElement}, or {@code typeArguments} is {@code
* null}
*
* @see #declaredType(TypeElement, TypeMirror...)
*
* @see javax.lang.model.util.Types#getDeclaredType(DeclaredType, TypeElement, TypeMirror...)
*
* @spec https://docs.oracle.com/javase/specs/jls/se23/html/jls-4.html#jls-4.5 Java Language Specification, section 4.5
*/
// Type factory method.
public DeclaredType declaredType(final DeclaredType enclosingType,
final TypeElement typeElement,
final TypeMirror... typeArguments);
/**
* Returns a non-{@code null} {@link List} of the <dfn>direct supertypes</dfn> of the supplied {@link TypeMirror},
* which is normally a {@linkplain TypeKind#DECLARED declared type}.
*
* <p>The direct supertypes returned by this method are actually a subset of the direct supertypes of a type as
* defined in the <a href="https://docs.oracle.com/javase/specs/jls/se25/html/jls-4.html#jls-4.10">Java Language
* Specification, section 4.10</a>. Specifically, the subset contains only those types that can be expressed in the
* {@code extends} or {@code implements} clauses of the Java language. For example, a type {@code Baz} can declare
* only that it {@code extends Foo<Bar>}, not {@code Foo<?>}, even though {@code Foo<?>} is a specification-described
* direct supertype of {@code Baz}.</p>
*
* @param t a {@link TypeMirror}; must not be {@code null}; must not be an {@linkplain TypeKind#EXECUTABLE executable
* type}, a {@linkplain TypeKind#MODULE module type}, or a {@linkplain TypeKind#PACKAGE package type}
*
* @return a non-{@code null}, immutable {@link List} of {@link TypeMirror}s representing the direct supertypes
*
* @exception NullPointerException if {@code t} is {@code null}
*
* @exception IllegalArgumentException if either argument is either an {@linkplain TypeKind#EXECUTABLE executable
* type}, a {@linkplain TypeKind#MODULE module type}, or a {@linkplain TypeKind#PACKAGE package type}
*
* @see javax.lang.model.util.Types#directSupertypes(TypeMirror)
*
* @see <a href="https://bugs.openjdk.org/browse/JDK-8055219">JDK-8055219</a>
*
* @spec https://docs.oracle.com/javase/specs/jls/se25/html/jls-4.html#jls-4.10 Java Language Specification, section
* 4.10
*/
public List<? extends TypeMirror> directSupertypes(final TypeMirror t);
/**
* Returns the {@link Element} responsible for declaring the supplied {@link TypeMirror}, which is most commonly a
* {@link DeclaredType}, a {@link TypeVariable}, a {@link NoType} with a {@link TypeKind} of {@link TypeKind#MODULE},
* or a {@link NoType} with a {@link TypeKind} of {@link TypeKind#PACKAGE}, <strong>or {@code null} if there is no
* such {@link Element}</strong>.
*
* @param t a {@link TypeMirror}; must not be {@code null}
*
* @return an {@link Element}, or {@code null}
*
* @exception NullPointerException if {@code t} is {@code null}
*
* @see javax.lang.model.util.Types#asElement(TypeMirror)
*/
public Element element(final TypeMirror t);
/**
* A convenience method that returns the <dfn>element type</dfn> of the supplied {@link TypeMirror}.
*
* <p>The element type of an {@linkplain TypeKind#ARRAY array type} is the element type of its {@linkplain
* ArrayType#getComponentType() component type}.</p>.
*
* <p>The element type of every other kind of type is the type itself. Note that the semantics of the prior sentence
* diverge deliberately, primarily for convenience, from those of the relevant section in the Java Language
* Specification.</p>
*
* @param t a {@link TypeMirror}; must not be {@code null}
*
* @return the <dfn>element type</dfn> of the supplied {@link TypeMirror}; never {@code null}
*
* @exception NullPointerException if {@code t} is {@code null}
*
* @spec https://docs.oracle.com/javase/specs/jls/se23/html/jls-10.html#jls-10.1 Java Language Specification, section
* 10.1
*/
// (Convenience.)
public default TypeMirror elementType(final TypeMirror t) {
return switch (t) {
case null -> throw new NullPointerException("t");
case UniversalType ut -> ut.elementType();
default -> {
try (var lock = lock()) {
yield t.getKind() == TypeKind.ARRAY ? this.elementType(((ArrayType)t).getComponentType()) : t;
}
}
};
}
/**
* Returns the <dfn>erasure</dfn> of the supplied {@link TypeMirror}.
*
* @param <T> a {@link TypeMirror} specialization
*
* @param t the {@link TypeMirror} representing the type whose erasure should be returned; must not be {@code null}
*
* @return the erasure of the supplied {@link TypeMirror}; never {@code null}
*
* @exception NullPointerException if {@code t} is {@code null}
*
* @exception IllegalArgumentException if {@code t} is a {@link NoType} with a {@link TypeKind} of {@link
* TypeKind#MODULE}, or a {@link NoType} with a {@link TypeKind} of {@link TypeKind#PACKAGE}
*
* @see javax.lang.model.util.Types#erasure(TypeMirror)
*
* @spec https://docs.oracle.com/javase/specs/jls/se23/html/jls-4.html#jls-4.6 Java Language Specification, section
* 4.6
*/
public <T extends TypeMirror> T erasure(final T t);
/**
* Returns an {@link ExecutableElement} corresponding to the supplied {@link Executable}.
*
* @param e an {@link Executable}; must not be {@code null}
*
* @return an {@link ExecutableElement} corresponding to the supplied {@link Executable}; never {@code null}
*
* @exception NullPointerException if {@code e} is {@code null}
*
* @exception IllegalArgumentException if somehow {@code e} is neither a {@link Constructor} nor a {@link Method}
*/
// (Convenience.)
public default ExecutableElement executableElement(final Executable e) {
return switch (e) {
case null -> throw new NullPointerException("e");
case Constructor<?> c ->
this.executableElement(this.typeElement(c.getDeclaringClass().getCanonicalName()),
this.noType(TypeKind.VOID),
"<init>",
this.types(c.getParameterTypes()));
case Method m ->
this.executableElement(this.typeElement(m.getDeclaringClass().getCanonicalName()),
this.type(m.getReturnType()),
m.getName(),
this.types(m.getParameterTypes()));
default -> throw new IllegalArgumentException("e: " + e);
};
}
/**
* A convenience method that returns an {@link ExecutableElement} representing the static initializer, constructor or
* method described by the supplied arguments, <strong>or {@code null} if no such {@link ExecutableElement}
* exists</strong>.
*
* @param declaringElement a {@link TypeElement} representing the class that declares the executable; must not be
* {@code null}
*
* @param returnType the {@linkplain ExecutableElement#getReturnType() return type} of the executable; must not be
* {@code null}
*
* @param name the {@linkplain ExecutableElement#getSimpleName() name} of the executable; must not be {@code null}
*
* @param parameterTypes {@link TypeMirror}s that represent the executable's {@linkplain
* ExecutableElement#getParameters() parameter types}
*
* @return an {@link ExecutableElement} with an {@link ElementKind} of {@link ElementKind#CONSTRUCTOR}, {@link
* ElementKind#METHOD}, or {@link ElementKind#STATIC_INIT}, or {@code null}
*
* @exception NullPointerException if any argument is {@code null}
*/
// (Convenience.)
public default ExecutableElement executableElement(final TypeElement declaringElement,
final TypeMirror returnType,
final CharSequence name,
final TypeMirror... parameterTypes) {
return switch (declaringElement) {
case null -> throw new NullPointerException("declaringElement");
case UniversalElement ue -> {
final List<? extends UniversalElement> ees = ue.getEnclosedElements();
yield ees.stream()
.sequential()
.filter(e -> e.getKind().isExecutable() && e.getSimpleName().contentEquals(name))
.map(UniversalElement.class::cast)
.filter(ee -> {
if (!this.sameType(returnType, ee.getReturnType())) {
return false;
}
final List<? extends UniversalElement> ps = ee.getParameters();
if (ps.size() != parameterTypes.length) {
return false;
}
for (int i = 0; i < parameterTypes.length; i++) {
if (!this.sameType(ps.get(i).asType(), parameterTypes[i])) {
return false;
}
}
return true;
})
.findFirst()
.orElse(null);
}
default -> {
try (var lock = this.lock()) {
final List<? extends Element> ees = declaringElement.getEnclosedElements();
yield ees.stream()
.sequential()
.filter(e -> e.getKind().isExecutable() && e.getSimpleName().contentEquals(name))
.map(ExecutableElement.class::cast)
.filter(ee -> {
if (!this.sameType(returnType, ee.getReturnType())) {
return false;
}
final List<? extends VariableElement> ps = ee.getParameters();
if (ps.size() != parameterTypes.length) {
return false;
}
for (int i = 0; i < parameterTypes.length; i++) {
if (!this.sameType(ps.get(i).asType(), parameterTypes[i])) {
return false;
}
}
return true;
})
.findFirst()
.orElse(null);
}
}
};
}
/**
* A convenience method that returns {@code true} if and only if the supplied {@link Element} is <dfn>generic</dfn>.
*
* @param e an {@link Element}; must not be {@code null}
*
* @return {@code true} if and only if the supplied {@link Element} is <dfn>generic</dfn>; {@code false} otherwise
*
* @exception NullPointerException if {@code e} is {@code null}
*
* @see Parameterizable
*
* @spec https://docs.oracle.com/javase/specs/jls/se23/html/jls-8.html#jls-8.1.2 Java Language Specification, section
* 8.1.2
*
* @spec https://docs.oracle.com/javase/specs/jls/se23/html/jls-8.html#jls-8.4.4 Java Language Specification, section
* 8.4.4
*
* @spec https://docs.oracle.com/javase/specs/jls/se23/html/jls-8.html#jls-8.8.4 Java Language Specification, section
* 8.8.4
*
* @spec https://docs.oracle.com/javase/specs/jls/se23/html/jls-9.html#jls-9.1.2 Java Language Specification, section
* 9.1.2
*/
// (Convenience.)
public default boolean generic(final Element e) {
return switch (e) {
case null -> throw new NullPointerException("e");
case UniversalElement ue -> ue.generic();
case Parameterizable p -> {
try (var lock = this.lock()) {
yield switch (e.getKind()) {
case CLASS, CONSTRUCTOR, ENUM, INTERFACE, METHOD, RECORD -> !p.getTypeParameters().isEmpty();
default -> false;
};
}
}
default -> false;
};
}
/**
* A convenience method that returns {@code true} if and only if the supplied {@link TypeMirror} is declared by an
* {@link Element} that {@linkplain #generic(Element) is generic}.
*
* @param t a {@link TypeMirror}; must not be {@code null}
*
* @return {@code true} if and only if the supplied {@link TypeMirror} is declared by an {@link Element} that
* {@linkplain #generic(Element) is generic}
*
* @exception NullPointerException if {@code t} is {@code null}
*
* @see #generic(Element)
*
* @spec https://docs.oracle.com/javase/specs/jls/se23/html/jls-8.html#jls-8.1.2 Java Language Specification, section
* 8.1.2
*
* @spec https://docs.oracle.com/javase/specs/jls/se23/html/jls-8.html#jls-8.4.4 Java Language Specification, section
* 8.4.4
*
* @spec https://docs.oracle.com/javase/specs/jls/se23/html/jls-8.html#jls-8.8.4 Java Language Specification, section
* 8.8.4
*
* @spec https://docs.oracle.com/javase/specs/jls/se23/html/jls-9.html#jls-9.1.2 Java Language Specification, section
* 9.1.2
*/
// (Convenience.)
public default boolean generic(final TypeMirror t) {
return switch (t) {
case null -> throw new NullPointerException("t");
case UniversalType ut -> ut.generic();
default -> {
try (var lock = this.lock()) {
final Element e = this.element(t);
yield e != null && this.generic(e);
}
}
};
}
/**
* A convenience method that returns {@code true} if and only if the supplied {@link Element} represents the (essentially
* primordial) {@code java.lang.Object} {@link Element}.
*
* @param e an {@link Element}; must not be {@code null}
*
* @return {@code true} if and only if the supplied {@link Element} represents the (essentially
* primordial) {@code java.lang.Object} {@link Element}; {@code false} otherwise
*
* @exception NullPointerException if {@code e} is {@code null}
*/
// (Convenience.)
public default boolean javaLangObject(final Element e) {
return switch (e) {
case null -> throw new NullPointerException("e");
case UniversalElement ue -> ue.javaLangObject();
default -> {
try (var lock = this.lock()) {
yield
e.getKind() == ElementKind.CLASS &&
((QualifiedNameable)e).getQualifiedName().contentEquals("java.lang.Object");
}
}
};
}
/**
* A convenience method that returns {@code true} if and only if the supplied {@link TypeMirror} represents the {@link
* DeclaredType} declared by the (essentially primordial) {@code java.lang.Object} element.
*
* @param t a {@link TypeMirror}; must not be {@code null}
*
* @return {@code true} represents the {@link
* DeclaredType} declared by the (essentially primordial) {@code java.lang.Object} element; {@code false} otherwise
*
* @exception NullPointerException if {@code t} is {@code null}
*
* @see #javaLangObject(Element)
*/
// (Convenience.)
public default boolean javaLangObject(final TypeMirror t) {
return switch (t) {
case null -> throw new NullPointerException("t");
case UniversalType ut -> ut.javaLangObject();
default -> {
try (var lock = this.lock()) {
yield
t.getKind() == DECLARED &&
javaLangObject(((DeclaredType)t).asElement());
}
}
};
}
/**
* A convenience method that returns the {@link TypeElement} representing the class named {@link Object
* java.lang.Object}.
*
* @return a non-{@code null} {@link TypeElement} whose {@linkplain TypeElement#getQualifiedName() qualified name} is
* {@linkplain Name#contentEquals(CharSequence) equal to} {@code java.lang.Object}
*
* @see #typeElement(CharSequence)
*/
// (Convenience.)
public default TypeElement javaLangObject() {
return this.typeElement("java.lang.Object");
}
// (Convenience.)
@Override // PrimordialDomain
public default DeclaredType javaLangObjectType() {
return (DeclaredType)this.javaLangObject().asType();
}
/**
* Returns a {@link ModuleElement} representing the module {@linkplain ModuleElement#getQualifiedName() named} by the
* supplied {@code qualifiedName}, <strong>or {@code null} if there is no such {@link ModuleElement}</strong>.
*
* @param qualifiedName a name suitable for naming a module; must not be {@code null}; may be {@linkplain
* CharSequence#isEmpty() empty}, in which case a {@link ModuleElement} representing an <dfn>unnamed module</dfn> will
* be returned
*
* @return a {@link ModuleElement}, or {@code null}
*
* @exception NullPointerException if {@code qualifiedName} is {@code null}
*
* @see javax.lang.model.util.Elements#getModuleElement(CharSequence)
*
* @spec https://docs.oracle.com/javase/specs/jls/se23/html/jls-7.html#jls-7.7 Java Language Specification, section
* 7.7
*/
// Element factory method.
public ModuleElement moduleElement(final CharSequence qualifiedName);
/**
* Returns a {@link Name} representing the supplied {@link CharSequence}.
*
* @param name a {@link CharSequence}; must not be {@code null}
*
* @return a non-{@code null} {@link Name} representing the supplied {@link name}
*
* @exception NullPointerException if {@code name} is {@code null}
*
* @see #lock()
*
* @see javax.lang.model.util.Elements#getName(CharSequence)
*/
// Element factory method.
public Name name(final CharSequence name);
/**
* Returns a {@link NoType} {@linkplain TypeMirror#getKind() bearing} the supplied {@link TypeKind}, if the supplied
* {@link TypeKind} is either {@link TypeKind#NONE} or {@link TypeKind#VOID}.
*
* @param kind a {@link TypeKind}; must be either {@link TypeKind#NONE} or {@link TypeKind#VOID}
*
* @return a non-{@code null} {@link NoType} {@linkplain TypeMirror#getKind() bearing} the supplied {@link TypeKind}
*
* @exception NullPointerException if {@code kind} is {@code null}
*
* @exception IllegalArgumentException if {@code kind} is non-{@code null} and neither {@link TypeKind#NONE} nor
* {@link TypeKind#VOID}
*
* @see TypeKind#NONE
*
* @see TypeKind#VOID
*
* @see javax.lang.model.util.Types#getNoType(TypeKind)
*
* @see NoType
*
* @spec https://docs.oracle.com/javase/specs/jls/se23/html/jls-8.html#jls-8.4.5 Java Language Specification, section
* 8.4.5
*/
// Type factory method.
@Override // PrimordialDomain
public NoType noType(final TypeKind kind);
/**
* Returns a {@link NullType} implementation {@linkplain TypeMirror#getKind() whose <code>TypeKind</code>} is {@link
* TypeKind#NULL}.
*
* @return a non-{@code null} {@link NullType}
*
* @see javax.lang.model.util.Types#getNullType()
*
* @see NullType
*
* @spec https://docs.oracle.com/javase/specs/jls/se23/html/jls-4.html#jls-4.1 Java Language Specification, section 4.1
*/
// Type factory method.
@Override // PrimordialDomain
public NullType nullType();
/**
* Returns the {@linkplain Origin origin} of the supplied {@link Element}.
*
* @param e a non-{@code null} {@link Element}
*
* @return a non-{@code null} {@link Origin}
*
* @see Elements#getOrigin(Element)
*
* @see Origin
*/
public Origin origin(final Element e);
/**
* Returns a {@link PackageElement} representing the package bearing the supplied {@code canonicalName}, <strong>or
* {@code null} if there is no such {@link PackageElement}</strong>.
*
* @param canonicalName a canonical name suitable for naming a package; must not be {@code null}; may be {@linkplain
* CharSequence#isEmpty() empty}, in which case a {@link ModuleElement} representing an <dfn>unnamed package</dfn> will
* be returned
*
* @return a {@link PackageElement}, or {@code null}
*
* @exception NullPointerException if {@code canonicalName} is {@code null}
*
* @see javax.lang.model.util.Elements#getPackageElement(CharSequence)
*
* @see PackageElement
*
* @spec https://docs.oracle.com/javase/specs/jls/se23/html/jls-6.html#jls-6.7 Java Language Specification, section
* 6.7
*/
// Element factory method.
public PackageElement packageElement(final CharSequence canonicalName);
/**
* Returns a {@link PackageElement} representing the package bearing the supplied {@code canonicalName} as seen from
* the module represented by the supplied {@link ModuleElement}, <strong>or {@code null} if there is no such {@link
* PackageElement}</strong>.
*
* @param asSeenFrom a {@link ModuleElement}; must not be {@code null}
*
* @param canonicalName a canonical name suitable for naming a package; must not be {@code null}; may be {@linkplain
* CharSequence#isEmpty() empty}, in which case a {@link ModuleElement} representing an <dfn>unnamed package</dfn> will
* be returned
*
* @return a {@link PackageElement}, or {@code null}
*
* @exception NullPointerException if either {@code asSeenFrom} or {@code canonicalName} is {@code null}
*
* @see javax.lang.model.util.Elements#getPackageElement(ModuleElement, CharSequence)
*
* @see PackageElement
*
* @spec https://docs.oracle.com/javase/specs/jls/se23/html/jls-6.html#jls-6.7 Java Language Specification, section
* 6.7
*/
// Element factory method.
public PackageElement packageElement(final ModuleElement asSeenFrom, final CharSequence canonicalName);
/**
* Returns a {@link Parameterizable} corresponding to the supplied (reflective) {@link GenericDeclaration}.
*
* @param gd a {@link GenericDeclaration}; must not be {@code null}
*
* @return a {@link Parameterizable} corresponding to the supplied {@link GenericDeclaration}; never {@code null}
*
* @exception NullPointerException if {@code gd} is {@code null}
*
* @exception IllegalArgumentException if {@code gd} is neither a {@link Class} nor an {@link Executable}
*
* @see Parameterizable
*/
// (Convenience.)
public default Parameterizable parameterizable(final GenericDeclaration gd) {
return switch (gd) {
case null -> throw new NullPointerException("gd");
case Class<?> c -> this.typeElement(c.getCanonicalName());
case Executable e -> this.executableElement(e);
default -> throw new IllegalArgumentException("gd: " + gd);
};
}
/**
* A convenience method that returns {@code true} if and only if {@code t} is a {@link DeclaredType}, {@linkplain
* TypeMirror#getKind() has a <code>TypeKind</code>} of {@link TypeKind#DECLARED DECLARED}, and {@linkplain
* DeclaredType#getTypeArguments() has a non-empty type arguments list}.
*
* @param t a {@link TypeMirror}; must not be {@code null}
*
* @return {@code true} if and only if {@code t} is a {@link DeclaredType}, {@linkplain
* TypeMirror#getKind() has a <code>TypeKind</code>} of {@link TypeKind#DECLARED DECLARED}, and {@linkplain
* DeclaredType#getTypeArguments() has a non-empty type arguments list}; {@code false} otherwise
*
* @exception NullPointerException if {@code t} is {@code null}
*/
// (Convenience.)
public default boolean parameterized(final TypeMirror t) {
return switch (t) {
case null -> throw new NullPointerException("t");
case UniversalType ut -> ut.parameterized();
default -> {
try (var lock = this.lock()) {
yield t.getKind() == DECLARED && !((DeclaredType)t).getTypeArguments().isEmpty();