Skip to content

Commit 2ba9939

Browse files
committed
Revise changes to DefaultGenerationContext and GeneratedClasses
In order to reduce the surface area of published APIs in the affected classes, this commit: - Reverts the changes made to GeneratedClasses in c354b10. - Reverts the changes made to DefaultGenerationContext in a28ec3a. - Makes the DefaultGenerationContext(DefaultGenerationContext, String) constructor protected. - Reworks the internals of TestContextGenerationContext to align with the above changes. See gh-30861 Closes gh-30895 Closes gh-30897
1 parent 3a278cc commit 2ba9939

File tree

3 files changed

+34
-31
lines changed

3 files changed

+34
-31
lines changed

spring-core/src/main/java/org/springframework/aot/generate/DefaultGenerationContext.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public DefaultGenerationContext(ClassNameGenerator classNameGenerator, Generated
7878
* @param generatedFiles the generated files
7979
* @param runtimeHints the runtime hints
8080
*/
81-
protected DefaultGenerationContext(GeneratedClasses generatedClasses,
81+
DefaultGenerationContext(GeneratedClasses generatedClasses,
8282
GeneratedFiles generatedFiles, RuntimeHints runtimeHints) {
8383

8484
Assert.notNull(generatedClasses, "'generatedClasses' must not be null");
@@ -90,9 +90,18 @@ protected DefaultGenerationContext(GeneratedClasses generatedClasses,
9090
this.runtimeHints = runtimeHints;
9191
}
9292

93-
private DefaultGenerationContext(DefaultGenerationContext existing, String name) {
94-
int sequence = existing.sequenceGenerator.computeIfAbsent(name, key -> new AtomicInteger()).getAndIncrement();
95-
String featureName = (sequence > 0 ? name + sequence : name);
93+
/**
94+
* Create a new {@link DefaultGenerationContext} instance based on the
95+
* supplied {@code existing} context and feature name.
96+
* @param existing the existing context upon which to base the new one
97+
* @param featureName the feature name to use
98+
* @since 6.0.12
99+
*/
100+
protected DefaultGenerationContext(DefaultGenerationContext existing, String featureName) {
101+
int sequence = existing.sequenceGenerator.computeIfAbsent(featureName, key -> new AtomicInteger()).getAndIncrement();
102+
if (sequence > 0) {
103+
featureName += sequence;
104+
}
96105
this.sequenceGenerator = existing.sequenceGenerator;
97106
this.generatedClasses = existing.generatedClasses.withFeatureNamePrefix(featureName);
98107
this.generatedFiles = existing.generatedFiles;

spring-core/src/main/java/org/springframework/aot/generate/GeneratedClasses.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -176,19 +176,6 @@ public GeneratedClass addForFeatureComponent(String featureName,
176176
return addForFeatureComponent(featureName, ClassName.get(targetComponent), type);
177177
}
178178

179-
/**
180-
* Create a new {@link GeneratedClasses} instance using the specified feature
181-
* name prefix to qualify generated class names for a dedicated round of code
182-
* generation.
183-
* @param featureNamePrefix the feature name prefix to use
184-
* @return a new instance for the specified feature name prefix
185-
* @since 6.0.12
186-
*/
187-
public GeneratedClasses withFeatureNamePrefix(String featureNamePrefix) {
188-
return new GeneratedClasses(this.classNameGenerator.withFeatureNamePrefix(featureNamePrefix),
189-
this.classes, this.classesByOwner);
190-
}
191-
192179
private GeneratedClass createAndAddGeneratedClass(String featureName,
193180
@Nullable ClassName targetComponent, Consumer<TypeSpec.Builder> type) {
194181

@@ -212,6 +199,18 @@ void writeTo(GeneratedFiles generatedFiles) {
212199
}
213200
}
214201

202+
/**
203+
* Create a new {@link GeneratedClasses} instance using the specified feature
204+
* name prefix to qualify generated class names for a dedicated round of code
205+
* generation.
206+
* @param featureNamePrefix the feature name prefix to use
207+
* @return a new instance for the specified feature name prefix
208+
*/
209+
GeneratedClasses withFeatureNamePrefix(String featureNamePrefix) {
210+
return new GeneratedClasses(this.classNameGenerator.withFeatureNamePrefix(featureNamePrefix),
211+
this.classes, this.classesByOwner);
212+
}
213+
215214
private record Owner(String featureNamePrefix, String featureName, @Nullable ClassName target) {
216215
}
217216

spring-test/src/main/java/org/springframework/test/context/aot/TestContextGenerationContext.java

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import org.springframework.aot.generate.ClassNameGenerator;
2020
import org.springframework.aot.generate.DefaultGenerationContext;
21-
import org.springframework.aot.generate.GeneratedClasses;
2221
import org.springframework.aot.generate.GeneratedFiles;
2322
import org.springframework.aot.hint.RuntimeHints;
2423

@@ -49,16 +48,13 @@ class TestContextGenerationContext extends DefaultGenerationContext {
4948
}
5049

5150
/**
52-
* Create a new {@link TestContextGenerationContext} instance backed by the
53-
* specified {@link GeneratedClasses}, {@link GeneratedFiles}, and
54-
* {@link RuntimeHints}.
55-
* @param generatedClasses the generated classes
56-
* @param generatedFiles the generated files
57-
* @param runtimeHints the runtime hints
51+
* Create a new {@link TestContextGenerationContext} instance based on the
52+
* supplied {@code existing} context and feature name.
53+
* @param existing the existing context upon which to base the new one
54+
* @param featureName the feature name to use
5855
*/
59-
private TestContextGenerationContext(GeneratedClasses generatedClasses, GeneratedFiles generatedFiles,
60-
RuntimeHints runtimeHints, String featureName) {
61-
super(generatedClasses, generatedFiles, runtimeHints);
56+
private TestContextGenerationContext(TestContextGenerationContext existing, String featureName) {
57+
super(existing, featureName);
6258
this.featureName = featureName;
6359
}
6460

@@ -67,8 +63,8 @@ private TestContextGenerationContext(GeneratedClasses generatedClasses, Generate
6763
* Create a new {@link TestContextGenerationContext} instance using the specified
6864
* feature name to qualify generated assets for a dedicated round of code generation.
6965
* <p>If <em>this</em> {@code TestContextGenerationContext} has a configured feature
70-
* name, the supplied feature name will be appended to the existing feature name
71-
* in order to avoid naming collisions.
66+
* name, the existing feature name will prepended to the supplied feature name in
67+
* order to avoid naming collisions.
7268
* @param featureName the feature name to use
7369
* @return a specialized {@link TestContextGenerationContext} for the specified
7470
* feature name
@@ -78,8 +74,7 @@ public TestContextGenerationContext withName(String featureName) {
7874
if (this.featureName != null) {
7975
featureName = this.featureName + featureName;
8076
}
81-
GeneratedClasses generatedClasses = getGeneratedClasses().withFeatureNamePrefix(featureName);
82-
return new TestContextGenerationContext(generatedClasses, getGeneratedFiles(), getRuntimeHints(), featureName);
77+
return new TestContextGenerationContext(this, featureName);
8378
}
8479

8580
}

0 commit comments

Comments
 (0)