square / anvil

A Kotlin compiler plugin to make dependency injection with Dagger 2 easier.
Apache License 2.0
1.28k stars 75 forks source link

`ContributesSubcomponent.Factory` doesn't support inherited functions #1003

Open esafirm opened 1 month ago

esafirm commented 1 month ago

The document said that

This follows all the rules of Subcomponent.Factory, except it must appear in classes annotated with @ContributesSubcomponent instead of @Subcomponent.

But ContributesSubcomponent.Factory actually doesn't support inherited function.

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

@ContributesSubcomponent(Any::class, parentScope = Unit::class)
interface SubcomponentInterface {
    @ContributesTo(Unit::class)
    interface AnyParentComponent {
        fun createFactory(): ComponentFactory
    }

    interface Creator {
        fun createComponent(): SubcomponentInterface
    }

    @Factory
    abstract class ComponentFactory : Creator
}

@MergeComponent(Unit::class)
interface ComponentInterface

The code above will fail if we're using @ContributesSubcomponent.Factory but will pass for @Subcomponent.Factory

Another thing is, while I haven't tested it, the KSP part of the code gen might support the above code, since in here it use getAllFunctions that will returns declared and inherited functions.