Fix handling of annotations on super classes in AnnotationsPropertySource
Closes gh-6006
This commit is contained in:
parent
3d0b682f0f
commit
e9a226c8f8
|
@ -87,13 +87,24 @@ public class AnnotationsPropertySource extends EnumerablePropertySource<Class<?>
|
||||||
List<Annotation> mergedAnnotations = new ArrayList<Annotation>();
|
List<Annotation> mergedAnnotations = new ArrayList<Annotation>();
|
||||||
for (Annotation annotation : AnnotationUtils.getAnnotations(source)) {
|
for (Annotation annotation : AnnotationUtils.getAnnotations(source)) {
|
||||||
if (!AnnotationUtils.isInJavaLangAnnotationPackage(annotation)) {
|
if (!AnnotationUtils.isInJavaLangAnnotationPackage(annotation)) {
|
||||||
mergedAnnotations.add(AnnotatedElementUtils.getMergedAnnotation(root,
|
mergedAnnotations
|
||||||
annotation.annotationType()));
|
.add(findMergedAnnotation(root, annotation.annotationType()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return mergedAnnotations;
|
return mergedAnnotations;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Annotation findMergedAnnotation(Class<?> source,
|
||||||
|
Class<? extends Annotation> annotationType) {
|
||||||
|
if (source == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
Annotation mergedAnnotation = AnnotatedElementUtils.getMergedAnnotation(source,
|
||||||
|
annotationType);
|
||||||
|
return mergedAnnotation != null ? mergedAnnotation
|
||||||
|
: findMergedAnnotation(source.getSuperclass(), annotationType);
|
||||||
|
}
|
||||||
|
|
||||||
private void collectProperties(Annotation annotation, Method attribute,
|
private void collectProperties(Annotation annotation, Method attribute,
|
||||||
PropertyMapping typeMapping, Map<String, Object> properties) {
|
PropertyMapping typeMapping, Map<String, Object> properties) {
|
||||||
PropertyMapping attributeMapping = AnnotationUtils.getAnnotation(attribute,
|
PropertyMapping attributeMapping = AnnotationUtils.getAnnotation(attribute,
|
||||||
|
|
|
@ -169,6 +169,22 @@ public class AnnotationsPropertySourceTests {
|
||||||
new AnnotationsPropertySource(PropertyMappedWithSelfAnnotatingAnnotation.class);
|
new AnnotationsPropertySource(PropertyMappedWithSelfAnnotatingAnnotation.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void typeLevelAnnotationOnSuperClass() {
|
||||||
|
AnnotationsPropertySource source = new AnnotationsPropertySource(
|
||||||
|
PropertyMappedAnnotationOnSuperClass.class);
|
||||||
|
assertThat(source.getPropertyNames()).containsExactly("value");
|
||||||
|
assertThat(source.getProperty("value")).isEqualTo("abc");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void aliasedPropertyMappedAttributeOnSuperClass() {
|
||||||
|
AnnotationsPropertySource source = new AnnotationsPropertySource(
|
||||||
|
AliasedPropertyMappedAnnotationOnSuperClass.class);
|
||||||
|
assertThat(source.getPropertyNames()).containsExactly("aliasing.value");
|
||||||
|
assertThat(source.getProperty("aliasing.value")).isEqualTo("baz");
|
||||||
|
}
|
||||||
|
|
||||||
static class NoAnnotation {
|
static class NoAnnotation {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -359,4 +375,13 @@ public class AnnotationsPropertySourceTests {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static class PropertyMappedAnnotationOnSuperClass extends TypeLevel {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static class AliasedPropertyMappedAnnotationOnSuperClass
|
||||||
|
extends PropertyMappedAttributeWithAnAlias {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue