How to obtain the right JavaFileManager in a Java annotation processor?

穿精又带淫゛_ 提交于 2019-12-19 06:06:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!