apt的处理过程是什么样的

来源:4-2 APT技术原理&开发流程

慕妹8319097

2021-06-03

执行javac,先调用注解处理器生成java文件,然后编译java文件为.class文件

还是先 生成一部分字节码,通过注解处理器生成java文件,然后再把生成的java文件进行编译
,有没有图可以描述一下这个流程

写回答

1回答

allenfeng

2021-06-04

参考 JDK Compilation Overview

The process of compiling a set of source files into a corresponding set of class files is not a simple one, but can be generally divided into three stages. Different parts of source files may proceed through the process at different rates, on an "as needed" basis.

javac flow

This process is handled by the JavaCompiler class.

  1. All the source files specified on the command line are read, parsed into syntax trees, and then all externally visible definitions are entered into the compiler's symbol tables.

  2. All appropriate annotation processors are called. If any annotation processors generate any new source or class files, the compilation is restarted, until no new files are created.

  3. Finally, the syntax trees created by the parser are analyzed and translated into class files. During the course of the analysis, references to additional classes may be found. The compiler will check the source and class path for these classes; if they are found on the source path, those files will be compiled as well, although they will not be subject to annotation processing.


编译过程分为三个步骤:

1. 解析源文件生成抽象语法树

2. 调用已注册的注解处理器(注解处理器中可以自行生成新的Java文件)

3. 所有源文件生成字节码


如果第2步中,注解处理器生成了新的源代码,那么JAVA编译器将重复第1-2步,直到不再生成新的源代码。


0
0

Android 应用程序构建实战+原理精讲

深度掌握 Gradle 使用,系统性学习 Android 应用构建知识

450 学习 · 116 问题

查看课程