Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure all annotations in the annotation overlay have target #414

Merged
merged 1 commit into from
Jul 29, 2024
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
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
Loading