@@ -11,53 +11,58 @@ import io.cucumber.core.resource.{ClasspathScanner, ClasspathSupport}
1111import scala .collection .JavaConverters ._
1212import scala .util .Try
1313
14- class ScalaBackend (classLoaderProvider : Supplier [ClassLoader ]) extends Backend {
14+ class ScalaBackend (lookup : Lookup , container : Container , classLoaderProvider : Supplier [ClassLoader ]) extends Backend {
1515
1616 private val classFinder = new ClasspathScanner (classLoaderProvider)
1717
18- private [scala] var scalaGlueInstances : Seq [ScalaDsl ] = Nil
18+ private var glueAdaptor : GlueAdaptor = _
19+ private [scala] var scalaGlueClasses : Seq [Class [_ <: ScalaDsl ]] = Nil
1920
2021 override def disposeWorld (): Unit = {
21- scalaGlueInstances = Nil
22+ // Nothing to do
2223 }
2324
2425 override def getSnippet (): Snippet = {
2526 new ScalaSnippet ()
2627 }
2728
2829 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+ }
3035 }
3136
3237 override def loadGlue (glue : Glue , gluePaths : JList [URI ]): Unit = {
3338
39+ glueAdaptor = new GlueAdaptor (glue)
40+
3441 val dslClasses = gluePaths.asScala
3542 .filter(gluePath => ClasspathSupport .CLASSPATH_SCHEME .equals(gluePath.getScheme))
3643 .map(ClasspathSupport .packageName)
3744 .flatMap(basePackageName => classFinder.scanForSubClassesInPackage(basePackageName, classOf [ScalaDsl ]).asScala)
3845 .filter(glueClass => ! glueClass.isInterface)
39- .filter(glueClass => glueClass.getConstructors.length > 0 )
4046
4147 val (clsClasses, objClasses) = dslClasses.partition(isRegularClass)
4248
49+ // Retrieve Scala objects (singletons)
4350 val objInstances = objClasses.map { cls =>
4451 val instField = cls.getDeclaredField(" MODULE$" )
4552 instField.setAccessible(true )
4653 instField.get(null ).asInstanceOf [ScalaDsl ]
4754 }
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
5655
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+ }
5862
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 )
6166 }
6267
6368 ()
0 commit comments