Last active
July 20, 2018 06:59
-
-
Save Rajin9601/b01cee9a3973c0de3ce2c3fdac048947 to your computer and use it in GitHub Desktop.
Dagger Component Overriding test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun test() { | |
val testAComponent = DaggerTestA_Component.builder() | |
.parentComponent(object: TestA.ParentComponent { | |
}) | |
.build() | |
val testBComponent = DaggerTestB_Component.builder() | |
.parentComponent(testAComponent) | |
.build() | |
val testInjected = TestInjected() | |
testBComponent.inject(testInjected) | |
Log.i("DAVID", testInjected.str) // B | |
} | |
class TestA() { | |
@dagger.Module | |
abstract class Module { | |
@dagger.Module | |
companion object { | |
@Provides | |
@JvmStatic | |
internal fun str(): String { | |
return "A" | |
} | |
} | |
} | |
interface ParentComponent | |
@dagger.Component(modules = [Module::class], dependencies = [ParentComponent::class]) | |
interface Component : TestB.ParentComponent { | |
@dagger.Component.Builder | |
interface Builder { | |
fun parentComponent(component: ParentComponent): Builder | |
fun build(): Component | |
} | |
} | |
} | |
class TestB() { | |
@dagger.Module | |
abstract class Module { | |
@dagger.Module | |
companion object { | |
@Provides | |
@JvmStatic | |
internal fun str(): String { | |
return "B" | |
} | |
} | |
} | |
interface ParentComponent | |
@dagger.Component(modules = [Module::class], dependencies = [ParentComponent::class]) | |
interface Component { | |
fun inject(testInjected: TestInjected) | |
@dagger.Component.Builder | |
interface Builder { | |
fun parentComponent(component: ParentComponent): Builder | |
fun build(): Component | |
} | |
} | |
} | |
class TestInjected() { | |
@Inject lateinit var str: String | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment