![Aayush Chaudhary](https://miro.medium.com/v2/resize:fill:88:88/1*yTxZUXiqT5qWHPnv0aVdsQ.png)
![ProAndroidDev](https://miro.medium.com/v2/resize:fill:48:48/1*XVtdl45m8YaYrPI4buJ5yQ.png)
Kotlin is a strong and expressive programming language that gives many options that make your code concise, readable, and maintainable. One in all these options is annotations, which let you add metadata to your code that compilers, instruments, and frameworks can use to simplify and automate widespread duties. On this weblog publish, we’ll discover Kotlin’s annotations and the way you need to use them to reinforce your code and make improvement sooner and simpler.
Kotlin annotations might help you simplify your code and scale back boilerplate by automating widespread duties like serialization, validation, and dependency injection. They’ll additionally assist you to implement coding requirements and finest practices by offering further context and constraints on how your code is used. Moreover, annotations can be utilized to generate documentation, assessments, and different artifacts based mostly in your code.
annotation class CustomAnnotation
If you wish to add extra content material to your annotations, you are able to do that by putting some further annotations on high of the annotation layer. These are referred to as meta annotations.
-
@Goal
Annotation is used to specify the doable targets of the annotation. Targets outline the weather of the code that the annotation might be utilized to (corresponding to lessons, features, properties, and expressions); -
@Retention
annotation is used to specify the retention coverage of the annotation. Retention insurance policies outline how lengthy annotated metadata is retained. Kotlin affords three retention insurance policies:
–SOURCE
: Feedback with this retention coverage are retained solely at
compile time and never out there at runtime. This implies they’ll
utilized by instruments like IDEs and code turbines for static execution
parse and generate code, however they can’t be accessed through reflection
throughout run time.
–BINARY
: Feedback with this retention coverage are retained in
compiled class recordsdata, however not out there at runtime. Which means
they can be utilized by different instruments and frameworks that work on
bytecode, however they can’t be accessed through reflection at runtime.
–RUNTIME
: Feedback with this retention coverage are retained in
class recordsdata are compiled and out there at runtime through reflection.
This implies they can be utilized to supply runtime habits corresponding to
configure dependency injection, serialization or logging. -
@Repeatable
annotation is used to specify {that a} remark might be utilized greater than as soon as to the identical goal aspect. By default, annotations in Kotlin can solely be utilized as soon as to the goal aspect. -
@MustBeDocumented
footnote is used to point that the remark ought to be recorded within the generated doc. By default, Kotlin feedback will not be included within the generated doc until you explicitly doc them. This may be problematic if you wish to create reusable annotations supposed to be used in a number of initiatives or by different builders.
You’ll be able to create customized captions utilizing annotation class
key phrase. This is an instance of easy methods to outline a customized caption:
@Goal(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
annotation class MyAnnotation(val someValue: String)
On this instance, we have now outlined an annotation referred to as MyAnnotation
has a single parameter referred to as someValue
kind String
.
The @Goal
annotations are used to specify the place annotations can be utilized. On this case, we have now set the goal to AnnotationTarget.CLASS
which signifies that annotations can solely be utilized to lessons.
The @Retention
annotation is used to specify the retention coverage of the annotation. On this case, we have now set the retention coverage to AnnotationRetention.RUNTIME
which suggests the annotation will likely be out there at runtime through reflection.
To make use of a customized annotation, merely apply it to a category like this:
@MyAnnotation("Hey, World!")
class MyClass {
// class implementation right here
}
On this instance, we have now utilized MyAnnotation
caption for MyClass
class and supply a worth for someValue
parameters.
To entry the worth of the annotation at runtime you need to use reflection:
val annotation = MyClass::class.annotations.discover { it's MyAnnotation } as MyAnnotation
println(annotation.someValue) // prints "Hey, World!"
This code retrieves the annotation utilized to MyClass
and transmit it to MyAnnotation
in order that we will entry its properties. On this case we’re printing the worth of someValue
parameters.