Skip to content

Commit

Permalink
Track only qualifier annotations in BeanOverrideHandler
Browse files Browse the repository at this point in the history
Closes gh-34260
  • Loading branch information
sbrannen committed Jan 14, 2025
1 parent 5a68d1f commit 3c3b8c7
Showing 1 changed file with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
Expand Down Expand Up @@ -72,7 +71,7 @@ public abstract class BeanOverrideHandler {

private final Field field;

private final Set<Annotation> fieldAnnotations;
private final Set<Annotation> qualifierAnnotations;

private final ResolvableType beanType;

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

this.field = field;
this.fieldAnnotations = annotationSet(field);
this.qualifierAnnotations = getQualifierAnnotations(field);
this.beanType = beanType;
this.beanName = beanName;
this.strategy = strategy;
Expand Down Expand Up @@ -251,14 +250,14 @@ public boolean equals(Object other) {

// by-type lookup
return (Objects.equals(this.field.getName(), that.field.getName()) &&
this.fieldAnnotations.equals(that.fieldAnnotations));
this.qualifierAnnotations.equals(that.qualifierAnnotations));
}

@Override
public int hashCode() {
int hash = Objects.hash(getClass(), this.beanType.getType(), this.beanName, this.strategy);
return (this.beanName != null ? hash : hash +
Objects.hash(this.field.getName(), this.fieldAnnotations));
Objects.hash(this.field.getName(), this.qualifierAnnotations));
}

@Override
Expand All @@ -271,9 +270,24 @@ public String toString() {
.toString();
}

private static Set<Annotation> annotationSet(Field field) {
Annotation[] annotations = field.getAnnotations();
return (annotations.length != 0 ? new HashSet<>(Arrays.asList(annotations)) : Collections.emptySet());

private static Set<Annotation> getQualifierAnnotations(Field field) {
Annotation[] candidates = field.getDeclaredAnnotations();
if (candidates.length == 0) {
return Collections.emptySet();
}
Set<Annotation> annotations = new HashSet<>(candidates.length - 1);
for (Annotation candidate : candidates) {
// Assume all non-BeanOverride annotations are "qualifiers".
if (!isBeanOverrideAnnotation(candidate.annotationType())) {
annotations.add(candidate);
}
}
return annotations;
}

private static boolean isBeanOverrideAnnotation(Class<? extends Annotation> type) {
return MergedAnnotations.from(type).isPresent(BeanOverride.class);
}

}

0 comments on commit 3c3b8c7

Please sign in to comment.