Skip to content

Commit 02cb9c3

Browse files
authored
Fixes AnnotationMirrors#containsAll bug; cleans up some Constable stuff (#42)
Signed-off-by: Laird Nelson <ljnelson@gmail.com>
1 parent 936d949 commit 02cb9c3

12 files changed

+252
-113
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ dependency:
3636
Always check https://search.maven.org/artifact/org.microbean/microbean-construct
3737
for up-to-date available versions.
3838
-->
39-
<version>0.0.23</version>
39+
<version>0.0.24</version>
4040
</dependency>
4141
```
4242

src/main/java/org/microbean/construct/DefaultDomain.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,8 @@
1313
*/
1414
package org.microbean.construct;
1515

16-
import java.lang.constant.ClassDesc;
1716
import java.lang.constant.Constable;
18-
import java.lang.constant.ConstantDesc;
1917
import java.lang.constant.DynamicConstantDesc;
20-
import java.lang.constant.MethodHandleDesc;
2118

2219
import java.util.List;
2320
import java.util.Objects;
@@ -54,6 +51,8 @@
5451

5552
import static java.lang.constant.ConstantDescs.BSM_INVOKE;
5653

54+
import static java.lang.constant.MethodHandleDesc.ofConstructor;
55+
5756
/**
5857
* A {@linkplain Domain domain of valid Java constructs} that can be used at annotation processing time or at runtime.
5958
*
@@ -268,10 +267,12 @@ public UniversalType declaredType(DeclaredType enclosingType,
268267
}
269268

270269
@Override // Constable
271-
public Optional<? extends ConstantDesc> describeConstable() {
270+
public Optional<DynamicConstantDesc<DefaultDomain>> describeConstable() {
272271
return
273-
Optional.of(DynamicConstantDesc.of(BSM_INVOKE,
274-
MethodHandleDesc.ofConstructor(ClassDesc.of(this.getClass().getName()))));
272+
Optional.of(DynamicConstantDesc.ofNamed(BSM_INVOKE,
273+
this.getClass().getSimpleName(),
274+
this.getClass().describeConstable().orElseThrow(),
275+
ofConstructor(this.getClass().describeConstable().orElseThrow())));
275276
}
276277

277278
@Override // Domain

src/main/java/org/microbean/construct/UniversalConstruct.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -218,21 +218,23 @@ public final T delegate() {
218218
}
219219

220220
@Override // Constable
221-
public final Optional<? extends ConstantDesc> describeConstable() {
221+
public final Optional<DynamicConstantDesc<T>> describeConstable() {
222222
final PrimordialDomain primordialDomain = this.domain();
223223
if (domain instanceof Domain d && d instanceof Constable dc) {
224224
final T delegate = this.delegate();
225225
final List<AnnotationMirror> annotations = this.annotations; // volatile read; may be null and that's OK
226226
return Constables.describe(delegate, d)
227227
.flatMap(delegateDesc -> Constables.describe(annotations)
228-
.map(annosDesc -> DynamicConstantDesc.of(BSM_INVOKE,
229-
ofConstructor(ClassDesc.of(this.getClass().getName()),
230-
CD_List,
231-
ClassDesc.of(delegate instanceof TypeMirror ? TypeMirror.class.getName() : Element.class.getName()),
232-
ClassDesc.of(PrimordialDomain.class.getName())),
233-
annosDesc,
234-
delegateDesc,
235-
dc.describeConstable().orElseThrow())));
228+
.map(annosDesc -> DynamicConstantDesc.ofNamed(BSM_INVOKE,
229+
this.getClass().getSimpleName(),
230+
this.getClass().describeConstable().orElseThrow(),
231+
ofConstructor(this.getClass().describeConstable().orElseThrow(),
232+
CD_List,
233+
(delegate instanceof TypeMirror ? TypeMirror.class : Element.class).describeConstable().orElseThrow(),
234+
PrimordialDomain.class.describeConstable().orElseThrow()),
235+
annosDesc,
236+
delegateDesc,
237+
dc.describeConstable().orElseThrow())));
236238
}
237239
return Optional.empty();
238240
}

src/main/java/org/microbean/construct/constant/Constables.java

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* -*- mode: Java; c-basic-offset: 2; indent-tabs-mode: nil; coding: utf-8-unix -*-
22
*
3-
* Copyright © 2024 microBean™.
3+
* Copyright © 2024–2026 microBean™.
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
66
* the License. You may obtain a copy of the License at
@@ -81,6 +81,7 @@
8181
import static org.microbean.construct.constant.ConstantDescs.CD_TypeParameterElement;
8282
import static org.microbean.construct.constant.ConstantDescs.CD_TypeMirror;
8383
import static org.microbean.construct.constant.ConstantDescs.CD_TypeVariable;
84+
import static org.microbean.construct.constant.ConstantDescs.CD_VariableElement;
8485
import static org.microbean.construct.constant.ConstantDescs.CD_WildcardType;
8586

8687
/**
@@ -119,7 +120,7 @@ public static final Optional<? extends ConstantDesc> describe(final Name n, fina
119120
default -> (d instanceof Constable c ? c.describeConstable() : Optional.<ConstantDesc>empty())
120121
.map(domainDesc -> DynamicConstantDesc.of(BSM_INVOKE,
121122
ofMethod(VIRTUAL,
122-
ClassDesc.of(Domain.class.getName()),
123+
Domain.class.describeConstable().orElseThrow(),
123124
"name",
124125
MethodTypeDesc.of(CD_Name,
125126
CD_CharSequence)),
@@ -218,7 +219,7 @@ public static final Optional<? extends ConstantDesc> describe(final ExecutableEl
218219
final ConstantDesc[] args = new ConstantDesc[5 + parameterCount];
219220
args[0] =
220221
ofMethod(VIRTUAL,
221-
ClassDesc.of(Domain.class.getName()),
222+
Domain.class.describeConstable().orElseThrow(),
222223
"executableElement",
223224
MethodTypeDesc.of(CD_ExecutableElement,
224225
CD_TypeElement,
@@ -271,7 +272,7 @@ public static final Optional<? extends ConstantDesc> describe(final ModuleElemen
271272
default -> describe(e.getQualifiedName(), d) // getQualifiedName() does not cause symbol completion
272273
.map(nameDesc -> DynamicConstantDesc.of(BSM_INVOKE,
273274
ofMethod(VIRTUAL,
274-
ClassDesc.of(Domain.class.getName()),
275+
Domain.class.describeConstable().orElseThrow(),
275276
"moduleElement",
276277
MethodTypeDesc.of(CD_ModuleElement,
277278
CD_CharSequence)),
@@ -300,7 +301,7 @@ public static final Optional<? extends ConstantDesc> describe(final PackageEleme
300301
default -> describe(e.getQualifiedName(), d) // getQualifiedName() does not cause symbol completion
301302
.map(nameDesc -> DynamicConstantDesc.of(BSM_INVOKE,
302303
ofMethod(VIRTUAL,
303-
ClassDesc.of(Domain.class.getName()),
304+
Domain.class.describeConstable().orElseThrow(),
304305
"packageElement",
305306
MethodTypeDesc.of(CD_PackageElement,
306307
CD_CharSequence)),
@@ -329,7 +330,7 @@ public static final Optional<? extends ConstantDesc> describe(final TypeElement
329330
default -> describe(e.getQualifiedName(), d) // getQualifiedName() does not cause symbol completion
330331
.map(nameDesc -> DynamicConstantDesc.of(BSM_INVOKE,
331332
ofMethod(VIRTUAL,
332-
ClassDesc.of(Domain.class.getName()),
333+
Domain.class.describeConstable().orElseThrow(),
333334
"typeElement",
334335
MethodTypeDesc.of(CD_TypeElement,
335336
CD_CharSequence)),
@@ -361,7 +362,7 @@ yield describe(e.getEnclosingElement(), d)
361362
.flatMap(parameterizableDesc -> describe(e.getSimpleName(), d)
362363
.map(nameDesc -> DynamicConstantDesc.of(BSM_INVOKE,
363364
ofMethod(VIRTUAL,
364-
ClassDesc.of(Domain.class.getName()),
365+
Domain.class.describeConstable().orElseThrow(),
365366
"typeParameterElement",
366367
MethodTypeDesc.of(CD_TypeParameterElement,
367368
CD_Parameterizable,
@@ -396,7 +397,7 @@ public static final Optional<? extends ConstantDesc> describe(final RecordCompon
396397
yield describe((TypeElement)e.getEnclosingElement(), d)
397398
.map(executableDesc -> DynamicConstantDesc.of(BSM_INVOKE,
398399
ofMethod(VIRTUAL,
399-
ClassDesc.of(Domain.class.getName()),
400+
Domain.class.describeConstable().orElseThrow(),
400401
"recordComponentElement",
401402
MethodTypeDesc.of(CD_RecordComponentElement,
402403
CD_ExecutableElement)),
@@ -430,11 +431,13 @@ yield describe(e.getSimpleName(), d)
430431
.flatMap(nameDesc -> describe(e.getEnclosingElement(), d)
431432
.map(enclosingElementDesc -> DynamicConstantDesc.of(BSM_INVOKE,
432433
ofMethod(VIRTUAL,
433-
ClassDesc.of(Domain.class.getName()),
434+
Domain.class.describeConstable().orElseThrow(),
434435
"variableElement",
435-
MethodTypeDesc.of(CD_Element,
436+
MethodTypeDesc.of(CD_VariableElement,
437+
CD_Element,
436438
CD_CharSequence)),
437439
((Constable)d).describeConstable().orElseThrow(),
440+
enclosingElementDesc,
438441
nameDesc)));
439442
}
440443
}
@@ -498,7 +501,7 @@ public static final Optional<? extends ConstantDesc> describe(final ArrayType t,
498501
yield describe(t.getComponentType(), d)
499502
.map(componentTypeDesc -> DynamicConstantDesc.of(BSM_INVOKE,
500503
ofMethod(VIRTUAL,
501-
ClassDesc.of(Domain.class.getName()),
504+
Domain.class.describeConstable().orElseThrow(),
502505
"arrayTypeOf",
503506
MethodTypeDesc.of(CD_ArrayType,
504507
CD_TypeMirror)),
@@ -540,7 +543,7 @@ yield switch (t.getKind()) {
540543
final TypeMirror enclosingType = t.getEnclosingType();
541544
// Call
542545
args[0] = ofMethod(VIRTUAL,
543-
ClassDesc.of(Domain.class.getName()),
546+
Domain.class.describeConstable().orElseThrow(),
544547
"declaredType",
545548
MethodTypeDesc.of(CD_DeclaredType,
546549
CD_DeclaredType,
@@ -597,7 +600,7 @@ public static final Optional<? extends ConstantDesc> describe(final NoType t, fi
597600
yield t.getKind().describeConstable()
598601
.map(typeKindDesc -> DynamicConstantDesc.of(BSM_INVOKE,
599602
ofMethod(VIRTUAL,
600-
ClassDesc.of(Domain.class.getName()),
603+
Domain.class.describeConstable().orElseThrow(),
601604
"noType",
602605
MethodTypeDesc.of(CD_NoType,
603606
CD_TypeKind)),
@@ -628,7 +631,7 @@ public static final Optional<? extends ConstantDesc> describe(final NullType t,
628631
default -> (d instanceof Constable c ? c.describeConstable() : Optional.<ConstantDesc>empty())
629632
.map(domainDesc -> DynamicConstantDesc.of(BSM_INVOKE,
630633
ofMethod(VIRTUAL,
631-
ClassDesc.of(Domain.class.getName()),
634+
Domain.class.describeConstable().orElseThrow(),
632635
"nullType",
633636
MethodTypeDesc.of(CD_NullType)),
634637
domainDesc));
@@ -661,7 +664,7 @@ public static final Optional<? extends ConstantDesc> describe(final PrimitiveTyp
661664
yield t.getKind().describeConstable()
662665
.map(typeKindDesc -> DynamicConstantDesc.of(BSM_INVOKE,
663666
ofMethod(VIRTUAL,
664-
ClassDesc.of(Domain.class.getName()),
667+
Domain.class.describeConstable().orElseThrow(),
665668
"primitiveType",
666669
MethodTypeDesc.of(CD_PrimitiveType,
667670
CD_TypeKind)),
@@ -703,7 +706,7 @@ public static final Optional<? extends ConstantDesc> describe(final TypeVariable
703706
final String name = d.toString(e.getSimpleName());
704707
yield Optional.of(DynamicConstantDesc.of(BSM_INVOKE,
705708
ofMethod(VIRTUAL,
706-
ClassDesc.of(Domain.class.getName()),
709+
Domain.class.describeConstable().orElseThrow(),
707710
"typeVariable",
708711
MethodTypeDesc.of(CD_TypeVariable,
709712
CD_Parameterizable,
@@ -740,7 +743,7 @@ yield describe(t.getExtendsBound(), d)
740743
.flatMap(extendsBoundDesc -> describe(t.getSuperBound(), d)
741744
.map(superBoundDesc -> DynamicConstantDesc.of(BSM_INVOKE,
742745
ofMethod(VIRTUAL,
743-
ClassDesc.of(Domain.class.getName()),
746+
Domain.class.describeConstable().orElseThrow(),
744747
"wildcardType",
745748
MethodTypeDesc.of(CD_WildcardType,
746749
CD_TypeMirror,
@@ -754,10 +757,8 @@ yield describe(t.getExtendsBound(), d)
754757
}
755758

756759
/**
757-
* Returns a nominal descriptor for the supplied {@link List}, or an {@linkplain Optional#empty() empty} {@link
758-
* Optional} if the supplied {@link List} cannot be described.
759-
*
760-
* @param <E> the supplied {@code list}'s element type
760+
* Returns a non-{@code null} nominal descriptor for the supplied {@link List}, or an {@linkplain Optional#empty()
761+
* empty} {@link Optional} if the supplied {@link List} cannot be described.
761762
*
762763
* @param list a {@link List} to be described; may be {@code null}; if non-{@code null} <strong>must be immutable and
763764
* must not contain {@code null} elements</strong> or undefined behavior will result
@@ -766,10 +767,10 @@ yield describe(t.getExtendsBound(), d)
766767
*
767768
* @see #describe(List, Function)
768769
*/
769-
public static final <E> Optional<? extends ConstantDesc> describe(final List<? extends E> list) {
770+
public static final Optional<? extends ConstantDesc> describe(final List<?> list) {
770771
return describe(list, e -> e instanceof Constable c ? c.describeConstable() : Optional.empty());
771772
}
772-
773+
773774
/**
774775
* Returns a nominal descriptor for the supplied {@link List}, or an {@linkplain Optional#empty() empty} {@link
775776
* Optional} if the supplied {@link List} cannot be described.
@@ -814,14 +815,14 @@ public static final <E> Optional<? extends ConstantDesc> describe(final List<? e
814815
// After 10 parameters, List.of() falls back to varargs.
815816
ofMethodTypeDesc = MethodTypeDesc.of(CD_List, CD_Object.arrayType());
816817
}
817-
final ConstantDesc[] args = new ConstantDesc[++size];
818+
final ConstantDesc[] args = new ConstantDesc[size + 1];
818819
args[0] = ofMethod(INTERFACE_STATIC, CD_List, "of", ofMethodTypeDesc);
819-
for (int i = 1; i < size; i++) {
820+
for (int i = 0; i < size; i++) {
820821
final ConstantDesc eDesc = f.apply(list.get(i)).orElse(null);
821822
if (eDesc == null) {
822823
yield Optional.empty();
823824
}
824-
args[i] = eDesc;
825+
args[i + 1] = eDesc;
825826
}
826827
yield Optional.of(DynamicConstantDesc.of(BSM_INVOKE, args));
827828
}
@@ -879,7 +880,7 @@ public static <K, V> Optional<? extends ConstantDesc> describe(final Map<? exten
879880
// After 20 parameters, Map.of() falls back to varargs.
880881
ofMethodTypeDesc = MethodTypeDesc.of(CD_Map, CD_Object.arrayType());
881882
}
882-
final ConstantDesc[] args = new ConstantDesc[++size];
883+
final ConstantDesc[] args = new ConstantDesc[size + 1];
883884
args[0] = ofMethod(INTERFACE_STATIC, CD_Map, "of", ofMethodTypeDesc);
884885
int i = 1;
885886
for (final Entry<? extends K, ? extends V> e : map.entrySet()) {
@@ -891,9 +892,8 @@ public static <K, V> Optional<? extends ConstantDesc> describe(final Map<? exten
891892
if (vDesc == null) {
892893
yield Optional.empty();
893894
}
894-
args[i] = kDesc;
895-
args[++i] = vDesc;
896-
++i;
895+
args[i++] = kDesc;
896+
args[i++] = vDesc;
897897
}
898898
yield Optional.of(DynamicConstantDesc.of(BSM_INVOKE, args));
899899
}

0 commit comments

Comments
 (0)