5

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
lannyf
  • 9,865
  • 12
  • 70
  • 152

1 Answers1

10

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;
Sergii Zhevzhyk
  • 4,074
  • 22
  • 28