22
22
import java .util .LinkedHashMap ;
23
23
import java .util .List ;
24
24
import java .util .Map ;
25
+ import java .util .function .Consumer ;
25
26
26
27
import javax .lang .model .element .Modifier ;
27
28
43
44
import org .springframework .javapoet .MethodSpec ;
44
45
import org .springframework .javapoet .TypeName ;
45
46
import org .springframework .javapoet .TypeSpec ;
47
+ import org .springframework .util .Assert ;
46
48
47
49
/**
48
50
* Builder for AOT repository fragments.
@@ -59,9 +61,9 @@ class AotRepositoryBuilder {
59
61
private final ProjectionFactory projectionFactory ;
60
62
private final AotRepositoryFragmentMetadata generationMetadata ;
61
63
62
- private @ Nullable ConstructorCustomizer constructorCustomizer ;
64
+ private @ Nullable Consumer < AotRepositoryConstructorBuilder > constructorCustomizer ;
63
65
private @ Nullable MethodContributorFactory methodContributorFactory ;
64
- private ClassCustomizer customizer ;
66
+ private Consumer < AotRepositoryClassBuilder > classCustomizer ;
65
67
66
68
private AotRepositoryBuilder (RepositoryInformation repositoryInformation , String moduleName ,
67
69
ProjectionFactory projectionFactory ) {
@@ -76,7 +78,7 @@ private AotRepositoryBuilder(RepositoryInformation repositoryInformation, String
76
78
.initializer ("$T.getLog($T.class)" , TypeName .get (LogFactory .class ), this .generationMetadata .getTargetTypeName ())
77
79
.build ());
78
80
79
- this .customizer = (info , builder ) -> {};
81
+ this .classCustomizer = (builder ) -> {};
80
82
}
81
83
82
84
/**
@@ -93,14 +95,14 @@ public static AotRepositoryBuilder forRepository(RepositoryInformation informati
93
95
}
94
96
95
97
/**
96
- * Configure a {@link ClassCustomizer } customizer.
98
+ * Configure a {@link AotRepositoryConstructorBuilder } customizer.
97
99
*
98
100
* @param classCustomizer must not be {@literal null}.
99
101
* @return {@code this}.
100
102
*/
101
- public AotRepositoryBuilder withClassCustomizer (ClassCustomizer classCustomizer ) {
103
+ public AotRepositoryBuilder withClassCustomizer (Consumer < AotRepositoryClassBuilder > classCustomizer ) {
102
104
103
- this .customizer = classCustomizer ;
105
+ this .classCustomizer = classCustomizer ;
104
106
return this ;
105
107
}
106
108
@@ -110,7 +112,8 @@ public AotRepositoryBuilder withClassCustomizer(ClassCustomizer classCustomizer)
110
112
* @param constructorCustomizer must not be {@literal null}.
111
113
* @return {@code this}.
112
114
*/
113
- public AotRepositoryBuilder withConstructorCustomizer (ConstructorCustomizer constructorCustomizer ) {
115
+ public AotRepositoryBuilder withConstructorCustomizer (
116
+ Consumer <AotRepositoryConstructorBuilder > constructorCustomizer ) {
114
117
115
118
this .constructorCustomizer = constructorCustomizer ;
116
119
return this ;
@@ -162,7 +165,12 @@ public AotBundle build() {
162
165
generationMetadata .getFields ().values ().forEach (builder ::addField );
163
166
164
167
// finally customize the file itself
165
- this .customizer .customize (repositoryInformation , builder );
168
+ this .classCustomizer .accept (customizer -> {
169
+
170
+ Assert .notNull (customizer , "ClassCustomizer must not be null" );
171
+ customizer .customize (builder );
172
+ });
173
+
166
174
JavaFile javaFile = JavaFile .builder (packageName (), builder .build ()).build ();
167
175
AotRepositoryMetadata metadata = getAotRepositoryMetadata (methodMetadata );
168
176
@@ -171,11 +179,11 @@ public AotBundle build() {
171
179
172
180
private MethodSpec buildConstructor () {
173
181
174
- AotRepositoryConstructorBuilder constructorBuilder = new AotRepositoryConstructorBuilder ( repositoryInformation ,
182
+ RepositoryConstructorBuilder constructorBuilder = new RepositoryConstructorBuilder (
175
183
generationMetadata );
176
184
177
185
if (constructorCustomizer != null ) {
178
- constructorCustomizer .customize (constructorBuilder );
186
+ constructorCustomizer .accept (constructorBuilder );
179
187
}
180
188
181
189
return constructorBuilder .buildConstructor ();
@@ -213,8 +221,7 @@ private void contributeMethod(Method method, RepositoryComposition repositoryCom
213
221
214
222
if (repositoryInformation .isQueryMethod (method ) && methodContributorFactory != null ) {
215
223
216
- MethodContributor <? extends QueryMethod > contributor = methodContributorFactory .create (method ,
217
- repositoryInformation );
224
+ MethodContributor <? extends QueryMethod > contributor = methodContributorFactory .create (method );
218
225
219
226
if (contributor != null ) {
220
227
@@ -273,20 +280,6 @@ public ProjectionFactory getProjectionFactory() {
273
280
return projectionFactory ;
274
281
}
275
282
276
- /**
277
- * Customizer interface to customize the AOT repository fragment class after it has been defined.
278
- */
279
- public interface ClassCustomizer {
280
-
281
- /**
282
- * Apply customization ot the AOT repository fragment class after it has been defined.
283
- *
284
- * @param information the repository information that is used for the AOT fragment.
285
- * @param builder the class builder to be customized.
286
- */
287
- void customize (RepositoryInformation information , TypeSpec .Builder builder );
288
-
289
- }
290
283
291
284
/**
292
285
* Customizer interface to customize the AOT repository fragment constructor through
@@ -313,12 +306,11 @@ public interface MethodContributorFactory {
313
306
* Apply customization ot the AOT repository fragment constructor.
314
307
*
315
308
* @param method the method to be contributed.
316
- * @param information repository information.
317
309
* @return the {@link MethodContributor} to be used. Can be {@literal null} if the method and method metadata should
318
310
* not be contributed.
319
311
*/
320
312
@ Nullable
321
- MethodContributor <? extends QueryMethod > create (Method method , RepositoryInformation information );
313
+ MethodContributor <? extends QueryMethod > create (Method method );
322
314
323
315
}
324
316
0 commit comments