如何获得'输入的目录'注释处理器中的源文件?

发布于 2025-01-18 03:35:36 字数 1249 浏览 1 评论 0原文

我想获取注释处理后正在编译的源文件的目录在不依赖目录/构建工具约定的情况下进行注释处理。

public class MyProcessor extends AbstractProcessor {

  @Override
  public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    // Get the directories with source files to be compiled here
    // For example, I should be able to find all classes which I obtain via the following in the directories I want:
    roundEnv.getElementsAnnotatedWith(MyAnnotation.class)
  }
}

我尝试了:

  1. 我使用paths.get(“。”)获得的相对路径。但是,我不仅需要依靠目录/构建工具约定来获得源目录,而且当构建从其他任何地方启动时,它也无效。
  2. 检查System.getProperties()System.getEnv()中的所有属性,以查看是否有任何有用的设置。
  3. 检查javax.lang.model.element.typeelementjavax.lang.model.type.type.typemirror和相应的实用方法,以查看我是否可以获得实例的位置(DID DID我想念什么吗?)
  4. 检查了standardjavafilemanager的方法的各种组合,其中一些建议返回我想要的东西,但返回null/空收集或为模块相关参数提供异常。示例:standardjavafilemanager.list(standardLocation.source_path,“”,set.of(kind.source),true)

//编辑

我正在尝试创建一个功能,其中可以根据超级类型来拾取类。为此,我创建了一个注释,可以指定这些超级类型。现在,我需要扫描所有源文件以检查我使用库的X的子类型的类。所述图书馆需要指向一个包含扫描类的目录,这就是为什么我需要源文件夹。

I want to get the directories of the source files which are getting compiled after annotation processing while doing the annotation processing without relying on directory/build tool conventions.

public class MyProcessor extends AbstractProcessor {

  @Override
  public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    // Get the directories with source files to be compiled here
    // For example, I should be able to find all classes which I obtain via the following in the directories I want:
    roundEnv.getElementsAnnotatedWith(MyAnnotation.class)
  }
}

I have tried:

  1. A relative path which I got with Paths.get("."). But not only would I still have to rely on directory/build tool conventions to get to the source directories, it also doesn't work when the build is started from anywhere else but the project root.
  2. Checking all properties in System.getProperties() and System.getenv() to see if there is anything useful being set.
  3. Checked javax.lang.model.element.TypeElement, javax.lang.model.type.TypeMirror and the corresponding utility methods to see if I can get an instance's location (Did I miss something?)
  4. Checked various combinations of StandardJavaFileManager's methods, some of which suggest to return what I want, but returning either null/empty collections or throwing exceptions for module related parameters. Example: standardJavaFileManager.list(StandardLocation.SOURCE_PATH, "", Set.of(Kind.SOURCE), true).

// edit

I'm trying to create a functionality where classes can be picked up based on super-types. For that, I created an Annotation where I can specify these super-types. I now need to scan all source files to check for classes that are sub-types of X, for which I am using a library. Said library needs to be pointed to a directory containing the classes to scan, which is why I need the source folder.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文