Skip to content

Commit 3f09fb2

Browse files
committed
Check overridden method for provides annotation
Fixes #269
1 parent d10eba0 commit 3f09fb2

File tree

5 files changed

+34
-14
lines changed

5 files changed

+34
-14
lines changed

ast/kapt/src/main/kotlin/me/tatarka/kotlin/ast/ModelAst.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -334,9 +334,7 @@ private class ModelAstClass(
334334
addAll(methods)
335335
for (superType in superTypes) {
336336
for (superMethod in superType.allMethods) {
337-
if (none { it.overrides(superMethod) }) {
338-
add(superMethod)
339-
}
337+
add(superMethod)
340338
}
341339
}
342340
}

ast/ksp/src/main/kotlin/me/tatarka/kotlin/ast/KSAst.kt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,9 @@ private class KSAstClass(override val resolver: Resolver, override val declarati
206206
for (type in declaration.inheritanceChain()) {
207207
for (declaration in type.declarations) {
208208
if (declaration is KSPropertyDeclaration) {
209-
if (declarations.none { resolver.overrides(it, declaration) }) {
210-
declarations.add(declaration)
211-
}
209+
declarations.add(declaration)
212210
} else if (declaration is KSFunctionDeclaration && !declaration.isConstructor()) {
213-
if (declarations.none { resolver.overrides(it, declaration) }) {
214-
declarations.add(declaration)
215-
}
211+
declarations.add(declaration)
216212
}
217213
}
218214
}

integration-tests/common/src/test/kotlin/me/tatarka/inject/test/InheritanceTest.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import assertk.assertions.hasClass
55
import assertk.assertions.isNotNull
66
import assertk.assertions.isSameAs
77
import me.tatarka.inject.annotations.Component
8+
import me.tatarka.inject.annotations.Inject
89
import me.tatarka.inject.annotations.Provides
10+
import me.tatarka.inject.annotations.Scope
911
import kotlin.test.Test
1012

1113
interface ComponentInterface {
@@ -59,6 +61,19 @@ abstract class ProvidesScopedInterfaceComponent : ProvidesScopedComponentInterfa
5961
abstract val foo: IFoo
6062
}
6163

64+
65+
interface AbstractProvidesInterface {
66+
@get:Provides val foo: IFoo
67+
}
68+
69+
@Component
70+
abstract class AbstractProvidesImplComponent: AbstractProvidesInterface {
71+
abstract val bar: BarImpl
72+
73+
override val foo: IFoo
74+
get() = Foo()
75+
}
76+
6277
class InheritanceTest {
6378
@Test
6479
fun generates_a_component_that_provides_a_dep_defined_in_an_implemented_interface() {

kotlin-inject-compiler/core/src/main/kotlin/me/tatarka/inject/compiler/InjectGenerator.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ fun AstAnnotated.qualifier(options: Options): AstAnnotation? {
232232
}
233233

234234
fun AstMember.isProvider(): Boolean =
235-
!isProvides() && isAbstract && when (this) {
235+
isAbstract && when (this) {
236236
is AstFunction -> parameters.isEmpty()
237237
is AstProperty -> true
238238
} && receiverParameterType == null && returnType.isNotUnit()

kotlin-inject-compiler/core/src/main/kotlin/me/tatarka/inject/compiler/TypeCollector.kt

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,21 @@ class TypeCollector(private val provider: AstProvider, private val options: Opti
267267
}
268268
}
269269

270-
for (method in astClass.allMethods) {
270+
val allMethods = astClass.allMethods
271+
// some methods may override others
272+
val methods = mutableMapOf<AstMember, AstMember?>()
273+
for (method in allMethods) {
274+
val overrides = methods.keys.find { it.overrides(method) }
275+
if (overrides != null) {
276+
methods[overrides] = method
277+
} else {
278+
methods[method] = null
279+
}
280+
}
281+
for (method in methods.keys) {
271282
val abstract = method.isAbstract
272-
if (method.isProvides()) {
283+
val overriden = methods[method]
284+
if (method.isProvides() || overriden?.isProvides() == true) {
273285
if (method.visibility == AstVisibility.PRIVATE) {
274286
provider.error("@Provides method must not be private", method)
275287
continue
@@ -296,8 +308,7 @@ class TypeCollector(private val provider: AstProvider, private val options: Opti
296308
} else {
297309
providesMethods.add(method)
298310
}
299-
}
300-
if (method.isProvider()) {
311+
} else if (method.isProvider()) {
301312
val scope = method.scopeType(options)
302313
if (scope != null) {
303314
provider.warn(

0 commit comments

Comments
 (0)