如何在Java注释处理器中获取正确的JavaFileManager?

我通过扩展在Eclipse上下文中调用的javax.annotation.processing.AbstractProcessor来编写Java注释处理器,它工作正常,除了我需要有关处理器工作的源路径和类路径的更多信息:

 @SupportedAnnotationTypes({"MyAnno"}) @SupportedSourceVersion(SourceVersion.RELEASE_8) public class Processor extends AbstractProcessor { @Override public boolean process(Set 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; } } 

问题是如何在注释处理上下文( process的实现)中检索有效的JavaFileManager ,它可以为我提供源路径和当前正在执行注释处理器的编译器的类路径。 最好,我会找到一个不涉及对Eclipse / JDT特定接口的依赖的解决方案。

我尝试过以下哪些不起作用:

 DiagnosticCollector diagnostics = new DiagnosticCollector(); StandardJavaFileManager fm = ToolProvider.getSystemJavaCompiler().getStandardFileManager(diagnostics, null, null); fm.getLocation(StandardLocation.CLASS_PATH); // prints an empty class path