Industrial Training




Kotlin Annotations


Annotations are used to attach metadata to classes, interface, parameters, and so on at compile time. Annotation can be used by compiler which reflects at runtime. We can change the meaning of the data or program according to annotation values.


Kotlin Meta-annotations


We can add meta-info while declaring annotation. Following are some meta-annotations:


Annotation Name Usage
@Target It targets all the possible kinds of elements which can be annotated with the annotation.
@Retention It specifies whether the annotation is stored in the compiled class files or whether it is visible through reflection at run time.
@Repeatable This meta-annotation determines that an annotation is applicable twice or more on a single code element.
@MustBeDocumented This meta-document specifies that the annotation is the part of the public API and should be included in the class or method.

Example of using annotation


@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION,  
AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.EXPRESSION)  
@Retention(AnnotationRetention.SOURCE)  
@MustBeDocumented  
annotation class MyClass  

Declaring an annotation


Annotation is declared by placing annotation modifier in front of a class.


annotation class MyClass  

Annotate a constructor


It is also possible to annotate the constructor of a class. This is done by adding the constructor keyword for constructor declaration and placing the annotation before it.


class MyClass@Inject constructor( dependency: MyDependency){  
//. . .   
}   

Annotate property assessors


class MyClass{  
var a: MyDependency? = null  
                    @Inject set  
}  


Using constructor as annotation


We can also use constructor as an annotation. Using constructor as annotation takes parameters.


annotation class MyClass(val why: String)  
@MyClass("parameter") class Foo{  
}  

The parameters which are used as an annotation cannot be nullable types. This is because the JVM does not support null as a value for an annotation attribute.
We can also use one annotation as a parameter to another annotation, at such situation it cannot takes the prefix @ character. Forexample:


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"))  

Kotlin also specifies that a class can takean argument of an annotation by using a KClass. The Kotlin compiler automatically converts it into java class, which leads to see the annotations and arguments normally.


import kotlin.reflect.KClass  
annotation class MyClass(val arg1: KClass<*>, val arg2: KClass)  
@MyClass(String::class, Int::class) class Foo  

Example of using TYPE annotation


Creating a java annotation interface Ann.java


import java.lang.annotation.ElementType;  
import java.lang.annotation.Retention;  
import java.lang.annotation.RetentionPolicy;  
import java.lang.annotation.Target;  
@Target(ElementType.TYPE)  
@Retention(RetentionPolicy.RUNTIME)  
@interface  Ann{  
int value();  
}  

Create a MyClass.kt class which uses the annotation interface Ann.


@Ann(value = 10)  
class MyClass{  
  
}  
fun main (args: Array){  
var c = MyClass()  
var x = c.javaClass.getAnnotation(Ann::class.java)  
    if(x!=null){  
println("Value:"+x?.value)  
    }  
}  

Output:
Value: 10



Hi I am Pluto.