注释 - 在构建时读取元素值
是否可以在构建时读取注释元素的值?例如,如果我定义了以下注释:
public @interface State {
String stage();
}
并且我在类中注释了一个方法,如下所示:
public class Foo {
@State(stage = "build")
public String doSomething() {
return "doing something";
}
}
How can I read the value of the @State comment element ' stage' 在构建时,在注释处理器中?我有一个处理器,构建如下:
@SupportedAnnotationTypes(value = {"State"})
@SupportedSourceVersion(SourceVersion.RELEASE_6)
public class StageProcessor extends AbstractProcessor {
@Override
public boolean process(Set<? extends TypeElement> elementTypes,
RoundEnvironment roundEnv) {
for (Element element : roundEnv.getRootElements()) {
// ... logic to read the value of element 'stage' from
// annotation 'State' in here.
}
return true;
}
}
Is it possible to read the value of an annotation element at build time? For example, if I have the following annotation defined:
public @interface State {
String stage();
}
and I annotate a method in a class, like so:
public class Foo {
@State(stage = "build")
public String doSomething() {
return "doing something";
}
}
How can I read the value of the @State annotation element 'stage' at build time, in an annotation processor? I have a processor built as follows:
@SupportedAnnotationTypes(value = {"State"})
@SupportedSourceVersion(SourceVersion.RELEASE_6)
public class StageProcessor extends AbstractProcessor {
@Override
public boolean process(Set<? extends TypeElement> elementTypes,
RoundEnvironment roundEnv) {
for (Element element : roundEnv.getRootElements()) {
// ... logic to read the value of element 'stage' from
// annotation 'State' in here.
}
return true;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是最好的答案,因为我自己没有这样做过,但鉴于已经过去了 3 个小时,我会尽力而为。
因此,您似乎需要在
META-INF/services
文件夹中创建一个名为javax.annotation.processing.Processor
的文件,其中列出了注释处理器的名称,其中一个每行。编辑:所以我相信读取注释的代码会类似于...
可以找到注释处理器的真实示例 此处。
Not the best answer as I haven't done this myself, but seeing as it's been 3 hours I'll do what I can.
So it appears that you need to create a file named
javax.annotation.processing.Processor
in yourMETA-INF/services
folder that lists the names of your annotation processors, one per line.EDIT: So then I believe the code to read the annotations would be something like...
A real-world example of an annotation processor can be found here.