Consistently skip processing of plain Java annotations
Closes gh-33580
This commit is contained in:
parent
0a645916cd
commit
fde7116ae4
|
@ -180,6 +180,9 @@ public class QualifierAnnotationAutowireCandidateResolver extends GenericTypeAwa
|
|||
SimpleTypeConverter typeConverter = new SimpleTypeConverter();
|
||||
for (Annotation annotation : annotationsToSearch) {
|
||||
Class<? extends Annotation> type = annotation.annotationType();
|
||||
if (isPlainJavaAnnotation(type)) {
|
||||
continue;
|
||||
}
|
||||
boolean checkMeta = true;
|
||||
boolean fallbackToMeta = false;
|
||||
if (isQualifier(type)) {
|
||||
|
@ -194,6 +197,9 @@ public class QualifierAnnotationAutowireCandidateResolver extends GenericTypeAwa
|
|||
boolean foundMeta = false;
|
||||
for (Annotation metaAnn : type.getAnnotations()) {
|
||||
Class<? extends Annotation> metaType = metaAnn.annotationType();
|
||||
if (isPlainJavaAnnotation(metaType)) {
|
||||
continue;
|
||||
}
|
||||
if (isQualifier(metaType)) {
|
||||
foundMeta = true;
|
||||
// Only accept fallback match if @Qualifier annotation has a value...
|
||||
|
@ -213,7 +219,17 @@ public class QualifierAnnotationAutowireCandidateResolver extends GenericTypeAwa
|
|||
}
|
||||
|
||||
/**
|
||||
* Checks whether the given annotation type is a recognized qualifier type.
|
||||
* Check whether the given annotation type is a plain "java." annotation,
|
||||
* typically from {@code java.lang.annotation}.
|
||||
* <p>Aligned with
|
||||
* {@code org.springframework.core.annotation.AnnotationsScanner#hasPlainJavaAnnotationsOnly}.
|
||||
*/
|
||||
private boolean isPlainJavaAnnotation(Class<? extends Annotation> annotationType) {
|
||||
return annotationType.getName().startsWith("java.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether the given annotation type is a recognized qualifier type.
|
||||
*/
|
||||
protected boolean isQualifier(Class<? extends Annotation> annotationType) {
|
||||
for (Class<? extends Annotation> qualifierType : this.qualifierTypes) {
|
||||
|
|
|
@ -103,7 +103,7 @@ abstract class AnnotationsScanner {
|
|||
|
||||
return switch (searchStrategy) {
|
||||
case DIRECT -> processElement(context, source, processor);
|
||||
case INHERITED_ANNOTATIONS -> processClassInheritedAnnotations(context, source, searchStrategy, processor);
|
||||
case INHERITED_ANNOTATIONS -> processClassInheritedAnnotations(context, source, processor);
|
||||
case SUPERCLASS -> processClassHierarchy(context, source, processor, false, Search.never);
|
||||
case TYPE_HIERARCHY -> processClassHierarchy(context, source, processor, true, searchEnclosingClass);
|
||||
};
|
||||
|
@ -111,18 +111,17 @@ abstract class AnnotationsScanner {
|
|||
|
||||
@Nullable
|
||||
private static <C, R> R processClassInheritedAnnotations(C context, Class<?> source,
|
||||
SearchStrategy searchStrategy, AnnotationsProcessor<C, R> processor) {
|
||||
AnnotationsProcessor<C, R> processor) {
|
||||
|
||||
try {
|
||||
if (isWithoutHierarchy(source, searchStrategy, Search.never)) {
|
||||
if (isWithoutHierarchy(source, Search.never)) {
|
||||
return processElement(context, source, processor);
|
||||
}
|
||||
Annotation[] relevant = null;
|
||||
int remaining = Integer.MAX_VALUE;
|
||||
int aggregateIndex = 0;
|
||||
Class<?> root = source;
|
||||
while (source != null && source != Object.class && remaining > 0 &&
|
||||
!hasPlainJavaAnnotationsOnly(source)) {
|
||||
while (source != null && source != Object.class && remaining > 0 && !hasPlainJavaAnnotationsOnly(source)) {
|
||||
R result = processor.doWithAggregate(context, aggregateIndex);
|
||||
if (result != null) {
|
||||
return result;
|
||||
|
@ -483,7 +482,7 @@ abstract class AnnotationsScanner {
|
|||
if (hasPlainJavaAnnotationsOnly(source)) {
|
||||
return true;
|
||||
}
|
||||
if (searchStrategy == SearchStrategy.DIRECT || isWithoutHierarchy(source, searchStrategy, searchEnclosingClass)) {
|
||||
if (searchStrategy == SearchStrategy.DIRECT || isWithoutHierarchy(source, searchEnclosingClass)) {
|
||||
if (source instanceof Method method && method.isBridge()) {
|
||||
return false;
|
||||
}
|
||||
|
@ -508,9 +507,7 @@ abstract class AnnotationsScanner {
|
|||
return (type.getName().startsWith("java.") || type == Ordered.class);
|
||||
}
|
||||
|
||||
private static boolean isWithoutHierarchy(AnnotatedElement source, SearchStrategy searchStrategy,
|
||||
Predicate<Class<?>> searchEnclosingClass) {
|
||||
|
||||
private static boolean isWithoutHierarchy(AnnotatedElement source, Predicate<Class<?>> searchEnclosingClass) {
|
||||
if (source == Object.class) {
|
||||
return true;
|
||||
}
|
||||
|
@ -522,7 +519,7 @@ abstract class AnnotationsScanner {
|
|||
}
|
||||
if (source instanceof Method sourceMethod) {
|
||||
return (Modifier.isPrivate(sourceMethod.getModifiers()) ||
|
||||
isWithoutHierarchy(sourceMethod.getDeclaringClass(), searchStrategy, searchEnclosingClass));
|
||||
isWithoutHierarchy(sourceMethod.getDeclaringClass(), searchEnclosingClass));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue