liveBook · Manning

Chapter 12: Annotations and reflection

What you will learn in chapter 12 of Kotlin in Action, 2E

Introduces annotations as metadata on declarations and reflection as a way to inspect arbitrary classes at run time, using a JSON library to illustrate both.

After this chapter you will be able to

What it builds on

Section by section

Introduction

Introduces annotations as metadata on declarations and reflection as a way to inspect arbitrary classes at run time, using a JSON library to illustrate both.

Key terms: annotations, metadata, declarations, reflection, run time, arbitrary classes, JKid, JSON serialization, deserialization

12.1 Declaring and applying annotations

This section explains that annotations attach metadata to declarations and that their configuration determines where tools can access it.

Key terms: annotations, metadata, declaration

12.1.1 Applying annotations to mark declarations

This section shows how to apply Kotlin annotations and supply valid, compile-time arguments, using @Deprecated as a detailed example.

Key terms: annotation, declaration, @Test, @Deprecated, message, replaceWith, WARNING, ERROR, HIDDEN, annotation arguments

12.1.2 Specifying the exact declaration an annotation refers to: Annotation targets

This section explains how use-site targets place annotations on the intended declaration generated from Kotlin code and how annotations can affect the Java API.

Key terms: use-site target, get, set, @JvmName, property, field, receiver, param, setparam, delegate

12.1.3 Using annotations to customize JSON serialization

This section shows how JKid converts Kotlin objects to and from JSON and how two annotations change the default JSON representation.

Key terms: serialize, deserialize, JSON representation, type argument, @JsonName, @JsonExclude, property names, keys, default value, deserialization

12.1.4 Creating your own annotation declarations

Explains how Kotlin annotation classes are declared, how their parameters work, and how applying them differs from applying Java annotations.

Key terms: annotation modifier, annotation class, metadata, primary constructor, val, named-argument syntax, value, JsonName

12.1.5 Meta-annotations: Controlling how an annotation is processed

This section explains how meta-annotations control where Kotlin annotations can be applied and whether they remain available at run time.

Key terms: meta-annotations, @Target, AnnotationTarget, ANNOTATION_CLASS, property target, AnnotationTarget.FIELD, @Retention, RUNTIME retention, reflection, .class file

12.1.6 Passing classes as annotation parameters to further control behavior

This section shows how a class reference passed to an annotation can choose an implementation for interface-typed properties during deserialization, and why the annotation parameter uses KClass<out Any>.

Key terms: @DeserializeInterface, CompanyImpl::class, ::class, class reference, KClass, KClass<CompanyImpl>, KClass<out Any>, out

12.1.7 Generic classes as annotation parameters

This subsection shows how to declare an annotation parameter that accepts a custom serializer class while restricting it to implementations of a generic serializer interface.

Key terms: JKid, @CustomSerializer, ValueSerializer, KClass<out ValueSerializer<*>>, star projection

12.2 Reflection: Introspecting Kotlin objects at run time

Introduces reflection as a way to inspect objects at run time, explains its use in serialization, and identifies Kotlin and Java reflection options.

Key terms: reflection, run time, serialization, Kotlin reflection API, Java reflection API, Java bytecode, kotlin-reflect.jar, org.jetbrains.kotlin:kotlin-reflect

12.2.1 The Kotlin reflection API: KClass, KCallable, KFunction, and KProperty

Introduces Kotlin reflection interfaces for inspecting classes and accessing functions and properties at run time.

Key terms: KClass, memberProperties, KCallable, KFunction, KFunction1, call, invoke, KProperty0, KProperty1, KMutableProperty0

12.2.2 Implementing object serialization using reflection

The section shows how JKid uses reflection to serialize an object’s properties into a JSON string.

Key terms: KProperty1<Any, *>, prop.get(obj), Any?, receiver, joinToStringBuilder, serializeString, serializePropertyValue, JSON arrays, nested objects, serialize

12.2.3 Customizing serialization with annotations

This section shows how JSON serialization uses reflection to honor annotations that exclude properties, rename JSON keys, and supply custom value serializers.

Key terms: @JsonExclude, KProperty, KAnnotatedElement, findAnnotation, filter, @JsonName, prop.name, @CustomSerializer, getSerializer, ValueSerializer

12.2.4 JSON parsing and object deserialization

This section explains how JKid parses JSON and gathers values in seeds before constructing deserialized Kotlin objects.

Key terms: deserialize, reified, inline, lexer, tokens, parser, deserializer, JsonObject, propertyName, createObject

12.2.5 The final step of deserialization: callBy() and creating objects using reflection

This section explains how JKid uses reflection to construct deserialized objects, convert JSON values to constructor parameter types, and cache the information needed to do so efficiently.

Key terms: KCallable.call, KCallable.callBy, default parameter values, KParameter.type, KType, ValueSerializer, @CustomSerializer, typeOf<>(), ClassInfoCache, ClassInfo

12.3 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 11: Generics Chapter 13: DSL construction →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.