Skip to content

Commit

Permalink
Merge pull request #64 from amzn/rwo/49
Browse files Browse the repository at this point in the history
Generate binding method for subcomponent factory
  • Loading branch information
vRallev authored Oct 28, 2024
2 parents 0a96521 + 97d2997 commit ec13e4e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import com.google.devtools.ksp.symbol.KSAnnotated
import com.google.devtools.ksp.symbol.KSClassDeclaration
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.TypeSpec
import com.squareup.kotlinpoet.ksp.addOriginatingKSFile
import com.squareup.kotlinpoet.ksp.toClassName
import com.squareup.kotlinpoet.ksp.writeTo
import me.tatarka.inject.annotations.Provides
import software.amazon.lastmile.kotlin.inject.anvil.ContextAware
import software.amazon.lastmile.kotlin.inject.anvil.ContributesSubcomponent
import software.amazon.lastmile.kotlin.inject.anvil.ContributesTo
Expand All @@ -27,7 +29,8 @@ import software.amazon.lastmile.kotlin.inject.anvil.internal.Subcomponent
* In the lookup package [LOOKUP_PACKAGE] a new interface is generated extending the contributed
* interface. To avoid name clashes the package name of the original interface is encoded in the
* interface name. This is very similar to [ContributesTo] with the key differences that there
* are more strict checks and that [Subcomponent] is added as marker.
* are more strict checks and that [Subcomponent] is added as marker. Further, an `@Provides`
* (binding) function is generated to be able to inject the factory type.
* ```
* package software.amazon.test
*
Expand All @@ -46,7 +49,10 @@ import software.amazon.lastmile.kotlin.inject.anvil.internal.Subcomponent
*
* @Origin(Subcomponent.Factory::class)
* @Subcomponent
* interface SoftwareAmazonTestSubcomponentFactory : Subcomponent.Factory
* interface SoftwareAmazonTestSubcomponentFactory : Subcomponent.Factory {
* @Provides
* fun provideSubcomponentFactory(): Subcomponent.Factory = this
* }
* ```
*/
@OptIn(KspExperimental::class)
Expand Down Expand Up @@ -95,6 +101,13 @@ internal class ContributesSubcomponentFactoryProcessor(
.addOriginAnnotation(factory)
.addAnnotation(Subcomponent::class)
.addSuperinterface(factory.toClassName())
.addFunction(
FunSpec.builder("provide${factory.innerClassNames()}")
.addAnnotation(Provides::class)
.returns(factory.toClassName())
.addStatement("return this")
.build(),
)
.build(),
)
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,47 @@ class ContributesSubcomponentProcessorTest {
}
}

@Test
fun `the factory interface is provided and can be injected in the parent scope`() {
compile(
"""
package software.amazon.test
import software.amazon.lastmile.kotlin.inject.anvil.AppScope
import software.amazon.lastmile.kotlin.inject.anvil.ContributesSubcomponent
import software.amazon.lastmile.kotlin.inject.anvil.ContributesTo
import software.amazon.lastmile.kotlin.inject.anvil.MergeComponent
import software.amazon.lastmile.kotlin.inject.anvil.SingleIn
import me.tatarka.inject.annotations.Component
import me.tatarka.inject.annotations.Provides
@MergeComponent(AppScope::class)
@Component
@SingleIn(AppScope::class)
interface ComponentInterface : ComponentInterfaceMerged {
val factory: LoggedInComponent.Factory
}
@ContributesSubcomponent(LoggedInScope::class)
@SingleIn(LoggedInScope::class)
interface LoggedInComponent {
@ContributesSubcomponent.Factory(AppScope::class)
interface Factory {
fun loggedInComponent(): LoggedInComponent
}
}
""",
scopesSource,
) {
val component = componentInterface.newComponent<Any>()
val loggedInComponent = component::class.java.methods
.single { it.name == "getFactory" }
.invoke(component)

assertThat(loggedInComponent).isNotNull()
}
}

private val JvmCompilationResult.componentInterface2: Class<*>
get() = classLoader.loadClass("software.amazon.test.ComponentInterface2")

Expand Down

0 comments on commit ec13e4e

Please sign in to comment.