Skip to content

Commit 88428ed

Browse files
committed
1 parent 36012ff commit 88428ed

File tree

12 files changed

+217
-181
lines changed

12 files changed

+217
-181
lines changed

spring-aop/src/test/java/org/springframework/aop/scope/ScopedProxyBeanRegistrationAotProcessorTests.java

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
package org.springframework.aop.scope;
1818

1919
import java.lang.reflect.Method;
20-
import java.util.ArrayList;
21-
import java.util.List;
2220
import java.util.Properties;
2321
import java.util.function.BiConsumer;
2422

@@ -27,17 +25,15 @@
2725

2826
import org.springframework.aop.framework.AopInfrastructureBean;
2927
import org.springframework.aot.generate.DefaultGenerationContext;
30-
import org.springframework.aot.generate.GeneratedMethods;
3128
import org.springframework.aot.generate.InMemoryGeneratedFiles;
32-
import org.springframework.aot.generate.MethodGenerator;
3329
import org.springframework.aot.generate.MethodReference;
3430
import org.springframework.aot.test.generator.compile.Compiled;
3531
import org.springframework.aot.test.generator.compile.TestCompiler;
3632
import org.springframework.beans.factory.BeanCreationException;
3733
import org.springframework.beans.factory.aot.AotFactoriesLoader;
3834
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotContribution;
39-
import org.springframework.beans.factory.aot.BeanFactoryInitializationCode;
4035
import org.springframework.beans.factory.aot.BeanRegistrationAotProcessor;
36+
import org.springframework.beans.factory.aot.MockBeanFactoryInitializationCode;
4137
import org.springframework.beans.factory.aot.TestBeanRegistrationsAotProcessor;
4238
import org.springframework.beans.factory.config.BeanDefinition;
4339
import org.springframework.beans.factory.config.PropertiesFactoryBean;
@@ -158,27 +154,4 @@ private void testCompile(BiConsumer<DefaultListableBeanFactory, Compiled> result
158154
});
159155
}
160156

161-
162-
static class MockBeanFactoryInitializationCode implements BeanFactoryInitializationCode {
163-
164-
private final GeneratedMethods generatedMethods = new GeneratedMethods();
165-
166-
private final List<MethodReference> initializers = new ArrayList<>();
167-
168-
@Override
169-
public MethodGenerator getMethodGenerator() {
170-
return this.generatedMethods;
171-
}
172-
173-
@Override
174-
public void addInitializer(MethodReference methodReference) {
175-
this.initializers.add(methodReference);
176-
}
177-
178-
List<MethodReference> getInitializers() {
179-
return this.initializers;
180-
}
181-
182-
}
183-
184157
}

spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanFactoryInitializationCode.java

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import org.springframework.aot.generate.MethodGenerator;
2020
import org.springframework.aot.generate.MethodReference;
21-
import org.springframework.lang.Nullable;
2221

2322
/**
2423
* Interface that can be used to configure the code that will be generated to
@@ -35,23 +34,7 @@ public interface BeanFactoryInitializationCode {
3534
*/
3635
String BEAN_FACTORY_VARIABLE = "beanFactory";
3736

38-
/**
39-
* Return the target class for this bean factory or {@code null} if there is
40-
* no target.
41-
* @return the target
42-
*/
43-
@Nullable
44-
default Class<?> getTarget() {
45-
return null;
46-
}
47-
48-
/**
49-
* Return the name of the bean factory or and empty string if no ID is available.
50-
* @return the bean factory name
51-
*/
52-
default String getName() {
53-
return "";
54-
}
37+
BeanFactoryNamingConvention getBeanFactoryNamingConvention();
5538

5639
/**
5740
* Return a {@link MethodGenerator} that can be used to add more methods to
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.beans.factory.aot;
18+
19+
import org.springframework.beans.factory.BeanFactory;
20+
import org.springframework.javapoet.ClassName;
21+
22+
/**
23+
* Naming strategy to use when processing a particular {@link BeanFactory}.
24+
*
25+
* @author Stephane Nicoll
26+
* @since 6.0
27+
*/
28+
public interface BeanFactoryNamingConvention {
29+
30+
/**
31+
* Generate a new {@link ClassName} for the specified {@code featureName}.
32+
* @param featureName the name of the feature that the generated class
33+
* supports for the bean factory this instance handles
34+
* @return a unique generated class name
35+
*/
36+
ClassName generateClassName(String featureName);
37+
38+
/**
39+
* Return the name of the bean factory or and empty string if no ID is available.
40+
* @return the bean factory name
41+
*/
42+
default String getBeanFactoryName() {
43+
return "";
44+
}
45+
46+
}

spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanRegistrationsAotContribution.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,16 @@ class BeanRegistrationsAotContribution
6161
public void applyTo(GenerationContext generationContext,
6262
BeanFactoryInitializationCode beanFactoryInitializationCode) {
6363

64-
ClassName className = generationContext.getClassNameGenerator().generateClassName(
65-
beanFactoryInitializationCode.getTarget(),
66-
beanFactoryInitializationCode.getName() + "BeanFactoryRegistrations");
64+
ClassName className = beanFactoryInitializationCode.getBeanFactoryNamingConvention()
65+
.generateClassName("BeanFactoryRegistrations");
6766
BeanRegistrationsCodeGenerator codeGenerator = new BeanRegistrationsCodeGenerator(
6867
className);
68+
String beanFactoryName = beanFactoryInitializationCode.getBeanFactoryNamingConvention()
69+
.getBeanFactoryName();
6970
GeneratedMethod registerMethod = codeGenerator.getMethodGenerator()
7071
.generateMethod("registerBeanDefinitions")
7172
.using(builder -> generateRegisterMethod(builder, generationContext,
72-
beanFactoryInitializationCode.getName(),
73-
codeGenerator));
73+
beanFactoryName, codeGenerator));
7474
JavaFile javaFile = codeGenerator.generatedJavaFile(className);
7575
generationContext.getGeneratedFiles().addSourceFile(javaFile);
7676
beanFactoryInitializationCode
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.beans.factory.aot;
18+
19+
import org.springframework.aot.generate.ClassNameGenerator;
20+
import org.springframework.javapoet.ClassName;
21+
import org.springframework.lang.Nullable;
22+
import org.springframework.util.StringUtils;
23+
24+
/**
25+
* Default {@link BeanFactoryNamingConvention} implementation.
26+
*
27+
* @author Stephane Nicoll
28+
* @since 6.0
29+
*/
30+
public class DefaultBeanFactoryNamingConvention implements BeanFactoryNamingConvention {
31+
32+
private final ClassNameGenerator classNameGenerator;
33+
34+
@Nullable
35+
private final Class<?> target;
36+
37+
private final String name;
38+
39+
public DefaultBeanFactoryNamingConvention(ClassNameGenerator classNameGenerator,
40+
@Nullable Class<?> target, @Nullable String name) {
41+
this.classNameGenerator = classNameGenerator;
42+
this.target = target;
43+
this.name = (!StringUtils.hasText(name)) ? "" : name;
44+
}
45+
46+
@Override
47+
public ClassName generateClassName(String featureName) {
48+
return this.classNameGenerator.generateClassName(this.target, this.name + featureName);
49+
}
50+
51+
@Override
52+
public String getBeanFactoryName() {
53+
return this.name;
54+
}
55+
56+
}

spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanRegistrationsAotContributionTests.java

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@
3030
import org.junit.jupiter.api.Test;
3131

