
属于重点,在系统中用到注解权限时非常有用,可以精确控制权限的粒度
注意:要想使用反射去读取注解,必须将Retention的值选为Runtime Java代码import javalangannotationAnnotation;import javalangreflectMethod;//读取注解信息public class ReadAnnotationInfoTest { public static void main(String[] args) throws Exception { // 测试AnnotationTest类,得到此类的类对象 Class c = ClassforName(comiwtxokhtdannotationAnnotationTest); // 获取该类所有声明的方法 Method[] methods = cgetDeclaredMethods(); // 声明注解集合 Annotation[] annotations; // 遍历所有的方法得到各方法上面的注解信息 for (Method method : methods) { // 获取每个方法上面所声明的所有注解信息 annotations = methodgetDeclaredAnnotations(); // 再遍历所有的注解,打印其基本信息 Systemoutprintln(methodgetName()); for (Annotation an : annotations) { Systemoutprintln(方法名为: + methodgetName() + 其上面的注解为: + anannotationType()getSimpleName()); Method[] meths = anannotationType()getDeclaredMethods(); // 遍历每个注解的所有变量 for (Method meth : meths) { Systemoutprintln(注解的变量名为: + methgetName()); } } } }}
以上就是关于Java 注解的读取注解信息的方法全部的内容,包括:Java 注解的读取注解信息的方法、、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)