Home

annotationProcessor

annotationProcessor refers to a program that runs during compilation to read annotations and generate, modify, or validate code and resources. In Java, processors typically extend AbstractProcessor or implement the Processor interface from the annotation processing API.

Processing occurs in rounds. Each round allows the processor to examine annotated elements, write new source

Registration uses the Java Service Provider mechanism: a file named META-INF/services/javax.annotation.processing.Processor lists the processor class. A

Common uses include code generation (for boilerplate or binding code), compile-time validation, and resource generation. The

Implementation notes: create a class extending AbstractProcessor and override process(Set<? extends TypeElement>, RoundEnvironment). Use RoundEnvironment to

Build tools support annotation processing. In Maven, configure the maven-compiler-plugin; in Gradle, add dependencies to the

Limitations include that processing occurs at compile time and can slow builds. Processors should be deterministic

files
via
the
Filer,
and
report
messages
through
the
Messager.
If
any
new
files
are
created,
another
round
runs
to
process
them.
The
cycle
ends
when
no
new
files
are
produced.
processor
declares
which
annotations
it
supports
and
which
source
version
it
targets,
via
getSupportedAnnotationTypes/getSupportedSourceVersion
or
the
corresponding
@SupportedAnnotationTypes/@SupportedSourceVersion
annotations.
ProcessingEnvironment
provides
utilities
such
as
Elements,
Types,
Messager,
and
Filer
to
inspect
code,
create
files,
and
report
diagnostics.
find
annotated
elements
and
Filer
to
write
new
Java
files.
To
automate
service
registration,
many
projects
use
the
AutoService
library
to
generate
the
META-INF
entry.
annotationProcessor
configuration.
Incremental
processing
can
improve
build
times.
and
idempotent
to
avoid
duplicate
output,
and
care
should
be
taken
with
round
semantics
and
dependency
on
generated
sources.