Scrivio Interview library
InternalsIntermediate6 min read · updated 12 November 2026

How does a JSR 269 annotation processor work? Describe the rounds, what a processor can read, and what it can and cannot change.

A JSR 269 annotation processor is a class that javac loads and runs during compilation, found through the -processor option or a META-INF/services/javax.annotation.processing.Processor file on the processor path. Processing runs in rounds: in each round javac gives processors the annotated elements of the sources it has, processors may generate new files, and any newly generated sources are processed in another round until a round produces nothing new. Processors read a model of the code through javax.lang.model elements and type mirrors, not loaded classes, and can create new source, class or resource files through the Filer and report errors through the Messager, but the standard API cannot modify existing classes; Lombok does that through internal compiler APIs.

Why this question is asked

Annotation processors power some of the most useful tools in the Java ecosystem: MapStruct, Dagger, Immutables, the Hibernate metamodel generator, Micronaut's dependency injection and Lombok. Interviewers ask how they work because it explains why these tools behave so differently from run-time frameworks, and why some break on JDK upgrades while others never do. In this answer, we will see how javac discovers processors, follow the rounds of processing, look at the model a processor reads, see what it can produce and what it cannot change, and note the recent change in how JDKs enable processors.

1How javac Discovers Processors

A processor implements javax.annotation.processing.Processor, usually by extending AbstractProcessor. javac finds processors in one of two ways. We can name them explicitly with -processor com.acme.BuilderProcessor. Or javac uses the ServiceLoader mechanism: it looks on the processor path for a file named META-INF/services/javax.annotation.processing.Processor, which lists processor class names, one per line. Build tools set this up for us; Maven's annotationProcessorPaths and Gradle's annotationProcessor configuration put processor jars on the processor path, separately from the compile class path.

Java
# META-INF/services/javax.annotation.processing.Processor inside the processor jar
com.acme.processor.BuilderProcessor
# Gradle
dependencies {
annotationProcessor("org.mapstruct:mapstruct-processor:1.6.3")
}

Recent JDKs changed the default here. For a long time, javac also ran any processor it found on the ordinary class path. Starting around JDK 23, that implicit discovery is off by default unless processing is requested explicitly, for example with -processor, --processor-path, or -proc:full; JDK 21 printed a warning about it first. Check your JDK version, and configure processors explicitly in the build so behaviour does not change with an upgrade.

2Processing in Rounds

Processing is iterative because a processor may generate new source files, and those new files may themselves contain annotations other processors care about. So javac works in rounds. In the first round it parses the sources being compiled, finds which annotations they use, and calls the process method of each processor that declared support for those annotations, passing a RoundEnvironment that lists the annotated elements of this round.

If any processor generated new source files, javac parses them and starts another round, offering only the new elements. When a round generates nothing new, javac runs one final round in which processingOver() returns true, so processors can finish up, and then it compiles everything, original and generated sources together, into class files.

Step throughHow javac runs processors
  1. Parse sources, build the element model
  2. Round 1: call process() of matching processorsRoundEnvironment lists annotated elements
  3. Did any processor generate new sources?
    YesParse them and run another roundNoRun the final round
  4. Final round: processingOver() is trueprocessors finish, no new elements
  5. Did any processor report an error?
    YesCompilation failsNoCompile all sources to class files

5 steps in order. Play them, or step through them one at a time.

3What a Processor Can Read: Elements and Type Mirrors

A processor does not see loaded classes, and it cannot call Class.forName on the code being compiled, because that code has not been compiled yet. Instead it reads the compiler's model through the javax.lang.model API. Elements represent declarations: a TypeElement for a class or interface, ExecutableElement for methods and constructors, VariableElement for fields and parameters. TypeMirror represents types, including generic types such as List<String>. Helpers from processingEnv, getElementUtils() and getTypeUtils(), answer questions such as whether one type is assignable to another.

Java
for (Element e : roundEnv.getElementsAnnotatedWith(GenerateBuilder.class)) {
TypeElement type = (TypeElement) e;
for (VariableElement field : ElementFilter.fieldsIn(type.getEnclosedElements())) {
String name = field.getSimpleName().toString(); // "price"
TypeMirror t = field.asType(); // java.math.BigDecimal
}
}

Notice that a processor can read declarations, their modifiers, annotations, types and documentation comments, but not method bodies. The standard model stops at signatures.

