参考博文:
(3)Annotate类
(4)JavacProcessingEnvironment类解读
(5)Java的注解相关的命令
调用步骤:
(1)调用JavacProcessingEnvironment的doProcessing()方法
其中的主要逻辑代码如下:
Round round = new Round(context,this, roots, classSymbols);
boolean errorStatus;
boolean moreToDo;
do {
// Run processors for round n
round.run(false, false);
// Processors for round n have run to completion.
// Check for errors and whether there is more work to do.
errorStatus = round.unrecoverableError();
moreToDo = moreToDo();
round.showDiagnostics(errorStatus || showResolveErrors);
// Set up next round.
// Copy mutable collections returned from filer.
Set<JavaFileObject> gsfs = filer.getGeneratedSourceFileObjects();
Map<String, JavaFileObject> gcs = filer.getGeneratedClasses();
round = round.next(this,
new LinkedHashSet<JavaFileObject>(gsfs),
new LinkedHashMap<String,JavaFileObject>(gcs));
// Check for errors during setup.
if (round.unrecoverableError()) {
errorStatus = true;
}
} while (moreToDo && !errorStatus);
// run last round
round.run(true, errorStatus);
(2)
lastRound=false时调用JavacProcessingEnvironment的discoverAndRunProcs()方法,传递Round对象在创建时准备好的一些参数,如:
/** The set of annotations to be processed this round. */
Set<TypeElement> annotationsPresent;
/** The set of top level classes to be processed this round. */
List<ClassSymbol> topLevelClasses;
在discoverAndRunProcs()方法中获取ProcessorStateIterator迭代器变量psi,创建JavacRoundEnvironment对象。
lastRound=true时调用DiscoveredProcessors.ProcessorStateIterator对象的runContributingProcs()方法