Skip to content

Commit

Permalink
Consistently skip processing of plain Java annotations
Browse files Browse the repository at this point in the history
Closes gh-33580
  • Loading branch information
jhoeller committed Oct 16, 2024
1 parent 0a64591 commit fde7116
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ protected boolean checkQualifiers(BeanDefinitionHolder bdHolder, Annotation[] an
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)) {
Expand All @@ -194,6 +197,9 @@ protected boolean checkQualifiers(BeanDefinitionHolder bdHolder, Annotation[] an
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...
Expand All @@ -213,7 +219,17 @@ protected boolean checkQualifiers(BeanDefinitionHolder bdHolder, Annotation[] an
}

/**
* 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,26 +103,25 @@ private static <C, R> R processClass(C context, Class<?> source, SearchStrategy

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);
};
}

@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;
Expand Down Expand Up @@ -483,7 +482,7 @@ static boolean isKnownEmpty(AnnotatedElement source, SearchStrategy searchStrate
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;
}
Expand All @@ -508,9 +507,7 @@ static boolean hasPlainJavaAnnotationsOnly(Class<?> type) {
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;
}
Expand All @@ -522,7 +519,7 @@ private static boolean isWithoutHierarchy(AnnotatedElement source, SearchStrateg
}
if (source instanceof Method sourceMethod) {
return (Modifier.isPrivate(sourceMethod.getModifiers()) ||
isWithoutHierarchy(sourceMethod.getDeclaringClass(), searchStrategy, searchEnclosingClass));
isWithoutHierarchy(sourceMethod.getDeclaringClass(), searchEnclosingClass));
}
return true;
}
Expand Down

0 comments on commit fde7116

Please sign in to comment.