Browse Source

Polishing

pull/1619/head
Juergen Hoeller 11 years ago
parent
commit
c88877f1c4
  1. 8
      spring-core/src/main/java/org/springframework/core/annotation/AbstractAliasAwareAnnotationAttributeExtractor.java
  2. 24
      spring-core/src/main/java/org/springframework/core/annotation/MapAnnotationAttributeExtractor.java
  3. 2
      spring-core/src/test/java/org/springframework/core/type/AnnotationMetadataTests.java

8
spring-core/src/main/java/org/springframework/core/annotation/AbstractAliasAwareAnnotationAttributeExtractor.java

@ -55,8 +55,8 @@ abstract class AbstractAliasAwareAnnotationAttributeExtractor<S> implements Anno
* of the supplied type; may be {@code null} if unknown * of the supplied type; may be {@code null} if unknown
* @param source the underlying source of annotation attributes; never {@code null} * @param source the underlying source of annotation attributes; never {@code null}
*/ */
AbstractAliasAwareAnnotationAttributeExtractor(Class<? extends Annotation> annotationType, AbstractAliasAwareAnnotationAttributeExtractor(
AnnotatedElement annotatedElement, S source) { Class<? extends Annotation> annotationType, AnnotatedElement annotatedElement, S source) {
Assert.notNull(annotationType, "annotationType must not be null"); Assert.notNull(annotationType, "annotationType must not be null");
Assert.notNull(source, "source must not be null"); Assert.notNull(source, "source must not be null");
@ -84,12 +84,12 @@ abstract class AbstractAliasAwareAnnotationAttributeExtractor<S> implements Anno
@Override @Override
public final Object getAttributeValue(Method attributeMethod) { public final Object getAttributeValue(Method attributeMethod) {
final String attributeName = attributeMethod.getName(); String attributeName = attributeMethod.getName();
Object attributeValue = getRawAttributeValue(attributeMethod); Object attributeValue = getRawAttributeValue(attributeMethod);
List<String> aliasNames = this.attributeAliasMap.get(attributeName); List<String> aliasNames = this.attributeAliasMap.get(attributeName);
if (aliasNames != null) { if (aliasNames != null) {
final Object defaultValue = AnnotationUtils.getDefaultValue(getAnnotationType(), attributeName); Object defaultValue = AnnotationUtils.getDefaultValue(getAnnotationType(), attributeName);
for (String aliasName : aliasNames) { for (String aliasName : aliasNames) {
Object aliasValue = getRawAttributeValue(aliasName); Object aliasValue = getRawAttributeValue(aliasName);

24
spring-core/src/main/java/org/springframework/core/annotation/MapAnnotationAttributeExtractor.java

@ -121,33 +121,33 @@ class MapAnnotationAttributeExtractor extends AbstractAliasAwareAnnotationAttrib
// if still null // if still null
if (attributeValue == null) { if (attributeValue == null) {
throw new IllegalArgumentException(String.format( throw new IllegalArgumentException(String.format(
"Attributes map [%s] returned null for required attribute [%s] defined by annotation type [%s].", "Attributes map [%s] returned null for required attribute [%s] defined by annotation type [%s].",
attributes, attributeName, annotationType.getName())); attributes, attributeName, annotationType.getName()));
} }
// finally, ensure correct type // finally, ensure correct type
Class<?> requiredReturnType = attributeMethod.getReturnType(); Class<?> requiredReturnType = attributeMethod.getReturnType();
Class<? extends Object> actualReturnType = attributeValue.getClass(); Class<? extends Object> actualReturnType = attributeValue.getClass();
if (!ClassUtils.isAssignable(requiredReturnType, actualReturnType)) { if (!ClassUtils.isAssignable(requiredReturnType, actualReturnType)) {
boolean converted = false; boolean converted = false;
// Nested map representing a single annotation? // Nested map representing a single annotation?
if (Annotation.class.isAssignableFrom(requiredReturnType) if (Annotation.class.isAssignableFrom(requiredReturnType) &&
&& Map.class.isAssignableFrom(actualReturnType)) { Map.class.isAssignableFrom(actualReturnType)) {
Class<? extends Annotation> nestedAnnotationType =
Class<? extends Annotation> nestedAnnotationType = (Class<? extends Annotation>) requiredReturnType; (Class<? extends Annotation>) requiredReturnType;
Map<String, Object> map = (Map<String, Object>) attributeValue; Map<String, Object> map = (Map<String, Object>) attributeValue;
attributes.put(attributeName, synthesizeAnnotation(map, nestedAnnotationType, null)); attributes.put(attributeName, synthesizeAnnotation(map, nestedAnnotationType, null));
converted = true; converted = true;
} }
// Nested array of maps representing an array of annotations? // Nested array of maps representing an array of annotations?
else if (requiredReturnType.isArray() else if (requiredReturnType.isArray() && actualReturnType.isArray() &&
&& Annotation.class.isAssignableFrom(requiredReturnType.getComponentType()) Annotation.class.isAssignableFrom(requiredReturnType.getComponentType()) &&
&& actualReturnType.isArray() Map.class.isAssignableFrom(actualReturnType.getComponentType())) {
&& Map.class.isAssignableFrom(actualReturnType.getComponentType())) { Class<? extends Annotation> nestedAnnotationType =
(Class<? extends Annotation>) requiredReturnType.getComponentType();
Class<? extends Annotation> nestedAnnotationType = (Class<? extends Annotation>) requiredReturnType.getComponentType();
Map<String, Object>[] maps = (Map<String, Object>[]) attributeValue; Map<String, Object>[] maps = (Map<String, Object>[]) attributeValue;
attributes.put(attributeName, synthesizeAnnotationArray(maps, nestedAnnotationType)); attributes.put(attributeName, synthesizeAnnotationArray(maps, nestedAnnotationType));
converted = true; converted = true;

2
spring-core/src/test/java/org/springframework/core/type/AnnotationMetadataTests.java

@ -194,7 +194,7 @@ public class AnnotationMetadataTests {
private void assertMetaAnnotationOverrides(AnnotationMetadata metadata) { private void assertMetaAnnotationOverrides(AnnotationMetadata metadata) {
AnnotationAttributes attributes = (AnnotationAttributes) metadata.getAnnotationAttributes( AnnotationAttributes attributes = (AnnotationAttributes) metadata.getAnnotationAttributes(
TestComponentScan.class.getName(), false); TestComponentScan.class.getName(), false);
String[] basePackages = attributes.getStringArray("basePackages"); String[] basePackages = attributes.getStringArray("basePackages");
assertThat("length of basePackages[]", basePackages.length, is(1)); assertThat("length of basePackages[]", basePackages.length, is(1));
assertThat("basePackages[0]", basePackages[0], is("org.example.componentscan")); assertThat("basePackages[0]", basePackages[0], is("org.example.componentscan"));

Loading…
Cancel
Save