Retention Policies and Target Types
Choose SOURCE, CLASS, or RUNTIME retention and restrict target elements with @Target.
The Three Retention Policies
SOURCE: discarded by the compiler (only useful to APT). CLASS: written to .class files, not available at runtime. RUNTIME: available via reflection. Most framework annotations use RUNTIME.
SOURCE Retention: Compile-Time Only
SOURCE annotations are processed by annotation processors and then discarded. @Override and @SuppressWarnings are SOURCE — they inform the compiler but are not in the .class file.
@Retention(RetentionPolicy.SOURCE)
public @interface GenerateBuilder {}
// APT generates a Builder class when it sees @GenerateBuilder
// The annotation itself disappears from the compiled outputAll lessons in this course
- Defining Annotations: Elements and Defaults
- Retention Policies and Target Types
- Runtime Annotation Processing
- Compile-Time Annotation Processors