Scrivio Interview library
LeadershipPrincipal7 min read · updated 12 November 2026

Looking back, why did Java choose annotation processing that can only generate new files rather than macros that rewrite code? What does that mean for your long-term choice between compile-time code generation and run-time reflection frameworks?

Java lets annotation processors read the program and generate new files, but never rewrite code that a person wrote. That keeps the meaning of written code stable, lets IDEs, debuggers and other tools understand a file without running anything, and protects compatibility across releases. For the long term, it means we should prefer standard, file-generating processors and build-time frameworks, which also give faster startup and work with native images, and keep run-time reflection for the places where its flexibility truly pays.

Why this question is asked

This question looks like history, but it is really about strategy. Every Java platform has to choose between frameworks that work out the wiring at build time and frameworks that discover it with reflection at run time, and that choice affects startup time, memory, native images and how painful each JDK upgrade is. Interviewers ask it of very senior engineers to see whether they understand why the language was designed this way and can turn that into a policy. In this answer, we will recap what annotation processors can and cannot do, look at why Java avoided macros, compare compile-time generation with run-time reflection, and finish with the strategy we would set.

1A Quick Recap of Annotation Processing

An annotation processor is a class that javac loads and runs during compilation. Java 6 standardised the API in JSR 269. The compiler works in rounds: in each round it hands the processor the elements carrying the annotations it asked for, as a read-only model from the javax.lang.model packages. The processor can inspect types, methods and annotations, report errors and warnings, and create new files through the Filer. If it creates new source files, javac runs another round so other processors can see them, and it stops when a round produces nothing new.

The important limit is what the Filer offers. It can create a new source file, class file or resource. It has no method to edit an existing one, and asking it to create a type that already exists fails. So a processor can add PointBuilder next to Point, but it can never add a method to Point.

Java
public class BuilderProcessor extends AbstractProcessor {
@Override public Set<String> getSupportedAnnotationTypes() { return Set.of("com.acme.Builder"); }
@Override public SourceVersion getSupportedSourceVersion() { return SourceVersion.latestSupported(); }
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment env) {
for (Element e : env.getElementsAnnotatedWith(Builder.class)) {
TypeElement type = (TypeElement) e;
String name = type.getQualifiedName() + "Builder"; // com.acme.PointBuilder
try (Writer w = processingEnv.getFiler().createSourceFile(name, type).openWriter()) {
w.write(render(type)); // a brand new file
} catch (IOException ex) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, ex.getMessage(), e);
}
}
return true;
}
}

Notice that the generated file is ordinary Java. It lands in the build's generated-sources folder, the IDE indexes it, we can open it, set breakpoints in it and read its stack traces. Nothing about Point.java changes meaning because a processor ran.

2Why Generating Only New Files Was the Safer Design

The main reason is that the code we read stays the code that runs. When we open a Java file, the Java Language Specification tells us exactly what it means, and no plugin on the class path can change that. Every tool in the ecosystem relies on this: IDEs, static analysers, refactoring tools, code review, coverage tools, and the debugger, which maps bytecode back to lines we can see.

Compatibility is the second reason. Java promises that code compiled today keeps compiling and running on future releases. A processor written against the public model API only depends on that stable API. A mechanism that rewrites the syntax tree depends on how the compiler represents code internally, which the JDK team must be free to change.

So the design trades power for predictability. Processors cannot do everything a macro could, but anything they do is visible as a file, and the language underneath them stays the same for everyone.

3What Macros Would Have Cost

A macro system lets code run at compile time and rewrite other code before it is compiled. Lisp, Rust and Scala have one form or another. The power is real, but so is the cost. An IDE that wants to show us the methods of a class must first expand every macro that might touch it, which means running user code inside the editor. Debuggers must map lines of expanded code back to the source we wrote. Every macro becomes part of the language as far as tools are concerned, and every tool vendor has to keep up.

Java has a live example of what this looks like: Lombok. It gives macro-like power by rewriting javac's internal tree, and as a result it needs its own IDE plugins, behaves differently in different tools, and can break on each new JDK. Scala's experience points the same way: its Scala 2 macros stayed experimental and were redesigned for Scala 3. The Java designers chose to keep that cost out of the platform.

4Compile-Time Generation Versus Run-Time Reflection

The same idea, work out as much as possible before the program runs, now shapes framework design. A run-time reflection framework, such as classic Spring, Hibernate or Jackson, scans classes, reads annotations and builds proxies when the application starts. That is very flexible: it can react to whatever is on the class path. But every startup repeats the same discovery, which costs time and memory, and a GraalVM native image cannot see reflective calls during its build unless we list them in reachability metadata (the exact file format depends on your GraalVM version).

