liveBook · Manning

Use-site target

As explained in Kotlin in Action, 2E

A use-site target specifies which generated bytecode element receives an annotation when one Kotlin declaration produces multiple elements.

A use-site target selects the generated bytecode element that receives an annotation when one Kotlin declaration produces several Java declarations. Kotlin properties can correspond to a field, getter, setter, setter parameter, and sometimes a constructor parameter, so the annotation may need an explicit destination. section 12.1.2

Renaming a property's Java accessors

class CertificateManager { @get:JvmName("obtainCertificate") @set:JvmName("putCertificate") var certificate: String = "-----BEGIN PRIVATE KEY-----" } The target appears between @ and the annotation name: get applies @JvmName to the getter, while set applies it to the setter. Java code can then call obtainCertificate() and putCertificate() instead of the generated accessor names. section 12.1.2

Common use-site targets include:

  • property: the Kotlin property itself. section 12.1.2
  • field: the field generated for a property. section 12.1.2
  • get and set: the property's getter and setter. section 12.1.2
  • param and setparam: a constructor parameter or property setter parameter. section 12.1.2
  • receiver, delegate, and file: an extension receiver, delegated-property field, or class containing top-level declarations. section 12.1.2
Figure 12.2 The use-site target (like get or set) is placed between the @ sign and the annotation name, and is separated from the name with a colon.
CH12_F02_Isakova

Use-site syntax places a target such as get or set between the at-sign and annotation name, separated by a colon. section 12.1.2

How Kotlin in Action, 2E covers it

How deep it goes
Explains target syntax, property accessors, Java defaults, and the supported target list through Kotlin-to-Java examples.
Examples
Demonstrates @get:JvmName and @set:JvmName for Java accessor names, plus @Suppress on a local cast.
What you can do after
Apply annotations to precise generated elements and rename Kotlin function or property accessors for Java callers.
How it connects
Connects annotations with Kotlin properties, Java declarations, getters, setters, constructor parameters, and bytecode.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kotlin in Action, 2E. The section links point to the parts of the book it draws on.