forked from microbean/microbean-construct
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConstables.java
More file actions
746 lines (715 loc) · 36.3 KB
/
Constables.java
File metadata and controls
746 lines (715 loc) · 36.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
/* -*- mode: Java; c-basic-offset: 2; indent-tabs-mode: nil; coding: utf-8-unix -*-
*
* Copyright © 2024 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.constant;
import java.lang.constant.ClassDesc;
import java.lang.constant.Constable;
import java.lang.constant.ConstantDesc;
import java.lang.constant.DynamicConstantDesc;
import java.lang.constant.MethodHandleDesc;
import java.lang.constant.MethodTypeDesc;
import java.util.List;
import java.util.Optional;
import javax.lang.model.AnnotatedConstruct;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.ModuleElement;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.Name;
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.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 org.microbean.construct.Domain;
import static java.lang.constant.ConstantDescs.BSM_INVOKE;
import static java.lang.constant.ConstantDescs.NULL;
import static java.lang.constant.DirectMethodHandleDesc.Kind.VIRTUAL;
import static org.microbean.construct.constant.ConstantDescs.CD_ArrayType;
import static org.microbean.construct.constant.ConstantDescs.CD_CharSequence;
import static org.microbean.construct.constant.ConstantDescs.CD_DeclaredType;
import static org.microbean.construct.constant.ConstantDescs.CD_Element;
import static org.microbean.construct.constant.ConstantDescs.CD_ExecutableElement;
import static org.microbean.construct.constant.ConstantDescs.CD_ModuleElement;
import static org.microbean.construct.constant.ConstantDescs.CD_Name;
import static org.microbean.construct.constant.ConstantDescs.CD_NoType;
import static org.microbean.construct.constant.ConstantDescs.CD_NullType;
import static org.microbean.construct.constant.ConstantDescs.CD_PackageElement;
import static org.microbean.construct.constant.ConstantDescs.CD_Parameterizable;
import static org.microbean.construct.constant.ConstantDescs.CD_PrimitiveType;
import static org.microbean.construct.constant.ConstantDescs.CD_RecordComponentElement;
import static org.microbean.construct.constant.ConstantDescs.CD_TypeElement;
import static org.microbean.construct.constant.ConstantDescs.CD_TypeKind;
import static org.microbean.construct.constant.ConstantDescs.CD_TypeParameterElement;
import static org.microbean.construct.constant.ConstantDescs.CD_TypeMirror;
import static org.microbean.construct.constant.ConstantDescs.CD_TypeVariable;
import static org.microbean.construct.constant.ConstantDescs.CD_WildcardType;
/**
* A utility class that returns nominal descriptors for constructs.
*
* @author <a href="https://about.me/lairdnelson" target="_top">Laird Nelson</a>
*
* @see #describe(Element, Domain)
*
* @see #describe(TypeMirror, Domain)
*/
@SuppressWarnings("try")
public final class Constables {
private Constables() {
super();
}
/**
* Returns a nominal descriptor for the supplied argument, presuming it to have originated from the supplied {@link
* Domain}, or an {@linkplain Optional#empty() empty} {@link Optional} if the supplied argument cannot be described.
*
* @param n the argument; may be {@code null}
*
* @param d the {@link Domain} from which the argument originated; must not be {@code null}
*
* @return a non-{@code null} {@link Optional}
*
* @exception NullPointerException if {@code d} is {@code null}
*/
public static final Optional<? extends ConstantDesc> describe(final Name n, final Domain d) {
return switch (n) {
case null -> Optional.of(NULL);
case Constable c -> c.describeConstable();
case ConstantDesc cd -> Optional.of(cd); // future proofing?
default -> (d instanceof Constable c ? c.describeConstable() : Optional.<ConstantDesc>empty())
.map(domainDesc -> DynamicConstantDesc.of(BSM_INVOKE,
MethodHandleDesc.ofMethod(VIRTUAL,
ClassDesc.of(Domain.class.getName()),
"name",
MethodTypeDesc.of(CD_Name,
CD_CharSequence)),
domainDesc,
d.toString(n)));
};
}
/**
* Returns a nominal descriptor for the supplied argument, presuming it to have originated from the supplied {@link
* Domain}, or an {@linkplain Optional#empty() empty} {@link Optional} if the supplied argument cannot be described.
*
* @param ac the argument; may be {@code null}
*
* @param d the {@link Domain} from which the argument originated; must not be {@code null}
*
* @return a non-{@code null} {@link Optional}
*
* @exception NullPointerException if {@code d} is {@code null}
*/
public static final Optional<? extends ConstantDesc> describe(final AnnotatedConstruct ac, final Domain d) {
return switch (ac) {
case null -> Optional.of(NULL);
case Constable c -> c.describeConstable();
case ConstantDesc cd -> Optional.of(cd); // future proofing?
case Element e -> describe(e, d);
case TypeMirror t -> describe(t, d);
default -> Optional.empty();
};
}
/**
* Returns a nominal descriptor for the supplied argument, presuming it to have originated from the supplied {@link
* Domain}, or an {@linkplain Optional#empty() empty} {@link Optional} if the supplied argument cannot be described.
*
* @param e the argument; may be {@code null}
*
* @param d the {@link Domain} from which the argument originated; must not be {@code null}
*
* @return a non-{@code null} {@link Optional}
*
* @exception NullPointerException if {@code d} is {@code null}
*/
public static final Optional<? extends ConstantDesc> describe(final Element e, final Domain d) {
return switch (e) {
case null -> Optional.of(NULL);
case Constable c -> c.describeConstable();
case ConstantDesc cd -> Optional.of(cd); // future proofing?
default -> {
try (var lock = d.lock()) {
yield switch (e.getKind()) {
case ANNOTATION_TYPE, CLASS, ENUM, INTERFACE, RECORD -> describe((TypeElement)e, d);
case BINDING_VARIABLE, EXCEPTION_PARAMETER, LOCAL_VARIABLE, OTHER, RESOURCE_VARIABLE ->
// No way to get these from javax.lang.model.Elements
Optional.empty();
case CONSTRUCTOR, INSTANCE_INIT, METHOD, STATIC_INIT -> describe((ExecutableElement)e, d);
case ENUM_CONSTANT, FIELD, PARAMETER -> describe((VariableElement)e, d);
case MODULE -> describe((ModuleElement)e, d);
case PACKAGE -> describe((PackageElement)e, d);
case RECORD_COMPONENT -> describe((RecordComponentElement)e, d);
case TYPE_PARAMETER -> describe((TypeParameterElement)e, d);
};
}
}
};
}
/**
* Returns a nominal descriptor for the supplied argument, presuming it to have originated from the supplied {@link
* Domain}, or an {@linkplain Optional#empty() empty} {@link Optional} if the supplied argument cannot be described.
*
* @param e the argument; may be {@code null}
*
* @param d the {@link Domain} from which the argument originated; must not be {@code null}
*
* @return a non-{@code null} {@link Optional}
*
* @exception NullPointerException if {@code d} is {@code null}
*/
public static final Optional<? extends ConstantDesc> describe(final ExecutableElement e, final Domain d) {
return switch (e) {
case null -> Optional.of(NULL);
case Constable c -> c.describeConstable();
case ConstantDesc cd -> Optional.of(cd); // future proofing?
default -> {
final ConstantDesc domainDesc = d instanceof Constable c ? c.describeConstable().orElse(null) : null;
if (domainDesc == null) {
yield Optional.empty();
}
try (var lock = d.lock()) {
// Trying to do this via flatMap etc. simply will not work because type inference does not work properly, even
// with hints/coercion. Feel free to try again, but make sure you keep this implementation around to revert
// back to.
final List<? extends VariableElement> parameters = e.getParameters();
final int parameterCount = parameters.size();
final ConstantDesc[] args = new ConstantDesc[5 + parameterCount];
args[0] =
MethodHandleDesc.ofMethod(VIRTUAL,
ClassDesc.of(Domain.class.getName()),
"executableElement",
MethodTypeDesc.of(CD_ExecutableElement,
CD_TypeElement,
CD_TypeMirror,
CD_CharSequence,
CD_TypeMirror.arrayType()));
args[1] = domainDesc;
args[2] = describe(e.getEnclosingElement(), d).orElse(null);
if (args[2] == null) {
yield Optional.empty();
}
args[3] = describe(e.getReturnType(), d).orElse(null);
if (args[3] == null) {
yield Optional.empty();
}
args[4] = describe(e.getSimpleName(), d).orElse(null);
if (args[4] == null) {
yield Optional.empty();
}
for (int i = 0; i < parameterCount; i++) {
int index = i + 5;
args[index] = describe(parameters.get(i).asType(), d).orElse(null);
if (args[index] == null) {
yield Optional.empty();
}
}
yield Optional.of(DynamicConstantDesc.of(BSM_INVOKE, args));
}
}
};
}
/**
* Returns a nominal descriptor for the supplied argument, presuming it to have originated from the supplied {@link
* Domain}, or an {@linkplain Optional#empty() empty} {@link Optional} if the supplied argument cannot be described.
*
* @param e the argument; may be {@code null}
*
* @param d the {@link Domain} from which the argument originated; must not be {@code null}
*
* @return a non-{@code null} {@link Optional}
*
* @exception NullPointerException if {@code d} is {@code null}
*/
public static final Optional<? extends ConstantDesc> describe(final ModuleElement e, final Domain d) {
return switch (e) {
case null -> Optional.of(NULL);
case Constable c -> c.describeConstable();
case ConstantDesc cd -> Optional.of(cd); // future proofing?
default -> describe(e.getQualifiedName(), d) // getQualifiedName() does not cause symbol completion
.map(nameDesc -> DynamicConstantDesc.of(BSM_INVOKE,
MethodHandleDesc.ofMethod(VIRTUAL,
ClassDesc.of(Domain.class.getName()),
"moduleElement",
MethodTypeDesc.of(CD_ModuleElement,
CD_CharSequence)),
((Constable)d).describeConstable().orElseThrow(),
nameDesc));
};
}
/**
* Returns a nominal descriptor for the supplied argument, presuming it to have originated from the supplied {@link
* Domain}, or an {@linkplain Optional#empty() empty} {@link Optional} if the supplied argument cannot be described.
*
* @param e the argument; may be {@code null}
*
* @param d the {@link Domain} from which the argument originated; must not be {@code null}
*
* @return a non-{@code null} {@link Optional}
*
* @exception NullPointerException if {@code d} is {@code null}
*/
public static final Optional<? extends ConstantDesc> describe(final PackageElement e, final Domain d) {
return switch (e) {
case null -> Optional.of(NULL);
case Constable c -> c.describeConstable();
case ConstantDesc cd -> Optional.of(cd); // future proofing?
default -> describe(e.getQualifiedName(), d) // getQualifiedName() does not cause symbol completion
.map(nameDesc -> DynamicConstantDesc.of(BSM_INVOKE,
MethodHandleDesc.ofMethod(VIRTUAL,
ClassDesc.of(Domain.class.getName()),
"packageElement",
MethodTypeDesc.of(CD_PackageElement,
CD_CharSequence)),
((Constable)d).describeConstable().orElseThrow(),
nameDesc));
};
}
/**
* Returns a nominal descriptor for the supplied argument, presuming it to have originated from the supplied {@link
* Domain}, or an {@linkplain Optional#empty() empty} {@link Optional} if the supplied argument cannot be described.
*
* @param e the argument; may be {@code null}
*
* @param d the {@link Domain} from which the argument originated; must not be {@code null}
*
* @return a non-{@code null} {@link Optional}
*
* @exception NullPointerException if {@code d} is {@code null}
*/
public static final Optional<? extends ConstantDesc> describe(final TypeElement e, final Domain d) {
return switch (e) {
case null -> Optional.of(NULL);
case Constable c -> c.describeConstable();
case ConstantDesc cd -> Optional.of(cd); // future proofing?
default -> describe(e.getQualifiedName(), d) // getQualifiedName() does not cause symbol completion
.map(nameDesc -> DynamicConstantDesc.of(BSM_INVOKE,
MethodHandleDesc.ofMethod(VIRTUAL,
ClassDesc.of(Domain.class.getName()),
"typeElement",
MethodTypeDesc.of(CD_TypeElement,
CD_CharSequence)),
((Constable)d).describeConstable().orElseThrow(),
nameDesc));
};
}
/**
* Returns a nominal descriptor for the supplied argument, presuming it to have originated from the supplied {@link
* Domain}, or an {@linkplain Optional#empty() empty} {@link Optional} if the supplied argument cannot be described.
*
* @param e the argument; may be {@code null}
*
* @param d the {@link Domain} from which the argument originated; must not be {@code null}
*
* @return a non-{@code null} {@link Optional}
*
* @exception NullPointerException if {@code d} is {@code null}
*/
public static final Optional<? extends ConstantDesc> describe(final TypeParameterElement e, final Domain d) {
return switch (e) {
case null -> Optional.of(NULL);
case Constable c -> c.describeConstable();
case ConstantDesc cd -> Optional.of(cd); // future proofing?
default -> {
try (var lock = d.lock()) {
yield describe(e.getEnclosingElement(), d)
.flatMap(parameterizableDesc -> describe(e.getSimpleName(), d)
.map(nameDesc -> DynamicConstantDesc.of(BSM_INVOKE,
MethodHandleDesc.ofMethod(VIRTUAL,
ClassDesc.of(Domain.class.getName()),
"typeParameterElement",
MethodTypeDesc.of(CD_TypeParameterElement,
CD_Parameterizable,
CD_Name)),
((Constable)d).describeConstable().orElseThrow(),
parameterizableDesc,
nameDesc)));
}
}
};
}
/**
* Returns a nominal descriptor for the supplied argument, presuming it to have originated from the supplied {@link
* Domain}, or an {@linkplain Optional#empty() empty} {@link Optional} if the supplied argument cannot be described.
*
* @param e the argument; may be {@code null}
*
* @param d the {@link Domain} from which the argument originated; must not be {@code null}
*
* @return a non-{@code null} {@link Optional}
*
* @exception NullPointerException if {@code d} is {@code null}
*/
public static final Optional<? extends ConstantDesc> describe(final RecordComponentElement e, final Domain d) {
return switch (e) {
case null -> Optional.of(NULL);
case Constable c -> c.describeConstable();
case ConstantDesc cd -> Optional.of(cd); // future proofing?
default -> {
try (var lock = d.lock()) {
yield describe((TypeElement)e.getEnclosingElement(), d)
.map(executableDesc -> DynamicConstantDesc.of(BSM_INVOKE,
MethodHandleDesc.ofMethod(VIRTUAL,
ClassDesc.of(Domain.class.getName()),
"recordComponentElement",
MethodTypeDesc.of(CD_RecordComponentElement,
CD_ExecutableElement)),
((Constable)d).describeConstable().orElseThrow(),
executableDesc));
}
}
};
}
/**
* Returns a nominal descriptor for the supplied argument, presuming it to have originated from the supplied {@link
* Domain}, or an {@linkplain Optional#empty() empty} {@link Optional} if the supplied argument cannot be described.
*
* @param e the argument; may be {@code null}
*
* @param d the {@link Domain} from which the argument originated; must not be {@code null}
*
* @return a non-{@code null} {@link Optional}
*
* @exception NullPointerException if {@code d} is {@code null}
*/
public static final Optional<? extends ConstantDesc> describe(final VariableElement e, final Domain d) {
return switch (e) {
case null -> Optional.of(NULL);
case Constable c -> c.describeConstable();
case ConstantDesc cd -> Optional.of(cd); // future proofing?
default -> {
try (var lock = d.lock()) {
yield describe(e.getSimpleName(), d)
.flatMap(nameDesc -> describe(e.getEnclosingElement(), d)
.map(enclosingElementDesc -> DynamicConstantDesc.of(BSM_INVOKE,
MethodHandleDesc.ofMethod(VIRTUAL,
ClassDesc.of(Domain.class.getName()),
"variableElement",
MethodTypeDesc.of(CD_Element,
CD_CharSequence)),
((Constable)d).describeConstable().orElseThrow(),
nameDesc)));
}
}
};
}
/**
* Returns a nominal descriptor for the supplied argument, presuming it to have originated from the supplied {@link
* Domain}, or an {@linkplain Optional#empty() empty} {@link Optional} if the supplied argument cannot be described.
*
* @param t the argument; may be {@code null}
*
* @param d the {@link Domain} from which the argument originated; must not be {@code null}
*
* @return a non-{@code null} {@link Optional}
*
* @exception NullPointerException if {@code d} is {@code null}
*/
public static final Optional<? extends ConstantDesc> describe(final TypeMirror t, final Domain d) {
return switch (t) {
case null -> Optional.of(NULL);
case Constable c -> c.describeConstable();
case ConstantDesc cd -> Optional.of(cd); // future proofing?
default -> {
try (var lock = d.lock()) {
yield switch (t.getKind()) {
case ARRAY -> describe((ArrayType)t, d);
case BOOLEAN, BYTE, CHAR, DOUBLE, FLOAT, INT, LONG, SHORT -> describe((PrimitiveType)t, d);
case DECLARED -> describe((DeclaredType)t, d);
case EXECUTABLE, INTERSECTION, UNION -> Optional.empty(); // No way to get these from javax.lang.model.util.Types
case ERROR, OTHER -> Optional.empty();
case MODULE, NONE, PACKAGE, VOID -> describe((NoType)t, d);
case NULL -> describe((NullType)t, d);
case TYPEVAR -> describe((TypeVariable)t, d); // Prefer working with TypeParameterElement instead
case WILDCARD -> describe((WildcardType)t, d);
};
}
}
};
}
/**
* Returns a nominal descriptor for the supplied argument, presuming it to have originated from the supplied {@link
* Domain}, or an {@linkplain Optional#empty() empty} {@link Optional} if the supplied argument cannot be described.
*
* @param t the argument; may be {@code null}
*
* @param d the {@link Domain} from which the argument originated; must not be {@code null}
*
* @return a non-{@code null} {@link Optional}
*
* @exception NullPointerException if {@code d} is {@code null}
*/
public static final Optional<? extends ConstantDesc> describe(final ArrayType t, final Domain d) {
return switch (t) {
case null -> Optional.of(NULL);
case Constable c -> c.describeConstable();
case ConstantDesc cd -> Optional.of(cd); // future proofing?
default -> {
try (var lock = d.lock()) {
yield describe(t.getComponentType(), d)
.map(componentTypeDesc -> DynamicConstantDesc.of(BSM_INVOKE,
MethodHandleDesc.ofMethod(VIRTUAL,
ClassDesc.of(Domain.class.getName()),
"arrayTypeOf",
MethodTypeDesc.of(CD_ArrayType,
CD_TypeMirror)),
((Constable)d).describeConstable().orElseThrow(),
componentTypeDesc));
}
}
};
}
/**
* Returns a nominal descriptor for the supplied argument, presuming it to have originated from the supplied {@link
* Domain}, or an {@linkplain Optional#empty() empty} {@link Optional} if the supplied argument cannot be described.
*
* @param t the argument; may be {@code null}
*
* @param d the {@link Domain} from which the argument originated; must not be {@code null}
*
* @return a non-{@code null} {@link Optional}
*
* @exception NullPointerException if {@code d} is {@code null}
*/
public static final Optional<? extends ConstantDesc> describe(final DeclaredType t, final Domain d) {
return switch (t) {
case null -> Optional.of(NULL);
case Constable c -> c.describeConstable();
case ConstantDesc cd -> Optional.of(cd); // future proofing?
default -> {
final ConstantDesc domainDesc = d instanceof Constable constableDomain ? constableDomain.describeConstable().orElse(null) : null;
if (domainDesc == null) {
yield Optional.empty();
}
try (var lock = d.lock()) {
yield switch (t.getKind()) {
case DECLARED -> {
final List<? extends TypeMirror> typeArguments = t.getTypeArguments();
final int typeArgumentCount = typeArguments.size();
final ConstantDesc[] args = new ConstantDesc[4 + typeArgumentCount];
final TypeMirror enclosingType = t.getEnclosingType();
// Call
args[0] = MethodHandleDesc.ofMethod(VIRTUAL,
ClassDesc.of(Domain.class.getName()),
"declaredType",
MethodTypeDesc.of(CD_DeclaredType,
CD_DeclaredType,
CD_TypeElement,
CD_TypeMirror.arrayType()));
args[1] = domainDesc;
args[2] = enclosingType.getKind() == TypeKind.NONE ? NULL : describe(enclosingType, d).orElse(null);
if (args[2] == null) {
yield Optional.empty();
}
args[3] = describe((TypeElement)t.asElement(), d).orElse(null);
if (args[3] == null) {
yield Optional.empty();
}
for (int i = 0; i < typeArgumentCount; i++) {
final int index = i + 3;
args[index] = describe(typeArguments.get(i), d).orElse(null);
if (args[index] == null) {
yield Optional.empty();
}
}
yield Optional.of(DynamicConstantDesc.of(BSM_INVOKE, args));
}
default -> Optional.empty(); // could be an error type
};
}
}
};
}
/**
* Returns a nominal descriptor for the supplied argument, presuming it to have originated from the supplied {@link
* Domain}, or an {@linkplain Optional#empty() empty} {@link Optional} if the supplied argument cannot be described.
*
* @param t the argument; may be {@code null}
*
* @param d the {@link Domain} from which the argument originated; must not be {@code null}
*
* @return a non-{@code null} {@link Optional}
*
* @exception NullPointerException if {@code d} is {@code null}
*/
public static final Optional<? extends ConstantDesc> describe(final NoType t, final Domain d) {
return switch (t) {
case null -> Optional.of(NULL);
case Constable c -> c.describeConstable();
case ConstantDesc cd -> Optional.of(cd); // future proofing?
default -> {
final ConstantDesc domainDesc = d instanceof Constable c ? c.describeConstable().orElse(null) : null;
if (domainDesc == null) {
yield Optional.empty();
}
try (var lock = d.lock()) {
yield t.getKind().describeConstable()
.map(typeKindDesc -> DynamicConstantDesc.of(BSM_INVOKE,
MethodHandleDesc.ofMethod(VIRTUAL,
ClassDesc.of(Domain.class.getName()),
"noType",
MethodTypeDesc.of(CD_NoType,
CD_TypeKind)),
domainDesc,
typeKindDesc));
}
}
};
}
/**
* Returns a nominal descriptor for the supplied argument, presuming it to have originated from the supplied {@link
* Domain}, or an {@linkplain Optional#empty() empty} {@link Optional} if the supplied argument cannot be described.
*
* @param t the argument; may be {@code null}
*
* @param d the {@link Domain} from which the argument originated; must not be {@code null}
*
* @return a non-{@code null} {@link Optional}
*
* @exception NullPointerException if {@code d} is {@code null}
*/
public static final Optional<? extends ConstantDesc> describe(final NullType t, final Domain d) {
return switch (t) {
case null -> Optional.of(NULL);
case Constable c -> c.describeConstable();
case ConstantDesc cd -> Optional.of(cd); // future proofing?
default -> (d instanceof Constable c ? c.describeConstable() : Optional.<ConstantDesc>empty())
.map(domainDesc -> DynamicConstantDesc.of(BSM_INVOKE,
MethodHandleDesc.ofMethod(VIRTUAL,
ClassDesc.of(Domain.class.getName()),
"nullType",
MethodTypeDesc.of(CD_NullType)),
domainDesc));
};
}
/**
* Returns a nominal descriptor for the supplied argument, presuming it to have originated from the supplied {@link
* Domain}, or an {@linkplain Optional#empty() empty} {@link Optional} if the supplied argument cannot be described.
*
* @param t the argument; may be {@code null}
*
* @param d the {@link Domain} from which the argument originated; must not be {@code null}
*
* @return a non-{@code null} {@link Optional}
*
* @exception NullPointerException if {@code d} is {@code null}
*/
public static final Optional<? extends ConstantDesc> describe(final PrimitiveType t, final Domain d) {
return switch (t) {
case null -> Optional.of(NULL);
case Constable c -> c.describeConstable();
case ConstantDesc cd -> Optional.of(cd); // future proofing?
default -> {
final ConstantDesc domainDesc = d instanceof Constable constableDomain ? constableDomain.describeConstable().orElse(null) : null;
if (domainDesc == null) {
yield Optional.empty();
}
try (var lock = d.lock()) {
yield t.getKind().describeConstable()
.map(typeKindDesc -> DynamicConstantDesc.of(BSM_INVOKE,
MethodHandleDesc.ofMethod(VIRTUAL,
ClassDesc.of(Domain.class.getName()),
"primitiveType",
MethodTypeDesc.of(CD_PrimitiveType,
CD_TypeKind)),
domainDesc,
typeKindDesc));
}
}
};
}
/**
* Returns a nominal descriptor for the supplied argument, presuming it to have originated from the supplied {@link
* Domain}, or an {@linkplain Optional#empty() empty} {@link Optional} if the supplied argument cannot be described.
*
* @param t the argument; may be {@code null}
*
* @param d the {@link Domain} from which the argument originated; must not be {@code null}
*
* @return a non-{@code null} {@link Optional}
*
* @exception NullPointerException if {@code d} is {@code null}
*/
public static final Optional<? extends ConstantDesc> describe(final TypeVariable t, final Domain d) {
return switch (t) {
case null -> Optional.of(NULL);
case Constable c -> c.describeConstable();
case ConstantDesc cd -> Optional.of(cd); // future proofing?
default -> {
final ConstantDesc domainDesc = d instanceof Constable constableDomain ? constableDomain.describeConstable().orElse(null) : null;
if (domainDesc == null) {
yield Optional.empty();
}
try (var lock = d.lock()) {
final TypeParameterElement e = (TypeParameterElement)t.asElement();
final ConstantDesc parameterizableDesc = describe(e.getEnclosingElement(), d).orElse(null);
if (parameterizableDesc == null) {
yield Optional.empty();
}
final String name = d.toString(e.getSimpleName());
yield Optional.of(DynamicConstantDesc.of(BSM_INVOKE,
MethodHandleDesc.ofMethod(VIRTUAL,
ClassDesc.of(Domain.class.getName()),
"typeVariable",
MethodTypeDesc.of(CD_TypeVariable,
CD_Parameterizable,
CD_CharSequence)),
domainDesc,
parameterizableDesc,
name));
}
}
};
}
/**
* Returns a nominal descriptor for the supplied argument, presuming it to have originated from the supplied {@link
* Domain}, or an {@linkplain Optional#empty() empty} {@link Optional} if the supplied argument cannot be described.
*
* @param t the argument; may be {@code null}
*
* @param d the {@link Domain} from which the argument originated; must not be {@code null}
*
* @return a non-{@code null} {@link Optional}
*
* @exception NullPointerException if {@code d} is {@code null}
*/
public static final Optional<? extends ConstantDesc> describe(final WildcardType t, final Domain d) {
return switch (t) {
case null -> Optional.of(NULL);
case Constable c -> c.describeConstable();
case ConstantDesc cd -> Optional.of(cd); // future proofing?
default -> {
try (var lock = d.lock()) {
yield describe(t.getExtendsBound(), d)
.flatMap(domainDesc -> describe(t.getExtendsBound(), d)
.flatMap(extendsBoundDesc -> describe(t.getSuperBound(), d)
.map(superBoundDesc -> DynamicConstantDesc.of(BSM_INVOKE,
MethodHandleDesc.ofMethod(VIRTUAL,
ClassDesc.of(Domain.class.getName()),
"wildcardType",
MethodTypeDesc.of(CD_WildcardType,
CD_TypeMirror,
CD_TypeMirror)),
domainDesc,
extendsBoundDesc,
superBoundDesc))));
}
}
};
}
}