# Annotations Annotations are tags that you can use to attach metadata to elements in your code. Tools and frameworks process this metadata during compilation and runtime, and perform different actions based on it. You can annotate your code to simplify and automate common tasks, such as generating boilerplate code, enforcing coding standards or writing documentation. Tip: If you want to develop your own annotation processors, you can use the [Kotlin Symbol Processing (KSP)](ksp-overview.html) API. ## Declaration Annotations are a special type of class. To declare an annotation, use the `annotation` keyword before the class declaration: ```KOTLIN annotation class Fancy ``` Additional attributes of the annotation can be specified by annotating the annotation class with meta-annotations: * [@Target](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.annotation/-target/index.html) specifies the possible kinds of elements which can be annotated with the annotation (such as classes, functions, properties, and expressions); * [@Retention](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.annotation/-retention/index.html) specifies whether the annotation is stored in the compiled class files and whether it's visible through reflection at runtime (by default, both are true); * [@Repeatable](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.annotation/-repeatable/index.html) allows using the same annotation on a single element multiple times; * [@MustBeDocumented](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.annotation/-must-be-documented/index.html) specifies that the annotation is part of the public API and should be included in the class or method signature shown in the generated API documentation. ```KOTLIN @Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION, AnnotationTarget.TYPE_PARAMETER, AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.EXPRESSION) @Retention(AnnotationRetention.SOURCE) @MustBeDocumented annotation class Fancy ``` ## Usage ```KOTLIN @Fancy class Foo { @Fancy fun baz(@Fancy foo: Int): Int { return (@Fancy 1) } } ``` If you need to annotate the primary constructor of a class, you need to add the `constructor` keyword to the constructor declaration, and add the annotations before it: ```KOTLIN class Foo @Inject constructor(dependency: MyDependency) { ... } ``` You can also annotate property accessors: ```KOTLIN class Foo { var x: MyDependency? = null @Inject set } ``` ## Constructors Annotations can have constructors that take parameters. ```KOTLIN annotation class Special(val why: String) @Special("example") class Foo {} ``` Allowed parameter types are: * Types that correspond to Java primitive types (Int, Long etc.) * Strings * Classes (`Foo::class`) * Enums * Other annotations * Arrays of the types listed above Annotation parameters cannot have nullable types, because the JVM does not support storing `null` as a value of an annotation attribute. If an annotation is used as a parameter of another annotation, its name is not prefixed with the `@` character: ```KOTLIN annotation class ReplaceWith(val expression: String) annotation class Deprecated( val message: String, val replaceWith: ReplaceWith = ReplaceWith("")) @Deprecated("This function is deprecated, use === instead", ReplaceWith("this === other")) ``` If you need to specify a class as an argument of an annotation, use a Kotlin class ([KClass](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.reflect/-k-class/index.html)). The Kotlin compiler will automatically convert it to a Java class, so that the Java code can access the annotations and arguments normally. ```KOTLIN import kotlin.reflect.KClass annotation class Ann(val arg1: KClass<*>, val arg2: KClass) @Ann(String::class, Int::class) class MyClass ``` ## Instantiation In Java, an annotation type is a form of an interface, so you can implement it and use an instance. As an alternative to this mechanism, Kotlin lets you call a constructor of an annotation class in arbitrary code and similarly use the resulting instance. ```KOTLIN annotation class InfoMarker(val info: String) fun processInfo(marker: InfoMarker): Unit = TODO() fun main(args: Array) { if (args.isNotEmpty()) processInfo(getAnnotationReflective(args)) else processInfo(InfoMarker("default")) } ``` Learn more about instantiation of annotation classes in [this KEEP](https://github.com/Kotlin/KEEP/blob/master/proposals/annotation-instantiation.md). ## Lambdas Annotations can also be used on lambdas. They will be applied to the `invoke()` method into which the body of the lambda is generated. This is useful for frameworks like [Quasar](https://docs.paralleluniverse.co/quasar/), which uses annotations for concurrency control. ```KOTLIN annotation class Suspendable val f = @Suspendable { Fiber.sleep(10) } ``` ## Annotation use-site targets When you're annotating a property or a primary constructor parameter, there are multiple Java elements that are generated from the corresponding Kotlin element, and therefore multiple possible locations for the annotation in the generated Java bytecode. To specify how exactly the annotation should be generated, use the following syntax: ```KOTLIN class Example(@field:Ann val foo, // annotate only the Java field @get:Ann val bar, // annotate only the Java getter @param:Ann val quux) // annotate only the Java constructor parameter ``` The same syntax can be used to annotate the entire file. To do this, put an annotation with the target `file` at the top level of a file, before the package directive or before all imports if the file is in the default package: ```KOTLIN @file:JvmName("Foo") package org.jetbrains.demo ``` If you have multiple annotations with the same target, you can avoid repeating the target by adding brackets after the target and putting all the annotations inside the brackets (except for the `all` meta-target): ```KOTLIN class Example { @set:[Inject VisibleForTesting] var collaborator: Collaborator } ``` The full list of supported use-site targets is: * `file` * `field` * `property` (annotations with this target are not visible to Java) * `get` (property getter) * `set` (property setter) * `all` (a meta-target for properties, see the [all meta-target](#all-meta-target) section for more information) * `receiver` (receiver parameter of an extension function or property) To annotate the receiver parameter of an extension function, use the following syntax: ```KOTLIN fun @receiver:Fancy String.myExtension() { ... } ``` * `param` (constructor parameter) * `setparam` (property setter parameter) * `delegate` (the field storing the delegate instance for a delegated property) ### Defaults when no use-site targets are specified If you don't specify a use-site target, the compiler chooses the target according to the `@Target` annotation of the annotation you use. If there are multiple applicable targets, the compiler chooses one or more of them in the following order: * The constructor parameter target (`param`). * The property target (`property`). * The field target (`field`), if it's applicable and the property target (`property`) isn't. If none of `param`, `property`, or `field` are applicable, the annotation is invalid, and you need to specify a use-site target explicitly. Let's use the [@Email annotation from Jakarta Bean Validation](https://jakarta.ee/specifications/bean-validation/3.0/apidocs/jakarta/validation/constraints/email): ```JAVA @Target(value={METHOD,FIELD,ANNOTATION_TYPE,CONSTRUCTOR,PARAMETER,TYPE_USE}) public @interface Email { } ``` With this annotation, consider the following example: ```KOTLIN data class User(val username: String, // @Email is now equivalent to @param:Email @field:Email @Email val email: String) { // @Email is still equivalent to @field:Email @Email val secondaryEmail: String? = null } ``` In this example, the `@Email` annotation applies to both the constructor parameter and the field targets for the `email` property because the property: * Is declared in the primary constructor. * Has no custom getter or setter, so the compiler generates a backing field. The `@Email` annotation only applies to the field target for the `secondaryEmail` property because the property: * Isn't declared in the primary constructor. * Has no custom getter or setter, so the compiler generates a backing field. ### `all` meta-target The `all` target makes it easier to apply the same annotation not only to the parameter and the property or field, but also to the corresponding getter and setter. Specifically, the annotation marked with `all` is propagated, if applicable: * To the constructor parameter (`param`) if the property is defined in the primary constructor. * To the property itself (`property`). * To the backing field (`field`) if the property has one. * To the getter (`get`). * To the setter parameter (`setparam`) if the property is defined as `var`. * To the Java-only target `RECORD_COMPONENT` if the class has the `@JvmRecord` annotation. Let's use the [@Email annotation from Jakarta Bean Validation](https://jakarta.ee/specifications/bean-validation/3.0/apidocs/jakarta/validation/constraints/email), which is defined as follows: ```JAVA @Target(value={METHOD,FIELD,ANNOTATION_TYPE,CONSTRUCTOR,PARAMETER,TYPE_USE}) public @interface Email { } ``` In the example below, this `@Email` annotation is applied to all relevant targets: ```KOTLIN data class User( val username: String, // Applies @Email to param, field, and get @all:Email val email: String, // Applies @Email to param, field, get, and setparam @all:Email var name: String, ) { // Applies @Email to field and getter (not param since it's not in the constructor) @all:Email val secondaryEmail: String? = null } ``` You can use the `all` meta-target with any property, both inside and outside the primary constructor. #### Limitations The `all` target comes with some limitations: * It does not propagate an annotation to types, potential extension receivers, or context receivers or parameters. * It cannot be used with multiple annotations: ```KOTLIN @all:[A B] // forbidden, use @all:A @all:B val x: Int = 5 ``` * It cannot be used with [delegated properties](delegated-properties.html). ## Java annotations Java annotations are 100% compatible with Kotlin: ```KOTLIN import org.junit.Test import org.junit.Assert.* import org.junit.Rule import org.junit.rules.* class Tests { // apply @Rule annotation to property getter @get:Rule val tempFolder = TemporaryFolder() @Test fun simple() { val f = tempFolder.newFile() assertEquals(42, getTheAnswer()) } } ``` Since the order of parameters for an annotation written in Java is not defined, you can't use a regular function call syntax for passing the arguments. Instead, you need to use the named argument syntax: ```JAVA // Java public @interface Ann { int intValue(); String stringValue(); } ``` ```KOTLIN // Kotlin @Ann(intValue = 1, stringValue = "abc") class C ``` Just like in Java, a special case is the `value` parameter; its value can be specified without an explicit name: ```JAVA // Java public @interface AnnWithValue { String value(); } ``` ```KOTLIN // Kotlin @AnnWithValue("abc") class C ``` ### Arrays as annotation parameters If the `value` argument in Java has an array type, it becomes a `vararg` parameter in Kotlin: ```JAVA // Java public @interface AnnWithArrayValue { String[] value(); } ``` ```KOTLIN // Kotlin @AnnWithArrayValue("abc", "foo", "bar") class C ``` For other arguments that have an array type, you need to use the array literal syntax or `arrayOf(...)`: ```JAVA // Java public @interface AnnWithArrayMethod { String[] names(); } ``` ```KOTLIN @AnnWithArrayMethod(names = ["abc", "foo", "bar"]) class C ``` ### Accessing properties of an annotation instance Values of an annotation instance are exposed as properties to Kotlin code: ```JAVA // Java public @interface Ann { int value(); } ``` ```KOTLIN // Kotlin fun foo(ann: Ann) { val i = ann.value } ``` ### Ability to not generate JVM 1.8+ annotation targets If a Kotlin annotation has `TYPE` among its Kotlin targets, the annotation maps to `java.lang.annotation.ElementType.TYPE_USE` in its list of Java annotation targets. This is just like how the `TYPE_PARAMETER` Kotlin target maps to the `java.lang.annotation.ElementType.TYPE_PARAMETER` Java target. This is an issue for Android clients with API levels less than 26, which don't have these targets in the API. To avoid generating the `TYPE_USE` and `TYPE_PARAMETER` annotation targets, use the new compiler argument `-Xno-new-java-annotation-targets`. ## Repeatable annotations Just like [in Java](https://docs.oracle.com/javase/tutorial/java/annotations/repeating.html), Kotlin has repeatable annotations, which can be applied to a single code element multiple times. To make your annotation repeatable, mark its declaration with the [@kotlin.annotation.Repeatable](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.annotation/-repeatable/) meta-annotation. This will make it repeatable both in Kotlin and Java. Java repeatable annotations are also supported from the Kotlin side. The main difference with the scheme used in Java is the absence of a containing annotation, which the Kotlin compiler generates automatically with a predefined name. For an annotation in the example below, it will generate the containing annotation `@Tag.Container`: ```KOTLIN @Repeatable annotation class Tag(val name: String) // The compiler generates the @Tag.Container containing annotation ``` You can set a custom name for a containing annotation by applying the [@kotlin.jvm.JvmRepeatable](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.jvm/-jvm-repeatable/) meta-annotation and passing an explicitly declared containing annotation class as an argument: ```KOTLIN @JvmRepeatable(Tags::class) annotation class Tag(val name: String) annotation class Tags(val value: Array) ``` To extract Kotlin or Java repeatable annotations via reflection, use the [KAnnotatedElement.findAnnotations()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.reflect.full/find-annotations.html) function. Learn more about Kotlin repeatable annotations in [this KEEP](https://github.com/Kotlin/KEEP/blob/master/proposals/repeatable-annotations.md).