當前位置:編程學習大全網 - 網站源碼 - Java 註解的讀取註解信息的方法

Java 註解的讀取註解信息的方法

屬於重點,在系統中用到註解權限時非常有用,可以精確控制權限的粒度

註意:要想使用反射去讀取註解,必須將Retention的值選為Runtime Java代碼import?java.lang.annotation.Annotation;import?java.lang.reflect.Method;//讀取註解信息public?class?ReadAnnotationInfoTest?{public?static?void?main(String[]?args)?throws?Exception?{//?測試AnnotationTest類,得到此類的類對象Class?c?=?Class.forName(com.iwtxokhtd.annotation.AnnotationTest);//?獲取該類所有聲明的方法Method[]?methods?=?c.getDeclaredMethods();//?聲明註解集合Annotation[]?annotations;//?遍歷所有的方法得到各方法上面的註解信息for?(Method?method?:?methods)?{//?獲取每個方法上面所聲明的所有註解信息annotations?=?method.getDeclaredAnnotations();//?再遍歷所有的註解,打印其基本信息System.out.println(method.getName());for?(Annotation?an?:?annotations)?{System.out.println(方法名為:?+?method.getName()?+?其上面的註解為:?+?an.annotationType().getSimpleName());Method[]?meths?=?an.annotationType().getDeclaredMethods();//?遍歷每個註解的所有變量for?(Method?meth?:?meths)?{System.out.println(註解的變量名為:?+?meth.getName());}}}}}

  • 上一篇:求助,求大俠幫我把下面的指標在倚天圖中,多頭顯示紅K線,空頭顯示綠K線。下面是源碼。
  • 下一篇:交付平臺源代碼
  • copyright 2024編程學習大全網