|
|
|
@ -1,5 +1,5 @@
@@ -1,5 +1,5 @@
|
|
|
|
|
/* |
|
|
|
|
* Copyright 2002-2022 the original author or authors. |
|
|
|
|
* Copyright 2002-2023 the original author or authors. |
|
|
|
|
* |
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -20,8 +20,10 @@ import java.lang.annotation.Annotation;
@@ -20,8 +20,10 @@ import java.lang.annotation.Annotation;
|
|
|
|
|
import java.util.ArrayDeque; |
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
import java.util.Deque; |
|
|
|
|
import java.util.HashSet; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.Map; |
|
|
|
|
import java.util.Set; |
|
|
|
|
|
|
|
|
|
import org.springframework.lang.Nullable; |
|
|
|
|
import org.springframework.util.ConcurrentReferenceHashMap; |
|
|
|
@ -40,6 +42,7 @@ import org.springframework.util.ConcurrentReferenceHashMap;
@@ -40,6 +42,7 @@ import org.springframework.util.ConcurrentReferenceHashMap;
|
|
|
|
|
* be searched once, regardless of how many times they are actually used. |
|
|
|
|
* |
|
|
|
|
* @author Phillip Webb |
|
|
|
|
* @author Sam Brannen |
|
|
|
|
* @since 5.2 |
|
|
|
|
* @see AnnotationTypeMapping |
|
|
|
|
*/ |
|
|
|
@ -60,19 +63,21 @@ final class AnnotationTypeMappings {
@@ -60,19 +63,21 @@ final class AnnotationTypeMappings {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private AnnotationTypeMappings(RepeatableContainers repeatableContainers, |
|
|
|
|
AnnotationFilter filter, Class<? extends Annotation> annotationType) { |
|
|
|
|
AnnotationFilter filter, Class<? extends Annotation> annotationType, |
|
|
|
|
Set<Class<? extends Annotation>> visitedAnnotationTypes) { |
|
|
|
|
|
|
|
|
|
this.repeatableContainers = repeatableContainers; |
|
|
|
|
this.filter = filter; |
|
|
|
|
this.mappings = new ArrayList<>(); |
|
|
|
|
addAllMappings(annotationType); |
|
|
|
|
addAllMappings(annotationType, visitedAnnotationTypes); |
|
|
|
|
this.mappings.forEach(AnnotationTypeMapping::afterAllMappingsSet); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void addAllMappings(Class<? extends Annotation> annotationType) { |
|
|
|
|
private void addAllMappings(Class<? extends Annotation> annotationType, |
|
|
|
|
Set<Class<? extends Annotation>> visitedAnnotationTypes) { |
|
|
|
|
Deque<AnnotationTypeMapping> queue = new ArrayDeque<>(); |
|
|
|
|
addIfPossible(queue, null, annotationType, null); |
|
|
|
|
addIfPossible(queue, null, annotationType, null, visitedAnnotationTypes); |
|
|
|
|
while (!queue.isEmpty()) { |
|
|
|
|
AnnotationTypeMapping mapping = queue.removeFirst(); |
|
|
|
|
this.mappings.add(mapping); |
|
|
|
@ -102,14 +107,15 @@ final class AnnotationTypeMappings {
@@ -102,14 +107,15 @@ final class AnnotationTypeMappings {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void addIfPossible(Deque<AnnotationTypeMapping> queue, AnnotationTypeMapping source, Annotation ann) { |
|
|
|
|
addIfPossible(queue, source, ann.annotationType(), ann); |
|
|
|
|
addIfPossible(queue, source, ann.annotationType(), ann, new HashSet<>()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void addIfPossible(Deque<AnnotationTypeMapping> queue, @Nullable AnnotationTypeMapping source, |
|
|
|
|
Class<? extends Annotation> annotationType, @Nullable Annotation ann) { |
|
|
|
|
Class<? extends Annotation> annotationType, @Nullable Annotation ann, |
|
|
|
|
Set<Class<? extends Annotation>> visitedAnnotationTypes) { |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
queue.addLast(new AnnotationTypeMapping(source, annotationType, ann)); |
|
|
|
|
queue.addLast(new AnnotationTypeMapping(source, annotationType, ann, visitedAnnotationTypes)); |
|
|
|
|
} |
|
|
|
|
catch (Exception ex) { |
|
|
|
|
AnnotationUtils.rethrowAnnotationConfigurationException(ex); |
|
|
|
@ -166,20 +172,37 @@ final class AnnotationTypeMappings {
@@ -166,20 +172,37 @@ final class AnnotationTypeMappings {
|
|
|
|
|
* @return type mappings for the annotation type |
|
|
|
|
*/ |
|
|
|
|
static AnnotationTypeMappings forAnnotationType(Class<? extends Annotation> annotationType) { |
|
|
|
|
return forAnnotationType(annotationType, AnnotationFilter.PLAIN); |
|
|
|
|
return forAnnotationType(annotationType, new HashSet<>()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Create {@link AnnotationTypeMappings} for the specified annotation type. |
|
|
|
|
* @param annotationType the source annotation type |
|
|
|
|
* @param visitedAnnotationTypes the set of annotations that we have already |
|
|
|
|
* visited; used to avoid infinite recursion for recursive annotations which |
|
|
|
|
* some JVM languages support (such as Kotlin) |
|
|
|
|
* @return type mappings for the annotation type |
|
|
|
|
*/ |
|
|
|
|
static AnnotationTypeMappings forAnnotationType(Class<? extends Annotation> annotationType, |
|
|
|
|
Set<Class<? extends Annotation>> visitedAnnotationTypes) { |
|
|
|
|
|
|
|
|
|
return forAnnotationType(annotationType, RepeatableContainers.standardRepeatables(), |
|
|
|
|
AnnotationFilter.PLAIN, visitedAnnotationTypes); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Create {@link AnnotationTypeMappings} for the specified annotation type. |
|
|
|
|
* @param annotationType the source annotation type |
|
|
|
|
* @param repeatableContainers the repeatable containers that may be used by |
|
|
|
|
* the meta-annotations |
|
|
|
|
* @param annotationFilter the annotation filter used to limit which |
|
|
|
|
* annotations are considered |
|
|
|
|
* @return type mappings for the annotation type |
|
|
|
|
*/ |
|
|
|
|
static AnnotationTypeMappings forAnnotationType( |
|
|
|
|
Class<? extends Annotation> annotationType, AnnotationFilter annotationFilter) { |
|
|
|
|
static AnnotationTypeMappings forAnnotationType(Class<? extends Annotation> annotationType, |
|
|
|
|
RepeatableContainers repeatableContainers, AnnotationFilter annotationFilter) { |
|
|
|
|
|
|
|
|
|
return forAnnotationType(annotationType, RepeatableContainers.standardRepeatables(), annotationFilter); |
|
|
|
|
return forAnnotationType(annotationType, repeatableContainers, annotationFilter, new HashSet<>()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -189,20 +212,25 @@ final class AnnotationTypeMappings {
@@ -189,20 +212,25 @@ final class AnnotationTypeMappings {
|
|
|
|
|
* the meta-annotations |
|
|
|
|
* @param annotationFilter the annotation filter used to limit which |
|
|
|
|
* annotations are considered |
|
|
|
|
* @param visitedAnnotationTypes the set of annotations that we have already |
|
|
|
|
* visited; used to avoid infinite recursion for recursive annotations which |
|
|
|
|
* some JVM languages support (such as Kotlin) |
|
|
|
|
* @return type mappings for the annotation type |
|
|
|
|
*/ |
|
|
|
|
static AnnotationTypeMappings forAnnotationType(Class<? extends Annotation> annotationType, |
|
|
|
|
RepeatableContainers repeatableContainers, AnnotationFilter annotationFilter) { |
|
|
|
|
RepeatableContainers repeatableContainers, AnnotationFilter annotationFilter, |
|
|
|
|
Set<Class<? extends Annotation>> visitedAnnotationTypes) { |
|
|
|
|
|
|
|
|
|
if (repeatableContainers == RepeatableContainers.standardRepeatables()) { |
|
|
|
|
return standardRepeatablesCache.computeIfAbsent(annotationFilter, |
|
|
|
|
key -> new Cache(repeatableContainers, key)).get(annotationType); |
|
|
|
|
key -> new Cache(repeatableContainers, key)).get(annotationType, visitedAnnotationTypes); |
|
|
|
|
} |
|
|
|
|
if (repeatableContainers == RepeatableContainers.none()) { |
|
|
|
|
return noRepeatablesCache.computeIfAbsent(annotationFilter, |
|
|
|
|
key -> new Cache(repeatableContainers, key)).get(annotationType); |
|
|
|
|
key -> new Cache(repeatableContainers, key)).get(annotationType, visitedAnnotationTypes); |
|
|
|
|
} |
|
|
|
|
return new AnnotationTypeMappings(repeatableContainers, annotationFilter, annotationType); |
|
|
|
|
return new AnnotationTypeMappings(repeatableContainers, annotationFilter, annotationType, |
|
|
|
|
visitedAnnotationTypes); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void clearCache() { |
|
|
|
@ -235,14 +263,20 @@ final class AnnotationTypeMappings {
@@ -235,14 +263,20 @@ final class AnnotationTypeMappings {
|
|
|
|
|
/** |
|
|
|
|
* Get or create {@link AnnotationTypeMappings} for the specified annotation type. |
|
|
|
|
* @param annotationType the annotation type |
|
|
|
|
* @param visitedAnnotationTypes the set of annotations that we have already |
|
|
|
|
* visited; used to avoid infinite recursion for recursive annotations which |
|
|
|
|
* some JVM languages support (such as Kotlin) |
|
|
|
|
* @return a new or existing {@link AnnotationTypeMappings} instance |
|
|
|
|
*/ |
|
|
|
|
AnnotationTypeMappings get(Class<? extends Annotation> annotationType) { |
|
|
|
|
return this.mappings.computeIfAbsent(annotationType, this::createMappings); |
|
|
|
|
AnnotationTypeMappings get(Class<? extends Annotation> annotationType, |
|
|
|
|
Set<Class<? extends Annotation>> visitedAnnotationTypes) { |
|
|
|
|
return this.mappings.computeIfAbsent(annotationType, key -> createMappings(key, visitedAnnotationTypes)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
AnnotationTypeMappings createMappings(Class<? extends Annotation> annotationType) { |
|
|
|
|
return new AnnotationTypeMappings(this.repeatableContainers, this.filter, annotationType); |
|
|
|
|
private AnnotationTypeMappings createMappings(Class<? extends Annotation> annotationType, |
|
|
|
|
Set<Class<? extends Annotation>> visitedAnnotationTypes) { |
|
|
|
|
return new AnnotationTypeMappings(this.repeatableContainers, this.filter, annotationType, |
|
|
|
|
visitedAnnotationTypes); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|