Build-time frameworks move that work to compilation. Dagger and Micronaut generate their dependency wiring with annotation processors, Quarkus does it in a build step, and MapStruct generates mappers. Spring Boot 3 added its own AOT engine, a build step that generates bean definition code and reflection hints for native images. The result starts faster, uses less memory and fails at build time instead of in production.

Two ways to wire an application
PropertyCompile-time generationRun-time reflection
Startup workDone during the buildRepeated on every start
Errors foundAt compile timeAt startup or on first use
Native imagesWorks out of the boxNeeds reflection metadata or hints
DebuggingGenerated code is readable JavaProxies and reflective stack frames
FlexibilityOnly what is known at build timeAnything on the class path at run time
Build-time frameworks trade some flexibility for startup speed and earlier errors.

Neither column wins everywhere. Reflection remains the right tool when the shape of the data is only known at run time, for example a generic admin tool that renders any entity. The point is to choose it on purpose.

5The Long-Term Strategy We Would Set

Putting this together, a platform strategy follows naturally. We prefer standard processors that generate new files, because they use only the public API and survive JDK upgrades. We reduce our reliance on tools that use internal compiler APIs, starting with shared libraries, because those are the ones that block upgrades. We favour framework versions with AOT support for services where startup and memory matter, such as scale-to-zero functions or large fleets of small services.

We do not ban reflection. We keep it where the flexibility pays, make sure each use is known and covered by hints if we build native images, and review new reflective frameworks the way we review any dependency that touches JDK internals.

Step throughChoosing a code generation approach for a new library
  1. Is the shape known at build time?
    YesGenerate code at build timeNoReflection may be justified
  2. Does it need internal javac APIs?
    YesReject or isolate itNoStandard processor is fine
  3. Will services run as native images?
    YesRequire AOT hints or generationNoStartup cost is acceptable
  4. Record the decision and ownerso it is reviewed on each JDK upgrade

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

6Follow-up questions

Is Lombok an annotation processor?

It registers itself as one, so javac runs it like any other processor. But instead of using the Filer to create new files, it modifies the compiler's internal syntax tree for the class being compiled. That is exactly the step the standard API forbids, and it is why Lombok needs updates for new JDKs while MapStruct usually does not.

Does Spring Boot's AOT support remove reflection completely?

No. It generates code for bean definitions and records hints for the reflection, proxies and resources that are still needed, so a native image can include them. Many libraries underneath, such as Jackson or Hibernate, still use reflection; AOT makes that use known at build time rather than removing it.

Why is generated code easier to support than reflection?

Because it is plain Java we can read, step through and search. A reflective framework decides what to call at run time, so a stack trace shows Method.invoke frames and proxies rather than the code path, and a misconfiguration only appears when that path runs.

7Conclusion

In this answer, we saw that Java's annotation processors can read the program and create new files but never rewrite existing code, which keeps the meaning of written code stable and lets every tool reason about it. Macros would have given more power but would have made IDEs, debuggers and compatibility much harder, as Lombok shows on a small scale. The same thinking favours build-time generation, which gives faster startup and native image support, over run-time reflection, which stays useful where flexibility pays. Our strategy prefers standard file-generating processors and reduces reliance on internal compiler APIs.

What to say in the interviewAbout 80 seconds aloud

Open with

Java's annotation processors can inspect the program through a read-only model and generate new source, class or resource files, but they cannot modify code someone wrote. I think that was the right call.

Then, if they want more

It means the source file I read is exactly what the language specification says it is, so IDEs, static analysis, refactoring tools and debuggers can reason about code without running plugins, and the JDK can change compiler internals without breaking processors.

Macros that rewrite code would have made every tool expand them first and would have tied the ecosystem to compiler internals. Lombok is a small example of that cost: it rewrites javac's internal tree, so it needs IDE plugins and breaks on new JDKs. For long-term strategy, I apply the same idea to frameworks.

Build-time generation, like Dagger, Micronaut, MapStruct, Quarkus or Spring's AOT engine, gives faster startup, earlier errors and native image support. Run-time reflection is more flexible, but it costs startup time and needs reflection metadata for native images.

So I would standardise on file-generating processors, reduce dependence on internal compiler APIs starting with shared libraries, and use reflection deliberately where the flexibility is worth it.

About this answer

Last updated
12 November 2026
Versions it names
Java 6, Spring Boot 3
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 "Looking back, why did Java choose annotation processing that can only generate new files...": https://scrivio-interview-library.netlify.app/java/annotations/a98fbcce4e/