22
22
import java .util .LinkedHashMap ;
23
23
import java .util .List ;
24
24
import java .util .Map ;
25
- import java .util .function .BiFunction ;
26
- import java .util .function .Consumer ;
27
25
28
26
import javax .lang .model .element .Modifier ;
29
27
30
28
import org .apache .commons .logging .Log ;
31
29
import org .apache .commons .logging .LogFactory ;
32
30
import org .jspecify .annotations .Nullable ;
31
+
33
32
import org .springframework .aot .generate .ClassNameGenerator ;
34
33
import org .springframework .aot .generate .Generated ;
35
34
import org .springframework .data .projection .ProjectionFactory ;
@@ -58,8 +57,8 @@ class AotRepositoryBuilder {
58
57
private final ProjectionFactory projectionFactory ;
59
58
private final AotRepositoryFragmentMetadata generationMetadata ;
60
59
61
- private @ Nullable Consumer < AotRepositoryConstructorBuilder > constructorCustomizer ;
62
- private @ Nullable BiFunction < Method , RepositoryInformation , @ Nullable MethodContributor <? extends QueryMethod >> methodContributorFunction ;
60
+ private @ Nullable ConstructorCustomizer constructorCustomizer ;
61
+ private @ Nullable MethodContributorFactory methodContributorFactory ;
63
62
private ClassCustomizer customizer ;
64
63
65
64
private AotRepositoryBuilder (RepositoryInformation repositoryInformation , String moduleName ,
@@ -109,23 +108,21 @@ public AotRepositoryBuilder withClassCustomizer(ClassCustomizer classCustomizer)
109
108
* @param constructorCustomizer must not be {@literal null}.
110
109
* @return {@code this}.
111
110
*/
112
- public AotRepositoryBuilder withConstructorCustomizer (
113
- Consumer <AotRepositoryConstructorBuilder > constructorCustomizer ) {
111
+ public AotRepositoryBuilder withConstructorCustomizer (ConstructorCustomizer constructorCustomizer ) {
114
112
115
113
this .constructorCustomizer = constructorCustomizer ;
116
114
return this ;
117
115
}
118
116
119
117
/**
120
- * Configure a {@link MethodContributor}.
118
+ * Configure a {@link MethodContributor} factory .
121
119
*
122
- * @param methodContributorFunction must not be {@literal null}.
120
+ * @param methodContributorFactory must not be {@literal null}.
123
121
* @return {@code this}.
124
122
*/
125
- public AotRepositoryBuilder withQueryMethodContributor (
126
- BiFunction <Method , RepositoryInformation , @ Nullable MethodContributor <? extends QueryMethod >> methodContributorFunction ) {
123
+ public AotRepositoryBuilder withQueryMethodContributor (MethodContributorFactory methodContributorFactory ) {
127
124
128
- this .methodContributorFunction = methodContributorFunction ;
125
+ this .methodContributorFactory = methodContributorFactory ;
129
126
return this ;
130
127
}
131
128
@@ -170,7 +167,7 @@ private MethodSpec buildConstructor() {
170
167
generationMetadata );
171
168
172
169
if (constructorCustomizer != null ) {
173
- constructorCustomizer .accept (constructorBuilder );
170
+ constructorCustomizer .customize (constructorBuilder );
174
171
}
175
172
176
173
return constructorBuilder .buildConstructor ();
@@ -191,7 +188,8 @@ private AotRepositoryMetadata getAotRepositoryMetadata(List<AotRepositoryMethod>
191
188
private void contributeMethod (Method method , RepositoryComposition repositoryComposition ,
192
189
List <AotRepositoryMethod > methodMetadata , TypeSpec .Builder builder ) {
193
190
194
- if (repositoryInformation .isCustomMethod (method ) || (repositoryInformation .isBaseClassMethod (method ) && !repositoryInformation .isQueryMethod (method ))) {
191
+ if (repositoryInformation .isCustomMethod (method )
192
+ || (repositoryInformation .isBaseClassMethod (method ) && !repositoryInformation .isQueryMethod (method ))) {
195
193
196
194
RepositoryFragment <?> fragment = repositoryComposition .findFragment (method );
197
195
@@ -205,9 +203,9 @@ private void contributeMethod(Method method, RepositoryComposition repositoryCom
205
203
return ;
206
204
}
207
205
208
- if (repositoryInformation .isQueryMethod (method ) && methodContributorFunction != null ) {
206
+ if (repositoryInformation .isQueryMethod (method ) && methodContributorFactory != null ) {
209
207
210
- MethodContributor <? extends QueryMethod > contributor = methodContributorFunction . apply (method ,
208
+ MethodContributor <? extends QueryMethod > contributor = methodContributorFactory . create (method ,
211
209
repositoryInformation );
212
210
213
211
if (contributor != null ) {
@@ -273,16 +271,50 @@ public ProjectionFactory getProjectionFactory() {
273
271
public interface ClassCustomizer {
274
272
275
273
/**
276
- * Apply customization ot the AOT repository fragment class after it has been defined..
274
+ * Apply customization ot the AOT repository fragment class after it has been defined.
277
275
*
278
- * @param information
279
- * @param metadata
280
- * @param builder
276
+ * @param information repository information.
277
+ * @param metadata metadata of the AOT repository fragment.
278
+ * @param builder the actual builder.
281
279
*/
282
280
void customize (RepositoryInformation information , AotRepositoryFragmentMetadata metadata , TypeSpec .Builder builder );
283
281
284
282
}
285
283
284
+ /**
285
+ * Customizer interface to customize the AOT repository fragment constructor through
286
+ * {@link AotRepositoryConstructorBuilder}.
287
+ */
288
+ public interface ConstructorCustomizer {
289
+
290
+ /**
291
+ * Apply customization ot the AOT repository fragment constructor.
292
+ *
293
+ * @param constructorBuilder the builder to be customized.
294
+ */
295
+ void customize (AotRepositoryConstructorBuilder constructorBuilder );
296
+
297
+ }
298
+
299
+ /**
300
+ * Factory interface to conditionally create {@link MethodContributor} instances. An implementation may decide whether
301
+ * to return a {@link MethodContributor} or {@literal null}, if no method (code or metadata) should be contributed.
302
+ */
303
+ public interface MethodContributorFactory {
304
+
305
+ /**
306
+ * Apply customization ot the AOT repository fragment constructor.
307
+ *
308
+ * @param method the method to be contributed.
309
+ * @param information repository information.
310
+ * @return the {@link MethodContributor} to be used. Can be {@literal null} if the method and method metadata should
311
+ * not be contributed.
312
+ */
313
+ @ Nullable
314
+ MethodContributor <? extends QueryMethod > create (Method method , RepositoryInformation information );
315
+
316
+ }
317
+
286
318
record AotBundle (JavaFile javaFile , AotRepositoryMetadata metadata ) {
287
319
}
288
320
0 commit comments