Move custom code fragments requirements at the right place

This commit moves the check on bean definitions having an instance
supplier where they are actually used.

Closes gh-31200
This commit is contained in:
Stephane Nicoll
2023-09-11 17:13:04 +02:00
parent 66a571fe27
commit bf5ce82254
3 changed files with 88 additions and 27 deletions

View File

@@ -27,7 +27,6 @@ import org.springframework.aot.generate.GenerationContext;
import org.springframework.aot.generate.MethodReference;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.RegisteredBean;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.javapoet.ClassName;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
@@ -66,10 +65,6 @@ class BeanDefinitionMethodGenerator {
RegisteredBean registeredBean, @Nullable String currentPropertyName,
List<BeanRegistrationAotContribution> aotContributions) {
RootBeanDefinition mbd = registeredBean.getMergedBeanDefinition();
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;
this.registeredBean = registeredBean;
this.currentPropertyName = currentPropertyName;

View File

@@ -73,6 +73,10 @@ class DefaultBeanRegistrationCodeFragments implements BeanRegistrationCodeFragme
@Override
public ClassName getTarget(RegisteredBean registeredBean) {
if (hasInstanceSupplier()) {
throw new IllegalStateException("Default code generation is not supported for bean definitions "
+ "declaring an instance supplier callback: " + registeredBean.getMergedBeanDefinition());
}
Class<?> target = extractDeclaringClass(registeredBean.getBeanType(), this.constructorOrFactoryMethod.get());
while (target.getName().startsWith("java.") && registeredBean.isInnerBean()) {
RegisteredBean parent = registeredBean.getParent();
@@ -224,7 +228,10 @@ class DefaultBeanRegistrationCodeFragments implements BeanRegistrationCodeFragme
@Override
public CodeBlock generateInstanceSupplierCode(GenerationContext generationContext,
BeanRegistrationCode beanRegistrationCode, boolean allowDirectSupplierShortcut) {
if (hasInstanceSupplier()) {
throw new IllegalStateException("Default code generation is not supported for bean definitions declaring "
+ "an instance supplier callback: " + this.registeredBean.getMergedBeanDefinition());
}
return new InstanceSupplierCodeGenerator(generationContext,
beanRegistrationCode.getClassName(), beanRegistrationCode.getMethods(), allowDirectSupplierShortcut)
.generateCode(this.registeredBean,this.constructorOrFactoryMethod.get());
@@ -239,4 +246,8 @@ class DefaultBeanRegistrationCodeFragments implements BeanRegistrationCodeFragme
return code.build();
}
private boolean hasInstanceSupplier() {
return this.registeredBean.getMergedBeanDefinition().getInstanceSupplier() != null;
}
}