Clarify the scope of target in ClassNameGenerator
Closes gh-28517
This commit is contained in:
@@ -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.
|
||||
* <p>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.
|
||||
* <p>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) {
|
||||
|
||||
Reference in New Issue
Block a user