Skip to content

Commit 81acbe7

Browse files
committed
Ensure AnnotationTypeMapping properly tracks convention-based mappings
AnnotationTypeMapping.addConventionMappings() sometimes adds convention-based mappings that it should not. For example, in certain circumstances an explicit annotation attribute override configured via @AliasFor can be incorrectly mapped as convention-based. Although this does not appear to cause negative side effects (other than unnecessary processing), this is technically a bug, and this commit address this issue. Closes gh-28773
1 parent 5397d47 commit 81acbe7

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -272,7 +272,7 @@ private void addConventionMappings() {
272272
for (int i = 0; i < mappings.length; i++) {
273273
String name = this.attributes.get(i).getName();
274274
int mapped = rootAttributes.indexOf(name);
275-
if (!MergedAnnotation.VALUE.equals(name) && mapped != -1) {
275+
if (!MergedAnnotation.VALUE.equals(name) && mapped != -1 && !isExplicitAttributeOverride(name)) {
276276
mappings[i] = mapped;
277277
MirrorSet mirrors = getMirrorSets().getAssigned(i);
278278
if (mirrors != null) {
@@ -284,6 +284,27 @@ private void addConventionMappings() {
284284
}
285285
}
286286

287+
/**
288+
* Determine if the given annotation attribute in the {@linkplain #getRoot()
289+
* root annotation} is an explicit annotation attribute override for an
290+
* attribute in a meta-annotation, explicit in the sense that the override
291+
* is declared via {@link AliasFor @AliasFor}.
292+
* <p>If the named attribute does not exist in the root annotation, this
293+
* method returns {@code false}.
294+
* @param name the name of the annotation attribute to check
295+
* @since 6.0
296+
*/
297+
private boolean isExplicitAttributeOverride(String name) {
298+
Method attribute = this.root.getAttributes().get(name);
299+
if (attribute != null) {
300+
AliasFor aliasFor = AnnotationsScanner.getDeclaredAnnotation(attribute, AliasFor.class);
301+
return ((aliasFor != null) &&
302+
(aliasFor.annotation() != Annotation.class) &&
303+
(aliasFor.annotation() != this.root.annotationType));
304+
}
305+
return false;
306+
}
307+
287308
private void addConventionAnnotationValues() {
288309
for (int i = 0; i < this.attributes.size(); i++) {
289310
Method attribute = this.attributes.get(i);

0 commit comments

Comments
 (0)