Skip to content

Commit

Permalink
Revert "Support recursive annotations in merged annotations"
Browse files Browse the repository at this point in the history
This reverts commit 3ec612a.
  • Loading branch information
poutsma committed Jun 14, 2022
1 parent df37e33 commit bf9f261
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 279 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2020 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.
Expand Down Expand Up @@ -82,8 +82,8 @@ final class AnnotationTypeMapping {
private final Set<Method> claimedAliases = new HashSet<>();


AnnotationTypeMapping(@Nullable AnnotationTypeMapping source, Class<? extends Annotation> annotationType,
@Nullable Annotation annotation, Set<Class<? extends Annotation>> visitedAnnotationTypes) {
AnnotationTypeMapping(@Nullable AnnotationTypeMapping source,
Class<? extends Annotation> annotationType, @Nullable Annotation annotation) {

this.source = source;
this.root = (source != null ? source.getRoot() : this);
Expand All @@ -103,7 +103,7 @@ final class AnnotationTypeMapping {
processAliases();
addConventionMappings();
addConventionAnnotationValues();
this.synthesizable = computeSynthesizableFlag(visitedAnnotationTypes);
this.synthesizable = computeSynthesizableFlag();
}


Expand Down Expand Up @@ -311,10 +311,7 @@ private boolean isBetterConventionAnnotationValue(int index, boolean isValueAttr
}

@SuppressWarnings("unchecked")
private boolean computeSynthesizableFlag(Set<Class<? extends Annotation>> visitedAnnotationTypes) {
// Track that we have visited the current annotation type.
visitedAnnotationTypes.add(this.annotationType);

private boolean computeSynthesizableFlag() {
// Uses @AliasFor for local aliases?
for (int index : this.aliasMappings) {
if (index != -1) {
Expand Down Expand Up @@ -343,15 +340,9 @@ private boolean computeSynthesizableFlag(Set<Class<? extends Annotation>> visite
if (type.isAnnotation() || (type.isArray() && type.getComponentType().isAnnotation())) {
Class<? extends Annotation> annotationType =
(Class<? extends Annotation>) (type.isAnnotation() ? type : type.getComponentType());
// Ensure we have not yet visited the current nested annotation type, in order
// to avoid infinite recursion for JVM languages other than Java that support
// recursive annotation definitions.
if (visitedAnnotationTypes.add(annotationType)) {
AnnotationTypeMapping mapping =
AnnotationTypeMappings.forAnnotationType(annotationType, visitedAnnotationTypes).get(0);
if (mapping.isSynthesizable()) {
return true;
}
AnnotationTypeMapping mapping = AnnotationTypeMappings.forAnnotationType(annotationType).get(0);
if (mapping.isSynthesizable()) {
return true;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2020 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.
Expand All @@ -20,10 +20,8 @@
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;
Expand All @@ -42,7 +40,6 @@
* be searched once, regardless of how many times they are actually used.
*
* @author Phillip Webb
* @author Sam Brannen
* @since 5.2
* @see AnnotationTypeMapping
*/
Expand All @@ -63,22 +60,19 @@ final class AnnotationTypeMappings {


private AnnotationTypeMappings(RepeatableContainers repeatableContainers,
AnnotationFilter filter, Class<? extends Annotation> annotationType,
Set<Class<? extends Annotation>> visitedAnnotationTypes) {
AnnotationFilter filter, Class<? extends Annotation> annotationType) {

this.repeatableContainers = repeatableContainers;
this.filter = filter;
this.mappings = new ArrayList<>();
addAllMappings(annotationType, visitedAnnotationTypes);
addAllMappings(annotationType);
this.mappings.forEach(AnnotationTypeMapping::afterAllMappingsSet);
}


private void addAllMappings(Class<? extends Annotation> annotationType,
Set<Class<? extends Annotation>> visitedAnnotationTypes) {

private void addAllMappings(Class<? extends Annotation> annotationType) {
Deque<AnnotationTypeMapping> queue = new ArrayDeque<>();
addIfPossible(queue, null, annotationType, null, visitedAnnotationTypes);
addIfPossible(queue, null, annotationType, null);
while (!queue.isEmpty()) {
AnnotationTypeMapping mapping = queue.removeFirst();
this.mappings.add(mapping);
Expand Down Expand Up @@ -108,15 +102,14 @@ private void addMetaAnnotationsToQueue(Deque<AnnotationTypeMapping> queue, Annot
}

private void addIfPossible(Deque<AnnotationTypeMapping> queue, AnnotationTypeMapping source, Annotation ann) {
addIfPossible(queue, source, ann.annotationType(), ann, new HashSet<>());
addIfPossible(queue, source, ann.annotationType(), ann);
}

private void addIfPossible(Deque<AnnotationTypeMapping> queue, @Nullable AnnotationTypeMapping source,
Class<? extends Annotation> annotationType, @Nullable Annotation ann,
Set<Class<? extends Annotation>> visitedAnnotationTypes) {
Class<? extends Annotation> annotationType, @Nullable Annotation ann) {

try {
queue.addLast(new AnnotationTypeMapping(source, annotationType, ann, visitedAnnotationTypes));
queue.addLast(new AnnotationTypeMapping(source, annotationType, ann));
}
catch (Exception ex) {
AnnotationUtils.rethrowAnnotationConfigurationException(ex);
Expand Down Expand Up @@ -173,37 +166,20 @@ AnnotationTypeMapping get(int index) {
* @return type mappings for the annotation type
*/
static AnnotationTypeMappings forAnnotationType(Class<? extends Annotation> annotationType) {
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);
return forAnnotationType(annotationType, AnnotationFilter.PLAIN);
}

/**
* 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,
RepeatableContainers repeatableContainers, AnnotationFilter annotationFilter) {
static AnnotationTypeMappings forAnnotationType(
Class<? extends Annotation> annotationType, AnnotationFilter annotationFilter) {

return forAnnotationType(annotationType, repeatableContainers, annotationFilter, new HashSet<>());
return forAnnotationType(annotationType, RepeatableContainers.standardRepeatables(), annotationFilter);
}

/**
Expand All @@ -213,24 +189,20 @@ static AnnotationTypeMappings forAnnotationType(Class<? extends Annotation> anno
* 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
*/
private static AnnotationTypeMappings forAnnotationType(Class<? extends Annotation> annotationType,
RepeatableContainers repeatableContainers, AnnotationFilter annotationFilter,
Set<Class<? extends Annotation>> visitedAnnotationTypes) {
static AnnotationTypeMappings forAnnotationType(Class<? extends Annotation> annotationType,
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)).get(annotationType);
}
if (repeatableContainers == RepeatableContainers.none()) {
return noRepeatablesCache.computeIfAbsent(annotationFilter,
key -> new Cache(repeatableContainers, key)).get(annotationType, visitedAnnotationTypes);
key -> new Cache(repeatableContainers, key)).get(annotationType);
}
return new AnnotationTypeMappings(repeatableContainers, annotationFilter, annotationType, visitedAnnotationTypes);
return new AnnotationTypeMappings(repeatableContainers, annotationFilter, annotationType);
}

static void clearCache() {
Expand Down Expand Up @@ -263,21 +235,14 @@ private static class Cache {
/**
* 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,
Set<Class<? extends Annotation>> visitedAnnotationTypes) {

return this.mappings.computeIfAbsent(annotationType, key -> createMappings(key, visitedAnnotationTypes));
AnnotationTypeMappings get(Class<? extends Annotation> annotationType) {
return this.mappings.computeIfAbsent(annotationType, this::createMappings);
}

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

return new AnnotationTypeMappings(this.repeatableContainers, this.filter, annotationType, visitedAnnotationTypes);
AnnotationTypeMappings createMappings(Class<? extends Annotation> annotationType) {
return new AnnotationTypeMappings(this.repeatableContainers, this.filter, annotationType);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2020 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.
Expand Down Expand Up @@ -80,7 +80,7 @@ void forAnnotationTypeWhenHasRepeatingMetaAnnotationReturnsMapping() {
@Test
void forAnnotationTypeWhenRepeatableMetaAnnotationIsFiltered() {
AnnotationTypeMappings mappings = AnnotationTypeMappings.forAnnotationType(WithRepeatedMetaAnnotations.class,
RepeatableContainers.standardRepeatables(), Repeating.class.getName()::equals);
Repeating.class.getName()::equals);
assertThat(getAll(mappings)).flatExtracting(AnnotationTypeMapping::getAnnotationType)
.containsExactly(WithRepeatedMetaAnnotations.class);
}
Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit bf9f261

Please sign in to comment.