diff --git a/spring-aop/src/main/java/org/springframework/aop/scope/ScopedProxyBeanRegistrationCodeFragmentsCustomizer.java b/spring-aop/src/main/java/org/springframework/aop/scope/ScopedProxyBeanRegistrationAotProcessor.java similarity index 82% rename from spring-aop/src/main/java/org/springframework/aop/scope/ScopedProxyBeanRegistrationCodeFragmentsCustomizer.java rename to spring-aop/src/main/java/org/springframework/aop/scope/ScopedProxyBeanRegistrationAotProcessor.java index 3a616cc9d4c..a86805dd3e7 100644 --- a/spring-aop/src/main/java/org/springframework/aop/scope/ScopedProxyBeanRegistrationCodeFragmentsCustomizer.java +++ b/spring-aop/src/main/java/org/springframework/aop/scope/ScopedProxyBeanRegistrationAotProcessor.java @@ -26,9 +26,10 @@ import org.apache.commons.logging.LogFactory; import org.springframework.aot.generate.GeneratedMethod; import org.springframework.aot.generate.GenerationContext; +import org.springframework.beans.factory.aot.BeanRegistrationAotContribution; +import org.springframework.beans.factory.aot.BeanRegistrationAotProcessor; import org.springframework.beans.factory.aot.BeanRegistrationCode; import org.springframework.beans.factory.aot.BeanRegistrationCodeFragments; -import org.springframework.beans.factory.aot.BeanRegistrationCodeFragmentsCustomizer; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.beans.factory.support.InstanceSupplier; @@ -39,39 +40,37 @@ import org.springframework.javapoet.CodeBlock; import org.springframework.lang.Nullable; /** - * {@link BeanRegistrationCodeFragmentsCustomizer} for - * {@link ScopedProxyFactoryBean}. + * {@link BeanRegistrationAotProcessor} for {@link ScopedProxyFactoryBean}. * * @author Stephane Nicoll * @author Phillip Webb * @since 6.0 */ -class ScopedProxyBeanRegistrationCodeFragmentsCustomizer - implements BeanRegistrationCodeFragmentsCustomizer { +class ScopedProxyBeanRegistrationAotProcessor + implements BeanRegistrationAotProcessor { private static final Log logger = LogFactory - .getLog(ScopedProxyBeanRegistrationCodeFragmentsCustomizer.class); + .getLog(ScopedProxyBeanRegistrationAotProcessor.class); @Override - public BeanRegistrationCodeFragments customizeBeanRegistrationCodeFragments( - RegisteredBean registeredBean, BeanRegistrationCodeFragments codeFragments) { - + public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) { Class beanType = registeredBean.getBeanType().toClass(); - if (!beanType.equals(ScopedProxyFactoryBean.class)) { - return codeFragments; - } - String targetBeanName = getTargetBeanName( - registeredBean.getMergedBeanDefinition()); - BeanDefinition targetBeanDefinition = getTargetBeanDefinition( - registeredBean.getBeanFactory(), targetBeanName); - if (targetBeanDefinition == null) { - logger.warn("Could not handle " + ScopedProxyFactoryBean.class.getSimpleName() - + ": no target bean definition found with name " + targetBeanName); - return codeFragments; + if (beanType.equals(ScopedProxyFactoryBean.class)) { + String targetBeanName = getTargetBeanName( + registeredBean.getMergedBeanDefinition()); + BeanDefinition targetBeanDefinition = getTargetBeanDefinition( + registeredBean.getBeanFactory(), targetBeanName); + if (targetBeanDefinition == null) { + logger.warn("Could not handle " + ScopedProxyFactoryBean.class.getSimpleName() + + ": no target bean definition found with name " + targetBeanName); + return null; + } + return BeanRegistrationAotContribution.ofBeanRegistrationCodeFragmentsCustomizer(codeFragments -> + new ScopedProxyBeanRegistrationCodeFragments(codeFragments, registeredBean, + targetBeanName, targetBeanDefinition)); } - return new ScopedProxyBeanRegistrationCodeFragments(codeFragments, registeredBean, - targetBeanName, targetBeanDefinition); + return null; } @Nullable diff --git a/spring-aop/src/main/resources/META-INF/spring/aot.factories b/spring-aop/src/main/resources/META-INF/spring/aot.factories index e71dbaf9cd3..44019be25b8 100644 --- a/spring-aop/src/main/resources/META-INF/spring/aot.factories +++ b/spring-aop/src/main/resources/META-INF/spring/aot.factories @@ -1,2 +1,2 @@ -org.springframework.beans.factory.aot.registration.BeanRegistrationCodeFragmentsCustomizer=\ -org.springframework.aop.scope.ScopedProxyBeanRegistrationCodeFragmentsCustomizer +org.springframework.beans.factory.aot.registration.BeanRegistrationAotProcessor=\ +org.springframework.aop.scope.ScopedProxyBeanRegistrationAotProcessor diff --git a/spring-aop/src/test/java/org/springframework/aop/scope/ScopedProxyBeanRegistrationCodeFragmentsCustomizerTests.java b/spring-aop/src/test/java/org/springframework/aop/scope/ScopedProxyBeanRegistrationAotProcessorTests.java similarity index 98% rename from spring-aop/src/test/java/org/springframework/aop/scope/ScopedProxyBeanRegistrationCodeFragmentsCustomizerTests.java rename to spring-aop/src/test/java/org/springframework/aop/scope/ScopedProxyBeanRegistrationAotProcessorTests.java index b0a4967b020..8f2147fb73d 100644 --- a/spring-aop/src/test/java/org/springframework/aop/scope/ScopedProxyBeanRegistrationCodeFragmentsCustomizerTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/scope/ScopedProxyBeanRegistrationAotProcessorTests.java @@ -50,13 +50,13 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** - * Tests for {@link ScopedProxyBeanRegistrationCodeFragmentsCustomizer}. + * Tests for {@link ScopedProxyBeanRegistrationAotProcessor}. * * @author Stephane Nicoll * @author Phillip Webb * @since 6.0 */ -class ScopedProxyBeanRegistrationCodeFragmentsCustomizerTests { +class ScopedProxyBeanRegistrationAotProcessorTests { private DefaultListableBeanFactory beanFactory; diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanDefinitionMethodGenerator.java b/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanDefinitionMethodGenerator.java index ad26b4a5a05..bc3105b0b8b 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanDefinitionMethodGenerator.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanDefinitionMethodGenerator.java @@ -56,8 +56,6 @@ class BeanDefinitionMethodGenerator { private final List aotContributions; - private final List codeFragmentsCustomizers; - /** * Create a new {@link BeanDefinitionMethodGenerator} instance. @@ -65,13 +63,11 @@ class BeanDefinitionMethodGenerator { * @param registeredBean the registered bean * @param innerBeanPropertyName the inner bean property name * @param aotContributions the AOT contributions - * @param codeFragmentsCustomizers the code fragments customizers */ BeanDefinitionMethodGenerator( BeanDefinitionMethodGeneratorFactory methodGeneratorFactory, RegisteredBean registeredBean, @Nullable String innerBeanPropertyName, - List aotContributions, - List codeFragmentsCustomizers) { + List aotContributions) { this.methodGeneratorFactory = methodGeneratorFactory; this.registeredBean = registeredBean; @@ -79,7 +75,6 @@ class BeanDefinitionMethodGenerator { .resolve(registeredBean); this.innerBeanPropertyName = innerBeanPropertyName; this.aotContributions = aotContributions; - this.codeFragmentsCustomizers = codeFragmentsCustomizers; } /** @@ -92,8 +87,7 @@ class BeanDefinitionMethodGenerator { MethodReference generateBeanDefinitionMethod(GenerationContext generationContext, BeanRegistrationsCode beanRegistrationsCode) { - BeanRegistrationCodeFragments codeFragments = getCodeFragments( - beanRegistrationsCode); + BeanRegistrationCodeFragments codeFragments = getCodeFragments(beanRegistrationsCode); Class target = codeFragments.getTarget(this.registeredBean, this.constructorOrFactoryMethod); if (!target.getName().startsWith("java.")) { @@ -115,7 +109,17 @@ class BeanDefinitionMethodGenerator { Modifier.PRIVATE); return MethodReference.ofStatic(beanRegistrationsCode.getClassName(), generatedMethod.getName().toString()); + } + + private BeanRegistrationCodeFragments getCodeFragments( + BeanRegistrationsCode beanRegistrationsCode) { + BeanRegistrationCodeFragments codeFragments = new DefaultBeanRegistrationCodeFragments( + beanRegistrationsCode, this.registeredBean, this.methodGeneratorFactory); + for (BeanRegistrationAotContribution aotContribution : this.aotContributions) { + codeFragments = aotContribution.customizeBeanRegistrationCodeFragments(codeFragments); + } + return codeFragments; } private GeneratedMethod generateBeanDefinitionMethod( @@ -126,8 +130,7 @@ class BeanDefinitionMethodGenerator { BeanRegistrationCodeGenerator codeGenerator = new BeanRegistrationCodeGenerator( className, methodGenerator, this.registeredBean, this.constructorOrFactoryMethod, codeFragments); - GeneratedMethod method = methodGenerator.generateMethod("get", "bean", - "definition"); + GeneratedMethod method = methodGenerator.generateMethod("get", "bean", "definition"); this.aotContributions.forEach(aotContribution -> aotContribution .applyTo(generationContext, codeGenerator)); return method.using(builder -> { @@ -140,18 +143,6 @@ class BeanDefinitionMethodGenerator { }); } - private BeanRegistrationCodeFragments getCodeFragments( - BeanRegistrationsCode beanRegistrationsCode) { - - BeanRegistrationCodeFragments codeFragments = new DefaultBeanRegistrationCodeFragments( - beanRegistrationsCode, this.registeredBean, this.methodGeneratorFactory); - for (BeanRegistrationCodeFragmentsCustomizer customizer : this.codeFragmentsCustomizers) { - codeFragments = customizer.customizeBeanRegistrationCodeFragments( - this.registeredBean, codeFragments); - } - return codeFragments; - } - private String getName() { if (this.innerBeanPropertyName != null) { return this.innerBeanPropertyName; diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanDefinitionMethodGeneratorFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanDefinitionMethodGeneratorFactory.java index c8961659b10..93467ff8ead 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanDefinitionMethodGeneratorFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanDefinitionMethodGeneratorFactory.java @@ -47,8 +47,6 @@ class BeanDefinitionMethodGeneratorFactory { private final List excludeFilters; - private final List codeGenerationCustomizers; - /** * Create a new {@link BeanDefinitionMethodGeneratorFactory} backed by the @@ -67,8 +65,6 @@ class BeanDefinitionMethodGeneratorFactory { BeanDefinitionMethodGeneratorFactory(AotFactoriesLoader loader) { this.aotProcessors = loader.load(BeanRegistrationAotProcessor.class); this.excludeFilters = loader.load(BeanRegistrationExcludeFilter.class); - this.codeGenerationCustomizers = loader - .load(BeanRegistrationCodeFragmentsCustomizer.class); } @@ -92,7 +88,7 @@ class BeanDefinitionMethodGeneratorFactory { List contributions = getAotContributions( registeredBean); return new BeanDefinitionMethodGenerator(this, registeredBean, - innerBeanPropertyName, contributions, this.codeGenerationCustomizers); + innerBeanPropertyName, contributions); } private boolean isExcluded(RegisteredBean registeredBean) { diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanRegistrationAotContribution.java b/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanRegistrationAotContribution.java index be02a4bbe5b..6c5a96cf118 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanRegistrationAotContribution.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanRegistrationAotContribution.java @@ -16,7 +16,10 @@ package org.springframework.beans.factory.aot; +import java.util.function.UnaryOperator; + import org.springframework.aot.generate.GenerationContext; +import org.springframework.util.Assert; /** * AOT contribution from a {@link BeanRegistrationAotProcessor} used to register @@ -29,6 +32,19 @@ import org.springframework.aot.generate.GenerationContext; @FunctionalInterface public interface BeanRegistrationAotContribution { + /** + * Customize the {@link BeanRegistrationCodeFragments} that will be used to + * generate the bean registration code. Custom code fragments can be used if + * default code generation isn't suitable. + * @param codeFragments the existing code fragments + * @return the code fragments to use, may be the original instance or a + * wrapper + */ + default BeanRegistrationCodeFragments customizeBeanRegistrationCodeFragments( + BeanRegistrationCodeFragments codeFragments) { + return codeFragments; + } + /** * Apply this contribution to the given {@link BeanRegistrationCode}. * @param generationContext the active generation context @@ -37,4 +53,33 @@ public interface BeanRegistrationAotContribution { void applyTo(GenerationContext generationContext, BeanRegistrationCode beanRegistrationCode); + /** + * Factory method that can be used to create a + * {@link BeanRegistrationAotContribution} that applies the given + * {@link BeanRegistrationCodeFragments} customizer. + * @param beanRegistrationCodeFragmentsCustomizer the + * {@link BeanRegistrationCodeFragments} customizer + * @return a new {@link BeanRegistrationAotContribution} instance + * @see #customizeBeanRegistrationCodeFragments(BeanRegistrationCodeFragments) + */ + static BeanRegistrationAotContribution ofBeanRegistrationCodeFragmentsCustomizer( + UnaryOperator beanRegistrationCodeFragmentsCustomizer) { + Assert.notNull(beanRegistrationCodeFragmentsCustomizer, + "BeanRegistrationCodeFragmentsCustomizer must not be null"); + return new BeanRegistrationAotContribution() { + + @Override + public BeanRegistrationCodeFragments customizeBeanRegistrationCodeFragments( + BeanRegistrationCodeFragments codeFragments) { + return beanRegistrationCodeFragmentsCustomizer.apply(codeFragments); + } + + @Override + public void applyTo(GenerationContext generationContext, + BeanRegistrationCode beanRegistrationCode) { + } + + }; + } + } diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanRegistrationCodeFragmentsCustomizer.java b/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanRegistrationCodeFragmentsCustomizer.java deleted file mode 100644 index d94ccd215d4..00000000000 --- a/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanRegistrationCodeFragmentsCustomizer.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * 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; - -import org.springframework.beans.factory.support.RegisteredBean; - -/** - * Strategy factory interface that can be used to customize the - * {@link BeanRegistrationCodeFragments} that us used for a given - * {@link RegisteredBean}. This interface can be used if default code generation - * isn't suitable for specific types of {@link RegisteredBean}. - * - * @author Phillip Webb - * @since 6.0 - */ -@FunctionalInterface -public interface BeanRegistrationCodeFragmentsCustomizer { - - /** - * Apply this {@link BeanRegistrationCodeFragmentsCustomizer} to the given - * {@link BeanRegistrationCodeFragments code fragments generator}. The - * returned code generator my be a - * {@link BeanRegistrationCodeFragmentsWrapper wrapper} around the original. - * @param registeredBean the registered bean - * @param codeFragments the existing code fragments - * @return the code generator to use, either the original or a wrapped one; - */ - BeanRegistrationCodeFragments customizeBeanRegistrationCodeFragments( - RegisteredBean registeredBean, BeanRegistrationCodeFragments codeFragments); - -} diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanRegistrationCodeGenerator.java b/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanRegistrationCodeGenerator.java index 2371751fbf8..b2d2fb6b028 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanRegistrationCodeGenerator.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanRegistrationCodeGenerator.java @@ -89,8 +89,7 @@ class BeanRegistrationCodeGenerator implements BeanRegistrationCode { CodeBlock instanceSupplierCode = this.codeFragments.generateInstanceSupplierCode( generationContext, this, this.constructorOrFactoryMethod, this.instancePostProcessors.isEmpty()); - builder.add( - this.codeFragments.generateSetBeanInstanceSupplierCode(generationContext, + builder.add(this.codeFragments.generateSetBeanInstanceSupplierCode(generationContext, this, instanceSupplierCode, this.instancePostProcessors)); builder.add(this.codeFragments.generateReturnCode(generationContext, this)); return builder.build(); diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanRegistrationAotContributionTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanRegistrationAotContributionTests.java index 252a7deccb9..eb1b1be7c28 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanRegistrationAotContributionTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanRegistrationAotContributionTests.java @@ -182,11 +182,6 @@ class AutowiredAnnotationBeanRegistrationAotContributionTests { private final List instancePostProcessors = new ArrayList<>(); - @Override - public void addInstancePostProcessor(MethodReference methodReference) { - this.instancePostProcessors.add(methodReference); - } - @Override public ClassName getClassName() { return null; @@ -197,6 +192,11 @@ class AutowiredAnnotationBeanRegistrationAotContributionTests { return null; } + @Override + public void addInstancePostProcessor(MethodReference methodReference) { + this.instancePostProcessors.add(methodReference); + } + } } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanDefinitionMethodGeneratorTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanDefinitionMethodGeneratorTests.java index 06c82db07bb..69f704daf46 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanDefinitionMethodGeneratorTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanDefinitionMethodGeneratorTests.java @@ -96,7 +96,7 @@ class BeanDefinitionMethodGeneratorTests { new RootBeanDefinition(TestBean.class)); BeanDefinitionMethodGenerator generator = new BeanDefinitionMethodGenerator( this.methodGeneratorFactory, registeredBean, null, - Collections.emptyList(), Collections.emptyList()); + Collections.emptyList()); MethodReference method = generator.generateBeanDefinitionMethod( this.generationContext, this.beanRegistrationsCode); testCompiledResult(method, (actual, compiled) -> { @@ -114,7 +114,7 @@ class BeanDefinitionMethodGeneratorTests { ResolvableType.forClassWithGenerics(GenericBean.class, Integer.class))); BeanDefinitionMethodGenerator generator = new BeanDefinitionMethodGenerator( this.methodGeneratorFactory, registeredBean, null, - Collections.emptyList(), Collections.emptyList()); + Collections.emptyList()); MethodReference method = generator.generateBeanDefinitionMethod( this.generationContext, this.beanRegistrationsCode); testCompiledResult(method, (actual, compiled) -> { @@ -147,8 +147,7 @@ class BeanDefinitionMethodGeneratorTests { List aotContributions = Collections .singletonList(aotContribution); BeanDefinitionMethodGenerator generator = new BeanDefinitionMethodGenerator( - this.methodGeneratorFactory, registeredBean, null, aotContributions, - Collections.emptyList()); + this.methodGeneratorFactory, registeredBean, null, aotContributions); MethodReference method = generator.generateBeanDefinitionMethod( this.generationContext, this.beanRegistrationsCode); testCompiledResult(method, (actual, compiled) -> { @@ -166,17 +165,55 @@ class BeanDefinitionMethodGeneratorTests { }); } + @Test + void generateBeanDefinitionMethodWhenHasCodeFragmentsCustomizerGeneratesMethod() { + RegisteredBean registeredBean = registerBean( + new RootBeanDefinition(TestBean.class)); + BeanRegistrationAotContribution aotContribution = BeanRegistrationAotContribution + .ofBeanRegistrationCodeFragmentsCustomizer(this::customizeBeanDefinitionCode); + List aotContributions = Collections.singletonList(aotContribution); + BeanDefinitionMethodGenerator generator = new BeanDefinitionMethodGenerator( + this.methodGeneratorFactory, registeredBean, null, aotContributions); + MethodReference method = generator.generateBeanDefinitionMethod( + this.generationContext, this.beanRegistrationsCode); + testCompiledResult(method, (actual, compiled) -> { + assertThat(actual.getBeanClass()).isEqualTo(TestBean.class); + SourceFile sourceFile = compiled.getSourceFile(".*BeanDefinitions"); + assertThat(sourceFile).contains("I am custom"); + }); + } + + private BeanRegistrationCodeFragments customizeBeanDefinitionCode( + BeanRegistrationCodeFragments codeFragments) { + return new BeanRegistrationCodeFragments(codeFragments) { + + @Override + public CodeBlock generateNewBeanDefinitionCode( + GenerationContext generationContext, + ResolvableType beanType, + BeanRegistrationCode beanRegistrationCode) { + CodeBlock.Builder builder = CodeBlock.builder(); + builder.addStatement("// I am custom"); + builder.add(super.generateNewBeanDefinitionCode(generationContext, beanType, beanRegistrationCode)); + return builder.build(); + } + + }; + } + @Test void generateBeanDefinitionMethodWhenHasAttributeFilterGeneratesMethod() { RootBeanDefinition beanDefinition = new RootBeanDefinition(TestBean.class); beanDefinition.setAttribute("a", "A"); beanDefinition.setAttribute("b", "B"); RegisteredBean registeredBean = registerBean(beanDefinition); - List fragmentCustomizers = Collections - .singletonList(this::customizeWithAttributeFilter); + BeanRegistrationAotContribution aotContribution = BeanRegistrationAotContribution + .ofBeanRegistrationCodeFragmentsCustomizer(this::customizeAttributeFilter); + List aotContributions = Collections + .singletonList(aotContribution); BeanDefinitionMethodGenerator generator = new BeanDefinitionMethodGenerator( this.methodGeneratorFactory, registeredBean, null, - Collections.emptyList(), fragmentCustomizers); + aotContributions); MethodReference method = generator.generateBeanDefinitionMethod( this.generationContext, this.beanRegistrationsCode); testCompiledResult(method, (actual, compiled) -> { @@ -185,8 +222,8 @@ class BeanDefinitionMethodGeneratorTests { }); } - private BeanRegistrationCodeFragments customizeWithAttributeFilter( - RegisteredBean registeredBean, BeanRegistrationCodeFragments codeFragments) { + private BeanRegistrationCodeFragments customizeAttributeFilter( + BeanRegistrationCodeFragments codeFragments) { return new BeanRegistrationCodeFragments(codeFragments) { @Override @@ -209,7 +246,7 @@ class BeanDefinitionMethodGeneratorTests { new RootBeanDefinition(AnnotatedBean.class)); BeanDefinitionMethodGenerator generator = new BeanDefinitionMethodGenerator( this.methodGeneratorFactory, innerBean, "testInnerBean", - Collections.emptyList(), Collections.emptyList()); + Collections.emptyList()); MethodReference method = generator.generateBeanDefinitionMethod( this.generationContext, this.beanRegistrationsCode); testCompiledResult(method, (actual, compiled) -> { @@ -230,7 +267,7 @@ class BeanDefinitionMethodGeneratorTests { RegisteredBean registeredBean = registerBean(beanDefinition); BeanDefinitionMethodGenerator generator = new BeanDefinitionMethodGenerator( this.methodGeneratorFactory, registeredBean, null, - Collections.emptyList(), Collections.emptyList()); + Collections.emptyList()); MethodReference method = generator.generateBeanDefinitionMethod( this.generationContext, this.beanRegistrationsCode); testCompiledResult(method, (actual, compiled) -> { @@ -264,7 +301,7 @@ class BeanDefinitionMethodGeneratorTests { RegisteredBean registeredBean = registerBean(beanDefinition); BeanDefinitionMethodGenerator generator = new BeanDefinitionMethodGenerator( this.methodGeneratorFactory, registeredBean, null, - Collections.emptyList(), Collections.emptyList()); + Collections.emptyList()); MethodReference method = generator.generateBeanDefinitionMethod( this.generationContext, this.beanRegistrationsCode); testCompiledResult(method, (actual, compiled) -> { @@ -297,8 +334,7 @@ class BeanDefinitionMethodGeneratorTests { .getMethodGenerator().generateMethod("aotContributedMethod") .using(builder -> builder.addComment("Example Contribution"))); BeanDefinitionMethodGenerator generator = new BeanDefinitionMethodGenerator( - this.methodGeneratorFactory, registeredBean, null, aotContributions, - Collections.emptyList()); + this.methodGeneratorFactory, registeredBean, null, aotContributions); MethodReference method = generator.generateBeanDefinitionMethod( this.generationContext, this.beanRegistrationsCode); testCompiledResult(method, (actual, compiled) -> { @@ -308,40 +344,6 @@ class BeanDefinitionMethodGeneratorTests { }); } - @Test - void generateBeanDefinitionMethodWhenHasBeanRegistrationCodeFragmentsCustomizerReturnsCodeGeneratesMethod() { - RegisteredBean registeredBean = registerBean( - new RootBeanDefinition(TestBean.class)); - List codeFragmentsCustomizers = new ArrayList<>(); - codeFragmentsCustomizers.add(this::customizeBeanRegistrationCodeFragments); - BeanDefinitionMethodGenerator generator = new BeanDefinitionMethodGenerator( - this.methodGeneratorFactory, registeredBean, null, - Collections.emptyList(), codeFragmentsCustomizers); - MethodReference method = generator.generateBeanDefinitionMethod( - this.generationContext, this.beanRegistrationsCode); - testCompiledResult(method, - (actual, compiled) -> assertThat( - compiled.getSourceFile(".*BeanDefinitions")) - .contains("// Custom Code")); - } - - private BeanRegistrationCodeFragments customizeBeanRegistrationCodeFragments( - RegisteredBean registeredBean, BeanRegistrationCodeFragments codeFragments) { - return new BeanRegistrationCodeFragments(codeFragments) { - - @Override - public CodeBlock generateReturnCode(GenerationContext generationContext, - BeanRegistrationCode beanRegistrationCode) { - CodeBlock.Builder builder = CodeBlock.builder(); - builder.addStatement("// Custom Code"); - builder.add(super.generateReturnCode(generationContext, - beanRegistrationCode)); - return builder.build(); - } - - }; - } - @Test @CompileWithTargetClassAccess(classes = PackagePrivateTestBean.class) void generateBeanDefinitionMethodWhenPackagePrivateBean() { @@ -349,7 +351,7 @@ class BeanDefinitionMethodGeneratorTests { new RootBeanDefinition(PackagePrivateTestBean.class)); BeanDefinitionMethodGenerator generator = new BeanDefinitionMethodGenerator( this.methodGeneratorFactory, registeredBean, null, - Collections.emptyList(), Collections.emptyList()); + Collections.emptyList()); MethodReference method = generator.generateBeanDefinitionMethod( this.generationContext, this.beanRegistrationsCode); testCompiledResult(method, false, (actual, compiled) -> { diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanRegistrationsAotContributionTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanRegistrationsAotContributionTests.java index 1cca7e1bdf7..3a39001e337 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanRegistrationsAotContributionTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanRegistrationsAotContributionTests.java @@ -86,7 +86,7 @@ class BeanRegistrationsAotContributionTests { new RootBeanDefinition(TestBean.class)); BeanDefinitionMethodGenerator generator = new BeanDefinitionMethodGenerator( this.methodGeneratorFactory, registeredBean, null, - Collections.emptyList(), Collections.emptyList()); + Collections.emptyList()); registrations.put("testBean", generator); BeanRegistrationsAotContribution contribution = new BeanRegistrationsAotContribution( registrations); @@ -106,7 +106,7 @@ class BeanRegistrationsAotContributionTests { new RootBeanDefinition(TestBean.class)); BeanDefinitionMethodGenerator generator = new BeanDefinitionMethodGenerator( this.methodGeneratorFactory, registeredBean, null, - Collections.emptyList(), Collections.emptyList()) { + Collections.emptyList()) { @Override MethodReference generateBeanDefinitionMethod(