Repository: evant/kotlin-inject
Pull request #47 (2020-08-26 17:32:09)

Fix referencing a scoped component from a different package


Patch:
diff --git a/kotlin-inject-compiler-core/src/main/kotlin/me/tatarka/inject/compiler/InjectGenerator.kt b/kotlin-inject-compiler-core/src/main/kotlin/me/tatarka/inject/compiler/InjectGenerator.kt
index 1968ac2..a920bfd 100644
--- a/kotlin-inject-compiler-core/src/main/kotlin/me/tatarka/inject/compiler/InjectGenerator.kt
+++ b/kotlin-inject-compiler-core/src/main/kotlin/me/tatarka/inject/compiler/InjectGenerator.kt
@@ -333,7 +333,7 @@ class InjectGenerator(provider: AstProvider, private val options: Options) :
     private fun provideScoped(key: TypeKey, result: Result.Scoped): CodeBlock {
         val codeBlock = CodeBlock.builder()
         if (result.name != null) {
-            codeBlock.add("(%L as Inject%N).", result.name, result.astClass.name)
+            codeBlock.add("(%L as %T).", result.name, ClassName(result.astClass.packageName, "Inject${result.astClass.name}"))
         }
         codeBlock.add("%N", key.type.simpleName.asScopedProp())
         return codeBlock.build()


Test patch:
diff --git a/integration-tests/src/test/kotlin/me/tatarka/inject/test/ScopeTest.kt b/integration-tests/src/test/kotlin/me/tatarka/inject/test/ScopeTest.kt
index 7496cbb..925e3d6 100644
--- a/integration-tests/src/test/kotlin/me/tatarka/inject/test/ScopeTest.kt
+++ b/integration-tests/src/test/kotlin/me/tatarka/inject/test/ScopeTest.kt
@@ -7,6 +7,9 @@ import assertk.assertions.isSameAs
 import me.tatarka.inject.annotations.Component
 import me.tatarka.inject.annotations.Inject
 import me.tatarka.inject.annotations.Provides
+import me.tatarka.inject.test.different.DifferentPackageFoo
+import me.tatarka.inject.test.different.DifferentPackageScopedComponent
+import me.tatarka.inject.test.different.create
 import kotlin.test.BeforeTest
 import kotlin.test.Test
 
@@ -51,6 +54,9 @@ class ScopedBar()
     abstract val bar: ScopedBar
 }
 
+@Component abstract class DifferentPackageChildComponent(@Component val parent: DifferentPackageScopedComponent) {
+    abstract val foo: DifferentPackageFoo
+}
 
 class ScopeTest {
     @BeforeTest
@@ -118,4 +124,10 @@ class ScopeTest {
 
         assertThat(component.bar).isSameAs(component.foo.bar)
     }
+
+    @Test fun generates_a_component_with_a_parent_scoped_component_in_a_different_package() {
+        val component = DifferentPackageChildComponent::class.create(DifferentPackageScopedComponent::class.create())
+
+        assertThat(component.foo).isSameAs(component.foo)
+    }
 }
\ No newline at end of file
diff --git a/integration-tests/src/test/kotlin/me/tatarka/inject/test/different/DifferentPackage.kt b/integration-tests/src/test/kotlin/me/tatarka/inject/test/different/DifferentPackage.kt
new file mode 100644
index 0000000..4f6caf6
--- /dev/null
+++ b/integration-tests/src/test/kotlin/me/tatarka/inject/test/different/DifferentPackage.kt
@@ -0,0 +1,11 @@
+package me.tatarka.inject.test.different
+
+import me.tatarka.inject.annotations.Component
+import me.tatarka.inject.annotations.Provides
+import me.tatarka.inject.test.CustomScope
+
+class DifferentPackageFoo
+
+@CustomScope @Component abstract class DifferentPackageScopedComponent {
+    @Provides @CustomScope fun differentPackageFoo() = DifferentPackageFoo()
+}
\ No newline at end of file