Fix handling of annotations on super classes in AnnotationsPropertySource

Closes gh-6006
This commit is contained in:
Andy Wilkinson 2016-05-23 12:35:00 +01:00
parent 3d0b682f0f
commit e9a226c8f8
2 changed files with 38 additions and 2 deletions

View File

@ -87,13 +87,24 @@ public class AnnotationsPropertySource extends EnumerablePropertySource<Class<?>
List<Annotation> mergedAnnotations = new ArrayList<Annotation>();
for (Annotation annotation : AnnotationUtils.getAnnotations(source)) {
if (!AnnotationUtils.isInJavaLangAnnotationPackage(annotation)) {
mergedAnnotations.add(AnnotatedElementUtils.getMergedAnnotation(root,
annotation.annotationType()));
mergedAnnotations
.add(findMergedAnnotation(root, annotation.annotationType()));
}
}
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,
PropertyMapping typeMapping, Map<String, Object> properties) {
PropertyMapping attributeMapping = AnnotationUtils.getAnnotation(attribute,

View File

@ -169,6 +169,22 @@ public class AnnotationsPropertySourceTests {
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 {
}
@ -359,4 +375,13 @@ public class AnnotationsPropertySourceTests {
}
static class PropertyMappedAnnotationOnSuperClass extends TypeLevel {
}
static class AliasedPropertyMappedAnnotationOnSuperClass
extends PropertyMappedAttributeWithAnAlias {
}
}