Skip to content

Commit fde7116

Browse files
committed
Consistently skip processing of plain Java annotations
Closes gh-33580
1 parent 0a64591 commit fde7116

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

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

+17-1
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,9 @@ protected boolean checkQualifiers(BeanDefinitionHolder bdHolder, Annotation[] an
180180
SimpleTypeConverter typeConverter = new SimpleTypeConverter();
181181
for (Annotation annotation : annotationsToSearch) {
182182
Class<? extends Annotation> type = annotation.annotationType();
183+
if (isPlainJavaAnnotation(type)) {
184+
continue;
185+
}
183186
boolean checkMeta = true;
184187
boolean fallbackToMeta = false;
185188
if (isQualifier(type)) {
@@ -194,6 +197,9 @@ protected boolean checkQualifiers(BeanDefinitionHolder bdHolder, Annotation[] an
194197
boolean foundMeta = false;
195198
for (Annotation metaAnn : type.getAnnotations()) {
196199
Class<? extends Annotation> metaType = metaAnn.annotationType();
200+
if (isPlainJavaAnnotation(metaType)) {
201+
continue;
202+
}
197203
if (isQualifier(metaType)) {
198204
foundMeta = true;
199205
// Only accept fallback match if @Qualifier annotation has a value...
@@ -213,7 +219,17 @@ protected boolean checkQualifiers(BeanDefinitionHolder bdHolder, Annotation[] an
213219
}
214220

215221
/**
216-
* Checks whether the given annotation type is a recognized qualifier type.
222+
* Check whether the given annotation type is a plain "java." annotation,
223+
* typically from {@code java.lang.annotation}.
224+
* <p>Aligned with
225+
* {@code org.springframework.core.annotation.AnnotationsScanner#hasPlainJavaAnnotationsOnly}.
226+
*/
227+
private boolean isPlainJavaAnnotation(Class<? extends Annotation> annotationType) {
228+
return annotationType.getName().startsWith("java.");
229+
}
230+
231+
/**
232+
* Check whether the given annotation type is a recognized qualifier type.
217233
*/
218234
protected boolean isQualifier(Class<? extends Annotation> annotationType) {
219235
for (Class<? extends Annotation> qualifierType : this.qualifierTypes) {

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

+7-10
Original file line numberDiff line numberDiff line change
@@ -103,26 +103,25 @@ private static <C, R> R processClass(C context, Class<?> source, SearchStrategy
103103

104104
return switch (searchStrategy) {
105105
case DIRECT -> processElement(context, source, processor);
106-
case INHERITED_ANNOTATIONS -> processClassInheritedAnnotations(context, source, searchStrategy, processor);
106+
case INHERITED_ANNOTATIONS -> processClassInheritedAnnotations(context, source, processor);
107107
case SUPERCLASS -> processClassHierarchy(context, source, processor, false, Search.never);
108108
case TYPE_HIERARCHY -> processClassHierarchy(context, source, processor, true, searchEnclosingClass);
109109
};
110110
}
111111

112112
@Nullable
113113
private static <C, R> R processClassInheritedAnnotations(C context, Class<?> source,
114-
SearchStrategy searchStrategy, AnnotationsProcessor<C, R> processor) {
114+
AnnotationsProcessor<C, R> processor) {
115115

116116
try {
117-
if (isWithoutHierarchy(source, searchStrategy, Search.never)) {
117+
if (isWithoutHierarchy(source, Search.never)) {
118118
return processElement(context, source, processor);
119119
}
120120
Annotation[] relevant = null;
121121
int remaining = Integer.MAX_VALUE;
122122
int aggregateIndex = 0;
123123
Class<?> root = source;
124-
while (source != null && source != Object.class && remaining > 0 &&
125-
!hasPlainJavaAnnotationsOnly(source)) {
124+
while (source != null && source != Object.class && remaining > 0 && !hasPlainJavaAnnotationsOnly(source)) {
126125
R result = processor.doWithAggregate(context, aggregateIndex);
127126
if (result != null) {
128127
return result;
@@ -483,7 +482,7 @@ static boolean isKnownEmpty(AnnotatedElement source, SearchStrategy searchStrate
483482
if (hasPlainJavaAnnotationsOnly(source)) {
484483
return true;
485484
}
486-
if (searchStrategy == SearchStrategy.DIRECT || isWithoutHierarchy(source, searchStrategy, searchEnclosingClass)) {
485+
if (searchStrategy == SearchStrategy.DIRECT || isWithoutHierarchy(source, searchEnclosingClass)) {
487486
if (source instanceof Method method && method.isBridge()) {
488487
return false;
489488
}
@@ -508,9 +507,7 @@ static boolean hasPlainJavaAnnotationsOnly(Class<?> type) {
508507
return (type.getName().startsWith("java.") || type == Ordered.class);
509508
}
510509

511-
private static boolean isWithoutHierarchy(AnnotatedElement source, SearchStrategy searchStrategy,
512-
Predicate<Class<?>> searchEnclosingClass) {
513-
510+
private static boolean isWithoutHierarchy(AnnotatedElement source, Predicate<Class<?>> searchEnclosingClass) {
514511
if (source == Object.class) {
515512
return true;
516513
}
@@ -522,7 +519,7 @@ private static boolean isWithoutHierarchy(AnnotatedElement source, SearchStrateg
522519
}
523520
if (source instanceof Method sourceMethod) {
524521
return (Modifier.isPrivate(sourceMethod.getModifiers()) ||
525-
isWithoutHierarchy(sourceMethod.getDeclaringClass(), searchStrategy, searchEnclosingClass));
522+
isWithoutHierarchy(sourceMethod.getDeclaringClass(), searchEnclosingClass));
526523
}
527524
return true;
528525
}

0 commit comments

Comments
 (0)