Eclipse - 注释处理器,获取项目路径 [英] Eclipse - Annotation processor, get project path

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

问题描述

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



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



我试过看System.properties和processingEnv.getOptions(),但没有有用的信息..



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

解决方案

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

  JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); 
StandardJavaFileManager fm = compiler.getStandardFileManager(null,null,null);

Iterable<?扩展文件> locations = fm.getLocation(StandardLocation.SOURCE_PATH);

如果您使用Eclipse,则需要将其配置为使用JDK作为运行时作为bennyl在评论中指出。






似乎没有API有义务返回源位置,因此上述解决方案将无法可靠地工作,只能在某些环境中运行。例如,Filer仅需要支持 CLASS_OUTPUT SOURCE_OUTPUT



最简单的解决方法可能是假设/需要一个特定的项目结构,其中编译的类在项目的直接子目录中(这是大多数IDE的默认值code> bin 或Mavens 目标目录)。如果你这样做,那么你可以创建一个临时资源文件,其中的文件管理器位于 SOURCE_OUTPUT 位置,并从该文件获取路径。

  Filer filer = processingEnv.getFiler(); 
FileObject resource = filer.createResource(StandardLocation.CLASS_OUTPUT,,tmp,(Element [])null);
Path projectPath = Paths.get(resource.toUri())。getParent()。getParent();
resource.delete();


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.

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

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..

解决方案

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

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


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.

The easiest workaround is probably to assume/require a specific project structure, where the compiled classes are in a direct subdirectory of the project (which is default for most IDEs bin or Mavens target directory). If you do that, then you can create a temporary resource file with the Filer at the SOURCE_OUTPUT location as and get the path from that file.

Filer filer = processingEnv.getFiler();
FileObject resource = filer.createResource(StandardLocation.CLASS_OUTPUT, "", "tmp", (Element[]) null);
Path projectPath = Paths.get(resource.toUri()).getParent().getParent();
resource.delete();

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