Process all meta and local @Import declarations

Includes the introduction of AnnotationUtils#findAllAnnotationAttributes
to support iterating through all annotations declared on a given type
and interrogating each for the presence of a meta-annotation. See tests
for details.
This commit is contained in:
Chris Beams
2011-05-06 19:05:15 +00:00
parent 856da7edb9
commit 89005a5b70
4 changed files with 253 additions and 4 deletions

View File

@@ -16,14 +16,20 @@
package org.springframework.core.annotation;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
import org.springframework.core.BridgeMethodResolver;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.SimpleMetadataReaderFactory;
import org.springframework.util.Assert;
/**
@@ -323,6 +329,60 @@ public abstract class AnnotationUtils {
return attrs;
}
/**
* Return a list of attribute maps for all declarations of the given target annotation
* on the given annotated class. Meta annotations are ordered first in the list, and if
* the target annotation is declared directly on the class, its map of attributes will be
* ordered last in the list.
* @param targetAnnotation the annotation to search for, both locally and as a meta-annotation
* @param annotatedClassName the class to search
* @param classValuesAsString whether class attributes should be returned as strings
* @see {@link #findAllAnnotationAttributes(Class, String)}
*/
public static List<Map<String, Object>> findAllAnnotationAttributes(
Class<? extends Annotation> targetAnnotation, String annotatedClassName, boolean classValuesAsString) throws IOException {
List<Map<String, Object>> allAttribs = new ArrayList<Map<String, Object>>();
MetadataReader reader = new SimpleMetadataReaderFactory().getMetadataReader(annotatedClassName);
AnnotationMetadata metadata = reader.getAnnotationMetadata();
String targetAnnotationType = targetAnnotation.getName();
for (String annotationType : metadata.getAnnotationTypes()) {
if (annotationType.equals(targetAnnotationType)) {
continue;
}
MetadataReader metaReader = new SimpleMetadataReaderFactory().getMetadataReader(annotationType);
Map<String, Object> targetAttribs =
metaReader.getAnnotationMetadata().getAnnotationAttributes(targetAnnotationType, classValuesAsString);
if (targetAttribs != null) {
allAttribs.add(targetAttribs);
}
}
Map<String, Object> localAttribs =
metadata.getAnnotationAttributes(targetAnnotationType, classValuesAsString);
if (localAttribs != null) {
allAttribs.add(localAttribs);
}
return allAttribs;
}
/**
* Return a list of attribute maps for all declarations of the given target annotation
* on the given annotated class. Meta annotations are ordered first in the list, and if
* the target annotation is declared directly on the class, its map of attributes will be
* ordered last in the list.
* @param targetAnnotation the annotation to search for, both locally and as a meta-annotation
* @param annotatedClassName the class to search
* @see {@link #findAllAnnotationAttributes(Class, String, boolean)}
*/
public static List<Map<String, Object>> findAllAnnotationAttributes(
Class<? extends Annotation> targetAnnotation, String annotatedClassName) throws IOException {
return findAllAnnotationAttributes(targetAnnotation, annotatedClassName, false);
}
/**
* Retrieve the <em>value</em> of the <code>&quot;value&quot;</code> attribute of a
* single-element Annotation, given an annotation instance.

View File

@@ -16,16 +16,30 @@
package org.springframework.core.annotation;
import static org.junit.Assert.*;
import static org.springframework.core.annotation.AnnotationUtils.*;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.springframework.core.annotation.AnnotationUtils.findAnnotation;
import static org.springframework.core.annotation.AnnotationUtils.findAnnotationDeclaringClass;
import static org.springframework.core.annotation.AnnotationUtils.getAnnotation;
import static org.springframework.core.annotation.AnnotationUtils.isAnnotationDeclaredLocally;
import static org.springframework.core.annotation.AnnotationUtils.isAnnotationInherited;
import java.io.IOException;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
/**
* @author Rod Johnson
@@ -204,6 +218,36 @@ public class AnnotationUtilsTests {
assertNotNull(order);
}
@Test
public void findAllComponentAnnotationAttributes() throws IOException {
List<Map<String,Object>> allAttribs =
AnnotationUtils.findAllAnnotationAttributes(Component.class, HasLocalAndMetaComponentAnnotation.class.getName());
Object value = null;
for (Map<String, Object> attribs : allAttribs) {
value = attribs.get("value");
}
assertThat(allAttribs.size(), is(3));
assertEquals("local", value);
}
@Component(value="meta1")
@Retention(RetentionPolicy.RUNTIME)
@interface Meta1 {
}
@Component(value="meta2")
@Retention(RetentionPolicy.RUNTIME)
@interface Meta2 {
}
@Meta1
@Component(value="local")
@Meta2
static class HasLocalAndMetaComponentAnnotation {
}
public static interface AnnotatedInterface {
@Order(0)