如何获得'输入的目录'注释处理器中的源文件?
我想获取注释处理后正在编译的源文件的目录而在不依赖目录/构建工具约定的情况下进行注释处理。
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)
}
}
我尝试了:
- 我使用
paths.get(“。”)
获得的相对路径。但是,我不仅需要依靠目录/构建工具约定来获得源目录,而且当构建从其他任何地方启动时,它也无效。 - 检查
System.getProperties()
和System.getEnv()
中的所有属性,以查看是否有任何有用的设置。 - 检查
javax.lang.model.element.typeelement
,javax.lang.model.type.type.typemirror
和相应的实用方法,以查看我是否可以获得实例的位置(DID DID我想念什么吗?) - 检查了
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:
- 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. - Checking all properties in
System.getProperties()
andSystem.getenv()
to see if there is anything useful being set. - 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?) - 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论