3

From my understanding, an annotation processor is a 'plugin' into the Java compiler, meaning that annotation processing is done at compile time.

Since bytecode manipulation can also be done at compile time, would it be possible to, at compile time, manipulate the bytecode from the classes generated by the annotation processor?

Does annotation processing occur before the compiler actually compiles anything so that the generated classes are then compiled in the main compilation stage?

Or more broadly, would someone explain, or point me to the Java documentation about the execution order of the javac compiler and annotation processor ?

Thank you

3
  • 1
    The last time I tried, the compiler did compile the classes (parse and analyze), as necessary to identify the present annotations, but did not write class files to disk by the time the annotation processor(s) ran. So manipulating these class files was not possible. The generated source files are compiled in a new round after the annotation processing completed, which may include another annotation processing. Hence, there can be an arbitrary number of rounds, until no new source code has been generated. Commented Dec 14, 2018 at 11:46
  • 'An annotation processor for a certain annotation takes java code (or compiled byte code) as input ' hannesdorfmann.com/annotation-processing/… Commented Dec 14, 2018 at 12:13
  • Would this mean that you could process a . class file before compilation? Commented Dec 14, 2018 at 12:15

1 Answer 1

4

Annotation processing happens after the "Parse and Enter" and before the "Analyse and Generate" phases. See the compiler overview for a visual representation of the compilation pipeline. This means classfiles don't exist yet at the point when annotation processors are run.

Not an annotation processor may als not alter the AST of the compilation units it processors. It may produce new types though (either as source code or as bytecode, though I'm not aware of any actual example of the latter), and it also may create super-types of processed types.

Sign up to request clarification or add additional context in comments.

2 Comments

Lombok is an annotation processor that manipulates the generated bytecode. Doesn't this contradict with your statement?
@VolkanYazıcı from what I can tell, the bytecode manipulation which Lombok does is very much a hack - it appears to implement its bytecode transformations by wrapping the OutputStream used when writing the final class file out to disk, buffering the written bytes, transforming the raw data and then actually forwarding it to disk. So while it is doing bytecode manipulation, it hardly seems like an intended use of the annotation processor API.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.