liveBook · Manning

KAnnotatedElement

As explained in Kotlin in Action, 2E

KAnnotatedElement is a Kotlin reflection interface implemented by runtime representations of declarations that can carry annotations.

KAnnotatedElement represents a runtime declaration that can carry annotations. Interfaces such as KClass, KFunction, KParameter, and KProperty extend it, so reflection code can inspect annotations through a common API. section 12.2.1

Filtering annotated properties

A serializer can inspect each reflected property and exclude those marked with @JsonExclude: val properties = kClass.memberProperties .filter { it.findAnnotation<JsonExclude>() == null } Here, property.annotations exposes all runtime-retained annotations, while findAnnotation<JsonExclude>() returns the matching annotation or null. The same lookup supports annotations carrying values, such as @JsonName("alias"). section 12.2.3

Reflection-based serializers use KAnnotatedElement to:

  • Read all annotations from a KProperty through its annotations collection. section 12.2.3
  • Find one annotation by type with findAnnotation, then branch on its presence or read its arguments. section 12.2.3

How Kotlin in Action, 2E covers it

How deep it goes
Defines the common annotation interface and demonstrates property lookup, filtering, argument access, and custom serializer selection.
Examples
Uses @JsonExclude, @JsonName, and @CustomSerializer within reflective JSON serialization.
What it assumes
Assumes basic Kotlin reflection and annotations, while establishing how declaration interfaces expose annotations at runtime.
What you can do after
Apply annotation lookup to filter reflected properties, rename serialized fields, and select custom value serializers.
How it connects
Connects Kotlin reflection with KClass, KProperty, KFunction, runtime-retained annotations, and findAnnotation.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kotlin in Action, 2E. The section links point to the parts of the book it draws on.