diff --git a/processor/common/src/main/kotlin/com/moriatsushi/koject/processor/analytics/KSClassAnalytics.kt b/processor/common/src/main/kotlin/com/moriatsushi/koject/processor/analytics/KSClassAnalytics.kt index a4dddd43..fd60111a 100644 --- a/processor/common/src/main/kotlin/com/moriatsushi/koject/processor/analytics/KSClassAnalytics.kt +++ b/processor/common/src/main/kotlin/com/moriatsushi/koject/processor/analytics/KSClassAnalytics.kt @@ -8,3 +8,17 @@ internal val KSClassDeclaration.primarySuperType: KSType? .map { it.resolve() } .filterNot { it.isAny } .firstOrNull() + +internal fun KSClassDeclaration.hasSuperType(ksType: KSType): Boolean { + superTypes + .map { it.resolve() } + .forEach { + if (it == ksType) return true + + val declaration = it.declaration + if (declaration is KSClassDeclaration && declaration.hasSuperType(ksType)) { + return true + } + } + return false +} diff --git a/processor/common/src/main/kotlin/com/moriatsushi/koject/processor/symbol/BindsAnnotation.kt b/processor/common/src/main/kotlin/com/moriatsushi/koject/processor/symbol/BindsAnnotation.kt index e47f9c4f..4e29d6f4 100644 --- a/processor/common/src/main/kotlin/com/moriatsushi/koject/processor/symbol/BindsAnnotation.kt +++ b/processor/common/src/main/kotlin/com/moriatsushi/koject/processor/symbol/BindsAnnotation.kt @@ -5,6 +5,7 @@ import com.google.devtools.ksp.symbol.KSType import com.moriatsushi.koject.Binds import com.moriatsushi.koject.processor.analytics.findAnnotation import com.moriatsushi.koject.processor.analytics.findArgumentByName +import com.moriatsushi.koject.processor.analytics.hasSuperType import com.moriatsushi.koject.processor.analytics.isNothing import com.moriatsushi.koject.processor.analytics.name import com.moriatsushi.koject.processor.analytics.primarySuperType @@ -23,13 +24,13 @@ internal fun KSClassDeclaration.findBindAnnotation(): BindsAnnotation? { val annotation = findAnnotation() ?: return null val argument = annotation.findArgumentByName("to") val toType = if (argument == null || argument.isNothing) { - primarySuperType - ?: throwNotFoundSuperTypeException(this) + primarySuperType ?: throwNotFoundSuperTypeException(this) } else { - superTypes - .map { it.resolve() } - .find { it == argument } - ?: throwNotIncludedSuperTypeException(this, argument) + if (hasSuperType(argument)) { + argument + } else { + throwNotIncludedSuperTypeException(this, argument) + } } return BindsAnnotation( toTypeName = toType.toTypeName(), diff --git a/processor/common/src/test/kotlin/com/moriatsushi/koject/processor/AppProcessorBindsTest.kt b/processor/common/src/test/kotlin/com/moriatsushi/koject/processor/AppProcessorBindsTest.kt index 0d5b0fc3..59542974 100644 --- a/processor/common/src/test/kotlin/com/moriatsushi/koject/processor/AppProcessorBindsTest.kt +++ b/processor/common/src/test/kotlin/com/moriatsushi/koject/processor/AppProcessorBindsTest.kt @@ -33,6 +33,15 @@ class AppProcessorBindsTest { assertCompileSucceed(result) } + @Test + fun success_nestedSupertype() { + val complication = compilationFactory.create(folder) + complication.sources = listOf(nestedSuperType) + val result = complication.compile() + + assertCompileSucceed(result) + } + @Test fun failed_notFoundSupertype() { val complication = compilationFactory.create(folder) @@ -82,6 +91,24 @@ class AppProcessorBindsTest { """, ) + private val nestedSuperType = SourceFile.kotlin( + "Test.kt", + """ + package com.testpackage + + import com.moriatsushi.koject.Binds + import com.moriatsushi.koject.Provides + + @Binds(to = Type2::class) + @Provides + class SampleImpl: Type1 + + interface Type1: Type2 + + interface Type2 + """, + ) + private val notFoundSupertypeCode = SourceFile.kotlin( "Test.kt", """