Consistently skip processing of plain Java annotations

Closes gh-33580
This commit is contained in:
Juergen Hoeller 2024-10-16 17:17:22 +02:00
parent 0a645916cd
commit fde7116ae4
2 changed files with 24 additions and 11 deletions

View File

@ -180,6 +180,9 @@ public class QualifierAnnotationAutowireCandidateResolver extends GenericTypeAwa
SimpleTypeConverter typeConverter = new SimpleTypeConverter(); SimpleTypeConverter typeConverter = new SimpleTypeConverter();
for (Annotation annotation : annotationsToSearch) { for (Annotation annotation : annotationsToSearch) {
Class<? extends Annotation> type = annotation.annotationType(); Class<? extends Annotation> type = annotation.annotationType();
if (isPlainJavaAnnotation(type)) {
continue;
}
boolean checkMeta = true; boolean checkMeta = true;
boolean fallbackToMeta = false; boolean fallbackToMeta = false;
if (isQualifier(type)) { if (isQualifier(type)) {
@ -194,6 +197,9 @@ public class QualifierAnnotationAutowireCandidateResolver extends GenericTypeAwa
boolean foundMeta = false; boolean foundMeta = false;
for (Annotation metaAnn : type.getAnnotations()) { for (Annotation metaAnn : type.getAnnotations()) {
Class<? extends Annotation> metaType = metaAnn.annotationType(); Class<? extends Annotation> metaType = metaAnn.annotationType();
if (isPlainJavaAnnotation(metaType)) {
continue;
}
if (isQualifier(metaType)) { if (isQualifier(metaType)) {
foundMeta = true; foundMeta = true;
// Only accept fallback match if @Qualifier annotation has a value... // 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) { protected boolean isQualifier(Class<? extends Annotation> annotationType) {
for (Class<? extends Annotation> qualifierType : this.qualifierTypes) { for (Class<? extends Annotation> qualifierType : this.qualifierTypes) {

View File

@ -103,7 +103,7 @@ abstract class AnnotationsScanner {
return switch (searchStrategy) { return switch (searchStrategy) {
case DIRECT -> processElement(context, source, processor); 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 SUPERCLASS -> processClassHierarchy(context, source, processor, false, Search.never);
case TYPE_HIERARCHY -> processClassHierarchy(context, source, processor, true, searchEnclosingClass); case TYPE_HIERARCHY -> processClassHierarchy(context, source, processor, true, searchEnclosingClass);
}; };
@ -111,18 +111,17 @@ abstract class AnnotationsScanner {
@Nullable @Nullable
private static <C, R> R processClassInheritedAnnotations(C context, Class<?> source, private static <C, R> R processClassInheritedAnnotations(C context, Class<?> source,
SearchStrategy searchStrategy, AnnotationsProcessor<C, R> processor) { AnnotationsProcessor<C, R> processor) {
try { try {
if (isWithoutHierarchy(source, searchStrategy, Search.never)) { if (isWithoutHierarchy(source, Search.never)) {
return processElement(context, source, processor); return processElement(context, source, processor);
} }
Annotation[] relevant = null; Annotation[] relevant = null;
int remaining = Integer.MAX_VALUE; int remaining = Integer.MAX_VALUE;
int aggregateIndex = 0; int aggregateIndex = 0;
Class<?> root = source; Class<?> root = source;
while (source != null && source != Object.class && remaining > 0 && while (source != null && source != Object.class && remaining > 0 && !hasPlainJavaAnnotationsOnly(source)) {
!hasPlainJavaAnnotationsOnly(source)) {
R result = processor.doWithAggregate(context, aggregateIndex); R result = processor.doWithAggregate(context, aggregateIndex);
if (result != null) { if (result != null) {
return result; return result;
@ -483,7 +482,7 @@ abstract class AnnotationsScanner {
if (hasPlainJavaAnnotationsOnly(source)) { if (hasPlainJavaAnnotationsOnly(source)) {
return true; return true;
} }
if (searchStrategy == SearchStrategy.DIRECT || isWithoutHierarchy(source, searchStrategy, searchEnclosingClass)) { if (searchStrategy == SearchStrategy.DIRECT || isWithoutHierarchy(source, searchEnclosingClass)) {
if (source instanceof Method method && method.isBridge()) { if (source instanceof Method method && method.isBridge()) {
return false; return false;
} }
@ -508,9 +507,7 @@ abstract class AnnotationsScanner {
return (type.getName().startsWith("java.") || type == Ordered.class); return (type.getName().startsWith("java.") || type == Ordered.class);
} }
private static boolean isWithoutHierarchy(AnnotatedElement source, SearchStrategy searchStrategy, private static boolean isWithoutHierarchy(AnnotatedElement source, Predicate<Class<?>> searchEnclosingClass) {
Predicate<Class<?>> searchEnclosingClass) {
if (source == Object.class) { if (source == Object.class) {
return true; return true;
} }
@ -522,7 +519,7 @@ abstract class AnnotationsScanner {
} }
if (source instanceof Method sourceMethod) { if (source instanceof Method sourceMethod) {
return (Modifier.isPrivate(sourceMethod.getModifiers()) || return (Modifier.isPrivate(sourceMethod.getModifiers()) ||
isWithoutHierarchy(sourceMethod.getDeclaringClass(), searchStrategy, searchEnclosingClass)); isWithoutHierarchy(sourceMethod.getDeclaringClass(), searchEnclosingClass));
} }
return true; return true;
} }