9
9
import java .util .Iterator ;
10
10
import java .util .Map ;
11
11
import java .util .ServiceLoader ;
12
+ import java .util .function .Supplier ;
12
13
import java .util .stream .Collectors ;
13
14
import java .util .stream .Stream ;
14
15
15
16
import static java .util .Objects .requireNonNull ;
16
17
17
18
public final class ObjectFactoryServiceLoader {
18
19
20
+ private final Supplier <ClassLoader > classLoaderSupplier ;
19
21
private final Options options ;
20
22
21
- public ObjectFactoryServiceLoader (Options options ) {
23
+ public ObjectFactoryServiceLoader (Supplier <ClassLoader > classLoaderSupplier , Options options ) {
24
+ this .classLoaderSupplier = requireNonNull (classLoaderSupplier );
22
25
this .options = requireNonNull (options );
23
26
}
24
27
@@ -38,9 +41,9 @@ public ObjectFactoryServiceLoader(Options options) {
38
41
* @return an instance of {@link ObjectFactory}
39
42
*/
40
43
ObjectFactory loadObjectFactory () {
41
- Class <? extends ObjectFactory > objectFactoryClass = this . options .getObjectFactoryClass ();
42
-
43
- final ServiceLoader <ObjectFactory > loader = ServiceLoader .load (ObjectFactory .class );
44
+ Class <? extends ObjectFactory > objectFactoryClass = options .getObjectFactoryClass ();
45
+ ClassLoader classLoader = classLoaderSupplier . get ();
46
+ ServiceLoader <ObjectFactory > loader = ServiceLoader .load (ObjectFactory .class , classLoader );
44
47
if (objectFactoryClass == null ) {
45
48
return loadSingleObjectFactoryOrDefault (loader );
46
49
@@ -50,7 +53,7 @@ ObjectFactory loadObjectFactory() {
50
53
}
51
54
52
55
private static ObjectFactory loadSingleObjectFactoryOrDefault (ServiceLoader <ObjectFactory > loader ) {
53
- final Iterator <ObjectFactory > objectFactories = loader .iterator ();
56
+ Iterator <ObjectFactory > objectFactories = loader .iterator ();
54
57
55
58
ObjectFactory objectFactory ;
56
59
if (objectFactories .hasNext ()) {
@@ -134,8 +137,8 @@ private <T> T cacheNewInstance(Class<T> type) {
134
137
return instance ;
135
138
} catch (NoSuchMethodException e ) {
136
139
throw new CucumberException (String .format (
137
- "%s doesn't have an empty constructor. If you need dependency injection, put cucumber-picocontainer on the classpath" ,
138
- type ), e );
140
+ "%s doesn't have an empty constructor. If you need dependency injection, put cucumber-picocontainer on the classpath" ,
141
+ type ), e );
139
142
} catch (Exception e ) {
140
143
throw new CucumberException (String .format ("Failed to instantiate %s" , type ), e );
141
144
}
0 commit comments