Skip to content

Commit 3c3b8c7

Browse files
committed
Track only qualifier annotations in BeanOverrideHandler
Closes gh-34260
1 parent 5a68d1f commit 3c3b8c7

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

spring-test/src/main/java/org/springframework/test/context/bean/override/BeanOverrideHandler.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.lang.annotation.Annotation;
2020
import java.lang.reflect.Field;
2121
import java.lang.reflect.Modifier;
22-
import java.util.Arrays;
2322
import java.util.Collections;
2423
import java.util.HashSet;
2524
import java.util.LinkedList;
@@ -72,7 +71,7 @@ public abstract class BeanOverrideHandler {
7271

7372
private final Field field;
7473

75-
private final Set<Annotation> fieldAnnotations;
74+
private final Set<Annotation> qualifierAnnotations;
7675

7776
private final ResolvableType beanType;
7877

@@ -86,7 +85,7 @@ protected BeanOverrideHandler(Field field, ResolvableType beanType, @Nullable St
8685
BeanOverrideStrategy strategy) {
8786

8887
this.field = field;
89-
this.fieldAnnotations = annotationSet(field);
88+
this.qualifierAnnotations = getQualifierAnnotations(field);
9089
this.beanType = beanType;
9190
this.beanName = beanName;
9291
this.strategy = strategy;
@@ -251,14 +250,14 @@ public boolean equals(Object other) {
251250

252251
// by-type lookup
253252
return (Objects.equals(this.field.getName(), that.field.getName()) &&
254-
this.fieldAnnotations.equals(that.fieldAnnotations));
253+
this.qualifierAnnotations.equals(that.qualifierAnnotations));
255254
}
256255

257256
@Override
258257
public int hashCode() {
259258
int hash = Objects.hash(getClass(), this.beanType.getType(), this.beanName, this.strategy);
260259
return (this.beanName != null ? hash : hash +
261-
Objects.hash(this.field.getName(), this.fieldAnnotations));
260+
Objects.hash(this.field.getName(), this.qualifierAnnotations));
262261
}
263262

264263
@Override
@@ -271,9 +270,24 @@ public String toString() {
271270
.toString();
272271
}
273272

274-
private static Set<Annotation> annotationSet(Field field) {
275-
Annotation[] annotations = field.getAnnotations();
276-
return (annotations.length != 0 ? new HashSet<>(Arrays.asList(annotations)) : Collections.emptySet());
273+
274+
private static Set<Annotation> getQualifierAnnotations(Field field) {
275+
Annotation[] candidates = field.getDeclaredAnnotations();
276+
if (candidates.length == 0) {
277+
return Collections.emptySet();
278+
}
279+
Set<Annotation> annotations = new HashSet<>(candidates.length - 1);
280+
for (Annotation candidate : candidates) {
281+
// Assume all non-BeanOverride annotations are "qualifiers".
282+
if (!isBeanOverrideAnnotation(candidate.annotationType())) {
283+
annotations.add(candidate);
284+
}
285+
}
286+
return annotations;
287+
}
288+
289+
private static boolean isBeanOverrideAnnotation(Class<? extends Annotation> type) {
290+
return MergedAnnotations.from(type).isPresent(BeanOverride.class);
277291
}
278292

279293
}

0 commit comments

Comments
 (0)