kotlin-inject icon indicating copy to clipboard operation
kotlin-inject copied to clipboard

Warn when scope annoation would have no effect

Open denchic45 opened this issue 3 years ago • 5 comments

If I pass the dependency through the provide function using scope, then everything works correctly:

    @MyScope
    @Provides
    fun provideFoo() = Foo()

generation:

foo = _scoped.get("com.example.Foo") {
        provideFoo()
      }

But if I declare a variable instead, then the scope is ignored:

    @MyScope
    abstract val foo: Foo

generation:

  public override val foo: Foo
    get() = Foo()

or:

    @MyScope
    abstract val lambdaFoo: () -> Foo

generation:

  public override val lambdaFoo: Function0<Foo>
    get() = {
      Foo()
    }

denchic45 avatar Oct 29 '22 19:10 denchic45

Try @get:MyScope

evant avatar Oct 30 '22 06:10 evant

doesn't seem to help

    @get:AppScope
    abstract val foo: Foo
  public override val foo: Foo
    get() = Foo()

denchic45 avatar Oct 30 '22 12:10 denchic45

Oh sorry missed what you are doing. Scope annotations go on @Provides methods or on the class to inject. You are putting it on the generated component method. Probably could produce a helpful error message for this.

evant avatar Oct 30 '22 18:10 evant

Yes, I think it makes sense to ban such cases

denchic45 avatar Oct 30 '22 19:10 denchic45

This is now partially addressed. Due to a limitation with the KAPT back-end it will miss cases where the property is annotated itself instead of it's getter. ex: @MyScope abstract val myProp is missed but @get:MyScope abstract val myProp is caught. This can be improved once the KAPT back-end is removed.

evant avatar Aug 27 '23 04:08 evant

@evant looks like it can be closed - warning shows when property is annotated. Pr isn't marked as closing it because it technically doesn't... Maybe I should've just mark it there

sergeshustoff avatar Jun 16 '24 06:06 sergeshustoff