Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
*
* @author Phillip Webb
* @author Sam Brannen
* @author Greg Taube
* @since 5.2
* @see AnnotationTypeMapping
*/
Expand Down Expand Up @@ -178,7 +179,7 @@ AnnotationTypeMapping get(int index) {
* @return type mappings for the annotation type
*/
static AnnotationTypeMappings forAnnotationType(Class<? extends Annotation> annotationType) {
return forAnnotationType(annotationType, new HashSet<>());
return forAnnotationType(annotationType, RepeatableContainers.standardRepeatables(), AnnotationFilter.PLAIN);
}

/**
Expand Down Expand Up @@ -208,7 +209,11 @@ static AnnotationTypeMappings forAnnotationType(Class<? extends Annotation> anno
static AnnotationTypeMappings forAnnotationType(Class<? extends Annotation> annotationType,
RepeatableContainers repeatableContainers, AnnotationFilter annotationFilter) {

return forAnnotationType(annotationType, repeatableContainers, annotationFilter, new HashSet<>());
Cache cache = getCache(repeatableContainers, annotationFilter);
if (cache != null) {
return cache.get(annotationType);
}
return new AnnotationTypeMappings(repeatableContainers, annotationFilter, annotationType, new HashSet<>());
}

/**
Expand All @@ -227,16 +232,26 @@ static AnnotationTypeMappings forAnnotationType(Class<? extends Annotation> anno
RepeatableContainers repeatableContainers, AnnotationFilter annotationFilter,
Set<Class<? extends Annotation>> visitedAnnotationTypes) {

Cache cache = getCache(repeatableContainers, annotationFilter);
if (cache != null) {
return cache.get(annotationType, visitedAnnotationTypes);
}
return new AnnotationTypeMappings(repeatableContainers, annotationFilter, annotationType,
visitedAnnotationTypes);
}

private static @Nullable Cache getCache(
RepeatableContainers repeatableContainers, AnnotationFilter annotationFilter) {

if (repeatableContainers == RepeatableContainers.standardRepeatables()) {
return standardRepeatablesCache.computeIfAbsent(annotationFilter,
key -> new Cache(repeatableContainers, key)).get(annotationType, visitedAnnotationTypes);
key -> new Cache(repeatableContainers, key));
}
if (repeatableContainers == RepeatableContainers.none()) {
return noRepeatablesCache.computeIfAbsent(annotationFilter,
key -> new Cache(repeatableContainers, key)).get(annotationType, visitedAnnotationTypes);
key -> new Cache(repeatableContainers, key));
}
return new AnnotationTypeMappings(repeatableContainers, annotationFilter, annotationType,
visitedAnnotationTypes);
return null;
}

static void clearCache() {
Expand Down Expand Up @@ -277,6 +292,17 @@ private static class Cache {
AnnotationTypeMappings get(Class<? extends Annotation> annotationType,
Set<Class<? extends Annotation>> visitedAnnotationTypes) {

return getOrCreate(annotationType, visitedAnnotationTypes);
}

AnnotationTypeMappings get(Class<? extends Annotation> annotationType) {
AnnotationTypeMappings result = this.mappings.get(annotationType);
return (result != null ? result : getOrCreate(annotationType, new HashSet<>()));
}

private AnnotationTypeMappings getOrCreate(Class<? extends Annotation> annotationType,
Set<Class<? extends Annotation>> visitedAnnotationTypes) {

AnnotationTypeMappings result = this.mappings.get(annotationType);
if (result != null) {
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@
*/
class AnnotationTypeMappingsTests {

@Test
void forAnnotationTypeWhenCalledTwiceReturnsCachedInstance() {
AnnotationTypeMappings first = AnnotationTypeMappings.forAnnotationType(SimpleAnnotation.class);
AnnotationTypeMappings second = AnnotationTypeMappings.forAnnotationType(SimpleAnnotation.class);
assertThat(second).isSameAs(first);
}

@Test
void forAnnotationTypeWhenNoMetaAnnotationsReturnsMappings() {
AnnotationTypeMappings mappings = AnnotationTypeMappings.forAnnotationType(SimpleAnnotation.class);
Expand Down