3232
import org.springframework.aot.generate.DefaultGenerationContext;
33-
import org.springframework.aot.generate.GeneratedMethods;
3433
import org.springframework.aot.generate.GenerationContext;
3534
import org.springframework.aot.generate.InMemoryGeneratedFiles;
36-
import org.springframework.aot.generate.MethodGenerator;
3735
import org.springframework.aot.generate.MethodReference;
3836
import org.springframework.aot.test.generator.compile.Compiled;
3937
import org.springframework.aot.test.generator.compile.TestCompiler;
@@ -64,8 +62,6 @@ class BeanRegistrationsAotContributionTests {
6462

6563
private DefaultListableBeanFactory beanFactory;
6664

67-
private MockSpringFactoriesLoader springFactoriesLoader;
68-
6965
private BeanDefinitionMethodGeneratorFactory methodGeneratorFactory;
7066

7167
private MockBeanFactoryInitializationCode beanFactoryInitializationCode = new MockBeanFactoryInitializationCode();
@@ -75,9 +71,8 @@ void setup() {
7571
this.generatedFiles = new InMemoryGeneratedFiles();
7672
this.generationContext = new DefaultGenerationContext(this.generatedFiles);
7773
this.beanFactory = new DefaultListableBeanFactory();
78-
this.springFactoriesLoader = new MockSpringFactoriesLoader();
7974
this.methodGeneratorFactory = new BeanDefinitionMethodGeneratorFactory(
80-
new AotFactoriesLoader(this.beanFactory, this.springFactoriesLoader));
75+
new AotFactoriesLoader(this.beanFactory, new MockSpringFactoriesLoader()));
8176
}
8277

8378
@Test
@@ -164,7 +159,7 @@ private void testCompiledResult(
164159
}
165160

166161
private JavaFile createJavaFile() {
167-
MethodReference initializer = this.beanFactoryInitializationCode.initializers
162+
MethodReference initializer = this.beanFactoryInitializationCode.getInitializers()
168163
.get(0);
169164
TypeSpec.Builder builder = TypeSpec.classBuilder("BeanFactoryConsumer");
170165
builder.addModifiers(Modifier.PUBLIC);
@@ -177,37 +172,4 @@ private JavaFile createJavaFile() {
177172
return JavaFile.builder("__", builder.build()).build();
178173
}
179174

180-
class MockBeanFactoryInitializationCode implements BeanFactoryInitializationCode {
181-
182-
private final GeneratedMethods generatedMethods = new GeneratedMethods();
183-
184-
private final List<MethodReference> initializers = new ArrayList<>();
185-
186-
private final String name;
187-
188-
MockBeanFactoryInitializationCode() {
189-
this("");
190-
}
191-
192-
MockBeanFactoryInitializationCode(String name) {
193-
this.name = name;
194-
}
195-
196-
@Override
197-
public String getName() {
198-
return this.name;
199-
}
200-
201-
@Override
202-
public MethodGenerator getMethodGenerator() {
203-
return this.generatedMethods;
204-
}
205-
206-
@Override
207-
public void addInitializer(MethodReference methodReference) {
208-
this.initializers.add(methodReference);
209-
}
210-
211-
}
212-
213175
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.beans.factory.aot;
18+
19+
import java.util.ArrayList;
20+
import java.util.Collections;
21+
import java.util.List;
22+
23+
import org.springframework.aot.generate.ClassNameGenerator;
24+
import org.springframework.aot.generate.GeneratedMethods;
25+
import org.springframework.aot.generate.MethodReference;
26+
27+
/**
28+
* Mock {@link BeanFactoryInitializationCode} implementation.
29+
*
30+
* @author Stephane Nicoll
31+
*/
32+
public class MockBeanFactoryInitializationCode implements BeanFactoryInitializationCode {
33+
34+
private final BeanFactoryNamingConvention namingConvention;
35+
36+
private final GeneratedMethods generatedMethods = new GeneratedMethods();
37+
38+
private final List<MethodReference> initializers = new ArrayList<>();
39+
40+
public MockBeanFactoryInitializationCode() {
41+
this("");
42+
}
43+
44+
public MockBeanFactoryInitializationCode(String name) {
45+
this.namingConvention = new DefaultBeanFactoryNamingConvention(
46+
new ClassNameGenerator(), null, name);
47+
}
48+
49+
@Override
50+
public BeanFactoryNamingConvention getBeanFactoryNamingConvention() {
51+
return this.namingConvention;
52+
}
53+
54+
@Override
55+
public GeneratedMethods getMethodGenerator() {
56+
return this.generatedMethods;
57+
}
58+
59+
@Override
60+
public void addInitializer(MethodReference methodReference) {
61+
this.initializers.add(methodReference);
62+
}
63+
64+
public List<MethodReference> getInitializers() {
65+
return Collections.unmodifiableList(this.initializers);
66+
}
67+
68+
}

0 commit comments

Comments
 (0)