Contribute introspection hints on registered beans
Prior to this commit, reflection hints registered for beans was selectively applied to only consider the methods that we'll actually need reflection on at runtime. This would rely on an undocumented behavior of GraalVM Native where calling `getDeclaredMethods` on a type would only return known metadata at runtime, ignoring the ones that were not registered during native compilation. As of oracle/graal#5171, this behavior is now fixed in GraalVM and aligns with the JVM behavior: all methods will be returned. This means that if during native compilation, introspection was not registered for the type a new `MissingReflectionMetadataException` will be raised. As a follow up of #29205, this commit contributes the "introspection on declared method" reflection hint for all registered beans. Closes gh-29246
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -31,6 +31,8 @@ import org.springframework.aot.generate.ClassNameGenerator;
|
||||
import org.springframework.aot.generate.GenerationContext;
|
||||
import org.springframework.aot.generate.MethodReference;
|
||||
import org.springframework.aot.generate.MethodReference.ArgumentCodeGenerator;
|
||||
import org.springframework.aot.hint.MemberCategory;
|
||||
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
|
||||
import org.springframework.aot.test.generate.TestGenerationContext;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.beans.factory.support.RegisteredBean;
|
||||
@@ -55,6 +57,7 @@ import static org.springframework.beans.factory.aot.BeanRegistrationsAotContribu
|
||||
* @author Phillip Webb
|
||||
* @author Sebastien Deleuze
|
||||
* @author Stephane Nicoll
|
||||
* @author Brian Clozel
|
||||
*/
|
||||
class BeanRegistrationsAotContributionTests {
|
||||
|
||||
@@ -153,6 +156,21 @@ class BeanRegistrationsAotContributionTests {
|
||||
assertThat(actual.getMethods()).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void applyToRegisterReflectionHints() {
|
||||
List<BeanRegistrationsCode> beanRegistrationsCodes = new ArrayList<>();
|
||||
RegisteredBean registeredBean = registerBean(
|
||||
new RootBeanDefinition(TestBean.class));
|
||||
BeanDefinitionMethodGenerator generator = new BeanDefinitionMethodGenerator(
|
||||
this.methodGeneratorFactory, registeredBean, null,
|
||||
Collections.emptyList());
|
||||
BeanRegistrationsAotContribution contribution = createContribution(generator);
|
||||
contribution.applyTo(this.generationContext, this.beanFactoryInitializationCode);
|
||||
assertThat(RuntimeHintsPredicates.reflection().onType(TestBean.class)
|
||||
.withMemberCategory(MemberCategory.INTROSPECT_DECLARED_METHODS))
|
||||
.accepts(this.generationContext.getRuntimeHints());
|
||||
}
|
||||
|
||||
private RegisteredBean registerBean(RootBeanDefinition rootBeanDefinition) {
|
||||
String beanName = "testBean";
|
||||
this.beanFactory.registerBeanDefinition(beanName, rootBeanDefinition);
|
||||
@@ -186,7 +204,7 @@ class BeanRegistrationsAotContributionTests {
|
||||
|
||||
private BeanRegistrationsAotContribution createContribution(
|
||||
BeanDefinitionMethodGenerator methodGenerator,String... aliases) {
|
||||
return new BeanRegistrationsAotContribution(Map.of("testBean", new Registration(methodGenerator, aliases)));
|
||||
return new BeanRegistrationsAotContribution(Map.of(new BeanRegistrationKey("testBean", TestBean.class), new Registration(methodGenerator, aliases)));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ class BeanRegistrationsAotProcessorTests {
|
||||
BeanRegistrationsAotContribution contribution = processor
|
||||
.processAheadOfTime(beanFactory);
|
||||
assertThat(contribution).extracting("registrations")
|
||||
.asInstanceOf(InstanceOfAssertFactories.MAP).containsKeys("b1", "b2");
|
||||
.asInstanceOf(InstanceOfAssertFactories.MAP).hasSize(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -63,7 +63,7 @@ class BeanRegistrationsAotProcessorTests {
|
||||
BeanRegistrationsAotContribution contribution = processor
|
||||
.processAheadOfTime(beanFactory);
|
||||
assertThat(contribution).extracting("registrations").asInstanceOf(InstanceOfAssertFactories.MAP)
|
||||
.hasEntrySatisfying("test", registration ->
|
||||
.hasEntrySatisfying(new BeanRegistrationKey("test", TestBean.class), registration ->
|
||||
assertThat(registration).extracting("aliases").asInstanceOf(InstanceOfAssertFactories.ARRAY)
|
||||
.singleElement().isEqualTo("testAlias"));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user