Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ dependency:
Always check https://search.maven.org/artifact/org.microbean/microbean-construct
for up-to-date available versions.
-->
<version>0.0.23</version>
<version>0.0.24</version>
</dependency>
```

Expand Down
13 changes: 7 additions & 6 deletions src/main/java/org/microbean/construct/DefaultDomain.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@
*/
package org.microbean.construct;

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.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -54,6 +51,8 @@

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

import static java.lang.constant.MethodHandleDesc.ofConstructor;

/**
* A {@linkplain Domain domain of valid Java constructs} that can be used at annotation processing time or at runtime.
*
Expand Down Expand Up @@ -268,10 +267,12 @@ public UniversalType declaredType(DeclaredType enclosingType,
}

@Override // Constable
public Optional<? extends ConstantDesc> describeConstable() {
public Optional<DynamicConstantDesc<DefaultDomain>> describeConstable() {
return
Optional.of(DynamicConstantDesc.of(BSM_INVOKE,
MethodHandleDesc.ofConstructor(ClassDesc.of(this.getClass().getName()))));
Optional.of(DynamicConstantDesc.ofNamed(BSM_INVOKE,
this.getClass().getSimpleName(),
this.getClass().describeConstable().orElseThrow(),
ofConstructor(this.getClass().describeConstable().orElseThrow())));
}

@Override // Domain
Expand Down
20 changes: 11 additions & 9 deletions src/main/java/org/microbean/construct/UniversalConstruct.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,21 +218,23 @@ public final T delegate() {
}

@Override // Constable
public final Optional<? extends ConstantDesc> describeConstable() {
public final Optional<DynamicConstantDesc<T>> describeConstable() {
final PrimordialDomain primordialDomain = this.domain();
if (domain instanceof Domain d && d instanceof Constable dc) {
final T delegate = this.delegate();
final List<AnnotationMirror> annotations = this.annotations; // volatile read; may be null and that's OK
return Constables.describe(delegate, d)
.flatMap(delegateDesc -> Constables.describe(annotations)
.map(annosDesc -> DynamicConstantDesc.of(BSM_INVOKE,
ofConstructor(ClassDesc.of(this.getClass().getName()),
CD_List,
ClassDesc.of(delegate instanceof TypeMirror ? TypeMirror.class.getName() : Element.class.getName()),
ClassDesc.of(PrimordialDomain.class.getName())),
annosDesc,
delegateDesc,
dc.describeConstable().orElseThrow())));
.map(annosDesc -> DynamicConstantDesc.ofNamed(BSM_INVOKE,
this.getClass().getSimpleName(),
this.getClass().describeConstable().orElseThrow(),
ofConstructor(this.getClass().describeConstable().orElseThrow(),
CD_List,
(delegate instanceof TypeMirror ? TypeMirror.class : Element.class).describeConstable().orElseThrow(),
PrimordialDomain.class.describeConstable().orElseThrow()),
annosDesc,
delegateDesc,
dc.describeConstable().orElseThrow())));
}
return Optional.empty();
}
Expand Down
60 changes: 30 additions & 30 deletions src/main/java/org/microbean/construct/constant/Constables.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* -*- mode: Java; c-basic-offset: 2; indent-tabs-mode: nil; coding: utf-8-unix -*-
*
* Copyright © 2024 microBean™.
* 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
Expand Down Expand Up @@ -81,6 +81,7 @@
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_VariableElement;
import static org.microbean.construct.constant.ConstantDescs.CD_WildcardType;

/**
Expand Down Expand Up @@ -119,7 +120,7 @@ public static final Optional<? extends ConstantDesc> describe(final Name n, fina
default -> (d instanceof Constable c ? c.describeConstable() : Optional.<ConstantDesc>empty())
.map(domainDesc -> DynamicConstantDesc.of(BSM_INVOKE,
ofMethod(VIRTUAL,
ClassDesc.of(Domain.class.getName()),
Domain.class.describeConstable().orElseThrow(),
"name",
MethodTypeDesc.of(CD_Name,
CD_CharSequence)),
Expand Down Expand Up @@ -218,7 +219,7 @@ public static final Optional<? extends ConstantDesc> describe(final ExecutableEl
final ConstantDesc[] args = new ConstantDesc[5 + parameterCount];
args[0] =
ofMethod(VIRTUAL,
ClassDesc.of(Domain.class.getName()),
Domain.class.describeConstable().orElseThrow(),
"executableElement",
MethodTypeDesc.of(CD_ExecutableElement,
CD_TypeElement,
Expand Down Expand Up @@ -271,7 +272,7 @@ public static final Optional<? extends ConstantDesc> describe(final ModuleElemen
default -> describe(e.getQualifiedName(), d) // getQualifiedName() does not cause symbol completion
.map(nameDesc -> DynamicConstantDesc.of(BSM_INVOKE,
ofMethod(VIRTUAL,
ClassDesc.of(Domain.class.getName()),
Domain.class.describeConstable().orElseThrow(),
"moduleElement",
MethodTypeDesc.of(CD_ModuleElement,
CD_CharSequence)),
Expand Down Expand Up @@ -300,7 +301,7 @@ public static final Optional<? extends ConstantDesc> describe(final PackageEleme
default -> describe(e.getQualifiedName(), d) // getQualifiedName() does not cause symbol completion
.map(nameDesc -> DynamicConstantDesc.of(BSM_INVOKE,
ofMethod(VIRTUAL,
ClassDesc.of(Domain.class.getName()),
Domain.class.describeConstable().orElseThrow(),
"packageElement",
MethodTypeDesc.of(CD_PackageElement,
CD_CharSequence)),
Expand Down Expand Up @@ -329,7 +330,7 @@ public static final Optional<? extends ConstantDesc> describe(final TypeElement
default -> describe(e.getQualifiedName(), d) // getQualifiedName() does not cause symbol completion
.map(nameDesc -> DynamicConstantDesc.of(BSM_INVOKE,
ofMethod(VIRTUAL,
ClassDesc.of(Domain.class.getName()),
Domain.class.describeConstable().orElseThrow(),
"typeElement",
MethodTypeDesc.of(CD_TypeElement,
CD_CharSequence)),
Expand Down Expand Up @@ -361,7 +362,7 @@ yield describe(e.getEnclosingElement(), d)
.flatMap(parameterizableDesc -> describe(e.getSimpleName(), d)
.map(nameDesc -> DynamicConstantDesc.of(BSM_INVOKE,
ofMethod(VIRTUAL,
ClassDesc.of(Domain.class.getName()),
Domain.class.describeConstable().orElseThrow(),
"typeParameterElement",
MethodTypeDesc.of(CD_TypeParameterElement,
CD_Parameterizable,
Expand Down Expand Up @@ -396,7 +397,7 @@ public static final Optional<? extends ConstantDesc> describe(final RecordCompon
yield describe((TypeElement)e.getEnclosingElement(), d)
.map(executableDesc -> DynamicConstantDesc.of(BSM_INVOKE,
ofMethod(VIRTUAL,
ClassDesc.of(Domain.class.getName()),
Domain.class.describeConstable().orElseThrow(),
"recordComponentElement",
MethodTypeDesc.of(CD_RecordComponentElement,
CD_ExecutableElement)),
Expand Down Expand Up @@ -430,11 +431,13 @@ yield describe(e.getSimpleName(), d)
.flatMap(nameDesc -> describe(e.getEnclosingElement(), d)
.map(enclosingElementDesc -> DynamicConstantDesc.of(BSM_INVOKE,
ofMethod(VIRTUAL,
ClassDesc.of(Domain.class.getName()),
Domain.class.describeConstable().orElseThrow(),
"variableElement",
MethodTypeDesc.of(CD_Element,
MethodTypeDesc.of(CD_VariableElement,
CD_Element,
CD_CharSequence)),
((Constable)d).describeConstable().orElseThrow(),
enclosingElementDesc,
nameDesc)));
}
}
Expand Down Expand Up @@ -498,7 +501,7 @@ public static final Optional<? extends ConstantDesc> describe(final ArrayType t,
yield describe(t.getComponentType(), d)
.map(componentTypeDesc -> DynamicConstantDesc.of(BSM_INVOKE,
ofMethod(VIRTUAL,
ClassDesc.of(Domain.class.getName()),
Domain.class.describeConstable().orElseThrow(),
"arrayTypeOf",
MethodTypeDesc.of(CD_ArrayType,
CD_TypeMirror)),
Expand Down Expand Up @@ -540,7 +543,7 @@ yield switch (t.getKind()) {
final TypeMirror enclosingType = t.getEnclosingType();
// Call
args[0] = ofMethod(VIRTUAL,
ClassDesc.of(Domain.class.getName()),
Domain.class.describeConstable().orElseThrow(),
"declaredType",
MethodTypeDesc.of(CD_DeclaredType,
CD_DeclaredType,
Expand Down Expand Up @@ -597,7 +600,7 @@ public static final Optional<? extends ConstantDesc> describe(final NoType t, fi
yield t.getKind().describeConstable()
.map(typeKindDesc -> DynamicConstantDesc.of(BSM_INVOKE,
ofMethod(VIRTUAL,
ClassDesc.of(Domain.class.getName()),
Domain.class.describeConstable().orElseThrow(),
"noType",
MethodTypeDesc.of(CD_NoType,
CD_TypeKind)),
Expand Down Expand Up @@ -628,7 +631,7 @@ public static final Optional<? extends ConstantDesc> describe(final NullType t,
default -> (d instanceof Constable c ? c.describeConstable() : Optional.<ConstantDesc>empty())
.map(domainDesc -> DynamicConstantDesc.of(BSM_INVOKE,
ofMethod(VIRTUAL,
ClassDesc.of(Domain.class.getName()),
Domain.class.describeConstable().orElseThrow(),
"nullType",
MethodTypeDesc.of(CD_NullType)),
domainDesc));
Expand Down Expand Up @@ -661,7 +664,7 @@ public static final Optional<? extends ConstantDesc> describe(final PrimitiveTyp
yield t.getKind().describeConstable()
.map(typeKindDesc -> DynamicConstantDesc.of(BSM_INVOKE,
ofMethod(VIRTUAL,
ClassDesc.of(Domain.class.getName()),
Domain.class.describeConstable().orElseThrow(),
"primitiveType",
MethodTypeDesc.of(CD_PrimitiveType,
CD_TypeKind)),
Expand Down Expand Up @@ -703,7 +706,7 @@ public static final Optional<? extends ConstantDesc> describe(final TypeVariable
final String name = d.toString(e.getSimpleName());
yield Optional.of(DynamicConstantDesc.of(BSM_INVOKE,
ofMethod(VIRTUAL,
ClassDesc.of(Domain.class.getName()),
Domain.class.describeConstable().orElseThrow(),
"typeVariable",
MethodTypeDesc.of(CD_TypeVariable,
CD_Parameterizable,
Expand Down Expand Up @@ -740,7 +743,7 @@ yield describe(t.getExtendsBound(), d)
.flatMap(extendsBoundDesc -> describe(t.getSuperBound(), d)
.map(superBoundDesc -> DynamicConstantDesc.of(BSM_INVOKE,
ofMethod(VIRTUAL,
ClassDesc.of(Domain.class.getName()),
Domain.class.describeConstable().orElseThrow(),
"wildcardType",
MethodTypeDesc.of(CD_WildcardType,
CD_TypeMirror,
Expand All @@ -754,10 +757,8 @@ yield describe(t.getExtendsBound(), d)
}

/**
* Returns a nominal descriptor for the supplied {@link List}, or an {@linkplain Optional#empty() empty} {@link
* Optional} if the supplied {@link List} cannot be described.
*
* @param <E> the supplied {@code list}'s element type
* Returns a non-{@code null} nominal descriptor for the supplied {@link List}, or an {@linkplain Optional#empty()
* empty} {@link Optional} if the supplied {@link List} cannot be described.
*
* @param list a {@link List} to be described; may be {@code null}; if non-{@code null} <strong>must be immutable and
* must not contain {@code null} elements</strong> or undefined behavior will result
Expand All @@ -766,10 +767,10 @@ yield describe(t.getExtendsBound(), d)
*
* @see #describe(List, Function)
*/
public static final <E> Optional<? extends ConstantDesc> describe(final List<? extends E> list) {
public static final Optional<? extends ConstantDesc> describe(final List<?> list) {
return describe(list, e -> e instanceof Constable c ? c.describeConstable() : Optional.empty());
}

/**
* Returns a nominal descriptor for the supplied {@link List}, or an {@linkplain Optional#empty() empty} {@link
* Optional} if the supplied {@link List} cannot be described.
Expand Down Expand Up @@ -814,14 +815,14 @@ public static final <E> Optional<? extends ConstantDesc> describe(final List<? e
// After 10 parameters, List.of() falls back to varargs.
ofMethodTypeDesc = MethodTypeDesc.of(CD_List, CD_Object.arrayType());
}
final ConstantDesc[] args = new ConstantDesc[++size];
final ConstantDesc[] args = new ConstantDesc[size + 1];
args[0] = ofMethod(INTERFACE_STATIC, CD_List, "of", ofMethodTypeDesc);
for (int i = 1; i < size; i++) {
for (int i = 0; i < size; i++) {
final ConstantDesc eDesc = f.apply(list.get(i)).orElse(null);
if (eDesc == null) {
yield Optional.empty();
}
args[i] = eDesc;
args[i + 1] = eDesc;
}
yield Optional.of(DynamicConstantDesc.of(BSM_INVOKE, args));
}
Expand Down Expand Up @@ -879,7 +880,7 @@ public static <K, V> Optional<? extends ConstantDesc> describe(final Map<? exten
// After 20 parameters, Map.of() falls back to varargs.
ofMethodTypeDesc = MethodTypeDesc.of(CD_Map, CD_Object.arrayType());
}
final ConstantDesc[] args = new ConstantDesc[++size];
final ConstantDesc[] args = new ConstantDesc[size + 1];
args[0] = ofMethod(INTERFACE_STATIC, CD_Map, "of", ofMethodTypeDesc);
int i = 1;
for (final Entry<? extends K, ? extends V> e : map.entrySet()) {
Expand All @@ -891,9 +892,8 @@ public static <K, V> Optional<? extends ConstantDesc> describe(final Map<? exten
if (vDesc == null) {
yield Optional.empty();
}
args[i] = kDesc;
args[++i] = vDesc;
++i;
args[i++] = kDesc;
args[i++] = vDesc;
}
yield Optional.of(DynamicConstantDesc.of(BSM_INVOKE, args));
}
Expand Down
Loading