<i id='Z4jhr'><tr id='Z4jhr'><dt id='Z4jhr'><q id='Z4jhr'><span id='Z4jhr'><b id='Z4jhr'><form id='Z4jhr'><ins id='Z4jhr'></ins><ul id='Z4jhr'></ul><sub id='Z4jhr'></sub></form><legend id='Z4jhr'></legend><bdo id='Z4jhr'><pre id='Z4jhr'><center id='Z4jhr'></center></pre></bdo></b><th id='Z4jhr'></th></span></q></dt></tr></i><div id='Z4jhr'><tfoot id='Z4jhr'></tfoot><dl id='Z4jhr'><fieldset id='Z4jhr'></fieldset></dl></div>
      <legend id='Z4jhr'><style id='Z4jhr'><dir id='Z4jhr'><q id='Z4jhr'></q></dir></style></legend>
      <tfoot id='Z4jhr'></tfoot>
    1. <small id='Z4jhr'></small><noframes id='Z4jhr'>

      • <bdo id='Z4jhr'></bdo><ul id='Z4jhr'></ul>

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

      时间:2023-08-23

      1. <legend id='skbgH'><style id='skbgH'><dir id='skbgH'><q id='skbgH'></q></dir></style></legend>
        <i id='skbgH'><tr id='skbgH'><dt id='skbgH'><q id='skbgH'><span id='skbgH'><b id='skbgH'><form id='skbgH'><ins id='skbgH'></ins><ul id='skbgH'></ul><sub id='skbgH'></sub></form><legend id='skbgH'></legend><bdo id='skbgH'><pre id='skbgH'><center id='skbgH'></center></pre></bdo></b><th id='skbgH'></th></span></q></dt></tr></i><div id='skbgH'><tfoot id='skbgH'></tfoot><dl id='skbgH'><fieldset id='skbgH'></fieldset></dl></div>
          <bdo id='skbgH'></bdo><ul id='skbgH'></ul>
            1. <small id='skbgH'></small><noframes id='skbgH'>

                  <tbody id='skbgH'></tbody>
                <tfoot id='skbgH'></tfoot>

              • 本文介绍了Eclipse - 注释处理器,获取项目路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

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

                I am building an annotation processor plugin for eclipse, what i would like to do is to examine several files inside the project folder during the processing.

                我想知道如何从我的处理器中获取项目路径.我相信可以做到这一点,因为项目源路径已提供给处理器 - 但我找不到到达它的方法.

                I would like to know how can I get the project path from within my processor. I believe this can be done because the project source path is provided to the processor - but I cannot find a way to reach it.

                我尝试查看 System.properties 和 processingEnv.getOptions() 但那里没有有用的信息..

                I tried looking at the System.properties and at the processingEnv.getOptions() but there is no useful information there..

                最终我也想在 Netbeans 上使用这个注释处理器,所以如果有一个公共 API 可以提供这些信息,那将是最好的 - 但任何帮助都将不胜感激..

                eventually I would like to use this annotation processor on Netbeans too so if there is a public API that can provide this information it will be the best - but any help will be appreciated..

                推荐答案

                处理环境为你提供了一个Filer 可用于加载(已知)资源.如果您需要绝对路径来发现文件或目录,可以使用 JavaFileManager 和 标准位置:

                The processing environment provides you with a Filer that can be used to load (known) resources. If you need absolute paths to discover files or directories, you can use a JavaFileManager and a StandardLocation:

                JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
                StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null);
                
                Iterable<? extends File> locations = fm.getLocation(StandardLocation.SOURCE_PATH);
                

                如果您使用的是 Eclipse,您需要将其配置为使用 JDK 作为运行时,正如 bennyl 在评论.

                If you are using Eclipse, you need to configure it to use the JDK as runtime as bennyl pointed out in the comments.

                似乎没有强制返回源位置的 API,因此上述解决方案无法可靠地工作,并且仅适用于某些环境.例如,Filer 只需要支持 CLASS_OUTPUTSOURCE_OUTPUT.

                It seems that there is no API that is obligated to return the source location, so the solution above won't work reliably and only with some environments. The Filer for example is only required to support CLASS_OUTPUT and SOURCE_OUTPUT.

                最简单的解决方法可能是假设/要求特定的项目结构,其中源目录和编译的类位于项目的特定子目录中(例如 srcbin 目录或 Maven 的 src/main/javatarget/classes).如果这样做,您可以通过在 SOURCE_OUTPUT 位置使用 Filer 创建临时资源来获取源路径,并从该文件的位置获取相对源路径.

                The easiest workaround is probably to assume/require a specific project structure, where the source directories and compiled classes are in a specific subdirectories of the project (e.g. the src and bin directories for most IDEs or src/main/java and target/classes for Maven). If you do that, you can get the source path by creating a temporary resource with the Filer at the SOURCE_OUTPUT location and get the source path relative from that file's location.

                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")
                

                这篇关于Eclipse - 注释处理器,获取项目路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  <bdo id='wuLid'></bdo><ul id='wuLid'></ul>

                    <tbody id='wuLid'></tbody>

                      <tfoot id='wuLid'></tfoot>
                      <i id='wuLid'><tr id='wuLid'><dt id='wuLid'><q id='wuLid'><span id='wuLid'><b id='wuLid'><form id='wuLid'><ins id='wuLid'></ins><ul id='wuLid'></ul><sub id='wuLid'></sub></form><legend id='wuLid'></legend><bdo id='wuLid'><pre id='wuLid'><center id='wuLid'></center></pre></bdo></b><th id='wuLid'></th></span></q></dt></tr></i><div id='wuLid'><tfoot id='wuLid'></tfoot><dl id='wuLid'><fieldset id='wuLid'></fieldset></dl></div>
                      1. <legend id='wuLid'><style id='wuLid'><dir id='wuLid'><q id='wuLid'></q></dir></style></legend>

                        <small id='wuLid'></small><noframes id='wuLid'>