java Get类方法,带有org注释。重新选举
我用过org。反思(http://code.google.com/p/reflections/)在我的项目中,用于加载带有特定注释的类。现在我有了类,我需要得到我自己制作的所有带有注释的方法。但当我创建Reflections对象时,它只要求包名,所以如果我使用GetMethodsNotatedWith方法,它将从给定的包类中获取所有方法,但我想从我的类中获取方法。我该怎么做
你可以在下面搜索框中键入要查询的问题!
我用过org。反思(http://code.google.com/p/reflections/)在我的项目中,用于加载带有特定注释的类。现在我有了类,我需要得到我自己制作的所有带有注释的方法。但当我创建Reflections对象时,它只要求包名,所以如果我使用GetMethodsNotatedWith方法,它将从给定的包类中获取所有方法,但我想从我的类中获取方法。我该怎么做
# 1 楼答案
如果你有一个
Class
对象,那就相当简单了。见this reference重要的代码部分是:
这样,您就可以使用
Method
对象数组# 2 楼答案
阅读Reflections文档,为了查询方法注释,您应该像这样实例化Reflections:
new Reflections("my.package", new MethodAnnotationsScanner())
另一个选项是使用Reflections查询api
Set<Method> set = getAllMethods(reflections.getTypesAnnotatedWith(...), withAnnotation(methodAnnotation))
import static org.Reflections.*;
# 3 楼答案
以下是你能做的: