Skip to content

Commit bd329fb

Browse files
committed
Accessor annotations must be supplemented by type annotations
It appears that calling `component.getAccessor().getAnnotationMirrors()` on a record component accessor does not return all annotations for some reason. This was determined by debugging and trial-and-error. The annotations returned for the accessor must be supplemented with annotations from the `asType()` called on the component. Note: the annotations from `asType()` are not complete either! It must be a union of both. Fixes #111
1 parent 077d24d commit bd329fb

2 files changed

Lines changed: 16 additions & 8 deletions

File tree

record-builder-processor/src/main/java/io/soabase/recordbuilder/processor/ElementUtils.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.Map;
2929
import java.util.Optional;
3030
import java.util.stream.Collectors;
31+
import java.util.stream.Stream;
3132

3233
public class ElementUtils {
3334
public static Optional<? extends AnnotationMirror> findAnnotationMirror(ProcessingEnvironment processingEnv,
@@ -36,6 +37,15 @@ public static Optional<? extends AnnotationMirror> findAnnotationMirror(Processi
3637
.filter(e -> e.getAnnotationType().toString().equals(annotationClass)).findFirst();
3738
}
3839

40+
public static List<? extends AnnotationMirror> getAccessorAnnotations(ProcessingEnvironment processingEnv,
41+
RecordComponentElement component) {
42+
var accessorMirrors = component.getAccessor().getAnnotationMirrors();
43+
var typeMirrors = component.asType().getAnnotationMirrors().stream()
44+
.filter(typeMirror -> accessorMirrors.stream().noneMatch(accessorMirror -> processingEnv.getTypeUtils()
45+
.isSameType(accessorMirror.getAnnotationType(), typeMirror.getAnnotationType())));
46+
return Stream.concat(accessorMirrors.stream(), typeMirrors).toList();
47+
}
48+
3949
public static Optional<? extends AnnotationValue> getAnnotationValue(
4050
Map<? extends ExecutableElement, ? extends AnnotationValue> values, String name) {
4151
return values.entrySet().stream().filter(e -> e.getKey().getSimpleName().toString().equals(name))

record-builder-processor/src/main/java/io/soabase/recordbuilder/processor/RecordFacade.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,15 @@ public static RecordFacade fromTypeElement(ProcessingEnvironment processingEnv,
5252

5353
private static List<RecordClassType> buildRecordComponents(ProcessingEnvironment processingEnv,
5454
TypeElement record) {
55-
var accessorAnnotations = record.getRecordComponents().stream().map(
56-
e -> (e.getAccessor() != null) ? e.getAccessor().getAnnotationMirrors() : List.<AnnotationMirror> of())
57-
.collect(Collectors.toList());
58-
var canonicalConstructorAnnotations = ElementUtils.findCanonicalConstructor(record)
59-
.map(constructor -> ((ExecutableElement) constructor).getParameters().stream()
60-
.map(Element::getAnnotationMirrors).collect(Collectors.toList()))
55+
Optional<? extends Element> canonicalConstructor = ElementUtils.findCanonicalConstructor(record);
56+
var canonicalConstructorAnnotations = canonicalConstructor.map(constructor -> ((ExecutableElement) constructor)
57+
.getParameters().stream().map(Element::getAnnotationMirrors).collect(Collectors.toList()))
6158
.orElse(List.of());
59+
6260
var recordComponents = record.getRecordComponents();
6361
return IntStream.range(0, recordComponents.size()).mapToObj(index -> {
64-
var thisAccessorAnnotations = (accessorAnnotations.size() > index) ? accessorAnnotations.get(index)
65-
: List.<AnnotationMirror> of();
62+
var thisAccessorAnnotations = ElementUtils.getAccessorAnnotations(processingEnv,
63+
recordComponents.get(index));
6664
var thisCanonicalConstructorAnnotations = (canonicalConstructorAnnotations.size() > index)
6765
? canonicalConstructorAnnotations.get(index) : List.<AnnotationMirror> of();
6866
return ElementUtils.getRecordClassType(processingEnv, recordComponents.get(index), thisAccessorAnnotations,

0 commit comments

Comments
 (0)