12 Annotations and reflection

 

This chapter covers

  • Applying and defining annotations
  • Using reflection to introspect classes at run time
  • A real example of a Kotlin project

Up to this point, you’ve seen many features for working with classes and functions, but they all require you to specify the exact names of classes and functions you’re using as part of the program source code. To call a function, you need to know the class in which it was defined as well as its name and parameter types. Annotations and reflection give you the power to go beyond that and to write code that deals with arbitrary classes that aren’t known in advance. You can use annotations to add additional metadata and semantics to declarations. For example, you could use them to indicate whether a declaration is deprecated (as you’ll see in section 12.1.1), you could use them for integrations with the compiler, your IDE, and external tools (as you’ll see in section 12.1.2), or you could use them to construct library-specific and custom semantics. Reflection allows you to analyze your declarations at run time.

Applying annotations is straightforward, but writing your own annotations, and especially writing the code that handles them, is less trivial. The syntax for using annotations is exactly the same as in Java, whereas the syntax for declaring your own annotation classes is a bit different. The general structure of the reflection APIs is also similar to Java, but the details differ.

12.1 Declaring and applying annotations

12.1.1 Applying annotations to mark declarations

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

12.1.3 Using annotations to customize JSON serialization

12.1.4 Creating your own annotation declarations

12.1.5 Meta-annotations: Controlling how an annotation is processed

12.1.6 Passing classes as annotation parameters to further control behavior

12.1.7 Generic classes as annotation parameters

12.2 Reflection: Introspecting Kotlin objects at run time

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

12.2.2 Implementing object serialization using reflection

12.2.3 Customizing serialization with annotations

12.2.4 JSON parsing and object deserialization

Summary