@@ -11,53 +11,58 @@ import io.cucumber.core.resource.{ClasspathScanner, ClasspathSupport}
11
11
import scala .collection .JavaConverters ._
12
12
import scala .util .Try
13
13
14
- class ScalaBackend (classLoaderProvider : Supplier [ClassLoader ]) extends Backend {
14
+ class ScalaBackend (lookup : Lookup , container : Container , classLoaderProvider : Supplier [ClassLoader ]) extends Backend {
15
15
16
16
private val classFinder = new ClasspathScanner (classLoaderProvider)
17
17
18
- private [scala] var scalaGlueInstances : Seq [ScalaDsl ] = Nil
18
+ private var glueAdaptor : GlueAdaptor = _
19
+ private [scala] var scalaGlueClasses : Seq [Class [_ <: ScalaDsl ]] = Nil
19
20
20
21
override def disposeWorld (): Unit = {
21
- scalaGlueInstances = Nil
22
+ // Nothing to do
22
23
}
23
24
24
25
override def getSnippet (): Snippet = {
25
26
new ScalaSnippet ()
26
27
}
27
28
28
29
override def buildWorld (): Unit = {
29
- // Nothing to do
30
+ // Instantiate all the glue classes and load the glue code from them
31
+ scalaGlueClasses.foreach { glueClass =>
32
+ val glueInstance = lookup.getInstance(glueClass)
33
+ glueAdaptor.loadRegistry(glueInstance.registry, scenarioScoped = true )
34
+ }
30
35
}
31
36
32
37
override def loadGlue (glue : Glue , gluePaths : JList [URI ]): Unit = {
33
38
39
+ glueAdaptor = new GlueAdaptor (glue)
40
+
34
41
val dslClasses = gluePaths.asScala
35
42
.filter(gluePath => ClasspathSupport .CLASSPATH_SCHEME .equals(gluePath.getScheme))
36
43
.map(ClasspathSupport .packageName)
37
44
.flatMap(basePackageName => classFinder.scanForSubClassesInPackage(basePackageName, classOf [ScalaDsl ]).asScala)
38
45
.filter(glueClass => ! glueClass.isInterface)
39
- .filter(glueClass => glueClass.getConstructors.length > 0 )
40
46
41
47
val (clsClasses, objClasses) = dslClasses.partition(isRegularClass)
42
48
49
+ // Retrieve Scala objects (singletons)
43
50
val objInstances = objClasses.map { cls =>
44
51
val instField = cls.getDeclaredField(" MODULE$" )
45
52
instField.setAccessible(true )
46
53
instField.get(null ).asInstanceOf [ScalaDsl ]
47
54
}
48
- val clsInstances = clsClasses.map {
49
- _.newInstance()
50
- }
51
-
52
- // FIXME we should not create instances above but fill the container like Cucumber Java does
53
- // https://github.com/cucumber/cucumber-jvm-scala/issues/1
54
- // clsClasses.foreach(container.addClass(_))
55
- scalaGlueInstances = objInstances.toSeq ++ clsInstances
56
55
57
- val glueAdaptor = new GlueAdaptor (glue)
56
+ // Regular Scala classes are added to the container, they will be instantiated by the container depending on its logic
57
+ // Object are not because by definition they are singletons
58
+ clsClasses.foreach { glueClass =>
59
+ container.addClass(glueClass)
60
+ scalaGlueClasses = scalaGlueClasses :+ glueClass
61
+ }
58
62
59
- scalaGlueInstances.foreach { glueInstance =>
60
- glueAdaptor.addDefinition(glueInstance.registry)
63
+ // For object, we add the definitions here, once for all
64
+ objInstances.foreach { glueInstance =>
65
+ glueAdaptor.loadRegistry(glueInstance.registry, scenarioScoped = false )
61
66
}
62
67
63
68
()
0 commit comments