77import static io .quarkus .grpc .deployment .GrpcDotNames .RETRIEVE_CHANNEL_METHOD ;
88import static io .quarkus .grpc .deployment .GrpcInterceptors .MICROMETER_INTERCEPTORS ;
99import static io .quarkus .grpc .deployment .ResourceRegistrationUtils .registerResourcesForProperties ;
10+ import static org .jboss .jandex .gizmo2 .Jandex2Gizmo .classDescOf ;
1011
12+ import java .lang .constant .ClassDesc ;
13+ import java .lang .constant .MethodTypeDesc ;
1114import java .util .ArrayList ;
1215import java .util .Collections ;
1316import java .util .HashMap ;
1619import java .util .Map ;
1720import java .util .Set ;
1821import java .util .function .BiFunction ;
19- import java .util .function .Consumer ;
2022import java .util .regex .Pattern ;
2123
2224import jakarta .enterprise .context .ApplicationScoped ;
3032import org .jboss .jandex .AnnotationTarget .Kind ;
3133import org .jboss .jandex .AnnotationValue ;
3234import org .jboss .jandex .ClassInfo ;
35+ import org .jboss .jandex .ClassType ;
3336import org .jboss .jandex .DotName ;
3437import org .jboss .jandex .FieldInfo ;
3538import org .jboss .jandex .IndexView ;
5962import io .quarkus .deployment .builditem .nativeimage .NativeImageResourceBuildItem ;
6063import io .quarkus .deployment .builditem .nativeimage .RuntimeInitializedClassBuildItem ;
6164import io .quarkus .deployment .recording .RecorderContext ;
62- import io .quarkus .gizmo .MethodCreator ;
63- import io .quarkus .gizmo .MethodDescriptor ;
64- import io .quarkus .gizmo .ResultHandle ;
65+ import io .quarkus .gizmo2 .Const ;
66+ import io .quarkus .gizmo2 .Expr ;
67+ import io .quarkus .gizmo2 .Reflection2Gizmo ;
68+ import io .quarkus .gizmo2 .creator .BlockCreator ;
69+ import io .quarkus .gizmo2 .desc .ClassMethodDesc ;
70+ import io .quarkus .gizmo2 .desc .ConstructorDesc ;
71+ import io .quarkus .gizmo2 .desc .MethodDesc ;
6572import io .quarkus .grpc .GrpcClient ;
6673import io .quarkus .grpc .MutinyClient ;
6774import io .quarkus .grpc .RegisterClientInterceptor ;
@@ -241,19 +248,14 @@ public void generateGrpcClientProducers(List<GrpcClientBuildItem> clients,
241248 .scope (Singleton .class )
242249 .unremovable ()
243250 .forceApplicationClass ()
244- .creator (new Consumer <>() {
245- @ Override
246- public void accept (MethodCreator mc ) {
247- GrpcClientProcessor .this .generateChannelProducer (mc , clientName , clientInfo );
248- }
249- })
251+ .creator (cg -> GrpcClientProcessor .this .generateChannelProducer (cg .createMethod (),
252+ clientName , clientInfo ))
250253 .destroyer (Channels .ChannelDestroyer .class );
251254 if (!clientInfo .interceptors .isEmpty ()) {
252255 for (String interceptorClass : clientInfo .interceptors ) {
253- configurator .addQualifier (AnnotationInstance .create (GrpcDotNames .REGISTER_CLIENT_INTERCEPTOR , null ,
254- new AnnotationValue [] { AnnotationValue .createClassValue ("value" ,
255- Type .create (DotName .createSimple (interceptorClass ),
256- org .jboss .jandex .Type .Kind .CLASS )) }));
256+ configurator .addQualifier (AnnotationInstance .builder (GrpcDotNames .REGISTER_CLIENT_INTERCEPTOR )
257+ .value (ClassType .create (interceptorClass ))
258+ .build ());
257259 }
258260 }
259261 syntheticBeans .produce (configurator .done ());
@@ -267,18 +269,13 @@ public void accept(MethodCreator mc) {
267269 .unremovable ()
268270 .forceApplicationClass ()
269271 .addType (MutinyClient .class )
270- .creator (new Consumer <>() {
271- @ Override
272- public void accept (MethodCreator mc ) {
273- GrpcClientProcessor .this .generateClientProducer (mc , clientName , clientInfo );
274- }
275- });
272+ .creator (cg -> GrpcClientProcessor .this .generateClientProducer (cg .createMethod (),
273+ clientName , clientInfo ));
276274 if (!clientInfo .interceptors .isEmpty ()) {
277275 for (String interceptorClass : clientInfo .interceptors ) {
278- configurator .addQualifier (AnnotationInstance .create (GrpcDotNames .REGISTER_CLIENT_INTERCEPTOR , null ,
279- new AnnotationValue [] { AnnotationValue .createClassValue ("value" ,
280- Type .create (DotName .createSimple (interceptorClass ),
281- org .jboss .jandex .Type .Kind .CLASS )) }));
276+ configurator .addQualifier (AnnotationInstance .builder (GrpcDotNames .REGISTER_CLIENT_INTERCEPTOR )
277+ .value (ClassType .create (interceptorClass ))
278+ .build ());
282279 }
283280 }
284281 syntheticBeans .produce (configurator .done ());
@@ -467,18 +464,9 @@ private DeploymentException invalidInjectionPoint(InjectionPointInfo injectionPo
467464 + " - only Mutiny service interfaces, blocking stubs, reactive stubs based on Mutiny and io.grpc.Channel can be injected via @GrpcClient" );
468465 }
469466
470- private void generateChannelProducer (MethodCreator mc , String clientName , ClientInfo client ) {
471- ResultHandle name = mc .load (clientName );
472- ResultHandle interceptorsArray = mc .newArray (String .class , client .interceptors .size ());
473- int idx = 0 ;
474- for (String interceptor : client .interceptors ) {
475- mc .writeArrayValue (interceptorsArray , idx ++, mc .load (interceptor ));
476- }
477- ResultHandle interceptorsSet = mc .invokeStaticInterfaceMethod (
478- MethodDescriptor .ofMethod (Set .class , "of" , Set .class , Object [].class ), interceptorsArray );
479- ResultHandle result = mc .invokeStaticMethod (CREATE_CHANNEL_METHOD , name , interceptorsSet );
480- mc .returnValue (result );
481- mc .close ();
467+ private void generateChannelProducer (BlockCreator bc , String clientName , ClientInfo client ) {
468+ bc .return_ (bc .invokeStatic (CREATE_CHANNEL_METHOD , Const .of (clientName ),
469+ bc .setOf (client .interceptors .stream ().toList (), Const ::of )));
482470 }
483471
484472 private static Set <DotName > getRawTypeClosure (ClassInfo classInfo , IndexView index ) {
@@ -508,50 +496,40 @@ private static Set<DotName> getRawTypeClosure(ClassInfo classInfo, IndexView ind
508496 return types ;
509497 }
510498
511- private void generateClientProducer (MethodCreator mc , String clientName , ClientInfo clientInfo ) {
512- ResultHandle name = mc .load (clientName );
513- ResultHandle interceptorsArray = mc .newArray (String .class , clientInfo .interceptors .size ());
514- int idx = 0 ;
515- for (String interceptor : clientInfo .interceptors ) {
516- mc .writeArrayValue (interceptorsArray , idx ++, mc .load (interceptor ));
517- }
518- ResultHandle interceptorsSet = mc .invokeStaticInterfaceMethod (
519- MethodDescriptor .ofMethod (Set .class , "of" , Set .class , Object [].class ), interceptorsArray );
499+ private void generateClientProducer (BlockCreator bc , String clientName , ClientInfo clientInfo ) {
500+ Const name = Const .of (clientName );
520501
521502 // First obtain the channel instance for the given service name
522- ResultHandle channel = mc . invokeStaticMethod (RETRIEVE_CHANNEL_METHOD , name , interceptorsSet );
523- ResultHandle client ;
503+ Expr channel = bc . invokeStatic (RETRIEVE_CHANNEL_METHOD , name ,
504+ bc . setOf ( clientInfo . interceptors . stream (). toList (), Const :: of )) ;
524505
506+ Expr client ;
525507 if (clientInfo .type == ClientType .MUTINY_CLIENT ) {
526508 // Instantiate the client, e.g. new HealthClient(serviceName,channel,GrpcClientConfigProvider.getStubConfigurator())
527- ResultHandle stubConfigurator = mc .invokeStaticMethod (GrpcDotNames .GET_STUB_CONFIGURATOR );
528- client = mc .newInstance (
529- MethodDescriptor .ofConstructor (clientInfo .implName .toString (), String .class .getName (),
530- Channel .class .getName (), BiFunction .class ),
531- name , channel , stubConfigurator );
509+ client = bc .new_ (
510+ ConstructorDesc .of (classDescOf (clientInfo .implName ), String .class , Channel .class , BiFunction .class ),
511+ name , channel , bc .invokeStatic (GrpcDotNames .GET_STUB_CONFIGURATOR ));
532512 } else {
533513 // Create the stub, e.g. newBlockingStub(channel)
534- MethodDescriptor factoryMethod = MethodDescriptor
535- .ofMethod (convertToServiceName (clientInfo .className ), clientInfo .type .getFactoryMethodName (),
536- clientInfo .className .toString (),
537- Channel .class .getName ());
538- client = mc .invokeStaticMethod (factoryMethod , channel );
514+ MethodDesc factoryMethod = ClassMethodDesc .of (convertToServiceName (clientInfo .className ),
515+ clientInfo .type .getFactoryMethodName (),
516+ MethodTypeDesc .of (classDescOf (clientInfo .className ), Reflection2Gizmo .classDescOf (Channel .class )));
517+ client = bc .invokeStatic (factoryMethod , channel );
539518
540519 // If needed, modify the call options, e.g. stub = stub.withCompression("gzip")
541- client = mc . invokeStaticMethod (CONFIGURE_STUB , name , client );
520+ client = bc . invokeStatic (CONFIGURE_STUB , name , client );
542521 if (clientInfo .type .isBlocking ()) {
543- client = mc . invokeStaticMethod (ADD_BLOCKING_CLIENT_INTERCEPTOR , client );
522+ client = bc . invokeStatic (ADD_BLOCKING_CLIENT_INTERCEPTOR , client );
544523 }
545524 }
546- mc .returnValue (client );
547- mc .close ();
525+ bc .return_ (client );
548526 }
549527
550- private String convertToServiceName (DotName stubName ) {
528+ private static ClassDesc convertToServiceName (DotName stubName ) {
551529 if (stubName .isInner ()) {
552- return stubName .prefix (). toString ( );
530+ return classDescOf ( stubName .prefix ());
553531 } else {
554- return stubName . toString ( );
532+ return classDescOf ( stubName );
555533 }
556534 }
557535}
0 commit comments