Week 106 — What is an annotation processor?
Question of the Week #106
What is an annotation processor?
5 Replies
Annotation processors allow running Java code during compilation for validation and code generation.
Annotation processors can be invoked when compiling code with specific annotations or when any class is compiled (given the annotation processor is registered and enabled).
An annotation processor can be created by implementing the
Processor
interface which is typically done by extending AbstractProcessor
. When extending AbstractProcessor
, one can specify the annotations to watch using SupportedAnnotationTypes
and the latest supported Java version of the source code using @SupportedSourceVersion
.
Annotation processing happens in multiple "rounds". Each "round" , the process
method is called. If this method returns returns true
, the processed annotations are "claimed" for this round resulting in other processors not processing these annotations.
Annotation processors are registered using the ServiceLoader
API. This can be done by adding a provides
statement in the module-info.java
file:
Non-modular annotation processors can be registered by including the fully qualified name of the annotation processor in a file named javax.annotation.processing.Processor
in META-INF/services
.
When compiling code using --processor-module-path
or --processor-path
pointing to the compiled code of the annotation processor, it will be run during compilation. Furthermore, it is possible to specify annotation processors explicitly using -processor
followed by the fully qualified name of the annotation processor class.
The -proc
argument can be used to specify whether it should do compiling, annotation processing or both. -proc:none
only compiles the code without running annotation processors. -proc:only
runs annotation processors but does not compile code. -proc:full
runs annotation processor and compiles the code.The
AbstractProcessor
class provides a field named processingEnv
that can be used to emit notes, warnings and errors or generate code.
📖 Sample answer from dan1st
Annotation processors are a way of extending the Java compiler to perform custom logic in response to annotations in your Java code. Typically, this custom logic is to generate other files (in fact, this is the official, stated purpose of annotation processors). These could be anything -- new Java source files, configuration files, etc.
Many tools in the Java ecosystem make use of annotation processing. For instance, Dagger is a dependency injection engine that generates static dependency information during compilation, which the runtime then uses for very performant dependency resolution and injection.
Submission from dangerously_casual
An annotation processor in Java operates during compilation to process specific annotations in source code. It generates additional files like source code or resources but cannot modify existing code. It’s useful for automating tasks, enforcing standards, or generating boilerplate code.
Annotation processors run in rounds during compilation:
1. Initialization: Initializes with a ProcessingEnvironment.
2. Processing: Processes annotations and generates files, triggering new rounds if files are created.
3. Completion: Ends when no new files are generated.
To create a processor, extend AbstractProcessor and override process, specifying supported annotations with @SupportedAnnotationTypes.
Example:
Widely used in frameworks like Lombok, annotation processors enhance functionality by generating code for tasks like getters and constructors. They ensure source code remains unchanged during compilation.
⭐ Submission from davinki