Java Eclipse-注释处理器,获取项目路径

Java Eclipse-注释处理器,获取项目路径,java,eclipse,eclipse-plugin,annotation-processing,Java,Eclipse,Eclipse Plugin,Annotation Processing,我正在为eclipse构建一个注释处理器插件, 我想做的是在处理过程中检查项目文件夹中的几个文件 我想知道如何从处理器中获取项目路径。 我相信这是可以做到的,因为项目源路径是提供给处理器的——但我找不到一种方法来实现它 我试图查看System.properties和processingev.getOptions(),但没有有用的信息 最后,我也希望在Netbeans上使用此注释处理器,因此如果有一个公共API可以提供此信息,它将是最好的—但任何帮助都将不胜感激。处理环境为您提供了一个可用于加载(

我正在为eclipse构建一个注释处理器插件, 我想做的是在处理过程中检查项目文件夹中的几个文件

我想知道如何从处理器中获取项目路径。 我相信这是可以做到的,因为项目源路径是提供给处理器的——但我找不到一种方法来实现它

我试图查看System.properties和processingev.getOptions(),但没有有用的信息


最后,我也希望在Netbeans上使用此注释处理器,因此如果有一个公共API可以提供此信息,它将是最好的—但任何帮助都将不胜感激。

处理环境为您提供了一个可用于加载(已知)资源的注释处理器。如果需要绝对路径来发现文件或目录,可以使用JavaFileManager和:


我通过生成源文件从
prosessingev
获取源路径:

String fetchSourcePath() {
    try {
        JavaFileObject generationForPath = processingEnv.getFiler().createSourceFile("PathFor" + getClass().getSimpleName());
        Writer writer = generationForPath.openWriter();
        String sourcePath = generationForPath.toUri().getPath();
        writer.close();
        generationForPath.delete();

        return sourcePath;
    } catch (IOException e) {
        processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, "Unable to determine source file path!");
    }

    return "";
}

在eclipse下,这对我不起作用-ToolProvider.getSystemJavaCompiler()从eclipse执行时返回null(我认为eclipse使用自己的编译器,不尊重此API)最终找到了解决方案-可以通过以下线程的说明来修复null编译器问题:-谢谢!对我来说,这仍然不起作用。
fm.getLocation(StandardLocation.SOURCE\u路径)
返回空值。。我在
AbstractProcessor
的扩展中从init方法调用它。有什么提示吗?使用maven处理器插件2.2.4对我来说也不起作用
fm.getLocation(StandardLocation.SOURCE\u PATH)
返回空值。只有返回NOTNULL的标准位置是无用的:CLASS_路径包含location/opt/buildmanagement/hudson/slaves/slave_1/maven3-agent.jar和/opt/buildmanagement/hudson/tools/apache-maven-3.0.5/boot/plexus-classworlds-2.4.jar;平台类路径包含一些JAR。其余的都是空的,这对我有效,而被接受的答案却不行。(但我不确定您是否需要写入/打开/关闭文件)
Filer filer = processingEnv.getFiler();
FileObject resource = filer.createResource(StandardLocation.CLASS_OUTPUT, "", "tmp", (Element[]) null);
Path projectPath = Paths.get(resource.toUri()).getParent().getParent();
resource.delete();
Path sourcePath = projectPath.resolve("src")
String fetchSourcePath() {
    try {
        JavaFileObject generationForPath = processingEnv.getFiler().createSourceFile("PathFor" + getClass().getSimpleName());
        Writer writer = generationForPath.openWriter();
        String sourcePath = generationForPath.toUri().getPath();
        writer.close();
        generationForPath.delete();

        return sourcePath;
    } catch (IOException e) {
        processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, "Unable to determine source file path!");
    }

    return "";
}