自訂 Java Annotation ,讀取註解值

來源:互聯網
上載者:User

標籤:tcl   target   present   turn   lan   tar   bsp   ati   tty   

1. 首先是自訂註解: 

Java代碼  
  1. package cn.veji.hibernate.po;  
  2.   
  3. import java.lang.annotation.ElementType;  
  4. import java.lang.annotation.Retention;  
  5. import java.lang.annotation.RetentionPolicy;  
  6. import java.lang.annotation.Target;  
  7.   
  8. @Target(ElementType.METHOD)  
  9. @Retention(RetentionPolicy.RUNTIME)  
  10. public @interface Privilege {  
  11.     String[] value();  
  12.   
  13. }  





2.定義使用註解類: 

Java代碼  
  1. package cn.veji.hibernate.po;  
  2.   
  3. package cn.veji.hibernate.po;  
  4.   
  5. public class TestPrivilege {  
  6.   
  7.     @Privilege( { "a" })  
  8.     public void a() {  
  9.     }  
  10.   
  11.     @Privilege( { "b" })  
  12.     public void b() {  
  13.     }  
  14.   
  15.     @Privilege( { "c" })  
  16.     public void c() {  
  17.     }  
  18.   
  19. }  




3.讀取註解值協助類: 

Java代碼  
  1. package cn.veji.hibernate.po;  
  2.   
  3. package cn.veji.hibernate.test;  
  4.   
  5. import java.lang.annotation.Annotation;  
  6. import java.lang.reflect.Method;  
  7. import java.util.HashMap;  
  8. import java.util.Map;  
  9.   
  10. import cn.veji.hibernate.po.Privilege;  
  11. import cn.veji.hibernate.po.TestPrivilege;  
  12.   
  13. public class AnnotationUtil {  
  14.   
  15.     public static AnnotationUtil anno = null;  
  16.   
  17.     public static AnnotationUtil getInstance() {  
  18.         if (anno == null) {  
  19.             anno = new AnnotationUtil();  
  20.         }  
  21.         return anno;  
  22.     }  
  23.   
  24.     /** 
  25.      * 讀取註解值 
  26.      *  
  27.      * @param annotationClasss 處理Annotation類名稱 
  28.      * @param annotationField 處理Annotation類屬性名稱 
  29.      * @param className 處理Annotation的使用類名稱 
  30.      * @return 
  31.      * @throws Exception 
  32.      */  
  33.     @SuppressWarnings("all")  
  34.     public Map<String, String> loadVlaue(Class annotationClasss,  
  35.             String annotationField, String className) throws Exception {  
  36.   
  37.         System.out.println("處理Annotation類名稱  === "+annotationClasss.getName());  
  38.         System.out.println("處理Annotation類屬性名稱  === "+annotationField);  
  39.         System.out.println("處理Annotation的調用類名稱  === "+className);  
  40.         Map<String, String> map = new HashMap<String, String>();  
  41.         Method[] methods = Class.forName(className).getDeclaredMethods();  
  42.         for (Method method : methods) {  
  43.             if (method.isAnnotationPresent(annotationClasss)) {  
  44.                 Annotation p = method.getAnnotation(annotationClasss);  
  45.                 Method m = p.getClass()  
  46.                         .getDeclaredMethod(annotationField, null);  
  47.                 String[] values = (String[]) m.invoke(p, null);  
  48.                 for (String key : values) {  
  49.                     System.out.println("註解值 === " + key);  
  50.                     map.put(key, key);  
  51.                 }  
  52.             }  
  53.         }  
  54.         System.out.println("map數量  === " + map.size());  
  55.         return map;  
  56.     }  
  57.   
  58.     public static void main(String[] args) throws Exception {  
  59.   
  60.         AnnotationUtil.getInstance().loadVlaue(Privilege.class, "value",  
  61.                 TestPrivilege.class.getName());  
  62.     }  
  63.   
  64. }  




4.執行結果: 

Java代碼  
    1. 處理Annotation類名稱  === cn.veji.hibernate.po.Privilege  
    2. 處理Annotation類屬性名稱  === value  
    3. 處理Annotation的調用類名稱  === cn.veji.hibernate.po.TestPrivilege  
    4. 註解值 === c  
    5. 註解值 === a  
    6. 註解值 === b  
    7. map數量  === 3  

自訂 Java Annotation ,讀取註解值

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.