Skip to content

Commit 210b178

Browse files
committed
Restore retrieval of plain annotations through direct presence checks
Includes deprecation of several AnnotationUtils methods and nullability refinements for passed-in function arguments at the MergedAnnotation API level... also, MergedAnnotation.getType() returns a Class now. Closes gh-22663 Closes gh-22685
1 parent f7a4850 commit 210b178

File tree

16 files changed

+431
-553
lines changed

16 files changed

+431
-553
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -521,9 +521,10 @@ private MergedAnnotation<?> findAutowiredAnnotation(AccessibleObject ao) {
521521
* @param ann the Autowired annotation
522522
* @return whether the annotation indicates that a dependency is required
523523
*/
524+
@SuppressWarnings("deprecation")
524525
protected boolean determineRequiredStatus(MergedAnnotation<?> ann) {
525-
return determineRequiredStatus(
526-
ann.asMap(mergedAnnotation -> new AnnotationAttributes()));
526+
return determineRequiredStatus((AnnotationAttributes)
527+
ann.asMap(mergedAnnotation -> new AnnotationAttributes(mergedAnnotation.getType())));
527528
}
528529

529530
/**
@@ -533,7 +534,7 @@ protected boolean determineRequiredStatus(MergedAnnotation<?> ann) {
533534
* or method when no beans are found.
534535
* @param ann the Autowired annotation
535536
* @return whether the annotation indicates that a dependency is required
536-
* @deprecated since 5.2 in favor of {@link #determineRequiredStatus(MergedAnnotation)}
537+
* @deprecated since 5.2, in favor of {@link #determineRequiredStatus(MergedAnnotation)}
537538
*/
538539
@Deprecated
539540
protected boolean determineRequiredStatus(AnnotationAttributes ann) {

spring-core/src/main/java/org/springframework/core/annotation/AbstractMergedAnnotation.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.lang.annotation.Annotation;
2020
import java.lang.reflect.Array;
21-
import java.util.Map;
2221
import java.util.NoSuchElementException;
2322
import java.util.Optional;
2423
import java.util.function.Predicate;
@@ -30,6 +29,7 @@
3029
* Abstract base class for {@link MergedAnnotation} implementations.
3130
*
3231
* @author Phillip Webb
32+
* @author Juergen Hoeller
3333
* @since 5.2
3434
* @param <A> the annotation type
3535
*/
@@ -167,19 +167,10 @@ public MergedAnnotation<A> filterDefaultValues() {
167167
}
168168

169169
@Override
170-
public Map<String, Object> asMap(MapValues... options) {
171-
return asMap(null, options);
172-
}
173-
174-
@Override
175-
public Optional<A> synthesize(
176-
@Nullable Predicate<? super MergedAnnotation<A>> condition)
170+
public Optional<A> synthesize(Predicate<? super MergedAnnotation<A>> condition)
177171
throws NoSuchElementException {
178172

179-
if (condition == null || condition.test(this)) {
180-
return Optional.of(synthesize());
181-
}
182-
return Optional.empty();
173+
return (condition.test(this) ? Optional.of(synthesize()) : Optional.empty());
183174
}
184175

185176
@Override
@@ -199,7 +190,7 @@ private <T> T getRequiredAttributeValue(String attributeName, Class<T> type) {
199190
T value = getAttributeValue(attributeName, type);
200191
if (value == null) {
201192
throw new NoSuchElementException("No attribute named '" + attributeName +
202-
"' present in merged annotation " + getType());
193+
"' present in merged annotation " + getType().getName());
203194
}
204195
return value;
205196
}

spring-core/src/main/java/org/springframework/core/annotation/AnnotatedElementUtils.java

Lines changed: 62 additions & 85 deletions
Large diffs are not rendered by default.

spring-core/src/main/java/org/springframework/core/annotation/AnnotationFilter.java

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@
1717
package org.springframework.core.annotation;
1818

1919
import java.lang.annotation.Annotation;
20-
import java.util.Arrays;
21-
import java.util.Collection;
22-
23-
import org.springframework.util.Assert;
2420

2521
/**
2622
* Callback interface that can be used to filter specific annotation types.
@@ -96,52 +92,4 @@ static AnnotationFilter packages(String... packages) {
9692
return new PackagesAnnotationFilter(packages);
9793
}
9894

99-
/**
100-
* Return an {@link AnnotationFilter} that is the most appropriate for, and
101-
* will always match the given annotation type. Whenever possible,
102-
* {@link AnnotationFilter#PLAIN} will be returned.
103-
* @param annotationType the annotation type to check
104-
* @return the most appropriate annotation filter
105-
*/
106-
static AnnotationFilter mostAppropriateFor(Class<?> annotationType) {
107-
return (PLAIN.matches(annotationType) ? NONE : PLAIN);
108-
}
109-
110-
/**
111-
* Return an {@link AnnotationFilter} that is the most appropriate for, and
112-
* will always match all the given annotation types. Whenever possible,
113-
* {@link AnnotationFilter#PLAIN} will be returned.
114-
* @param annotationTypes the annotation types to check
115-
* @return the most appropriate annotation filter
116-
*/
117-
static AnnotationFilter mostAppropriateFor(Class<?>... annotationTypes) {
118-
return mostAppropriateFor(Arrays.asList(annotationTypes));
119-
}
120-
121-
/**
122-
* Return an {@link AnnotationFilter} that is the most appropriate for, and
123-
* will always match all the given annotation types. Whenever possible,
124-
* {@link AnnotationFilter#PLAIN} will be returned.
125-
* @param annotationTypes the annotation types to check (may be class names
126-
* or class types)
127-
* @return the most appropriate annotation filter
128-
*/
129-
@SuppressWarnings("unchecked")
130-
static AnnotationFilter mostAppropriateFor(Collection<?> annotationTypes) {
131-
for (Object annotationType : annotationTypes) {
132-
if (annotationType == null) {
133-
continue;
134-
}
135-
Assert.isTrue(annotationType instanceof Class || annotationType instanceof String,
136-
"AnnotationType must be a Class or String");
137-
if (annotationType instanceof Class && PLAIN.matches((Class<Annotation>) annotationType)) {
138-
return NONE;
139-
}
140-
if (annotationType instanceof String && PLAIN.matches((String) annotationType)) {
141-
return NONE;
142-
}
143-
}
144-
return PLAIN;
145-
}
146-
14795
}

