Java learning pathSection 19

Annotation Processing

Learn how Java metadata is processed and used by the compiler and runtime tools.

1

Built-in Annotations

Java ships with built-in annotations such as @Override, @Deprecated, @SuppressWarnings, and @FunctionalInterface.

These annotations help compilers enforce contracts and communicate intent to developers.

Annotated source enters the compiler and annotation processors, which generate sources and validation metadata. Generated sources feed later processing rounds; retention controls source, class-file, and runtime visibility.
Built-in Annotations
2

Custom Annotations

Custom annotations let developers define their own metadata types and attach them to classes, methods, or fields.

They are often used in frameworks, validation libraries, and code generation tools.

3

Meta-Annotations

Meta-annotations are annotations that can be applied to other annotations, such as @Target, @Retention, and @Inherited.

They define how custom annotations are used and how they behave at runtime or compile time.

4

Retention Policies

Retention policies define whether metadata is kept in source, class files, or runtime. The choice affects visibility to compile-time tools and reflection.

This is crucial for frameworks that need annotation information at runtime.

5

Target Types

Target types restrict where an annotation can be used, such as on methods, parameters, types, fields, or packages.

This helps keep annotation usage precise and semantically meaningful.

6

Annotation Processing API

The Annotation Processing API allows tools to inspect and generate source or bytecode during compilation.

It is used by libraries such as Lombok, MapStruct, and many code-generation frameworks.

7

Runtime Annotation Processing

Runtime annotation processing reads annotations using reflection or framework-specific logic at application startup.

This allows frameworks to react to metadata without modifying application source manually.