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:
Brian Clozel 2023-03-29 18:09:33 +02:00
parent 7e905e3e00
commit b374824319
6 changed files with 72 additions and 17 deletions

View File

@ -0,0 +1,27 @@
/*
* Copyright 2002-2022 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.beans.factory.aot;
/**
* Record class holding key information for beans registered in a bean factory.
* @param beanName the name of the registered bean
* @param beanClass the type of the registered bean
* @author Brian Clozel
* @since 6.0
*/
record BeanRegistrationKey(String beanName, Class<?> beanClass) {
}

View File

@ -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.
@ -26,6 +26,8 @@ import org.springframework.aot.generate.GeneratedMethods;
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.RuntimeHints;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.javapoet.ClassName;
import org.springframework.javapoet.CodeBlock;
@ -38,6 +40,7 @@ import org.springframework.javapoet.MethodSpec;
* @author Phillip Webb
* @author Sebastien Deleuze
* @author Stephane Nicoll
* @author Brian Clozel
* @since 6.0
* @see BeanRegistrationsAotProcessor
*/
@ -46,9 +49,11 @@ class BeanRegistrationsAotContribution
private static final String BEAN_FACTORY_PARAMETER_NAME = "beanFactory";
private final Map<String, Registration> registrations;
private final Map<BeanRegistrationKey, Registration> registrations;
BeanRegistrationsAotContribution(Map<String, Registration> registrations) {
BeanRegistrationsAotContribution(
Map<BeanRegistrationKey, Registration> registrations) {
this.registrations = registrations;
}
@ -69,6 +74,7 @@ class BeanRegistrationsAotContribution
GeneratedMethod generatedAliasesMethod = codeGenerator.getMethods().add("registerAliases",
this::generateRegisterAliasesMethod);
beanFactoryInitializationCode.addInitializer(generatedAliasesMethod.toMethodReference());
generateRegisterHints(generationContext.getRuntimeHints(), this.registrations);
}
private void generateRegisterBeanDefinitionsMethod(MethodSpec.Builder method,
@ -80,14 +86,14 @@ class BeanRegistrationsAotContribution
method.addParameter(DefaultListableBeanFactory.class,
BEAN_FACTORY_PARAMETER_NAME);
CodeBlock.Builder code = CodeBlock.builder();
this.registrations.forEach((beanName, registration) -> {
this.registrations.forEach((registeredBean, registration) -> {
MethodReference beanDefinitionMethod = registration.methodGenerator
.generateBeanDefinitionMethod(generationContext,
beanRegistrationsCode);
CodeBlock methodInvocation = beanDefinitionMethod.toInvokeCodeBlock(
ArgumentCodeGenerator.none(), beanRegistrationsCode.getClassName());
code.addStatement("$L.registerBeanDefinition($S, $L)",
BEAN_FACTORY_PARAMETER_NAME, beanName,
BEAN_FACTORY_PARAMETER_NAME, registeredBean.beanName(),
methodInvocation);
});
method.addCode(code.build());
@ -99,15 +105,20 @@ class BeanRegistrationsAotContribution
method.addParameter(DefaultListableBeanFactory.class,
BEAN_FACTORY_PARAMETER_NAME);
CodeBlock.Builder code = CodeBlock.builder();
this.registrations.forEach((beanName, registration) -> {
this.registrations.forEach((registeredBean, registration) -> {
for (String alias : registration.aliases) {
code.addStatement("$L.registerAlias($S, $S)",
BEAN_FACTORY_PARAMETER_NAME, beanName, alias);
BEAN_FACTORY_PARAMETER_NAME, registeredBean.beanName(), alias);
}
});
method.addCode(code.build());
}
private void generateRegisterHints(RuntimeHints runtimeHints, Map<BeanRegistrationKey, Registration> registrations) {
registrations.keySet().forEach(beanRegistrationKey -> runtimeHints.reflection()
.registerType(beanRegistrationKey.beanClass(), MemberCategory.INTROSPECT_DECLARED_METHODS));
}
/**
* Gather the necessary information to register a particular bean.
* @param methodGenerator the {@link BeanDefinitionMethodGenerator} to use

View File

@ -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,7 @@ import org.springframework.lang.Nullable;
* @author Phillip Webb
* @author Sebastien Deleuze
* @author Stephane Nicoll
* @author Brian Clozel
* @since 6.0
*/
class BeanRegistrationsAotProcessor implements BeanFactoryInitializationAotProcessor {
@ -40,15 +41,15 @@ class BeanRegistrationsAotProcessor implements BeanFactoryInitializationAotProce
public BeanRegistrationsAotContribution processAheadOfTime(ConfigurableListableBeanFactory beanFactory) {
BeanDefinitionMethodGeneratorFactory beanDefinitionMethodGeneratorFactory =
new BeanDefinitionMethodGeneratorFactory(beanFactory);
Map<String, Registration> registrations = new LinkedHashMap<>();
Map<BeanRegistrationKey, Registration> registrations = new LinkedHashMap<>();
for (String beanName : beanFactory.getBeanDefinitionNames()) {
RegisteredBean registeredBean = RegisteredBean.of(beanFactory, beanName);
BeanDefinitionMethodGenerator beanDefinitionMethodGenerator =
beanDefinitionMethodGeneratorFactory.getBeanDefinitionMethodGenerator(registeredBean);
if (beanDefinitionMethodGenerator != null) {
registrations.put(beanName, new Registration(beanDefinitionMethodGenerator,
beanFactory.getAliases(beanName)));
registrations.put(new BeanRegistrationKey(beanName, registeredBean.getBeanClass()),
new Registration(beanDefinitionMethodGenerator, beanFactory.getAliases(beanName)));
}
}

View File

@ -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)));
}
}

View File

@ -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"));
}

View File

@ -18,7 +18,6 @@ package org.springframework.context.generator;
import java.util.function.BiConsumer;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.aot.hint.RuntimeHints;
@ -72,7 +71,6 @@ class ApplicationContextAotGeneratorRuntimeHintsTests {
}
@Test
@Disabled("until gh-29246 is re-applied")
void generateApplicationContextWithMultipleInitDestroyMethods() {
GenericApplicationContext context = new AnnotationConfigApplicationContext();
RootBeanDefinition beanDefinition = new RootBeanDefinition(InitDestroyComponent.class);