Skip to content

Commit

Permalink
Refine instance supplier check in BeanDefinitionMethodGenerator
Browse files Browse the repository at this point in the history
This commit refines the instance supplier check in
BeanDefinitionMethodGenerator constructor in order to allow
overriding by an AOT contribution.

Closes gh-29556
  • Loading branch information
sdeleuze committed Feb 15, 2023
1 parent 9a4df5a commit ac521a3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class BeanDefinitionMethodGenerator {
List<BeanRegistrationAotContribution> aotContributions) {

RootBeanDefinition mbd = registeredBean.getMergedBeanDefinition();
if (mbd.getInstanceSupplier() != null) {
if (mbd.getInstanceSupplier() != null && aotContributions.isEmpty()) {
throw new IllegalArgumentException("Code generation is not supported for bean definitions declaring an instance supplier callback : " + mbd);
}
this.methodGeneratorFactory = methodGeneratorFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,23 @@ void generateBeanDefinitionMethodGeneratesMethod() {
});
}

@Test // gh-29556
void generateBeanDefinitionMethodGeneratesMethodWithInstanceSupplier() {
RegisteredBean registeredBean = registerBean(new RootBeanDefinition(TestBean.class, TestBean::new));
BeanDefinitionMethodGenerator generator = new BeanDefinitionMethodGenerator(
this.methodGeneratorFactory, registeredBean, null,
List.of((generationContext, beanRegistrationCode) -> { }));
MethodReference method = generator.generateBeanDefinitionMethod(
this.generationContext, this.beanRegistrationsCode);
compile(method, (actual, compiled) -> {
SourceFile sourceFile = compiled.getSourceFile(".*BeanDefinitions");
assertThat(sourceFile).contains("Get the bean definition for 'testBean'");
assertThat(sourceFile).contains("beanType = TestBean.class");
assertThat(sourceFile).contains("setInstanceSupplier(TestBean::new)");
assertThat(actual).isInstanceOf(RootBeanDefinition.class);
});
}

@Test
void generateBeanDefinitionMethodWhenHasInnerClassTargetMethodGeneratesMethod() {
this.beanFactory.registerBeanDefinition("testBeanConfiguration", new RootBeanDefinition(
Expand Down Expand Up @@ -493,8 +510,8 @@ void generateBeanDefinitionMethodWhenBeanIsOfPrimitiveType() {
testBeanDefinitionMethodInCurrentFile(Boolean.class, beanDefinition);
}

@Test
void throwExceptionWithInstanceSupplier() {
@Test // gh-29556
void throwExceptionWithInstanceSupplierWithoutAotContribution() {
RegisteredBean registeredBean = registerBean(new RootBeanDefinition(TestBean.class, TestBean::new));
assertThatIllegalArgumentException().isThrownBy(() -> new BeanDefinitionMethodGenerator(
this.methodGeneratorFactory, registeredBean, null,
Expand Down

0 comments on commit ac521a3

Please sign in to comment.