This is a followup to #28896.

Comment From: sbrannen

A prototype for this issue can be viewed in the following feature branch.

https://github.com/spring-projects/spring-framework/compare/main...sbrannen:spring-framework:issues/gh-31360-find-type-use-annotations

Comment From: sbrannen

@janeisklar provided feedback on the original use case for this in https://github.com/spring-projects/spring-framework/pull/28896#issuecomment-1752003102.

In case it helps: I have been using this ~feature~bug to create beans in a @TestConfiguration class that needed to be annotated with some custom annotations.

It's not a feature that one's life depends on, but it can be quite handy.

However, as I pointed out in https://github.com/spring-projects/spring-framework/pull/28896#issuecomment-1752092794, it's also possible to achieve the same goal with a "local class" instead of an "anonymous class".

Comment From: sbrannen

As mentioned in the previous comment, a local class can be used instead of an anonymous class to achieve the same effect.

In addition, Spring Framework has never had explicit support for finding TYPE_USE annotations, and we do not plan to add such support.

In light of that, I am closing this issue.

However, if there is significant demand for this in the future, accompanied with compelling use cases, we may consider reopening this issue.


Since I may at some point delete my feature branch for this, I am including the proposed changes for AnnotationScanner.getDeclaredAnnotations(...) here so that they don't get lost.

            if (source instanceof Class<?> clazz && clazz.isAnonymousClass()) {
                // Find TYPE_USE annotations on an anonymous class.
                List<Annotation> annotationList = new ArrayList<>();
                AnnotatedType annotatedSuperclass = clazz.getAnnotatedSuperclass();
                if (annotatedSuperclass != null) {
                    Collections.addAll(annotationList, annotatedSuperclass.getDeclaredAnnotations());
                }
                for (AnnotatedType annotatedInterface : clazz.getAnnotatedInterfaces()) {
                    Collections.addAll(annotationList, annotatedInterface.getDeclaredAnnotations());
                }
                annotations = annotationList.toArray(Annotation[]::new);
            }
            else {
                // Find standard "declaration" annotations.
                annotations = source.getDeclaredAnnotations();
            }