@@ -29,7 +29,7 @@ final class BeanReader {
29
29
private final List <MethodReader > injectMethods ;
30
30
private final List <MethodReader > factoryMethods ;
31
31
private final List <MethodReader > observerMethods ;
32
- private final Element postConstructMethod ;
32
+ private final Optional < MethodReader > postConstructMethod ;
33
33
private final Element preDestroyMethod ;
34
34
35
35
private final ImportTypeMap importTypes = new ImportTypeMap ();
@@ -159,6 +159,8 @@ BeanReader read() {
159
159
factoryMethod .addImports (importTypes );
160
160
}
161
161
162
+ postConstructMethod .ifPresent (m -> m .addImports (importTypes ));
163
+
162
164
conditions .addImports (importTypes );
163
165
return this ;
164
166
}
@@ -236,6 +238,13 @@ Set<UType> allGenericTypes() {
236
238
for (MethodReader factoryMethod : factoryMethods ()) {
237
239
factoryMethod .addDependsOnGeneric (allUTypes );
238
240
}
241
+
242
+ postConstructMethod .ifPresent (
243
+ m ->
244
+ m .params ().stream ()
245
+ .filter (MethodParam ::isGenericParam )
246
+ .map (MethodParam ::getFullUType )
247
+ .forEach (allUTypes ::add ));
239
248
return allUTypes ;
240
249
}
241
250
@@ -260,7 +269,7 @@ String metaKey() {
260
269
* Return true if lifecycle via annotated methods is required.
261
270
*/
262
271
boolean hasLifecycleMethods () {
263
- return (postConstructMethod != null || preDestroyMethod != null || typeReader .isClosable ());
272
+ return (postConstructMethod . isPresent () || preDestroyMethod != null || typeReader .isClosable ());
264
273
}
265
274
266
275
List <MetaData > createFactoryMethodMeta () {
@@ -322,9 +331,10 @@ void buildRegister(Append writer) {
322
331
323
332
void addLifecycleCallbacks (Append writer , String indent ) {
324
333
325
- if (postConstructMethod != null && !registerProvider ()) {
326
- writer . indent ( indent ). append ( " builder.addPostConstruct($bean::%s);" , postConstructMethod .getSimpleName ()). eol ( );
334
+ if (postConstructMethod . isPresent () && !registerProvider ()) {
335
+ writePostConstruct ( writer , indent , postConstructMethod .get () );
327
336
}
337
+
328
338
if (preDestroyMethod != null ) {
329
339
lifeCycleNotSupported ("@PreDestroy" );
330
340
var priority = preDestroyPriority == null || preDestroyPriority == 1000 ? "" : ", " + preDestroyPriority ;
@@ -334,17 +344,54 @@ void addLifecycleCallbacks(Append writer, String indent) {
334
344
}
335
345
}
336
346
347
+ private void writePostConstruct (Append writer , String indent , MethodReader postConstruct ) {
348
+
349
+ writer .indent (indent ).append (" builder.addPostConstruct(" );
350
+ final var simplename = postConstruct .name ();
351
+
352
+ final var params = postConstruct .params ();
353
+ if (params .isEmpty () || Constants .BEANSCOPE .equals (params .get (0 ).getFullUType ().shortType ())) {
354
+ writer .append ("$bean::%s);" , simplename ).eol ();
355
+ } else {
356
+ writer .append ("b -> $bean.%s(" , simplename );
357
+
358
+ writeLifeCycleGet (writer , params , "b" , "b" );
359
+ writer .append (");" ).eol ();
360
+ }
361
+ }
362
+
337
363
void prototypePostConstruct (Append writer , String indent ) {
338
- if (postConstructMethod != null ) {
339
- var postConstruct = (ExecutableElement ) postConstructMethod ;
340
- writer .indent (indent ).append (" bean.%s(" , postConstructMethod .getSimpleName ());
341
- if (postConstruct .getParameters ().isEmpty ()) {
342
- writer .append (");" ).eol ();
364
+ postConstructMethod .ifPresent (
365
+ m -> {
366
+ writer .indent (indent ).append (" bean.%s(" , m .name ());
367
+ if (m .params ().isEmpty ()) {
368
+ writer .append (");" ).eol ();
369
+ } else {
370
+ writeLifeCycleGet (
371
+ writer , m .params (), "builder" , "builder.get(io.avaje.inject.BeanScope.class)" );
372
+ writer .append (";" ).eol ();
373
+ }
374
+ writer .eol ();
375
+ });
376
+ }
377
+
378
+ private void writeLifeCycleGet (
379
+ Append writer , final List <MethodParam > params , String builderName , String beanScopeString ) {
380
+ final var size = params .size ();
381
+ for (int i = 0 ; i < size ; i ++) {
382
+ final var param = params .get (i );
383
+
384
+ if (Constants .BEANSCOPE .equals (param .getFullUType ().fullWithoutAnnotations ())) {
385
+ writer .append (beanScopeString );
343
386
} else {
344
- writer .append ("builder.get(io.avaje.inject.BeanScope.class));" ).eol ();
387
+ param .builderGetDependency (writer , builderName );
388
+ }
389
+
390
+ if (i + 1 != size ) {
391
+ writer .append (", " );
345
392
}
346
- writer .eol ();
347
393
}
394
+ writer .append (")" );
348
395
}
349
396
350
397
private void lifeCycleNotSupported (String lifecycle ) {
0 commit comments