ClassNameGenerator
can currently only be created with a required non-null Class<?> defaultTarget
.
Consequently, any generated class name must be based off that; however, there are times when source code needs to be generated using a predefined class name.
For example, the AotTestAttributesCodeGenerator
class in spring-test
currently must hard code a constant for org.springframework.test.context.aot.AotTestAttributes__Generated
since the generated mappings are created with a ClassNameGenerator
that uses AotTestAttributes
as the defaultTarget
class and "Generated"
as the feature name.
Ideally, we should be able to predefine the mappings class name as org.springframework.test.context.aot.GeneratedAotTestAttributes
(or similar) and supply that to the ClassNameGenerator
.
There is a similar issue in Spring Boot where code relies on a generated name.
Comment From: sbrannen
There is a similar issue in Spring Boot where code relies on a generated name.
I believe it's related to contexts within an application context hierarchy.
@philwebb, perhaps you can provide further details on that.
Comment From: philwebb
We have this line in SpringApplication
which relies on the name generated by ApplicationContextInitializationCodeGenerator
.
Comment From: snicoll
ClassNameGenerator can currently only be created with a required non-null Class<?> defaultTarget.
I find this bit confusing. That part is not a problem per se, it's rather that the current generatedFiles
does not offer a way to include a GeneratedClass
with a dedicated name. Looking at the code, the above wouldn't matter if GeneratedClasses
had a way to "register" a new class based on a ClassName
. We have getOrCreate
as well as add
to take into consideration.
Comment From: snicoll
So it turns out this need is because the TCF needs to generate two classes at a fixed place, without taking care of a featureNamePrefix if any. I find this a little odd considering that GeneratedClasses
is designed completely around the need of naming things consistently. It's also a dangerous API to put in people's hand. They shouldn't be allowed to determine the exact name of the class.
I am going to decline this for that reason. I also would like we take the time to revisit the arrangement in the TCF so that it doesn't generate class names that look like any other class (i.e. non-generated ones).