问题
I've written an Java annotation processor by extending javax.annotation.processing.AbstractProcessor
which is called in the Eclipse context and it works fine, except that I need more information about the source path and class path for my processor to work:
@SupportedAnnotationTypes({"MyAnno"})
@SupportedSourceVersion(SourceVersion.RELEASE_8)
public class Processor extends AbstractProcessor {
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
// this used to work in Java4 but not anymore (since Java6?):
String sourcePath = processingEnv.getOptions().get("sourcepath");
String classPath = processingEnv.getOptions().get("classpath");
for (Element e : roundEnv.getElementsAnnotatedWith(MyAnno.class)) {
myProcess(e, sourcePath, classPath);
}
return true;
}
}
The question is how to retrieve in the annotation processing context (the implementation of process
) a valid JavaFileManager
which can give me the source path and the classpath of the compiler which is currently executing the annotation processor. Preferably, I would find out about a solution which does not involve a dependency on Eclipse/JDT specific interfaces.
I've tried the following which does not work:
DiagnosticCollector<JavaFileObject> diagnostics =
new DiagnosticCollector<JavaFileObject>();
StandardJavaFileManager fm = ToolProvider.getSystemJavaCompiler().getStandardFileManager(diagnostics, null, null);
fm.getLocation(StandardLocation.CLASS_PATH); // prints an empty class path
来源:https://stackoverflow.com/questions/29658252/how-to-obtain-the-right-javafilemanager-in-a-java-annotation-processor