Annotation Processing
Learn how Java metadata is processed and used by the compiler and runtime tools.
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.
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.
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.
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.
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.
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.
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.