Support multiple @ImportRuntimeHints in the type hierarchy
This commit copies and adapts the logic from DefaultListableBeanFactory#findMergedAnnotationOnBean private method to make it suitable for returning multiple annotations found in the type hierarchy in order to support this use case with @ImportRuntimeHints. Closes gh-29361
This commit is contained in:
@@ -16,8 +16,12 @@
|
||||
|
||||
package org.springframework.context.aot;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@@ -32,8 +36,12 @@ import org.springframework.beans.factory.aot.AotServices;
|
||||
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotContribution;
|
||||
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotProcessor;
|
||||
import org.springframework.beans.factory.aot.BeanFactoryInitializationCode;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.context.annotation.ImportRuntimeHints;
|
||||
import org.springframework.core.annotation.MergedAnnotation;
|
||||
import org.springframework.core.annotation.MergedAnnotations;
|
||||
import org.springframework.core.log.LogMessage;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
@@ -45,6 +53,7 @@ import org.springframework.lang.Nullable;
|
||||
* classes or bean methods.
|
||||
*
|
||||
* @author Brian Clozel
|
||||
* @author Sebastien Deleuze
|
||||
*/
|
||||
class RuntimeHintsBeanFactoryInitializationAotProcessor implements BeanFactoryInitializationAotProcessor {
|
||||
|
||||
@@ -66,15 +75,50 @@ class RuntimeHintsBeanFactoryInitializationAotProcessor implements BeanFactoryIn
|
||||
Set<Class<? extends RuntimeHintsRegistrar>> registrarClasses = new LinkedHashSet<>();
|
||||
for (String beanName : beanFactory
|
||||
.getBeanNamesForAnnotation(ImportRuntimeHints.class)) {
|
||||
ImportRuntimeHints annotation = beanFactory.findAnnotationOnBean(beanName,
|
||||
ImportRuntimeHints.class);
|
||||
if (annotation != null) {
|
||||
registrarClasses.addAll(extractFromBeanDefinition(beanName, annotation));
|
||||
}
|
||||
findAnnotationsOnBean(beanFactory, beanName,
|
||||
ImportRuntimeHints.class).forEach(annotation ->
|
||||
registrarClasses.addAll(extractFromBeanDefinition(beanName, annotation)));
|
||||
}
|
||||
return registrarClasses;
|
||||
}
|
||||
|
||||
private <A extends Annotation> List<A> findAnnotationsOnBean(ConfigurableListableBeanFactory beanFactory,
|
||||
String beanName, Class<A> annotationType) {
|
||||
|
||||
List<A> annotations = new ArrayList<>();
|
||||
Class<?> beanType = beanFactory.getType(beanName, true);
|
||||
if (beanType != null) {
|
||||
MergedAnnotations.from(beanType, MergedAnnotations.SearchStrategy.TYPE_HIERARCHY)
|
||||
.stream(annotationType)
|
||||
.filter(MergedAnnotation::isPresent)
|
||||
.forEach(mergedAnnotation -> annotations.add(mergedAnnotation.synthesize()));
|
||||
}
|
||||
if (beanFactory.containsBeanDefinition(beanName)) {
|
||||
BeanDefinition bd = beanFactory.getBeanDefinition(beanName);
|
||||
if (bd instanceof RootBeanDefinition rbd) {
|
||||
// Check raw bean class, e.g. in case of a proxy.
|
||||
if (rbd.hasBeanClass() && rbd.getFactoryMethodName() == null) {
|
||||
Class<?> beanClass = rbd.getBeanClass();
|
||||
if (beanClass != beanType) {
|
||||
MergedAnnotations.from(beanClass, MergedAnnotations.SearchStrategy.TYPE_HIERARCHY)
|
||||
.stream(annotationType)
|
||||
.filter(MergedAnnotation::isPresent)
|
||||
.forEach(mergedAnnotation -> annotations.add(mergedAnnotation.synthesize()));
|
||||
}
|
||||
}
|
||||
// Check annotations declared on factory method, if any.
|
||||
Method factoryMethod = rbd.getResolvedFactoryMethod();
|
||||
if (factoryMethod != null) {
|
||||
MergedAnnotations.from(factoryMethod, MergedAnnotations.SearchStrategy.TYPE_HIERARCHY)
|
||||
.stream(annotationType)
|
||||
.filter(MergedAnnotation::isPresent)
|
||||
.forEach(mergedAnnotation -> annotations.add(mergedAnnotation.synthesize()));
|
||||
}
|
||||
}
|
||||
}
|
||||
return annotations;
|
||||
}
|
||||
|
||||
private Set<Class<? extends RuntimeHintsRegistrar>> extractFromBeanDefinition(String beanName,
|
||||
ImportRuntimeHints annotation) {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user