2020import org .apache .commons .logging .Log ;
2121import org .apache .commons .logging .LogFactory ;
2222import org .jspecify .annotations .Nullable ;
23+
2324import org .springframework .aot .generate .GeneratedClass ;
2425import org .springframework .aot .generate .GeneratedTypeReference ;
2526import org .springframework .aot .generate .GenerationContext ;
3132import org .springframework .data .repository .config .AotRepositoryContext ;
3233import org .springframework .data .repository .core .RepositoryInformation ;
3334import org .springframework .data .repository .query .QueryMethod ;
34- import org .springframework .javapoet .ClassName ;
3535import org .springframework .javapoet .TypeName ;
3636
3737/**
@@ -46,8 +46,7 @@ public class RepositoryContributor {
4646 private static final Log logger = LogFactory .getLog (RepositoryContributor .class );
4747 private static final String FEATURE_NAME = "AotRepository" ;
4848
49- private AotRepositoryBuilder builder ;
50- private final AotRepositoryContext repositoryContext ;
49+ private final AotRepositoryBuilder builder ;
5150 private @ Nullable TypeReference contributedTypeName ;
5251
5352 /**
@@ -57,7 +56,6 @@ public class RepositoryContributor {
5756 */
5857 public RepositoryContributor (AotRepositoryContext repositoryContext ) {
5958
60- this .repositoryContext = repositoryContext ;
6159 builder = AotRepositoryBuilder .forRepository (repositoryContext .getRepositoryInformation (),
6260 repositoryContext .getModuleName (), createProjectionFactory ());
6361 }
@@ -98,39 +96,45 @@ public void contribute(GenerationContext generationContext) {
9896 .withConstructorCustomizer (this ::customizeConstructor ) //
9997 .withQueryMethodContributor (this ::contributeQueryMethod ); //
10098
99+ // TODO: temporary fix until we have a better representation of constructor arguments
100+ // decouple the description of arguments from the actual code used in the constructor initialization, super calls,
101+ // etc.
102+ RepositoryConstructorBuilder constructorBuilder = new RepositoryConstructorBuilder (builder .getGenerationMetadata ());
103+ customizeConstructor (constructorBuilder );
104+
101105 GeneratedClass generatedClass = generationContext .getGeneratedClasses ().getOrAddForFeatureComponent (FEATURE_NAME ,
102- ClassName .bestGuess (builder .intendedTargetClassName ().getCanonicalName ()), targetTypeSpec -> {
106+ builder .getClassName (), targetTypeSpec -> {
107+
108+ // capture the actual type name early on so that we can use it in the constructor.
109+ builder .withClassName (targetTypeSpec .build ().name ());
103110
104111 AotBundle aotBundle = builder .build (targetTypeSpec );
105- {
106- Class <?> repositoryInterface = getRepositoryInformation ().getRepositoryInterface ();
107- String repositoryJsonFileName = getRepositoryJsonFileName (repositoryInterface );
108- String repositoryJson ;
109- try {
110- repositoryJson = aotBundle .metadata ().toJson ().toString (2 );
111- } catch (JSONException e ) {
112- throw new RuntimeException (e );
113- }
114-
115- if (logger .isTraceEnabled ()) {
116- logger .trace ("""
117- ------ AOT Repository.json: %s ------
118- %s
119- -------------------
120- """ .formatted (repositoryJsonFileName , repositoryJson ));
121-
122- logger .trace ("""
123- ------ AOT Generated Repository: %s ------
124- %s
125- -------------------
126- """ .formatted (null , aotBundle .javaFile ()));
127- }
128-
129- generationContext .getGeneratedFiles ().addResourceFile (repositoryJsonFileName , repositoryJson );
112+ Class <?> repositoryInterface = getRepositoryInformation ().getRepositoryInterface ();
113+ String repositoryJsonFileName = getRepositoryJsonFileName (repositoryInterface );
114+ String repositoryJson ;
115+ try {
116+ repositoryJson = aotBundle .metadata ().toJson ().toString (2 );
117+ } catch (JSONException e ) {
118+ throw new RuntimeException (e );
130119 }
120+
121+ if (logger .isTraceEnabled ()) {
122+ logger .trace ("""
123+ ------ AOT Repository.json: %s ------
124+ %s
125+ -------------------
126+ """ .formatted (repositoryJsonFileName , repositoryJson ));
127+
128+ logger .trace ("""
129+ ------ AOT Generated Repository: %s ------
130+ %s
131+ -------------------
132+ """ .formatted (null , aotBundle .javaFile ()));
133+ }
134+
135+ generationContext .getGeneratedFiles ().addResourceFile (repositoryJsonFileName , repositoryJson );
131136 });
132137
133- builder .prepare (generatedClass .getName ()); // initialize ctor argument resolution and set type name to target
134138 this .contributedTypeName = GeneratedTypeReference .of (generatedClass .getName ());
135139
136140 // generate native runtime hints - needed cause we're using the repository proxy
0 commit comments