Consistent exposure of empty attribute arrays in AnnotationMetadata

Issue: SPR-17347
This commit is contained in:
Juergen Hoeller
2018-10-09 23:14:13 +02:00
parent fdf340306d
commit 83909e6e1e
2 changed files with 21 additions and 2 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.core.type.classreading;
import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
@@ -82,6 +83,20 @@ class RecursiveAnnotationArrayVisitor extends AbstractRecursiveAnnotationVisitor
if (!this.allNestedAttributes.isEmpty()) {
this.attributes.put(this.attributeName, this.allNestedAttributes.toArray(new AnnotationAttributes[0]));
}
else if (!this.attributes.containsKey(this.attributeName)) {
Class<? extends Annotation> annotationType = this.attributes.annotationType();
if (annotationType != null) {
try {
Class<?> attributeType = annotationType.getMethod(this.attributeName).getReturnType();
if (attributeType.isArray()) {
this.attributes.put(this.attributeName, Array.newInstance(attributeType.getComponentType(), 0));
}
}
catch (NoSuchMethodException ex) {
// Corresponding attribute method not found: cannot expose empty array.
}
}
}
}
}