AbstractProcessor,如何声明注释,哪个目标是另一个注释? [英] AbstractProcessor, how to claim for annotations which target is another annotation?

查看:216
本文介绍了AbstractProcessor,如何声明注释,哪个目标是另一个注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下注释:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Interceptor {
  Class<? extends Behaviour> value();

}

我的图书馆的用户可以扩展其API,以创建使用@Interceptor注释的自定义注释,如下所示:

The users of my library can extend its API creating custom annotations annotated with @Interceptor, as follows:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Interceptor(BypassInterceptor.class)
public @interface Bypass {
}

AbstractProcessor 提供了一种方法称为 getSupportedAnnotationTypes 其中返回处理器支持的注释类型的名称.但是,如果我指定@Interceptor的名称,如下所示:

AbstractProcessor provides a method called getSupportedAnnotationTypes which returns the names of the annotation types supported by the processor. But if I specify the name of @Interceptor, as follows:

 @Override public Set<String> getSupportedAnnotationTypes() {
    Set<String> annotations = new LinkedHashSet();
    annotations.add(Interceptor.class.getCanonicalName());
    return annotations;
  }

processor#process 方法.

The processor#process method will not be notified when a class is annotated with @Bypass annotation.

因此,当使用AbstractProcessor时,如何声明目标是另一个注释的注释?

So, when using an AbstractProcessor, how to claim for annotations which target is another annotation?

推荐答案

如果注释处理器正在扫描使用注释进行元注释的所有注释,则需要为支持的注释类型指定"*",然后检查每个注释的声明(使用ProcessingEnvironment.getElements()来确定它是否具有感兴趣的元注释.

If your annotation processor is scanning for all annotations that are meta-annotated with your annotation, you'll need to specify "*" for your supported annotation types, and then inspect each annotation's declaration (using ProcessingEnvironment.getElements() to determine whether it has the meta-annotation of interest.

这篇关于AbstractProcessor,如何声明注释,哪个目标是另一个注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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