注释 - 在构建时读取元素值

发布于 2024-11-15 10:48:06 字数 958 浏览 1 评论 0原文

是否可以在构建时读取注释元素的值?例如,如果我定义了以下注释:

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 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羅雙樹 2024-11-22 10:48:06

这不是最好的答案,因为我自己没有这样做过,但鉴于已经过去了 3 个小时,我会尽力而为。

注释处理概述

除非注释处理是
使用 -proc:none 选项禁用,
编译器搜索任何
注释处理器是
可用的。搜索路径可以是
使用 -processorpath 指定
选项;如果没有给出,用户
使用类路径。处理器是
通过服务定位
名为
的提供者配置文件
META-INF/services/javax.annotation.processing.Processor
在搜索路径上。此类文件应该
包含任何注释的名称
要使用的处理器,每个列出一个
线。或者,处理器可以是
明确指定,使用
-处理器选项。

因此,您似乎需要在 META-INF/services 文件夹中创建一个名为 javax.annotation.processing.Processor 的文件,其中列出了注释处理器的名称,其中一个每行。

编辑:所以我相信读取注释的代码会类似于...

    for (Element element : roundEnv.getRootElements()) {
        State state = element.getAnnotation(State.class);
        if(state != null) {
            String stage = state.stage();
            System.out.println("The element " + element + " has stage " + stage);
        }
    }

可以找到注释处理器的真实示例 此处

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.

Overview of annotation processing

Unless annotation processing is
disabled with the -proc:none option,
the compiler searches for any
annotation processors that are
available. The search path can be
specified with the -processorpath
option; if it is not given, the user
class path is used. Processors are
located by means of service
provider-configuration files named
META-INF/services/javax.annotation.processing.Processor
on the search path. Such files should
contain the names of any annotation
processors to be used, listed one per
line. Alternatively, processors can be
specified explicitly, using the
-processor option.

So it appears that you need to create a file named javax.annotation.processing.Processor in your META-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...

    for (Element element : roundEnv.getRootElements()) {
        State state = element.getAnnotation(State.class);
        if(state != null) {
            String stage = state.stage();
            System.out.println("The element " + element + " has stage " + stage);
        }
    }

A real-world example of an annotation processor can be found here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文