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.21</version>
<version>0.0.22</version>
</dependency>
```

Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.14.1</version>
<version>3.15.0</version>
<configuration>
<compilerArgs>
<arg>-Xlint:all</arg>
Expand Down Expand Up @@ -369,7 +369,7 @@
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.20.1</version>
<version>2.21.0</version>
</plugin>
<plugin>
<groupId>io.smallrye</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
import java.lang.constant.ConstantDesc;
import java.lang.constant.DynamicConstantDesc;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;

import javax.lang.model.element.AnnotationMirror;
Expand Down Expand Up @@ -69,7 +71,7 @@ public final class SyntheticAnnotationMirror implements AnnotationMirror, Consta

private final TypeElement annotationTypeElement;

private final Map<ExecutableElement, AnnotationValue> elementValues;
private final Map<? extends ExecutableElement, ? extends AnnotationValue> elementValues;


/*
Expand Down Expand Up @@ -142,6 +144,39 @@ public SyntheticAnnotationMirror(final TypeElement annotationTypeElement,
this.elementValues = m.isEmpty() ? Map.of() : unmodifiableMap(m);
}

/**
* Creates a new {@link SyntheticAnnotationMirror} that is an effective <dfn>copy</dfn> of the supplied {@link
* AnnotationMirror}.
*
* @param a a non-{@code null} {@link AnnotationMirror} to semantically copy
*
* @exception NullPointerException if {@code a} is {@code null}
*/
public SyntheticAnnotationMirror(final AnnotationMirror a) {
super();
this.annotationTypeElement = new SyntheticAnnotationTypeElement((TypeElement)a.getAnnotationType().asElement());
final Map<? extends ExecutableElement, ? extends AnnotationValue> originalElementValues = a.getElementValues();
if (originalElementValues.isEmpty()) {
// If there are no explicit values, then...there are no explicit values whether the annotation interface type
// contains/encloses any elements at all.
this.elementValues = Map.of();
} else {
// There are explicit values. That also means that the annotation interface type contains/encloses at least one
// element.
final List<ExecutableElement> syntheticElements = methodsIn(this.annotationTypeElement.getEnclosedElements());
assert !syntheticElements.isEmpty();
final Map<ExecutableElement, AnnotationValue> newElementValues = newLinkedHashMap(originalElementValues.size());
for (final Entry<? extends ExecutableElement, ? extends AnnotationValue> originalEntry : originalElementValues.entrySet()) {
final ExecutableElement originalElement = originalEntry.getKey();
final ExecutableElement syntheticElement = element(syntheticElements, originalElement.getSimpleName());
if (syntheticElement != null) {
newElementValues.put(syntheticElement, originalEntry.getValue());
}
}
this.elementValues = unmodifiableMap(newElementValues);
}
}


/*
* Instance methods.
Expand Down Expand Up @@ -172,6 +207,11 @@ public final DeclaredType getAnnotationType() {
return this.elementValues;
}

@Override
public final String toString() {
return "@" + this.annotationTypeElement.toString(); // TODO: not anywhere near good enough
}


/*
* Static methods.
Expand All @@ -197,4 +237,13 @@ private static final Optional<? extends ConstantDesc> describeExecutableElement(
};
}

private static final <E extends Element> E element(final Iterable<? extends E> elements, final CharSequence simpleName) {
for (final E e : elements) {
if (e.getSimpleName().contentEquals(simpleName)) {
return e;
}
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
import static javax.lang.model.type.TypeKind.ARRAY;
import static javax.lang.model.type.TypeKind.DECLARED;

import static javax.lang.model.util.ElementFilter.methodsIn;

import static org.microbean.construct.element.AnnotationMirrors.validAnnotationInterfaceElementScalarType;

/**
Expand Down Expand Up @@ -285,6 +287,46 @@ public SyntheticAnnotationTypeElement(final List<? extends AnnotationMirror> ann
this(annotationMirrors, new SyntheticName(fullyQualifiedName), elements);
}

/**
* Creates a new {@link SyntheticAnnotationTypeElement}, mostly, if not exclusively, for use by {@link
* SyntheticAnnotationMirror} instances.
*
* @param e a non-{@code null} {@link TypeElement} whose {@link TypeElement#getKind() getKind()} method returns {@link
* javax.lang.model.element.ElementKind#ANNOTATION_TYPE}
*
* @exception NullPointerException if {@code e} is {@code null}
*
* @exception IllegalArgumentException if the supplied {@link TypeElement}'s {@link TypeElement#getKind() getKind()}
* method does not return {@link javax.lang.model.element.ElementKind#ANNOTATION_TYPE}
*/
public SyntheticAnnotationTypeElement(final TypeElement e) {
super();
if (e.getKind() != ANNOTATION_TYPE) {
throw new IllegalArgumentException("e: " + e);
}
this.annotationMirrors = new CopyOnWriteArrayList<>(e.getAnnotationMirrors());
Name n = e.getSimpleName();
this.sn = n instanceof SyntheticName sn ? sn : new SyntheticName(n.toString());
n = e.getQualifiedName();
this.fqn = n instanceof SyntheticName sn ? sn : new SyntheticName(n.toString());
// Deliberate: Type is an inner class and hence cannot be shared
this.type = new Type();
final List<ExecutableElement> elements = methodsIn(e.getEnclosedElements());
if (elements.isEmpty()) {
this.elements = List.of();
} else {
final List<InternalAnnotationElement> elements0 = new ArrayList<>(elements.size());
for (final ExecutableElement ee :elements) {
// Deliberate: no instanceof InternalAnnotationElement check, i.e. wrapping/copying is deliberate and necessary
elements0.add(new InternalAnnotationElement(ee.getAnnotationMirrors(),
ee.getReturnType(),
ee.getSimpleName(),
ee.getDefaultValue()));
}
this.elements = unmodifiableList(elements0);
}
}

/**
* Creates a new {@link SyntheticAnnotationTypeElement}, mostly, if not exclusively, for use by {@link
* SyntheticAnnotationMirror} instances.
Expand Down Expand Up @@ -320,8 +362,8 @@ public SyntheticAnnotationTypeElement(final List<? extends AnnotationMirror> ann
this.elements = List.of();
} else {
final List<InternalAnnotationElement> elements0 = new ArrayList<>(elements.size());
for (final SyntheticAnnotationElement e : elements) {
elements0.add(new InternalAnnotationElement(e.annotationMirrors(), e.type(), e.name(), e.defaultValue()));
for (final SyntheticAnnotationElement sae : elements) {
elements0.add(new InternalAnnotationElement(sae.annotationMirrors(), sae.type(), sae.name(), sae.defaultValue()));
}
this.elements = unmodifiableList(elements0);
}
Expand Down Expand Up @@ -572,7 +614,7 @@ public SyntheticAnnotationElement(final List<? extends AnnotationMirror> annotat
case ArrayType t when t.getKind() == ARRAY -> validateScalarType(t.getComponentType());
default -> validateScalarType(type);
};
if (name.equals("getClass") || name.equals("hashCode") || name.equals("toString")) {
if (name.contentEquals("getClass") || name.contentEquals("hashCode") || name.contentEquals("toString")) {
// java.lang.Object-declared methods that might otherwise meet annotation element requirements
throw new IllegalArgumentException("name: " + name);
}
Expand Down Expand Up @@ -728,13 +770,15 @@ private final class InternalAnnotationElement implements ExecutableElement {

private InternalAnnotationElement(final List<? extends AnnotationMirror> annotationMirrors,
final TypeMirror type,
final SyntheticName name,
final SyntheticAnnotationValue defaultValue) {
final Name name,
final AnnotationValue defaultValue) {
super();
this.annotationMirrors = new CopyOnWriteArrayList<>(annotationMirrors);
this.t = new Type(type);
this.name = requireNonNull(name, "name");
this.defaultValue = defaultValue;
// This Type is NOT an inner class and cannot receive annotations so this is OK.
this.t = type instanceof Type t ? t : new Type(type);
this.name = name instanceof SyntheticName sn ? sn : new SyntheticName(name);
this.defaultValue =
defaultValue instanceof SyntheticAnnotationValue sav ? sav : new SyntheticAnnotationValue(defaultValue.getValue());
}


Expand Down Expand Up @@ -861,6 +905,7 @@ private Type(final TypeMirror type) { // the "return type"
this.type = switch (type) {
case null -> throw new NullPointerException("type");
case ArrayType t when t.getKind() == ARRAY -> validateScalarType(t.getComponentType());
case Type t -> t.type;
default -> validateScalarType(type);
};
}
Expand Down
Loading