4What a Processor Can Produce, and What It Cannot Change

The Filer, from processingEnv.getFiler(), creates new files: source files with createSourceFile, class files with createClassFile, and resources such as service files or JSON metadata with createResource. The Messager reports diagnostics. A printMessage call with Diagnostic.Kind.ERROR and an element makes compilation fail with an error pointing at that element in the source, exactly like a compiler error.

What the standard API cannot do is change existing code. There is no method to add a field to a class being compiled, or to edit a method. That is a deliberate design choice: processors add to the program, they never rewrite it, so the code we wrote is the code that compiles. Tools that generate builders or mappers therefore create new classes next to ours, such as OrderBuilder or OrderMapperImpl.

Step throughWhere a processor sits inside javac
  1. Our sourcesOrder.java with @GenerateBuilder
  2. javac parser and element modeljavax.lang.model Elements and TypeMirrors
  3. Annotation processorsread the model; Filer writes new files; Messager reports
  4. Generated sourcesOrderBuilder.java, compiled in the same run
  5. Class filesoriginals and generated classes together

5 layers, from the top down. Step through them one at a time.

5How Lombok Is Different

Lombok adds getters, constructors and builders to the very classes we wrote, which the standard API forbids. It does it by registering as an annotation processor and then reaching past the public API into javac's internal classes, the com.sun.tools.javac abstract syntax tree, and modifying the tree in memory before it is compiled. Because those internals are not a public, stable API, they can change in any JDK release, and the module system's strong encapsulation of JDK internals has made such access harder over time. That is the root of the follow-up question about why Lombok tends to need an update for new JDKs.

6Follow-up questions

Why does Lombok break more often on JDK upgrades than MapStruct?

MapStruct uses only the standard JSR 269 API: it reads the element model and writes new source files, which the JDK keeps compatible across releases. Lombok modifies existing classes by manipulating javac's internal syntax tree through non-public APIs, which the JDK may change in any release and increasingly encapsulates, so each new JDK can require a new Lombok version.

Can a processor see the output of another processor?

Yes, in the next round. If processor A generates a source file containing an annotation that processor B supports, javac parses that file and offers its elements to B in the following round. Processors cannot rely on running in a specific order within a single round.

What does returning true from process() mean?

It claims the annotations for that round, so later processors are not asked to process them. Most processors return false, or only claim annotations they own, so that other tools sharing the same annotations still run.

7Conclusion

In this answer, we saw that javac discovers processors through the -processor option or the META-INF/services/javax.annotation.processing.Processor file on the processor path, and that recent JDKs no longer run processors found on the class path by default. Processing runs in rounds until no new sources appear, then a final round. Processors read the code through javax.lang.model elements and type mirrors, not loaded classes, generate new files through the Filer and report errors through the Messager, but cannot modify existing classes; Lombok does that through internal compiler APIs, which is why it is more fragile across JDK upgrades.

What to say in the interviewAbout 90 seconds aloud

Open with

An annotation processor is code that runs inside javac. javac finds it either through the -processor option or through ServiceLoader, a META-INF/services/javax.annotation.processing.Processor file in a jar on the processor path; build tools put it there with Maven's annotationProcessorPaths or Gradle's annotationProcessor configuration.

Then, if they want more

One recent change: newer JDKs, around 23, no longer run processors found on the plain class path by default, so you configure them explicitly, and I would check the exact behaviour for your version. Processing happens in rounds. In each round javac calls process on processors that support the annotations present, with a RoundEnvironment listing the annotated elements.

If a processor generates new source files, javac parses them and runs another round, until nothing new is produced, then there is a final round where processingOver is true, and then everything is compiled together. The processor does not see loaded classes, because the code is not compiled yet.

It reads the compiler's model through javax.lang.model: TypeElement, ExecutableElement, VariableElement and TypeMirror, which covers declarations and types but not method bodies. It can create new source, class or resource files through the Filer and report errors at a specific element through the Messager. What it cannot do through the standard API is modify existing classes.

Lombok does that by using javac's internal syntax tree APIs, which is why it needs updates for new JDKs while MapStruct, which only generates new files, does not.

About this answer

Last updated
12 November 2026
Versions it names
Java 21, Java 23
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "How does a JSR 269 annotation processor work? Describe the rounds, what a processor can...": https://scrivio-interview-library.netlify.app/java/annotations/9b7674481a/