Skip to content

Commit

Permalink
Polish reflection hints on bean registration interfaces
Browse files Browse the repository at this point in the history
This commit ensures that we register reflection hints on interfaces in
the entire type hierarchy, including extended interfaces.

Closes gh-31350
  • Loading branch information
bclozel committed Oct 3, 2023
1 parent 74fc8bd commit 28c9761
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,21 @@ private void generateRegisterHints(RuntimeHints runtimeHints, Map<BeanRegistrati
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);
}
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();
}
});
currentClass = currentClass.getSuperclass();
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
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;
Expand Down Expand Up @@ -151,6 +152,9 @@ void applyToRegisterReflectionHints() {
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) {
Expand Down

0 comments on commit 28c9761

Please sign in to comment.