Annotations
Annotations allow developers to add extra meaning to classes, interfaces, parameters, and so on at compile time. They are a form of meta-programming in that respect. Annotations can then be used by the compiler or by your own code via reflection at runtime. Depending on the annotation value, the meaning of the program or data can change.
Annotations are present in Java as well as Kotlin, and so the most common annotations are those that are provided as part of the Kotlin or Java standard libraries. Some annotations you may be familiar with already are @SuppressWarnings
and @tailrec
.
To define your own annotation, simply prefix a class with the keyword annotation
:
annotation class Foo
This annotation can then be used in classes, functions, parameters, and so on. In fact, annotations can pretty much be used anywhere, as the following table shows:
Target |
Example |
Class |
|
Interface |
|
Object |
|
Parameter | ... |