Skip to content

Commit 6837111

Browse files
committed
Refactor AnnotationUtils#findAllAnnotationAttributes
Remove all convenience variants of #findAllAnnotationAttributes and refactor the remaining method to accept a MetadataReaderFactory instead of creating its own SimpleMetadataReaderFactory internally. This allows clients to use non-default class loaders as well as customize the particular MetadataReaderFactory to be used (e.g. 'simple' vs 'caching', etc). Issue: SPR-8752
1 parent 3bb01ee commit 6837111

File tree

3 files changed

+16
-25
lines changed

3 files changed

+16
-25
lines changed

org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ protected void doProcessConfigurationClass(ConfigurationClass configClass, Annot
210210

211211
// process any @Import annotations
212212
List<Map<String, Object>> allImportAttribs =
213-
AnnotationUtils.findAllAnnotationAttributes(Import.class, metadata.getClassName(), true);
213+
AnnotationUtils.findAllAnnotationAttributes(Import.class, metadata.getClassName(), true, metadataReaderFactory);
214214
for (Map<String, Object> importAttribs : allImportAttribs) {
215215
processImport(configClass, (String[]) importAttribs.get("value"), true);
216216
}

org.springframework.core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.core.BridgeMethodResolver;
3131
import org.springframework.core.type.AnnotationMetadata;
3232
import org.springframework.core.type.classreading.MetadataReader;
33+
import org.springframework.core.type.classreading.MetadataReaderFactory;
3334
import org.springframework.core.type.classreading.SimpleMetadataReaderFactory;
3435
import org.springframework.util.Assert;
3536

@@ -354,30 +355,32 @@ else if (value instanceof Class[]) {
354355
}
355356

356357
/**
357-
* Return a list of attribute maps for all declarations of the given target annotation
358-
* on the given annotated class. Meta annotations are ordered first in the list, and if
359-
* the target annotation is declared directly on the class, its map of attributes will be
358+
* Return a list of attribute maps for all declarations of the given annotation
359+
* on the given annotated class using the given MetadataReaderFactory to introspect
360+
* annotation metadata. Meta-annotations are ordered first in the list, and if the
361+
* target annotation is declared directly on the class, its map of attributes will be
360362
* ordered last in the list.
361363
* @param targetAnnotation the annotation to search for, both locally and as a meta-annotation
362-
* @param annotatedClassName the class to search
364+
* @param annotatedClassName the class to inspect
363365
* @param classValuesAsString whether class attributes should be returned as strings
366+
* @param metadataReaderFactory factory used to create metadata readers for each type
364367
* @since 3.1
365-
* @see {@link #findAllAnnotationAttributes(Class, String)}
366368
*/
367369
public static List<Map<String, Object>> findAllAnnotationAttributes(
368-
Class<? extends Annotation> targetAnnotation, String annotatedClassName, boolean classValuesAsString) throws IOException {
370+
Class<? extends Annotation> targetAnnotation, String annotatedClassName,
371+
boolean classValuesAsString, MetadataReaderFactory metadataReaderFactory) throws IOException {
369372

370373
List<Map<String, Object>> allAttribs = new ArrayList<Map<String, Object>>();
371374

372-
MetadataReader reader = new SimpleMetadataReaderFactory().getMetadataReader(annotatedClassName);
375+
MetadataReader reader = metadataReaderFactory.getMetadataReader(annotatedClassName);
373376
AnnotationMetadata metadata = reader.getAnnotationMetadata();
374377
String targetAnnotationType = targetAnnotation.getName();
375378

376379
for (String annotationType : metadata.getAnnotationTypes()) {
377380
if (annotationType.equals(targetAnnotationType)) {
378381
continue;
379382
}
380-
MetadataReader metaReader = new SimpleMetadataReaderFactory().getMetadataReader(annotationType);
383+
MetadataReader metaReader = metadataReaderFactory.getMetadataReader(annotationType);
381384
Map<String, Object> targetAttribs =
382385
metaReader.getAnnotationMetadata().getAnnotationAttributes(targetAnnotationType, classValuesAsString);
383386
if (targetAttribs != null) {
@@ -394,21 +397,6 @@ public static List<Map<String, Object>> findAllAnnotationAttributes(
394397
return allAttribs;
395398
}
396399

397-
/**
398-
* Return a list of attribute maps for all declarations of the given target annotation
399-
* on the given annotated class. Meta annotations are ordered first in the list, and if
400-
* the target annotation is declared directly on the class, its map of attributes will be
401-
* ordered last in the list.
402-
* @param targetAnnotation the annotation to search for, both locally and as a meta-annotation
403-
* @param annotatedClassName the class to search
404-
* @since 3.1
405-
* @see {@link #findAllAnnotationAttributes(Class, String, boolean)}
406-
*/
407-
public static List<Map<String, Object>> findAllAnnotationAttributes(
408-
Class<? extends Annotation> targetAnnotation, String annotatedClassName) throws IOException {
409-
return findAllAnnotationAttributes(targetAnnotation, annotatedClassName, false);
410-
}
411-
412400
/**
413401
* Retrieve the <em>value</em> of the <code>&quot;value&quot;</code> attribute of a
414402
* single-element Annotation, given an annotation instance.

org.springframework.core/src/test/java/org/springframework/core/annotation/AnnotationUtilsTests.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
import org.junit.Test;
4141
import org.springframework.core.Ordered;
42+
import org.springframework.core.type.classreading.SimpleMetadataReaderFactory;
4243
import org.springframework.stereotype.Component;
4344

4445
/**
@@ -221,7 +222,9 @@ public void testFindAnnotationFromInterfaceOnSuper() throws Exception {
221222
@Test
222223
public void findAllComponentAnnotationAttributes() throws IOException {
223224
List<Map<String,Object>> allAttribs =
224-
AnnotationUtils.findAllAnnotationAttributes(Component.class, HasLocalAndMetaComponentAnnotation.class.getName());
225+
AnnotationUtils.findAllAnnotationAttributes(Component.class,
226+
HasLocalAndMetaComponentAnnotation.class.getName(), false,
227+
new SimpleMetadataReaderFactory());
225228

226229
Object value = null;
227230
for (Map<String, Object> attribs : allAttribs) {

0 commit comments

Comments
 (0)