4141import org .springframework .javapoet .ClassName ;
4242import org .springframework .javapoet .FieldSpec ;
4343import org .springframework .javapoet .JavaFile ;
44+ import org .springframework .javapoet .MethodSpec ;
4445import org .springframework .javapoet .TypeName ;
4546import org .springframework .javapoet .TypeSpec ;
4647
5354class AotRepositoryBuilder {
5455
5556 private final RepositoryInformation repositoryInformation ;
57+ private final String moduleName ;
5658 private final ProjectionFactory projectionFactory ;
5759 private final AotRepositoryFragmentMetadata generationMetadata ;
5860
5961 private @ Nullable Consumer <AotRepositoryConstructorBuilder > constructorCustomizer ;
6062 private @ Nullable BiFunction <Method , RepositoryInformation , @ Nullable MethodContributor <? extends QueryMethod >> methodContributorFunction ;
6163 private ClassCustomizer customizer ;
6264
63- private AotRepositoryBuilder (RepositoryInformation repositoryInformation , ProjectionFactory projectionFactory ) {
65+ private AotRepositoryBuilder (RepositoryInformation repositoryInformation , String moduleName ,
66+ ProjectionFactory projectionFactory ) {
6467
6568 this .repositoryInformation = repositoryInformation ;
69+ this .moduleName = moduleName ;
6670 this .projectionFactory = projectionFactory ;
6771
6872 this .generationMetadata = new AotRepositoryFragmentMetadata (className ());
@@ -74,54 +78,71 @@ private AotRepositoryBuilder(RepositoryInformation repositoryInformation, Projec
7478 this .customizer = (info , metadata , builder ) -> {};
7579 }
7680
77- public static <M extends QueryMethod > AotRepositoryBuilder forRepository (RepositoryInformation repositoryInformation ,
81+ /**
82+ * Create a new {@code AotRepositoryBuilder} for the given {@link RepositoryInformation}.
83+ *
84+ * @param information must not be {@literal null}.
85+ * @param moduleName must not be {@literal null}.
86+ * @param projectionFactory must not be {@literal null}.
87+ * @return
88+ */
89+ public static AotRepositoryBuilder forRepository (RepositoryInformation information , String moduleName ,
7890 ProjectionFactory projectionFactory ) {
79- return new AotRepositoryBuilder (repositoryInformation , projectionFactory );
91+ return new AotRepositoryBuilder (information , moduleName , projectionFactory );
8092 }
8193
94+ /**
95+ * Configure a {@link ClassCustomizer} customizer.
96+ *
97+ * @param classCustomizer must not be {@literal null}.
98+ * @return {@code this}.
99+ */
100+ public AotRepositoryBuilder withClassCustomizer (ClassCustomizer classCustomizer ) {
101+
102+ this .customizer = classCustomizer ;
103+ return this ;
104+ }
105+
106+ /**
107+ * Configure a {@link AotRepositoryConstructorBuilder} customizer.
108+ *
109+ * @param constructorCustomizer must not be {@literal null}.
110+ * @return {@code this}.
111+ */
82112 public AotRepositoryBuilder withConstructorCustomizer (
83113 Consumer <AotRepositoryConstructorBuilder > constructorCustomizer ) {
84114
85115 this .constructorCustomizer = constructorCustomizer ;
86116 return this ;
87117 }
88118
119+ /**
120+ * Configure a {@link MethodContributor}.
121+ *
122+ * @param methodContributorFunction must not be {@literal null}.
123+ * @return {@code this}.
124+ */
89125 public AotRepositoryBuilder withQueryMethodContributor (
90126 BiFunction <Method , RepositoryInformation , @ Nullable MethodContributor <? extends QueryMethod >> methodContributorFunction ) {
91- this .methodContributorFunction = methodContributorFunction ;
92- return this ;
93- }
94127
95- public AotRepositoryBuilder withClassCustomizer (ClassCustomizer classCustomizer ) {
96-
97- this .customizer = classCustomizer ;
128+ this .methodContributorFunction = methodContributorFunction ;
98129 return this ;
99130 }
100131
101132 public AotBundle build () {
102133
134+ List <AotRepositoryMethod > methodMetadata = new ArrayList <>();
135+ RepositoryComposition repositoryComposition = repositoryInformation .getRepositoryComposition ();
136+
103137 // start creating the type
104138 TypeSpec .Builder builder = TypeSpec .classBuilder (this .generationMetadata .getTargetTypeName ()) //
105139 .addModifiers (Modifier .PUBLIC ) //
106140 .addAnnotation (Generated .class ) //
107- .addJavadoc ("AOT generated repository implementation for {@link $T}.\n " ,
141+ .addJavadoc ("AOT generated $L repository implementation for {@link $T}.\n " , moduleName ,
108142 repositoryInformation .getRepositoryInterface ());
109143
110144 // create the constructor
111- AotRepositoryConstructorBuilder constructorBuilder = new AotRepositoryConstructorBuilder (repositoryInformation ,
112- generationMetadata );
113- if (constructorCustomizer != null ) {
114- constructorCustomizer .accept (constructorBuilder );
115- }
116-
117- builder .addMethod (constructorBuilder .buildConstructor ());
118-
119- List <AotRepositoryMethod > methodMetadata = new ArrayList <>();
120- AotRepositoryMetadata .RepositoryType repositoryType = repositoryInformation .isReactiveRepository ()
121- ? AotRepositoryMetadata .RepositoryType .REACTIVE
122- : AotRepositoryMetadata .RepositoryType .IMPERATIVE ;
123-
124- RepositoryComposition repositoryComposition = repositoryInformation .getRepositoryComposition ();
145+ builder .addMethod (buildConstructor ());
125146
126147 Arrays .stream (repositoryInformation .getRepositoryInterface ().getMethods ())
127148 .sorted (Comparator .<Method , String > comparing (it -> {
@@ -136,12 +157,35 @@ public AotBundle build() {
136157
137158 // finally customize the file itself
138159 this .customizer .customize (repositoryInformation , generationMetadata , builder );
160+
139161 JavaFile javaFile = JavaFile .builder (packageName (), builder .build ()).build ();
162+ AotRepositoryMetadata metadata = getAotRepositoryMetadata (methodMetadata );
163+
164+ return new AotBundle (javaFile , metadata );
165+ }
166+
167+ private MethodSpec buildConstructor () {
168+
169+ AotRepositoryConstructorBuilder constructorBuilder = new AotRepositoryConstructorBuilder (repositoryInformation ,
170+ generationMetadata );
171+
172+ if (constructorCustomizer != null ) {
173+ constructorCustomizer .accept (constructorBuilder );
174+ }
175+
176+ return constructorBuilder .buildConstructor ();
177+ }
140178
141- AotRepositoryMetadata metadata = new AotRepositoryMetadata (repositoryInformation .getRepositoryInterface ().getName (),
142- repositoryInformation .moduleName () != null ? repositoryInformation .moduleName () : "" , repositoryType , methodMetadata );
179+ private AotRepositoryMetadata getAotRepositoryMetadata (List <AotRepositoryMethod > methodMetadata ) {
143180
144- return new AotBundle (javaFile , metadata .toJson ());
181+ AotRepositoryMetadata .RepositoryType repositoryType = repositoryInformation .isReactiveRepository ()
182+ ? AotRepositoryMetadata .RepositoryType .REACTIVE
183+ : AotRepositoryMetadata .RepositoryType .IMPERATIVE ;
184+
185+ String jsonModuleName = moduleName .replaceAll ("Reactive" , "" ).trim ();
186+
187+ return new AotRepositoryMetadata (repositoryInformation .getRepositoryInterface ().getName (), jsonModuleName ,
188+ repositoryType , methodMetadata );
145189 }
146190
147191 private void contributeMethod (Method method , RepositoryComposition repositoryComposition ,
@@ -185,8 +229,7 @@ private void contributeMethod(Method method, RepositoryComposition repositoryCom
185229 private AotRepositoryMethod getFragmentMetadata (Method method , RepositoryFragment <?> fragment ) {
186230
187231 String signature = fragment .getSignatureContributor ().getName ();
188- String implementation = fragment .getImplementation ().map (it -> it .getClass ().getName ()).orElse (null );
189-
232+ String implementation = fragment .getImplementationClass ().map (Class ::getName ).orElse (null );
190233 AotFragmentTarget fragmentTarget = new AotFragmentTarget (signature , implementation );
191234
192235 return new AotRepositoryMethod (method .getName (), method .toGenericString (), null , fragmentTarget );
@@ -240,7 +283,7 @@ public interface ClassCustomizer {
240283
241284 }
242285
243- record AotBundle (JavaFile javaFile , JSONObject metadata ) {
286+ record AotBundle (JavaFile javaFile , AotRepositoryMetadata metadata ) {
244287 }
245288
246289}
0 commit comments