> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Kotlin Annotation

Annotations are a mechanism for attaching metadata to declarations and expressions in Kotlin. They are resolved at compile-time and can be retained in the compiled class files for runtime reflection, but they do not directly alter the execution semantics of the annotated code.

## Declaration

An annotation is defined using the `annotation` modifier preceding a `class` declaration. Annotation classes can contain a body, but the language strictly limits its contents. They can include companion objects, nested classes, interfaces, and objects, but cannot contain functions, `init` blocks, or properties outside the primary constructor.

```kotlin theme={"dark"}
annotation class MarkerAnnotation
```

## Meta-Annotations

The behavior and scope of an annotation are configured using meta-annotations (annotations applied to the annotation class itself).

* `@Target`: Defines the syntactic elements to which the annotation can be applied (e.g., `CLASS`, `FUNCTION`, `PROPERTY`, `EXPRESSION`).
* `@Retention`: Determines the lifecycle of the annotation.
  * `SOURCE`: Discarded by the compiler; not present in the compiled binary.
  * `BINARY`: Stored in the compiled class file but not visible via reflection at runtime.
  * `RUNTIME`: (Default) Stored in the compiled class file and accessible via runtime reflection.
* `@Repeatable`: Permits the annotation to be applied multiple times to the same element.
* `@MustBeDocumented`: Instructs documentation generation tools (like Dokka) to include the annotation in the public API signature.

```kotlin theme={"dark"}
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
@Repeatable
@MustBeDocumented
annotation class Component
```

## Constructors and Parameters

Annotations can accept parameters via a primary constructor. All parameters must be declared as read-only (`val`).

The type system strictly limits annotation parameters to the following:

* Primitive types (`Int`, `Double`, `Boolean`, etc.)
* `String`
* Classes (`KClass`)
* Enums
* Other annotations
* Arrays of the aforementioned types

Nullable types and default JVM classes (like `java.lang.Class`) are strictly prohibited.

```kotlin theme={"dark"}
enum class Priority { LOW, HIGH }

annotation class Task(
    val id: String,
    val priority: Priority = Priority.LOW,
    val relatedClasses: Array<KClass<*>>
)
```

## Application Syntax

Annotations are applied by prefixing the target element with the `@` symbol, followed by the annotation name and any required constructor arguments.

```kotlin theme={"dark"}
@Task(id = "T-101", relatedClasses = [String::class, Int::class])
fun processData() { TODO() }
```

If an annotation is used as a parameter within another annotation, the `@` prefix is omitted for the nested annotation.

```kotlin theme={"dark"}
annotation class TaskGroup(val primaryTask: Task)

@TaskGroup(primaryTask = Task(id = "T-102", relatedClasses = []))
class Worker
```

## Use-Site Targets

Because a single Kotlin property declaration can generate multiple Java bytecode elements (a backing field, a getter, a setter, and a constructor parameter), Kotlin provides use-site targets to explicitly specify which generated element receives the annotation.

The syntax follows the pattern `@target:AnnotationName`.

Supported use-site targets include:

* `file`: Applies to the entire file (must be placed at the top of the file, before `package`).
* `property`: Applies to the Kotlin property (invisible to Java reflection).
* `field`: Applies to the generated Java backing field.
* `get` / `set`: Applies to the generated property getter or setter.
* `receiver`: Applies to the receiver parameter of an extension function or property.
* `param`: Applies to the constructor parameter.
* `setparam`: Applies to the parameter of the property setter.
* `delegate`: Applies to the field storing the delegate instance for a delegated property.

```kotlin theme={"dark"}
class User(
    @field:MarkerAnnotation val name: String,   // Annotates the backing field
    @get:MarkerAnnotation val age: Int,         // Annotates the getter method
    @param:MarkerAnnotation val role: String    // Annotates the constructor parameter
)
```

## Expression and Lambda Annotations

Annotations can be applied directly to expressions or lambda functions, provided the annotation's `@Target` explicitly includes `AnnotationTarget.EXPRESSION`.

```kotlin theme={"dark"}
@Target(AnnotationTarget.EXPRESSION)
@Retention(AnnotationRetention.SOURCE)
annotation class InlineMarker

val multiplier = @InlineMarker { x: Int -> x * 2 }
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Kotlin Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
