Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dagger 2, what is difference between @set:Inject and @Inject

In a android, kotlin project, seeing this @set:Inject but could not find a good explanation. Anyone knows?

object Controller {

    @set:Inject
    lateinit var someData: SomeData
like image 657
lannyf Avatar asked Jul 25 '18 15:07

lannyf


1 Answers

The @Inject annotation can be used for method, constructor, or field:

@Target(value={METHOD,CONSTRUCTOR,FIELD})

It is important to remember that Java code will be generated from this Kotlin code and for one statement in Kotlin you can have multiple Java elements and that is why @set:Inject explicitly specifies that the @Inject annotation should be applied to the setter that will be generated in Java.

What happens if there is no use-site target defined? Official documentation provides a good explanation:

If you don't specify a use-site target, the target is chosen according to the @Target annotation of the annotation being used. If there are multiple applicable targets, the first applicable target from the following list is used:

  • param (constructor parameter);
  • property (annotations with this target are not visible to Java);
  • field;
like image 90
Sergii Zhevzhyk Avatar answered Sep 28 '22 16:09

Sergii Zhevzhyk