From 63fae8c5a7ea21e4ff59ee094cc4541c048e8e54 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 20 Jun 2022 11:11:26 +0200 Subject: [PATCH] Clarify the scope of target in ClassNameGenerator Closes gh-28517 --- .../aot/generate/ClassNameGenerator.java | 32 ++++++++++++------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/aot/generate/ClassNameGenerator.java b/spring-core/src/main/java/org/springframework/aot/generate/ClassNameGenerator.java index 43fe89cd38..ec6d7cfd99 100644 --- a/spring-core/src/main/java/org/springframework/aot/generate/ClassNameGenerator.java +++ b/spring-core/src/main/java/org/springframework/aot/generate/ClassNameGenerator.java @@ -27,12 +27,13 @@ import org.springframework.util.ClassUtils; import org.springframework.util.StringUtils; /** - * Generates unique class names that can be used in ahead-of-time generated - * source code. This class is stateful so the same instance should be used for - * all name generation. Most commonly the class name generator is obtained via a - * {@link GenerationContext}. + * Generate unique class names based on an optional target {@link Class} and + * a feature name. This class is stateful so the same instance should be used + * for all name generation. Most commonly the class name generator is obtained + * via a {@link GenerationContext}. * * @author Phillip Webb + * @author Stephane Nicoll * @since 6.0 */ public final class ClassNameGenerator { @@ -47,10 +48,18 @@ public final class ClassNameGenerator { /** - * Generate a new class name for the given {@code target} / - * {@code featureName} combination. - * @param target the target of the newly generated class or {@code null} if - * there is not target. + * Generate a unique {@link ClassName} based on the specified {@code target} + * class and {@code featureName}. If a {@code target} is specified, the + * generated class name is a suffixed version of it. + *

For instance, a {@code com.example.Demo} target with an + * {@code Initializer} feature name leads to a + * {@code com.example.Demo__Initializer} generated class name. If such a + * feature was already requested for this target, a counter is used to + * ensure uniqueness. + *

If there is no target, the {@code featureName} is used to generate the + * class name in the {@value #AOT_PACKAGE} package. + * @param target the class the newly generated class relates to, or + * {@code null} if there is not target * @param featureName the name of the feature that the generated class * supports * @return a unique generated class name @@ -58,10 +67,11 @@ public final class ClassNameGenerator { public ClassName generateClassName(@Nullable Class target, String featureName) { Assert.hasLength(featureName, "'featureName' must not be empty"); featureName = clean(featureName); - if(target != null) { - return generateSequencedClassName(target.getName().replace("$", "_") + SEPARATOR + StringUtils.capitalize(featureName)); + if (target != null) { + return generateSequencedClassName(target.getName().replace("$", "_") + + SEPARATOR + StringUtils.capitalize(featureName)); } - return generateSequencedClassName(AOT_PACKAGE+ featureName); + return generateSequencedClassName(AOT_PACKAGE + featureName); } private String clean(String name) {