Annotations
Annotations play a crucial role in Kotlin, providing metadata and influencing code behavior. The Analysis API allows you to access and analyze annotations applied to both declarations and types.
KaAnnotated
KaAnnotated is an interface exposing a list of annotations. All types and almost all symbols implement it.
The only property KaAnnotated declares is annotations of the KaAnnotationList type. KaAnnotationList itself is implements a List<KaAnnotation>, so you can directly iterate over annotations:
KaAnnotationsList is not just a list. It can also efficiently check whether an annotation with some ClassId is there:
You can also get a list of annotations with a specific ClassId. Kotlin allows repeatable annotations, so a list of them is returned:
Finally, KaAnnotationList exposes a collection of all annotation ClassIds:
Use-Site Targets
Annotations on declarations can have use-site targets, specifying where the annotation applies (e.g., property, field, or parameter). You can use the useSiteTarget property of KtAnnotation to access this information.