Polishing annotation processing internals

This commit is contained in:
Sam Brannen 2023-08-12 15:41:48 +02:00
parent 2ede74fa9c
commit a33b14338f
2 changed files with 14 additions and 18 deletions

View File

@ -16,6 +16,7 @@
package org.springframework.context.annotation; package org.springframework.context.annotation;
import java.lang.annotation.Annotation;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.Map; import java.util.Map;
@ -271,32 +272,26 @@ public abstract class AnnotationConfigUtils {
} }
@Nullable @Nullable
static AnnotationAttributes attributesFor(AnnotatedTypeMetadata metadata, Class<?> annotationClass) { static AnnotationAttributes attributesFor(AnnotatedTypeMetadata metadata, Class<?> annotationType) {
return attributesFor(metadata, annotationClass.getName()); return attributesFor(metadata, annotationType.getName());
} }
@Nullable @Nullable
static AnnotationAttributes attributesFor(AnnotatedTypeMetadata metadata, String annotationClassName) { static AnnotationAttributes attributesFor(AnnotatedTypeMetadata metadata, String annotationTypeName) {
return AnnotationAttributes.fromMap(metadata.getAnnotationAttributes(annotationClassName)); return AnnotationAttributes.fromMap(metadata.getAnnotationAttributes(annotationTypeName));
}
static Set<AnnotationAttributes> attributesForRepeatable(AnnotationMetadata metadata,
Class<?> containerClass, Class<?> annotationClass) {
return attributesForRepeatable(metadata, containerClass.getName(), annotationClass.getName());
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
static Set<AnnotationAttributes> attributesForRepeatable( static Set<AnnotationAttributes> attributesForRepeatable(AnnotationMetadata metadata,
AnnotationMetadata metadata, String containerClassName, String annotationClassName) { Class<? extends Annotation> containerType, Class<? extends Annotation> annotationType) {
Set<AnnotationAttributes> result = new LinkedHashSet<>(); Set<AnnotationAttributes> result = new LinkedHashSet<>();
// Direct annotation present? // Direct annotation present or meta-present?
addAttributesIfNotNull(result, metadata.getAnnotationAttributes(annotationClassName)); addAttributesIfNotNull(result, metadata.getAnnotationAttributes(annotationType.getName()));
// Container annotation present? // Container annotation present or meta-present?
Map<String, Object> container = metadata.getAnnotationAttributes(containerClassName); Map<String, Object> container = metadata.getAnnotationAttributes(containerType.getName());
if (container != null && container.containsKey("value")) { if (container != null && container.containsKey("value")) {
for (Map<String, Object> containedAttributes : (Map<String, Object>[]) container.get("value")) { for (Map<String, Object> containedAttributes : (Map<String, Object>[]) container.get("value")) {
addAttributesIfNotNull(result, containedAttributes); addAttributesIfNotNull(result, containedAttributes);

View File

@ -31,7 +31,8 @@ import org.springframework.util.MultiValueMap;
/** /**
* Defines access to the annotations of a specific type ({@link AnnotationMetadata class} * Defines access to the annotations of a specific type ({@link AnnotationMetadata class}
* or {@link MethodMetadata method}), in a form that does not necessarily require * or {@link MethodMetadata method}), in a form that does not necessarily require
* class loading. * class loading of the types being inspected. Note, however, that classes for
* encountered annotations will be loaded.
* *
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Mark Fisher * @author Mark Fisher
@ -48,7 +49,7 @@ public interface AnnotatedTypeMetadata {
/** /**
* Get annotation details based on the direct annotations and meta-annotations * Get annotation details based on the direct annotations and meta-annotations
* of the underlying element. * of the underlying element.
* @return merged annotations based on the direct annotations * @return merged annotations based on the direct annotations and meta-annotations
* @since 5.2 * @since 5.2
*/ */
MergedAnnotations getAnnotations(); MergedAnnotations getAnnotations();