From 2ba9939bd871761f090da4995908327e77a0da18 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sat, 15 Jul 2023 14:43:39 +0200 Subject: [PATCH] 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 c354b1014d. - Reverts the changes made to DefaultGenerationContext in a28ec3a0a8. - 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 --- .../generate/DefaultGenerationContext.java | 17 ++++++++++--- .../aot/generate/GeneratedClasses.java | 25 +++++++++---------- .../aot/TestContextGenerationContext.java | 23 +++++++---------- 3 files changed, 34 insertions(+), 31 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/aot/generate/DefaultGenerationContext.java b/spring-core/src/main/java/org/springframework/aot/generate/DefaultGenerationContext.java index ba63925c99a..034af2da875 100644 --- a/spring-core/src/main/java/org/springframework/aot/generate/DefaultGenerationContext.java +++ b/spring-core/src/main/java/org/springframework/aot/generate/DefaultGenerationContext.java @@ -78,7 +78,7 @@ public class DefaultGenerationContext implements GenerationContext { * @param generatedFiles the generated files * @param runtimeHints the runtime hints */ - protected DefaultGenerationContext(GeneratedClasses generatedClasses, + DefaultGenerationContext(GeneratedClasses generatedClasses, GeneratedFiles generatedFiles, RuntimeHints runtimeHints) { Assert.notNull(generatedClasses, "'generatedClasses' must not be null"); @@ -90,9 +90,18 @@ public class DefaultGenerationContext implements GenerationContext { this.runtimeHints = runtimeHints; } - private DefaultGenerationContext(DefaultGenerationContext existing, String name) { - int sequence = existing.sequenceGenerator.computeIfAbsent(name, key -> new AtomicInteger()).getAndIncrement(); - String featureName = (sequence > 0 ? name + sequence : name); + /** + * Create a new {@link DefaultGenerationContext} instance based on the + * supplied {@code existing} context and feature name. + * @param existing the existing context upon which to base the new one + * @param featureName the feature name to use + * @since 6.0.12 + */ + protected DefaultGenerationContext(DefaultGenerationContext existing, String featureName) { + int sequence = existing.sequenceGenerator.computeIfAbsent(featureName, key -> new AtomicInteger()).getAndIncrement(); + if (sequence > 0) { + featureName += sequence; + } this.sequenceGenerator = existing.sequenceGenerator; this.generatedClasses = existing.generatedClasses.withFeatureNamePrefix(featureName); this.generatedFiles = existing.generatedFiles; diff --git a/spring-core/src/main/java/org/springframework/aot/generate/GeneratedClasses.java b/spring-core/src/main/java/org/springframework/aot/generate/GeneratedClasses.java index 62940a79252..5e057001bb2 100644 --- a/spring-core/src/main/java/org/springframework/aot/generate/GeneratedClasses.java +++ b/spring-core/src/main/java/org/springframework/aot/generate/GeneratedClasses.java @@ -176,19 +176,6 @@ public class GeneratedClasses { return addForFeatureComponent(featureName, ClassName.get(targetComponent), type); } - /** - * Create a new {@link GeneratedClasses} instance using the specified feature - * name prefix to qualify generated class names for a dedicated round of code - * generation. - * @param featureNamePrefix the feature name prefix to use - * @return a new instance for the specified feature name prefix - * @since 6.0.12 - */ - public GeneratedClasses withFeatureNamePrefix(String featureNamePrefix) { - return new GeneratedClasses(this.classNameGenerator.withFeatureNamePrefix(featureNamePrefix), - this.classes, this.classesByOwner); - } - private GeneratedClass createAndAddGeneratedClass(String featureName, @Nullable ClassName targetComponent, Consumer type) { @@ -212,6 +199,18 @@ public class GeneratedClasses { } } + /** + * Create a new {@link GeneratedClasses} instance using the specified feature + * name prefix to qualify generated class names for a dedicated round of code + * generation. + * @param featureNamePrefix the feature name prefix to use + * @return a new instance for the specified feature name prefix + */ + GeneratedClasses withFeatureNamePrefix(String featureNamePrefix) { + return new GeneratedClasses(this.classNameGenerator.withFeatureNamePrefix(featureNamePrefix), + this.classes, this.classesByOwner); + } + private record Owner(String featureNamePrefix, String featureName, @Nullable ClassName target) { } diff --git a/spring-test/src/main/java/org/springframework/test/context/aot/TestContextGenerationContext.java b/spring-test/src/main/java/org/springframework/test/context/aot/TestContextGenerationContext.java index 2220386293a..9e148223943 100644 --- a/spring-test/src/main/java/org/springframework/test/context/aot/TestContextGenerationContext.java +++ b/spring-test/src/main/java/org/springframework/test/context/aot/TestContextGenerationContext.java @@ -18,7 +18,6 @@ package org.springframework.test.context.aot; import org.springframework.aot.generate.ClassNameGenerator; import org.springframework.aot.generate.DefaultGenerationContext; -import org.springframework.aot.generate.GeneratedClasses; import org.springframework.aot.generate.GeneratedFiles; import org.springframework.aot.hint.RuntimeHints; @@ -49,16 +48,13 @@ class TestContextGenerationContext extends DefaultGenerationContext { } /** - * Create a new {@link TestContextGenerationContext} instance backed by the - * specified {@link GeneratedClasses}, {@link GeneratedFiles}, and - * {@link RuntimeHints}. - * @param generatedClasses the generated classes - * @param generatedFiles the generated files - * @param runtimeHints the runtime hints + * Create a new {@link TestContextGenerationContext} instance based on the + * supplied {@code existing} context and feature name. + * @param existing the existing context upon which to base the new one + * @param featureName the feature name to use */ - private TestContextGenerationContext(GeneratedClasses generatedClasses, GeneratedFiles generatedFiles, - RuntimeHints runtimeHints, String featureName) { - super(generatedClasses, generatedFiles, runtimeHints); + private TestContextGenerationContext(TestContextGenerationContext existing, String featureName) { + super(existing, featureName); this.featureName = featureName; } @@ -67,8 +63,8 @@ class TestContextGenerationContext extends DefaultGenerationContext { * Create a new {@link TestContextGenerationContext} instance using the specified * feature name to qualify generated assets for a dedicated round of code generation. *

If this {@code TestContextGenerationContext} has a configured feature - * name, the supplied feature name will be appended to the existing feature name - * in order to avoid naming collisions. + * name, the existing feature name will prepended to the supplied feature name in + * order to avoid naming collisions. * @param featureName the feature name to use * @return a specialized {@link TestContextGenerationContext} for the specified * feature name @@ -78,8 +74,7 @@ class TestContextGenerationContext extends DefaultGenerationContext { if (this.featureName != null) { featureName = this.featureName + featureName; } - GeneratedClasses generatedClasses = getGeneratedClasses().withFeatureNamePrefix(featureName); - return new TestContextGenerationContext(generatedClasses, getGeneratedFiles(), getRuntimeHints(), featureName); + return new TestContextGenerationContext(this, featureName); } }