spring-core/src/main/java/org/springframework/core/annotation/AnnotationTypeMappings.java

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ final class AnnotationTypeMappings {
5555
private final List<AnnotationTypeMapping> mappings;
5656

5757

58-
private AnnotationTypeMappings(AnnotationFilter filter,
59-
Class<? extends Annotation> annotationType) {
58+
private AnnotationTypeMappings(AnnotationFilter filter, Class<? extends Annotation> annotationType) {
6059
this.filter = filter;
6160
this.mappings = new ArrayList<>();
6261
addAllMappings(annotationType);
@@ -97,27 +96,23 @@ private void addMetaAnnotationsToQueue(Deque<AnnotationTypeMapping> queue, Annot
9796
}
9897
}
9998

100-
private void addIfPossible(Deque<AnnotationTypeMapping> queue,
101-
AnnotationTypeMapping parent, Annotation annotation) {
102-
addIfPossible(queue, parent, annotation.annotationType(), annotation);
99+
private void addIfPossible(Deque<AnnotationTypeMapping> queue, AnnotationTypeMapping parent, Annotation ann) {
100+
addIfPossible(queue, parent, ann.annotationType(), ann);
103101
}
104102

105-
private void addIfPossible(Deque<AnnotationTypeMapping> queue,
106-
@Nullable AnnotationTypeMapping parent,
107-
Class<? extends Annotation> annotationType, @Nullable Annotation annotation) {
103+
private void addIfPossible(Deque<AnnotationTypeMapping> queue, @Nullable AnnotationTypeMapping parent,
104+
Class<? extends Annotation> annotationType, @Nullable Annotation ann) {
108105

109106
try {
110-
queue.addLast(new AnnotationTypeMapping(parent, annotationType, annotation));
107+
queue.addLast(new AnnotationTypeMapping(parent, annotationType, ann));
111108
}
112109
catch (Exception ex) {
113110
if (ex instanceof AnnotationConfigurationException) {
114111
throw (AnnotationConfigurationException) ex;
115112
}
116113
if (failureLogger.isEnabled()) {
117-
failureLogger.log(
118-
"Failed to introspect meta-annotation "
119-
+ annotationType.getName(),
120-
(parent != null) ? parent.getAnnotationType() : null, ex);
114+
failureLogger.log("Failed to introspect meta-annotation " + annotationType.getName(),
115+
(parent != null ? parent.getAnnotationType() : null), ex);
121116
}
122117
}
123118
}
@@ -167,7 +162,7 @@ AnnotationTypeMapping get(int index) {
167162
* @return type mappings for the annotation type
168163
*/
169164
static AnnotationTypeMappings forAnnotationType(Class<? extends Annotation> annotationType) {
170-
return forAnnotationType(annotationType, AnnotationFilter.mostAppropriateFor(annotationType));
165+
return forAnnotationType(annotationType, AnnotationFilter.PLAIN);
171166
}
172167

173168
/**
@@ -197,7 +192,6 @@ private static class Cache {
197192

198193
private final Map<Class<? extends Annotation>, AnnotationTypeMappings> mappings;
199194

200-
201195
/**
202196
* Create a cache instance with the specified filter.
203197
* @param filter the annotation filter
@@ -207,7 +201,6 @@ private static class Cache {
207201
this.mappings = new ConcurrentReferenceHashMap<>();
208202
}
209203

210-
211204
/**
212205
* Return or create {@link AnnotationTypeMappings} for the specified
213206
* annotation type.
@@ -218,8 +211,7 @@ AnnotationTypeMappings get(Class<? extends Annotation> annotationType) {
218211
return this.mappings.computeIfAbsent(annotationType, this::createMappings);
219212
}
220213

221-
AnnotationTypeMappings createMappings(
222-
Class<? extends Annotation> annotationType) {
214+
AnnotationTypeMappings createMappings(Class<? extends Annotation> annotationType) {
223215
return new AnnotationTypeMappings(this.filter, annotationType);
224216
}
225217

0 commit comments

Comments
 (0)