11/*
2- * Copyright (c) 2023, 2024 Oracle and/or its affiliates.
2+ * Copyright (c) 2023, 2025 Oracle and/or its affiliates.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
2525import java .util .Locale ;
2626import java .util .Objects ;
2727import java .util .Optional ;
28- import java .util .ServiceLoader ;
2928import java .util .Set ;
3029import java .util .function .Supplier ;
3130import java .util .stream .Collectors ;
3837import io .helidon .codegen .classmodel .Method ;
3938import io .helidon .codegen .classmodel .TypeArgument ;
4039import io .helidon .common .Errors ;
41- import io .helidon .common .HelidonServiceLoader ;
4240import io .helidon .common .types .AccessModifier ;
4341import io .helidon .common .types .Annotations ;
4442import io .helidon .common .types .TypeName ;
4543import io .helidon .common .types .TypeNames ;
4644
45+ import static io .helidon .builder .codegen .Types .BUILDER_SUPPORT ;
4746import static io .helidon .builder .codegen .Types .CONFIG_BUILDER_SUPPORT ;
4847import static io .helidon .builder .codegen .Types .REGISTRY_BUILDER_SUPPORT ;
4948import static io .helidon .codegen .CodegenUtil .capitalize ;
@@ -624,8 +623,7 @@ private static void preBuildPrototypeMethod(InnerClass.Builder classBuilder,
624623 property ,
625624 propertyConfigured ,
626625 configuredOption ,
627- providerType ,
628- defaultDiscoverServices );
626+ providerType );
629627 } else {
630628 serviceLoaderPropertyDiscovery (preBuildBuilder ,
631629 property ,
@@ -655,24 +653,16 @@ private static void serviceLoaderPropertyDiscovery(Method.Builder preBuildBuilde
655653 AnnotationDataOption configuredOption ,
656654 TypeName providerType ,
657655 boolean defaultDiscoverServices ) {
658- preBuildBuilder .addContentLine ("{" );
659- preBuildBuilder .addContent ("var serviceLoader = " )
660- .addContent (HelidonServiceLoader .class )
661- .addContent (".create(" )
662- .addContent (ServiceLoader .class )
663- .addContent (".load(" )
664- .addContent (providerType .genericTypeName ())
665- .addContentLine (".class));" );
656+ TypeName typeName = property .typeHandler ().declaredType ();
666657 if (propertyConfigured ) {
667- TypeName typeName = property .typeHandler ().declaredType ();
668658 if (typeName .isList () || typeName .isSet ()) {
669659 preBuildBuilder .addContent ("this.add" )
670660 .addContent (capitalize (property .name ()))
671661 .addContent ("(" )
672662 .addContent (CONFIG_BUILDER_SUPPORT )
673663 .addContent (".discoverServices(config, \" " )
674664 .addContent (configuredOption .configKey ())
675- .addContent ("\" , serviceLoader, " )
665+ .addContent ("\" , " )
676666 .addContent (providerType .genericTypeName ())
677667 .addContent (".class, " )
678668 .addContent (property .typeHandler ().actualType ().genericTypeName ())
@@ -686,7 +676,7 @@ private static void serviceLoaderPropertyDiscovery(Method.Builder preBuildBuilde
686676 .addContent (CONFIG_BUILDER_SUPPORT )
687677 .addContent (".discoverService(config, \" " )
688678 .addContent (configuredOption .configKey ())
689- .addContent ("\" , serviceLoader, " )
679+ .addContent ("\" , " )
690680 .addContent (providerType )
691681 .addContent (".class, " )
692682 .addContent (property .typeHandler ().actualType ().genericTypeName ())
@@ -701,11 +691,34 @@ private static void serviceLoaderPropertyDiscovery(Method.Builder preBuildBuilde
701691 .addContentLine (");" );
702692 }
703693 } else {
704- if (defaultDiscoverServices ) {
705- preBuildBuilder .addContentLine ("this." + property .name () + "(serviceLoader.asList());" );
694+ if (typeName .isList () || typeName .isSet ()) {
695+ preBuildBuilder .addContent ("this.add" )
696+ .addContent (capitalize (property .name ()))
697+ .addContent ("(" )
698+ .addContent (BUILDER_SUPPORT )
699+ .addContent (".discoverServices(" )
700+ .addContent (providerType .genericTypeName ())
701+ .addContent (".class, " )
702+ .addContent (property .name ())
703+ .addContent ("DiscoverServices, " )
704+ .addContent (property .name ())
705+ .addContentLine ("));" );
706+ } else {
707+ preBuildBuilder
708+ .addContent (BUILDER_SUPPORT )
709+ .addContent (".discoverService(" )
710+ .addContent (providerType )
711+ .addContent (".class, " )
712+ .addContent (property .name ())
713+ .addContent ("DiscoverServices, " )
714+ .addContent (Optional .class )
715+ .addContent (".ofNullable(" )
716+ .addContent (property .name ())
717+ .addContent (")).ifPresent(this::" )
718+ .addContent (property .setterName ())
719+ .addContentLine (");" );
706720 }
707721 }
708- preBuildBuilder .addContentLine ("}" );
709722 }
710723
711724 private static void serviceRegistryProperty (Method .Builder preBuildBuilder ,
@@ -754,10 +767,9 @@ private static void serviceRegistryPropertyDiscovery(Method.Builder preBuildBuil
754767 PrototypeProperty property ,
755768 boolean propertyConfigured ,
756769 AnnotationDataOption configuredOption ,
757- TypeName providerType ,
758- boolean defaultDiscoverServices ) {
770+ TypeName providerType ) {
771+ TypeName typeName = property . typeHandler (). declaredType ();
759772 if (propertyConfigured ) {
760- TypeName typeName = property .typeHandler ().declaredType ();
761773 if (typeName .isList () || typeName .isSet ()) {
762774 preBuildBuilder .addContent ("this.add" )
763775 .addContent (capitalize (property .name ()))
@@ -811,10 +823,42 @@ private static void serviceRegistryPropertyDiscovery(Method.Builder preBuildBuil
811823 .decreaseContentPadding ();
812824 }
813825 } else {
814- if (defaultDiscoverServices ) {
815- preBuildBuilder .addContent ("this." + property .name () + "(registry.all(" )
816- .addContent (providerType .genericTypeName ())
817- .addContentLine (".class));" );
826+ if (typeName .isList ()) {
827+ preBuildBuilder
828+ .addContent ("this.add" )
829+ .addContent (capitalize (property .name ()))
830+ .addContent ("(" )
831+ .addContent (REGISTRY_BUILDER_SUPPORT )
832+ .addContent (".serviceList(registry, " )
833+ .addContentCreate (property .typeHandler ().actualType ())
834+ .addContent (", " )
835+ .addContent (property .name ())
836+ .addContentLine ("DiscoverServices));" );
837+ } else if (typeName .isSet ()) {
838+ preBuildBuilder
839+ .addContent ("this.add" )
840+ .addContent (capitalize (property .name ()))
841+ .addContent ("(" )
842+ .addContent (REGISTRY_BUILDER_SUPPORT )
843+ .addContent (".serviceSet(registry, " )
844+ .addContentCreate (property .typeHandler ().actualType ())
845+ .addContent (", " )
846+ .addContent (property .name ())
847+ .addContentLine ("DiscoverServices));" );
848+ } else {
849+ preBuildBuilder
850+ .addContent (REGISTRY_BUILDER_SUPPORT )
851+ .addContent (".service(registry, " )
852+ .addContentCreate (property .typeHandler ().actualType ())
853+ .addContent (", " )
854+ .addContent (Optional .class )
855+ .addContent (".ofNullable(" )
856+ .addContent (property .name ())
857+ .addContent ("), " )
858+ .addContent (property .name ())
859+ .addContent ("DiscoverServices).ifPresent(this::" )
860+ .addContent (property .setterName ())
861+ .addContentLine (");" );
818862 }
819863 }
820864 }
0 commit comments