Skip to main content

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.

The @Target meta-annotation in Kotlin dictates the specific syntactic elements to which a custom annotation can be applied. By applying @Target to an annotation class declaration, the compiler enforces structural constraints, preventing the annotation from being attached to invalid language constructs during compilation. It accepts a vararg of kotlin.annotation.AnnotationTarget enum values.
@Target(
    AnnotationTarget.CLASS,
    AnnotationTarget.FUNCTION,
    AnnotationTarget.VALUE_PARAMETER
)
annotation class RestrictedAnnotation

The AnnotationTarget Enum

The AnnotationTarget enum provides granular control over Kotlin’s language features, which often map to multiple underlying JVM elements. The available targets include:
  • CLASS: Classes, interfaces, objects, and annotation classes.
  • ANNOTATION_CLASS: Restricted exclusively to other annotation classes (used for defining meta-annotations).
  • TYPE_PARAMETER: Generic type parameter declarations (e.g., <T>).
  • PROPERTY: Kotlin properties.
  • FIELD: Backing fields of properties.
  • LOCAL_VARIABLE: Variables declared within a function or block scope.
  • VALUE_PARAMETER: Parameters of functions or constructors.
  • CONSTRUCTOR: Primary or secondary constructors.
  • FUNCTION: Functions (excluding constructors).
  • PROPERTY_GETTER / PROPERTY_SETTER: Explicit or implicit property accessors.
  • TYPE: Type usages (e.g., the type specified in a variable declaration, function return type, or supertype).
  • EXPRESSION: Arbitrary expressions.
  • FILE: File-level declarations.
  • TYPEALIAS: Typealias declarations.

Default Behavior

If the @Target meta-annotation is omitted from an annotation class declaration, the Kotlin compiler applies a default set of targets. The annotation implicitly becomes applicable to: CLASS, PROPERTY, FIELD, LOCAL_VARIABLE, VALUE_PARAMETER, CONSTRUCTOR, FUNCTION, PROPERTY_GETTER, and PROPERTY_SETTER. It implicitly excludes TYPE, TYPE_PARAMETER, EXPRESSION, FILE, and TYPEALIAS (note that ANNOTATION_CLASS is technically covered by the inclusion of CLASS).

Resolution and Use-Site Targets

Because a primary constructor parameter declared with val or var simultaneously declares a property, it can generate multiple underlying JVM elements (the constructor parameter, a backing field, a getter, and a setter). The @Target annotation dictates how the compiler resolves ambiguous applications in these overlapping constructs. If an annotation’s @Target includes both PROPERTY and FIELD, applying it to a property declaration defaults to the PROPERTY target. To force the annotation onto the backing field or an accessor, the developer must use use-site target syntax, provided those targets are explicitly permitted by the annotation’s @Target declaration.
@Target(AnnotationTarget.PROPERTY, AnnotationTarget.FIELD)
annotation class DualTargetAnnotation

class Example {
    // Resolves to AnnotationTarget.PROPERTY based on default compiler resolution
    @DualTargetAnnotation 
    val defaultTarget: String = ""

    // Explicitly resolves to AnnotationTarget.FIELD using use-site syntax
    @field:DualTargetAnnotation 
    val explicitTarget: String = ""
}
Master Kotlin with Deep Grasping Methodology!Learn More