99import java .lang .reflect .Modifier ;
1010import java .util .Arrays ;
1111import java .util .Comparator ;
12- import java .util .Objects ;
1312import java .util .Optional ;
1413import java .util .ServiceConfigurationError ;
1514import java .util .ServiceLoader ;
@@ -49,8 +48,7 @@ public static <T> Stream<T> loadAll(Class<T> clazz) {
4948 .filter (IntegrationsLoader ::isSupportedOperatingSystem )
5049 .filter (IntegrationsLoader ::passesStaticAvailabilityCheck )
5150 .sorted (Comparator .comparingInt (IntegrationsLoader ::getPriority ).reversed ())
52- .map (IntegrationsLoader ::instantiateServiceProvider )
53- .filter (Objects ::nonNull )
51+ .flatMap (IntegrationsLoader ::instantiateServiceProvider )
5452 .filter (IntegrationsLoader ::passesInstanceAvailabilityCheck )
5553 .peek (impl -> logServiceIsAvailable (clazz , impl .getClass ()));
5654 }
@@ -71,12 +69,20 @@ private static boolean isSupportedOperatingSystem(ServiceLoader.Provider<?> prov
7169 return annotations .length == 0 || Arrays .stream (annotations ).anyMatch (OperatingSystem .Value ::isCurrent );
7270 }
7371
74- private static <T > T instantiateServiceProvider (ServiceLoader .Provider <T > provider ) {
72+ private static <T > Stream < T > instantiateServiceProvider (ServiceLoader .Provider <T > provider ) {
7573 try {
76- return provider .get ();
74+ return Stream . of ( provider .get () );
7775 } catch (ServiceConfigurationError err ) {
78- LOG .warn ("Unable to load service provider {}." , provider .type ().getName (), err );
79- return null ;
76+ //ServiceLoader.Provider::get throws this error if (from javadoc)
77+ if (err .getCause () == null || //the public static "provider()" method of a provider factory returns null
78+ err .getCause () instanceof ExceptionInInitializerError || // * the service provider cannot be instantiated,
79+ err .getCause () instanceof NoClassDefFoundError ||
80+ err .getCause () instanceof RuntimeException ) {
81+ LOG .warn ("Unable to load service provider {}." , provider .type ().getName (), err );
82+ return Stream .empty ();
83+ } else {
84+ throw err ;
85+ }
8086 }
8187 }
8288
0 commit comments