EZ

Eduzan

Learning Hub

Eduzan
Eduzan / Kotlin

Kotlin Miscellaneous

In Kotlin, annotations provide a way to attach metadata to code. This metadata can then be used by development tools, libraries, or frameworks to process the code without altering its behavior. Annotations are applied to code elements such as classes, functions, properties, or parameters and are typically evaluated at compile-time.

Annotations frequently contain the following parameters, which must be compile-time constants:

1. Primitive types (e.g., Int, Long, etc.)
2. Strings
3. Enumerations
4. Classes
5. Other annotations
6. Arrays of the types mentioned above

Applying Annotations

To apply an annotation, simply use the annotation name prefixed with the @ symbol before the code element you wish to annotate. For example:

@Positive val number: Int

If an annotation accepts parameters, these can be passed inside parentheses, much like a function call:

@AllowedLanguage("Kotlin")

When passing another annotation as a parameter to an annotation, omit the @ symbol. For instance:

@Deprecated("Use === instead", ReplaceWith("this === other"))

When using class objects as parameters, use ::class:

@Throws(IOException::class)

An annotation that requires parameters looks similar to a class with a primary constructor:

annotation class Prefix(val prefix: String)

Annotating Specific Elements

1. Annotating a Constructor : You can annotate class constructors by using the constructor keyword:

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

2. Annotating a Property : Annotations can be applied to properties within a class. For example:

class Language(
    @AllowedLanguages(["Java", "Kotlin"]) val name: String
)

Built-in Annotations in Kotlin

Kotlin provides several built-in annotations that offer additional functionality. These annotations are often used to annotate other annotations.

1. @Target: The @Target annotation specifies where an annotation can be applied, such as classes, functions, or parameters. For example:

@Target(AnnotationTarget.CONSTRUCTOR, AnnotationTarget.LOCAL_VARIABLE)
annotation class CustomAnnotation

class Example @CustomAnnotation constructor(val number: Int) {
    fun show() {
        println("Constructor annotated with @CustomAnnotation")
        println("Number: $number")
    }
}

fun main() {
    val example = Example(5)
    example.show()

    @CustomAnnotation val message: String
    message = "Hello Kotlin"
    println("Local variable annotated")
    println(message)
}

Output:

Constructor annotated with @CustomAnnotation
Number: 5
Local variable annotated
Hello Kotlin

2. @Retention: The @Retention annotation controls how long the annotation is retained. It can be retained in the source code, in the compiled class files, or even at runtime. The parameter for this annotation is an instance of the AnnotationRetention enum:

  • SOURCE
  • BINARY
  • RUNTIME

Example:

@Retention(AnnotationRetention.RUNTIME)
annotation class RuntimeAnnotation

@RuntimeAnnotation
fun main() {
    println("Function annotated with @RuntimeAnnotation")
}

Output:

Function annotated with @RuntimeAnnotation

3. @Repeatable: The @Repeatable annotation allows multiple annotations of the same type to be applied to an element. This is currently limited to source retention annotations in Kotlin.

Example:

@Repeatable
@Retention(AnnotationRetention.SOURCE)
annotation class RepeatableAnnotation(val value: Int)

@RepeatableAnnotation(1)
@RepeatableAnnotation(2)
fun main() {
    println("Multiple @RepeatableAnnotation applied")
}

Output:

Multiple @RepeatableAnnotation applied
End of lesson.