Introduction
In this blog, we'll go over the basics of Kotlin annotations. Annotations are a way to embed metadata in your code. You will learn how to use them and how to make and personalize your own custom annotations. After that, we'll talk about JVM-related annotations and their different use cases. In the next section, we will start by understanding the utility and use of Kotlin Annotations.
Kotlin Annotations
Annotations are used at compile time to attach metadata to classes, interfaces, parameters, etc. The compiler can apply annotations, which reflect at runtime. According to the annotation values, we can change the meaning of the data or program.
Declaring an Annotation
Annotation is declared by appending the annotation modifier to a class name. Defining a class first and then adding the annotation keyword before it is a must. Annotation declarations cannot include any code by definition.
annotation class TestClass
The annotation declaration shown above depicts a simple annotation with no input parameters. One can use the following syntax to declare an annotation with parameters.
annotation class Calc(val sum: Integer)
Kotlin Meta-Annotations
We should describe which code components our custom annotations may apply to and where we should keep them while declaring the annotations. Meta-annotations are annotations that define meta-information (additional information).
Some of the meta-annotations are listed as follows:
@Target
This meta-annotation indicates the code elements to which this annotation could refer. It has a mandatory parameter that must be either an instance or an array of the AnnotationTarget enumeration. Thus one can specify the elements to which they want to apply the annotation. Some elements include LOCAL_VARIABLE, PROPERTY_GETTER, PROPERTY_SETTER, VALUE_PARAMETER, CONSTRUCTOR, etc.
The following is a sample program to demonstrate @Target annotation.
@Target(AnnotationTarget.CONSTRUCTOR)
annotation class TargetConstructorAnnotation
class Ninja @TargetConstructorAnnotation constructor(val ninja_id:Int){
fun display(){
println("Example of constructor annotation using @Target")
println("Ninja ID is $ninja_id")
}
}
fun main(){
val obj = Ninja(1)
obj.display()
}
Output:
Example of constructor annotation using @Target
Ninja ID is 1
@Retention
@Retention indicates whether the annotation should be stored in the .class file and whether it should be seen in the reflection. Its required parameter must be an AnnotationRetention enumeration instance with the following elements, SOURCE, BINARY and RUNTIME. The default value for the @Retention is RUNTIME in the Kotlin programming language.
The following is a sample program to demonstrate @Retention annotation.
@Retention(AnnotationRetention.RUNTIME)
annotation class RetentionAnnotation
// Annotating main function
@RetentionAnnotation fun main(){
println("Example of @Retention annotation")
}
Output:
Example of @Retention annotation
@Repeatable
If an element can have several annotations of the same kind, it is marked as @Repeatable. There are no parameters for this meta-annotation.
The following is a sample program to demonstrate @Repeatable annotation.
@Repeatable
@Retention(AnnotationRetention.SOURCE) // As per the latest Kotlin versions, the Repeatable annotation can be used with Retention policy set as source
annotation class RepeatableAnnotationExample (val name: String)
@RepeatableAnnotationExample("Coding")
@RepeatableAnnotationExample("Ninjas")
fun test(){
println("Example of repeatable annotation being applied on the test function")
}
fun main(){
test()
}
Output:
Example of repeatable annotation being applied on the test function
@MustBeDocumented
The annotation's documentation must be included in the generated documentation if @MustBeDocumented is true. This meta-annotation, too, takes no parameters.
The following is a sample program to demonstrate @MustBeDocumented annotation.
@MustBeDocumented
annotation class MustBeDocumentedExample
// Annotating main function using @MustBeDocumented annotation
@MustBeDocumentedExample fun main(){
println("Example of @MustBeDocumented annotation")
}
Output:
Example of @MustBeDocumented annotation




