Consistent exposure of empty attribute arrays in AnnotationMetadata

Issue: SPR-17347

(cherry picked from commit 83909e6e1e)
This commit is contained in:
Juergen Hoeller
2018-10-09 23:14:13 +02:00
parent d61a7ed1f0
commit 53430760f3
2 changed files with 23 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;
@@ -27,6 +28,8 @@ import org.springframework.lang.Nullable;
import org.springframework.util.ObjectUtils;
/**
* {@link AnnotationVisitor} to recursively visit annotation arrays.
*
* @author Chris Beams
* @author Juergen Hoeller
* @since 3.1.1
@@ -80,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.
}
}
}
}
}