Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java annotation processing and bytecode manipulation

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

like image 487
Will D Avatar asked Dec 08 '25 06:12

Will D


1 Answers

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.

like image 182
Gunnar Avatar answered Dec 10 '25 00:12

Gunnar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!