diff --git a/core/src/main/java/org/jboss/jandex/AnnotationOverlayImpl.java b/core/src/main/java/org/jboss/jandex/AnnotationOverlayImpl.java index 76b2c0bc..5b3c1d43 100644 --- a/core/src/main/java/org/jboss/jandex/AnnotationOverlayImpl.java +++ b/core/src/main/java/org/jboss/jandex/AnnotationOverlayImpl.java @@ -320,22 +320,43 @@ public boolean hasAnnotation(Predicate predicate) { @Override public void add(Class 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 annotations) { - this.annotations.addAll(Objects.requireNonNull(annotations)); + Objects.requireNonNull(annotations); + if (annotations.stream().anyMatch(it -> it.target() == null)) { + List 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 diff --git a/core/src/test/java/org/jboss/jandex/test/AnnotationOverlayTest.java b/core/src/test/java/org/jboss/jandex/test/AnnotationOverlayTest.java index ca0602ff..4af83e64 100644 --- a/core/src/test/java/org/jboss/jandex/test/AnnotationOverlayTest.java +++ b/core/src/test/java/org/jboss/jandex/test/AnnotationOverlayTest.java @@ -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 { @@ -276,20 +277,24 @@ 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("_"); } @@ -297,6 +302,7 @@ private void assertOverlay(String expectedValues, AnnotationTransformation... tr } 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("_"); } } @@ -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()); } } diff --git a/core/src/test/java/org/jboss/jandex/test/MutableAnnotationOverlayTest.java b/core/src/test/java/org/jboss/jandex/test/MutableAnnotationOverlayTest.java index 8e90c53d..f747115a 100644 --- a/core/src/test/java/org/jboss/jandex/test/MutableAnnotationOverlayTest.java +++ b/core/src/test/java/org/jboss/jandex/test/MutableAnnotationOverlayTest.java @@ -132,6 +132,7 @@ private void assertOverlay(String expectedValues, BiConsumer