Browse Source

Add reflection hints for bean registration interfaces

Prior to this commit, the bean registration AOT contributions would
register introspection and invocation hints on both declared and public
methods for bean types. The bean introspection algorithm also looks at
default methods implemented by interfaces when collecting bean property
information.

This commit ensures that introspection hints are registered for all
implemented interfaces when registering beans.

Closes gh-31350
pull/31352/head
Brian Clozel 2 years ago
parent
commit
b832df6087
  1. 3
      spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanRegistrationsAotContribution.java
  2. 5
      spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanRegistrationsAotContributionTests.java

3
spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanRegistrationsAotContribution.java

@ -118,6 +118,9 @@ class BeanRegistrationsAotContribution
ReflectionHints hints = runtimeHints.reflection(); ReflectionHints hints = runtimeHints.reflection();
Class<?> beanClass = beanRegistrationKey.beanClass(); Class<?> beanClass = beanRegistrationKey.beanClass();
hints.registerType(beanClass, MemberCategory.INTROSPECT_PUBLIC_METHODS, MemberCategory.INTROSPECT_DECLARED_METHODS); hints.registerType(beanClass, MemberCategory.INTROSPECT_PUBLIC_METHODS, MemberCategory.INTROSPECT_DECLARED_METHODS);
for (Class<?> interfaceType : beanClass.getInterfaces()) {
hints.registerType(interfaceType, MemberCategory.INTROSPECT_PUBLIC_METHODS);
}
// Workaround for https://github.com/oracle/graal/issues/6510 // Workaround for https://github.com/oracle/graal/issues/6510
if (beanClass.isRecord()) { if (beanClass.isRecord()) {
hints.registerType(beanClass, MemberCategory.INVOKE_PUBLIC_METHODS, MemberCategory.INVOKE_DECLARED_METHODS); hints.registerType(beanClass, MemberCategory.INVOKE_PUBLIC_METHODS, MemberCategory.INVOKE_DECLARED_METHODS);

5
spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanRegistrationsAotContributionTests.java

@ -149,6 +149,11 @@ class BeanRegistrationsAotContributionTests {
assertThat(reflection().onType(TestBean.class) assertThat(reflection().onType(TestBean.class)
.withMemberCategories(MemberCategory.INTROSPECT_PUBLIC_METHODS, MemberCategory.INTROSPECT_DECLARED_METHODS)) .withMemberCategories(MemberCategory.INTROSPECT_PUBLIC_METHODS, MemberCategory.INTROSPECT_DECLARED_METHODS))
.accepts(this.generationContext.getRuntimeHints()); .accepts(this.generationContext.getRuntimeHints());
for (Class<?> interfaceType : TestBean.class.getInterfaces()) {
assertThat(reflection().onType(interfaceType)
.withMemberCategory(MemberCategory.INTROSPECT_PUBLIC_METHODS))
.accepts(this.generationContext.getRuntimeHints());
}
} }
@Test @Test

Loading…
Cancel
Save