自定义 Java Annotation ,读取注解值

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  
posted @ 2017-08-23 16:34  疯子110  阅读(973)  评论(0编辑  收藏  举报