Polish reflection hints on bean registration interfaces

This commit ensures that we register reflection hints on interfaces in
the entire type hierarchy, including extended interfaces.

Closes gh-31350
This commit is contained in:
Brian Clozel 2023-10-03 16:02:29 +02:00
parent 74fc8bd12d
commit 28c97610c2
2 changed files with 18 additions and 9 deletions

View File

@ -115,18 +115,23 @@ class BeanRegistrationsAotContribution
ReflectionHints hints = runtimeHints.reflection();
Class<?> beanClass = beanRegistrationKey.beanClass();
hints.registerType(beanClass, MemberCategory.INTROSPECT_PUBLIC_METHODS, MemberCategory.INTROSPECT_DECLARED_METHODS);
Class<?> currentClass = beanClass;
while (currentClass != null && currentClass != Object.class) {
for (Class<?> interfaceType : currentClass.getInterfaces()) {
if (!ClassUtils.isJavaLanguageInterface(interfaceType)) {
hints.registerType(interfaceType, MemberCategory.INTROSPECT_PUBLIC_METHODS);
}
}
currentClass = currentClass.getSuperclass();
}
introspectPublicMethodsOnAllInterfaces(hints, beanClass);
});
}
private void introspectPublicMethodsOnAllInterfaces(ReflectionHints hints, Class<?> type) {
Class<?> currentClass = type;
while (currentClass != null && currentClass != Object.class) {
for (Class<?> interfaceType : currentClass.getInterfaces()) {
if (!ClassUtils.isJavaLanguageInterface(interfaceType)) {
hints.registerType(interfaceType, MemberCategory.INTROSPECT_PUBLIC_METHODS);
introspectPublicMethodsOnAllInterfaces(hints, interfaceType);
}
}
currentClass = currentClass.getSuperclass();
}
}
/**
* Gather the necessary information to register a particular bean.
* @param methodGenerator the {@link BeanDefinitionMethodGenerator} to use

View File

@ -36,6 +36,7 @@ import org.springframework.beans.factory.aot.BeanRegistrationsAotContribution.Re
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RegisteredBean;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.testfixture.beans.AgeHolder;
import org.springframework.beans.testfixture.beans.Employee;
import org.springframework.beans.testfixture.beans.ITestBean;
import org.springframework.beans.testfixture.beans.TestBean;
@ -151,6 +152,9 @@ class BeanRegistrationsAotContributionTests {
assertThat(reflection().onType(ITestBean.class)
.withMemberCategory(MemberCategory.INTROSPECT_PUBLIC_METHODS))
.accepts(this.generationContext.getRuntimeHints());
assertThat(reflection().onType(AgeHolder.class)
.withMemberCategory(MemberCategory.INTROSPECT_PUBLIC_METHODS))
.accepts(this.generationContext.getRuntimeHints());
}
private RegisteredBean registerBean(RootBeanDefinition rootBeanDefinition) {