Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ContributeSubComponent: Support returning Super Type #1070

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,13 @@ internal object ContributesSubcomponentCodeGen : AnvilApplicabilityChecker {
.filter { it.isAbstract }
.toList()

if (functions.size != 1 || functions[0].returnType?.resolve()
?.resolveKSClassDeclaration() != this
) {
val returnType = functions.singleOrNull()?.returnType?.resolve()?.resolveKSClassDeclaration()
if (returnType != this) {

val isReturnSuperType = returnType != null && this.superTypes
.any { type -> type.resolve().resolveKSClassDeclaration() == returnType }
if (isReturnSuperType) return

throw KspAnvilException(
node = factory,
message = "A factory must have exactly one abstract function returning the " +
Expand Down Expand Up @@ -393,6 +397,11 @@ internal object ContributesSubcomponentCodeGen : AnvilApplicabilityChecker {
?.asClassReference()

if (returnType != this) {

val isReturnSuperType = returnType != null && this.directSuperTypeReferences()
.any { it.asClassReference() == returnType }
if (isReturnSuperType) return

throw AnvilCompilationExceptionClassReference(
classReference = factory,
message = "A factory must have exactly one abstract function returning the " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,10 @@ internal class ContributesSubcomponentHandlerGenerator(
)
}

val superTypes by lazy {
contribution.clazz.directSuperTypeReferences()
.map { it.asClassReference() }
}
val createComponentFunctions = factory.memberFunctions
// filter by `isAbstract` even for interfaces,
// otherwise we get `toString()`, `equals()`, and `hashCode()`.
Expand All @@ -349,7 +353,7 @@ internal class ContributesSubcomponentHandlerGenerator(
?.asClassReference()
?: return@filter false

returnType.fqName == contributionFqName
returnType.fqName == contributionFqName || superTypes.any { it == returnType }
}

if (createComponentFunctions.size != 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class ContributesSubcomponentGeneratorTest(
}
}

@Test fun `there is a hint for contributed subcomponents with an interace factory`() {
@Test fun `there is a hint for contributed subcomponents with an interface factory`() {
compile(
"""
package com.squareup.test
Expand Down Expand Up @@ -571,6 +571,46 @@ class ContributesSubcomponentGeneratorTest(
}
}

@Test fun `a factory function may returns the component super type`() {
compile(
"""
package com.squareup.test

import com.squareup.anvil.annotations.ContributesSubcomponent
import com.squareup.anvil.annotations.ContributesSubcomponent.Factory
import com.squareup.anvil.annotations.ContributesTo
import com.squareup.anvil.annotations.MergeComponent

interface BaseSubcomponentInterface {
interface Factory {
fun createComponent(): BaseSubcomponentInterface
}
}

@ContributesSubcomponent(Any::class, parentScope = Unit::class)
interface SubcomponentInterface : BaseSubcomponentInterface {
@Factory
interface ComponentFactory: BaseSubcomponentInterface.Factory

@ContributesTo(Unit::class)
interface ParentComponent {
fun createFactory(): ComponentFactory
}
}

@MergeComponent(Unit::class)
interface ComponentInterface
""",
mode = mode,
) {
assertThat(subcomponentInterface.hintSubcomponent?.java).isEqualTo(subcomponentInterface)
assertThat(subcomponentInterface.hintSubcomponentParentScope).isEqualTo(Unit::class)

assertThat(subcomponentInterface.componentFactoryInterface.methods.map { it.name })
.containsExactly("createComponent")
}
}

@Test
fun `using Dagger's @Subcomponent_Factory is an error`() {
compile(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.squareup.anvil.sample

import org.junit.Test

class UserComponentTest {

@Test fun `UserComponent is generated`() {
val parent = DaggerAppComponent.create() as UserComponent.Parent
val baseComponent = parent.user().create()
val userComponent = baseComponent as UserComponent

assert(baseComponent.description() == UserDescriptionModule.provideDescription())
assert(userComponent.username() == UserDescriptionModule.provideName())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.squareup.anvil.sample

import com.squareup.anvil.annotations.ContributesSubcomponent
import com.squareup.anvil.annotations.ContributesTo
import com.squareup.scopes.AppScope
import com.squareup.scopes.UserScope
import dagger.Module
import dagger.Provides
import javax.inject.Named

interface UserDescriptionProvider {
@Named("userDesc")
fun description(): String
}

@ContributesTo(UserScope::class)
@Module
object UserDescriptionModule {

@Named("userName")
@Provides
fun provideName(): String = "Anvil User"

@Named("userDesc")
@Provides
fun provideDescription(): String = "User description"
}

@ContributesSubcomponent(
scope = UserScope::class,
parentScope = AppScope::class,
)
interface UserComponent : UserDescriptionProvider {

@Named("userName")
fun username(): String

@ContributesSubcomponent.Factory
interface Factory {
fun create(): UserDescriptionProvider
}

@ContributesTo(AppScope::class)
interface Parent {
fun user(): Factory
}
}
2 changes: 2 additions & 0 deletions sample/scopes/src/main/java/com/squareup/scopes/AppScope.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package com.squareup.scopes

abstract class AppScope private constructor()

abstract class UserScope private constructor()
Loading