使用 org.refelections 获取带注释的类方法
我在我的项目中使用了 org.reflections (http://code.google.com/p/reflections/) 来加载带有某些注释的类。现在我有课程,我需要获取我自己制作的带有注释的所有方法。但是当我创建 Reflections 对象时,它只要求包名称,因此如果我使用 getMethodsAnnotatedWith 方法,它将从给定的包类中获取所有方法,但我想从我的类中获取方法。我怎样才能做到这一点?
I have used org.reflections (http://code.google.com/p/reflections/) in my project for loading classes with certain annotations. Now I have class and I need to get all methods with annotation that I have made myself. But when I create Reflections object, it asks just for package name, so if I would use getMethodsAnnotatedWith method, it would get all methods from given package classes, but I want to get methods from my class. How can I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以执行以下操作:
Here is what you can do:
如果您有一个 Class 对象,那就相当简单了。请参阅此参考。
重要的代码部分是:
这样您将获得要使用的
Method
对象的数组。That's rather simple if you have a
Class
object. See this reference.Important code part is:
That way you will get an array of
Method
objects to work with.阅读 Reflections 文档,为了查询方法注释,您应该像这样实例化 Reflections:
new Reflections("my.package", new MethodAnnotationsScanner())
另一种选择是使用 Reflections 查询 api,例如即,
Setset = getAllMethods(reflections.getTypesAnnotatedWith(...), withAnnotation(methodAnnotation))
导入 static org.Reflections.*;
Reading the Reflections documentation, in order query for methods annotations you should instantiate Reflections like this:
new Reflections("my.package", new MethodAnnotationsScanner())
Another option is to use the Reflections query api, such that,
Set<Method> set = getAllMethods(reflections.getTypesAnnotatedWith(...), withAnnotation(methodAnnotation))
import static org.Reflections.*;