3131import io .quarkus .arc .InterceptionProxy ;
3232import io .quarkus .arc .SyntheticCreationalContext ;
3333import io .quarkus .arc .processor .InjectionPointInfo .TypeAndQualifiers ;
34- import io .quarkus .gizmo .FieldDescriptor ;
35- import io .quarkus .gizmo .MethodCreator ;
36- import io .quarkus .gizmo .MethodDescriptor ;
37- import io .quarkus .gizmo .ResultHandle ;
34+ import io .quarkus .gizmo2 .Var ;
35+ import io .quarkus .gizmo2 .creator .BlockCreator ;
36+ import io .quarkus .gizmo2 .creator .ClassCreator ;
37+ import io .quarkus .gizmo2 .desc .FieldDesc ;
38+ import io .quarkus .gizmo2 .desc .MethodDesc ;
3839
3940/**
4041 * This construct is not thread-safe.
@@ -52,8 +53,8 @@ public abstract class BeanConfiguratorBase<THIS extends BeanConfiguratorBase<THI
5253 protected Boolean alternative ;
5354 protected final List <StereotypeInfo > stereotypes ;
5455 protected String name ;
55- protected Consumer <MethodCreator > creatorConsumer ;
56- protected Consumer <MethodCreator > destroyerConsumer ;
56+ protected Consumer <CreateGeneration > creatorConsumer ;
57+ protected Consumer <DestroyGeneration > destroyerConsumer ;
5758 protected boolean defaultBean ;
5859 protected boolean removable ;
5960 protected Type providerType ;
@@ -63,7 +64,7 @@ public abstract class BeanConfiguratorBase<THIS extends BeanConfiguratorBase<THI
6364 protected final Set <TypeAndQualifiers > injectionPoints ;
6465 protected Integer startupPriority ;
6566 protected InterceptionProxyInfo interceptionProxy ;
66- protected Consumer <MethodCreator > checkActiveConsumer ;
67+ protected Consumer <CheckActiveGeneration > checkActiveConsumer ;
6768
6869 protected BeanConfiguratorBase (DotName implClazz ) {
6970 this .implClazz = implClazz ;
@@ -434,13 +435,12 @@ public THIS injectInterceptionProxy(DotName bindingsSource) {
434435 }
435436
436437 public <U extends T > THIS creator (Class <? extends BeanCreator <U >> creatorClazz ) {
437- return creator (mc -> {
438+ return creator (cg -> {
439+ BlockCreator bc = cg .createMethod ();
440+
438441 // return new FooBeanCreator().create(syntheticCreationalContext)
439- ResultHandle creatorHandle = mc .newInstance (MethodDescriptor .ofConstructor (creatorClazz ));
440- ResultHandle ret = mc .invokeInterfaceMethod (
441- MethodDescriptor .ofMethod (BeanCreator .class , "create" , Object .class , SyntheticCreationalContext .class ),
442- creatorHandle , mc .getMethodParam (0 ));
443- mc .returnValue (ret );
442+ MethodDesc createDesc = MethodDesc .of (BeanCreator .class , "create" , Object .class , SyntheticCreationalContext .class );
443+ bc .return_ (bc .invokeInterface (createDesc , bc .new_ (creatorClazz ), cg .syntheticCreationalContext ()));
444444 });
445445 }
446446
@@ -452,32 +452,27 @@ public <U extends T> THIS creator(Class<? extends BeanCreator<U>> creatorClazz)
452452 * Furthermore, the consumer can also read the instance field of name {@code params} and type {@link Map}. This map holds
453453 * all parameters set via one of the {@code BeanConfigurator#param()} methods.
454454 *
455- * @param methodCreatorConsumer
456- * @return self
457455 */
458- public THIS creator (Consumer <MethodCreator > methodCreatorConsumer ) {
459- this .creatorConsumer = methodCreatorConsumer ;
456+ public THIS creator (Consumer <CreateGeneration > creatorConsumer ) {
457+ this .creatorConsumer = creatorConsumer ;
460458 return cast (this );
461459 }
462460
463461 public <U extends T > THIS destroyer (Class <? extends BeanDestroyer <U >> destroyerClazz ) {
464- return destroyer (mc -> {
462+ return destroyer (dg -> {
463+ BlockCreator bc = dg .destroyMethod ();
464+
465465 // new FooBeanDestroyer().destroy(instance, context, params)
466- ResultHandle paramsHandle = mc .readInstanceField (
467- FieldDescriptor .of (mc .getMethodDescriptor ().getDeclaringClass (), "params" , Map .class ),
468- mc .getThis ());
469- ResultHandle destoyerHandle = mc .newInstance (MethodDescriptor .ofConstructor (destroyerClazz ));
470- ResultHandle [] params = { mc .getMethodParam (0 ), mc .getMethodParam (1 ), paramsHandle };
471- mc .invokeInterfaceMethod (
472- MethodDescriptor .ofMethod (BeanDestroyer .class , "destroy" , void .class , Object .class , CreationalContext .class ,
473- Map .class ),
474- destoyerHandle , params );
475- mc .returnValue (null );
466+ MethodDesc destroyDesc = MethodDesc .of (BeanDestroyer .class , "destroy" , void .class , Object .class ,
467+ CreationalContext .class , Map .class );
468+ bc .invokeInterface (destroyDesc , bc .new_ (destroyerClazz ), dg .destroyedInstance (),
469+ dg .creationalContext (), dg .paramsMap ());
470+ bc .return_ ();
476471 });
477472 }
478473
479- public THIS destroyer (Consumer <MethodCreator > methodCreatorConsumer ) {
480- this .destroyerConsumer = methodCreatorConsumer ;
474+ public THIS destroyer (Consumer <DestroyGeneration > destroyerConsumer ) {
475+ this .destroyerConsumer = destroyerConsumer ;
481476 return cast (this );
482477 }
483478
@@ -487,11 +482,11 @@ public THIS destroyer(Consumer<MethodCreator> methodCreatorConsumer) {
487482 * @see #checkActive(Consumer)
488483 */
489484 public THIS checkActive (Class <? extends Supplier <ActiveResult >> checkActiveClazz ) {
490- return checkActive (mc -> {
485+ return checkActive (cag -> {
486+ BlockCreator bc = cag .checkActiveMethod ();
487+
491488 // return new FooActiveResultSupplier().get()
492- ResultHandle supplierHandle = mc .newInstance (MethodDescriptor .ofConstructor (checkActiveClazz ));
493- mc .returnValue (mc .invokeInterfaceMethod (MethodDescriptor .ofMethod (Supplier .class , "get" , Object .class ),
494- supplierHandle ));
489+ bc .return_ (bc .invokeInterface (MethodDescs .SUPPLIER_GET , bc .new_ (checkActiveClazz )));
495490 });
496491 }
497492
@@ -507,8 +502,8 @@ public THIS checkActive(Class<? extends Supplier<ActiveResult>> checkActiveClazz
507502 *
508503 * @return the procedure that generates the bytecode for checking whether this bean is active or not
509504 */
510- public THIS checkActive (Consumer <MethodCreator > methodCreatorConsumer ) {
511- this .checkActiveConsumer = methodCreatorConsumer ;
505+ public THIS checkActive (Consumer <CheckActiveGeneration > checkActiveConsumer ) {
506+ this .checkActiveConsumer = checkActiveConsumer ;
512507 return cast (this );
513508 }
514509
@@ -537,4 +532,88 @@ public void accept(AnnotationInstance qualifier) {
537532 addQualifier (qualifier );
538533 }
539534
535+ public interface CreateGeneration {
536+ /**
537+ * {@return the generated class of the synthetic bean}
538+ * This class contains the generated {@code create} method.
539+ *
540+ * @see #createMethod()
541+ */
542+ ClassCreator beanClass ();
543+
544+ /**
545+ * {@return the {@link BlockCreator} for the generated {@code create} method}
546+ * This method is supposed to contain the creation logic.
547+ */
548+ BlockCreator createMethod ();
549+
550+ /**
551+ * {@return the field on the {@link #beanClass()} that contains the parameter map}
552+ */
553+ default Var paramsMap () {
554+ ClassCreator cc = beanClass ();
555+ return cc .this_ ().field (FieldDesc .of (cc .type (), "params" , Map .class ));
556+ }
557+
558+ /**
559+ * {@return the parameter of the generated creation method that contains the {@link SyntheticCreationalContext}}
560+ *
561+ * @see #createMethod()
562+ */
563+ Var syntheticCreationalContext ();
564+ }
565+
566+ public interface DestroyGeneration {
567+ /**
568+ * {@return the generated class of the synthetic bean}
569+ * This class contains the generated {@code destroy} method.
570+ *
571+ * @see #destroyMethod()
572+ */
573+ ClassCreator beanClass ();
574+
575+ /**
576+ * {@return the {@link BlockCreator} for the generated {@code destroy} method}
577+ * This method is supposed to contain the destruction logic.
578+ */
579+ BlockCreator destroyMethod ();
580+
581+ /**
582+ * {@return the field on the {@link #beanClass()} that contains the parameter map}
583+ */
584+ default Var paramsMap () {
585+ ClassCreator cc = beanClass ();
586+ return cc .this_ ().field (FieldDesc .of (cc .type (), "params" , Map .class ));
587+ }
588+
589+ /**
590+ * {@return the parameter of the generated destruction method that contains the destroyed instance}
591+ *
592+ * @see #destroyMethod()
593+ */
594+ Var destroyedInstance ();
595+
596+ /**
597+ * {@return the parameter of the generated destruction method that contains the {@link CreationalContext}}
598+ *
599+ * @see #destroyMethod()
600+ */
601+ Var creationalContext ();
602+ }
603+
604+ public interface CheckActiveGeneration {
605+ /**
606+ * {@return the generated class of the synthetic bean}
607+ * This class contains the generated {@code checkActive} method.
608+ *
609+ * @see #checkActiveMethod()
610+ */
611+ ClassCreator beanClass ();
612+
613+ /**
614+ * {@return the {@link BlockCreator} for the generated {@code checkActive} method}
615+ * This method is supposed to contain the logic to determine whether the bean is active or not.
616+ */
617+ BlockCreator checkActiveMethod ();
618+ }
540619}
0 commit comments