DEBUG.P("processorIterator="+processorIterator);
} else if (processors != null) {
processorIterator = processors.iterator();
} else {
String processorNames = options.get("-processor");
JavaFileManager fileManager = context.get(JavaFileManager.class);
try {
// If processorpath is not explicitly set, use the classpath.
//在javax.tools.StandardLocation定义了ANNOTATION_PROCESSOR_PATH
//没有指定“-processorpath <路径>”选项时,从
ClassLoader processorCL = fileManager.hasLocation(ANNOTATION_PROCESSOR_PATH)
? fileManager.getClassLoader(ANNOTATION_PROCESSOR_PATH)
: fileManager.getClassLoader(CLASS_PATH);
/*
* If the "-processor" option is used, search the appropriate
* path for the named class. Otherwise, use a service
* provider mechanism to create the processor iterator.
*/
DEBUG.P("processorNames="+processorNames);
if (processorNames != null) {
processorIterator = new NameProcessIterator(processorNames, processorCL, log);
} else {
//为什么要@SuppressWarnings("unchecked")呢?
//因为Service.providers返回的是一个没有范型化的LazyIterator类实例
//而it是一个范型化的Iterator<Processor>
@SuppressWarnings("unchecked")
Iterator<Processor> it =
Service.providers(Processor.class, processorCL);
//Service不包含在javac的源码中,在rt.jar文件中
processorIterator = it;
}
} catch (SecurityException e) {
/*
* A security exception will occur if we can't create a classloader.
* Ignore the exception if, with hindsight, we didn't need it anyway
* (i.e. no processor was specified either explicitly, or implicitly,
* in service configuration file.) Otherwise, we cannot continue.
*/
if (fileManager instanceof JavacFileManager) {
StandardJavaFileManager standardFileManager = (JavacFileManager) fileManager;
Iterable<? extends File> workingPath = fileManager.hasLocation(ANNOTATION_PROCESSOR_PATH)
? standardFileManager.getLocation(ANNOTATION_PROCESSOR_PATH)
: standardFileManager.getLocation(CLASS_PATH);
if (needClassLoader(processorNames, workingPath) ) {
log.error("proc.cant.create.loader", e.getLocalizedMessage());