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
-
1The 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.Holger– Holger2018年12月14日 11:46:51 +00:00Commented 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/…Will D– Will D2018年12月14日 12:13:40 +00:00Commented Dec 14, 2018 at 12:13
-
Would this mean that you could process a . class file before compilation?Will D– Will D2018年12月14日 12:15:06 +00:00Commented Dec 14, 2018 at 12:15
1 Answer 1
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.
2 Comments
Explore related questions
See similar questions with these tags.