查找带注释的注释 [英] Finding annotated annotations

查看:169
本文介绍了查找带注释的注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个注释 @MarkerAnnotation 。它可以直接添加到方法中。或任何其他注释,例如

  @MarkerAnnotation 
@Interface CustomAnnotation {...}

@CustomAnnotation 也可以直接添加到方法中。这是许多框架允许用户添加自己的注释(例如,春天)的标准方式。



现在,给定一个类,我想找到所有直接或间接标记的方法 @MarkerAnnotation 。对于每种方法,我还想找到关联的 @MarkerAnnotation 和/或 @CustomAnnotation 。有没有我可以使用的工具或我必须手动完成?

解决方案

Spring有一个他们的核心项目中的AnnotationUtils 类可以满足您的需求。 janadoc


在提供的方法上查找annotationType的单个注释,
遍历其超级方法(即,来自超类和接口)
如果注释不直接出现在给定方法本身上。
正确处理编译器生成的桥接方法。



如果注释不是直接在方法上出现
,将搜索元注释。 / p>

默认情况下,方法的注释不会继承,因此我们需要
显式处理。



There is an annotation @MarkerAnnotation. It can be added directly to method. or to any other annotation, like

@MarkerAnnotation
@Interface CustomAnnotation {...}

and this @CustomAnnotation can also be added directly to methods. That's the standard way many frameworks let users add their own annotation (e.g. spring).

Now, given a class, I want to find all methods marked directly or indirectly with @MarkerAnnotation. For every method I also want to find the associated @MarkerAnnotation and/or @CustomAnnotation. Is there any tool that I can use or I have to do it manually?

解决方案

Spring has an AnnotationUtils class in their core project that does what you want. The javadoc for findAnnotation:

Find a single Annotation of annotationType on the supplied Method, traversing its super methods (i.e., from superclasses and interfaces) if the annotation is not directly present on the given method itself. Correctly handles bridge Methods generated by the compiler.

Meta-annotations will be searched if the annotation is not directly present on the method.

Annotations on methods are not inherited by default, so we need to handle this explicitly.

这篇关于查找带注释的注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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