Skip to content

Commit

Permalink
Merge pull request #414 from Ladicek/annotation-overlay-target
Browse files Browse the repository at this point in the history
Make sure all annotations in the annotation overlay have target
  • Loading branch information
Ladicek authored Jul 29, 2024
2 parents 383f87b + e555f04 commit 03e6ecd
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
27 changes: 24 additions & 3 deletions core/src/main/java/org/jboss/jandex/AnnotationOverlayImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -320,22 +320,43 @@ public boolean hasAnnotation(Predicate<AnnotationInstance> predicate) {
@Override
public void add(Class<? extends Annotation> annotationClass) {
Objects.requireNonNull(annotationClass);
annotations.add(AnnotationInstance.builder(annotationClass).build());
annotations.add(AnnotationInstance.builder(annotationClass).buildWithTarget(declaration));
}

@Override
public void add(AnnotationInstance annotation) {
if (annotation.target() == null) {
annotation = AnnotationInstance.create(annotation, declaration);
}
annotations.add(Objects.requireNonNull(annotation));
}

@Override
public void addAll(AnnotationInstance... annotations) {
Collections.addAll(this.annotations, Objects.requireNonNull(annotations));
Objects.requireNonNull(annotations);
for (int i = 0; i < annotations.length; i++) {
if (annotations[i].target() == null) {
annotations[i] = AnnotationInstance.create(annotations[i], declaration);
}
}
Collections.addAll(this.annotations, annotations);
}

@Override
public void addAll(Collection<AnnotationInstance> annotations) {
this.annotations.addAll(Objects.requireNonNull(annotations));
Objects.requireNonNull(annotations);
if (annotations.stream().anyMatch(it -> it.target() == null)) {
List<AnnotationInstance> fixed = new ArrayList<>();
for (AnnotationInstance annotation : annotations) {
if (annotation.target() == null) {
fixed.add(AnnotationInstance.create(annotation, declaration));
} else {
fixed.add(annotation);
}
}
annotations = fixed;
}
this.annotations.addAll(annotations);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ private void assertOverlay(String expectedValues, AnnotationTransformation... tr
if (inheritedAnnotations) {
assertTrue(overlay.hasAnnotation(clazz, MyInheritedAnnotation.class));
assertNotNull(overlay.annotation(clazz, MyInheritedAnnotation.class));
assertNotNull(overlay.annotation(clazz, MyInheritedAnnotation.class).target());
assertEquals("i", overlay.annotation(clazz, MyInheritedAnnotation.class).value().asString());
assertEquals(1, overlay.annotationsWithRepeatable(clazz, MyInheritedAnnotation.class).size());
} else {
Expand All @@ -276,27 +277,32 @@ private void assertOverlay(String expectedValues, AnnotationTransformation... tr

for (Declaration declaration : Arrays.asList(clazz, field, method, parameter1, parameter2)) {
if (overlay.hasAnnotation(declaration, MyAnnotation.DOT_NAME)) {
assertNotNull(overlay.annotation(declaration, MyAnnotation.DOT_NAME).target());
values.append(overlay.annotation(declaration, MyAnnotation.DOT_NAME).value().asString()).append("_");
}
if (overlay.hasAnnotation(declaration, MyOtherAnnotation.DOT_NAME)) {
assertNotNull(overlay.annotation(declaration, MyOtherAnnotation.DOT_NAME).target());
values.append(overlay.annotation(declaration, MyOtherAnnotation.DOT_NAME).value().asString())
.append("_");
}
if (declaration != method) {
if (overlay.hasAnnotation(declaration, MyRepeatableAnnotation.DOT_NAME)) {
assertNotNull(overlay.annotation(declaration, MyRepeatableAnnotation.DOT_NAME).target());
values.append(overlay.annotation(declaration, MyRepeatableAnnotation.DOT_NAME).value().asString())
.append("_");
}
if (overlay.hasAnnotation(declaration, MyRepeatableAnnotation.List.DOT_NAME)) {
AnnotationInstance annotation = overlay.annotation(declaration,
MyRepeatableAnnotation.List.DOT_NAME);
assertNotNull(annotation.target());
for (AnnotationInstance nestedAnnotation : annotation.value().asNestedArray()) {
values.append(nestedAnnotation.value().asString()).append("_");
}
}
} else { // just to test `annotationsWithRepeatable`, no other reason
for (AnnotationInstance annotation : overlay.annotationsWithRepeatable(declaration,
MyRepeatableAnnotation.DOT_NAME)) {
assertNotNull(annotation.target());
values.append(annotation.value().asString()).append("_");
}
}
Expand All @@ -308,6 +314,7 @@ private void assertOverlay(String expectedValues, AnnotationTransformation... tr
} else {
assertTrue(overlay.hasAnnotation(declaration, MyClassRetainedAnnotation.class));
assertNotNull(overlay.annotation(declaration, MyClassRetainedAnnotation.class));
assertNotNull(overlay.annotation(declaration, MyClassRetainedAnnotation.class).target());
assertEquals(1, overlay.annotationsWithRepeatable(declaration, MyClassRetainedAnnotation.class).size());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ private void assertOverlay(String expectedValues, BiConsumer<IndexView, MutableA
if (inheritedAnnotations) {
assertTrue(overlay.hasAnnotation(clazz, MyInheritedAnnotation.class));
assertNotNull(overlay.annotation(clazz, MyInheritedAnnotation.class));
assertNotNull(overlay.annotation(clazz, MyInheritedAnnotation.class).target());
assertEquals("i", overlay.annotation(clazz, MyInheritedAnnotation.class).value().asString());
assertEquals(1, overlay.annotationsWithRepeatable(clazz, MyInheritedAnnotation.class).size());
} else {
Expand All @@ -154,27 +155,32 @@ private void assertOverlay(String expectedValues, BiConsumer<IndexView, MutableA

for (Declaration declaration : Arrays.asList(clazz, field, method, parameter1, parameter2)) {
if (overlay.hasAnnotation(declaration, MyAnnotation.DOT_NAME)) {
assertNotNull(overlay.annotation(declaration, MyAnnotation.DOT_NAME).target());
values.append(overlay.annotation(declaration, MyAnnotation.DOT_NAME).value().asString()).append("_");
}
if (overlay.hasAnnotation(declaration, MyOtherAnnotation.DOT_NAME)) {
assertNotNull(overlay.annotation(declaration, MyOtherAnnotation.DOT_NAME).target());
values.append(overlay.annotation(declaration, MyOtherAnnotation.DOT_NAME).value().asString())
.append("_");
}
if (declaration != method) {
if (overlay.hasAnnotation(declaration, MyRepeatableAnnotation.DOT_NAME)) {
assertNotNull(overlay.annotation(declaration, MyRepeatableAnnotation.DOT_NAME).target());
values.append(overlay.annotation(declaration, MyRepeatableAnnotation.DOT_NAME).value().asString())
.append("_");
}
if (overlay.hasAnnotation(declaration, MyRepeatableAnnotation.List.DOT_NAME)) {
AnnotationInstance annotation = overlay.annotation(declaration,
MyRepeatableAnnotation.List.DOT_NAME);
assertNotNull(annotation.target());
for (AnnotationInstance nestedAnnotation : annotation.value().asNestedArray()) {
values.append(nestedAnnotation.value().asString()).append("_");
}
}
} else { // just to test `annotationsWithRepeatable`, no other reason
for (AnnotationInstance annotation : overlay.annotationsWithRepeatable(declaration,
MyRepeatableAnnotation.DOT_NAME)) {
assertNotNull(annotation.target());
values.append(annotation.value().asString()).append("_");
}
}
Expand All @@ -186,6 +192,7 @@ private void assertOverlay(String expectedValues, BiConsumer<IndexView, MutableA
} else {
assertTrue(overlay.hasAnnotation(declaration, MyClassRetainedAnnotation.class));
assertNotNull(overlay.annotation(declaration, MyClassRetainedAnnotation.class));
assertNotNull(overlay.annotation(declaration, MyClassRetainedAnnotation.class).target());
assertEquals(1, overlay.annotationsWithRepeatable(declaration, MyClassRetainedAnnotation.class).size());
}
}
Expand Down

0 comments on commit 03e6ecd

Please sign in to comment.