1. 程式人生 > >android 自定義註解 通過反射獲取註解屬性值

android 自定義註解 通過反射獲取註解屬性值

參考文章:http://xuwenjin666.iteye.com/blog/1637247

1.自定義註解

package cn.veji.hibernate.po;  

import java.lang.annotation.ElementType;  
import java.lang.annotation.Retention;  
import java.lang.annotation.RetentionPolicy;  
import java.lang.annotation.Target;  

@Target(ElementType.METHOD)  
@Retention(RetentionPolicy.RUNTIME
) public @interface Privilege { String[] value(); }
public enum ElementType {  
    TYPE,// 類、介面、註解型別或列舉  
     FIELD, //屬性  
     METHOD, //方法  
     PARAMETER,// 用於描述引數  
     CONSTRUCTOR,//構造方法  
      LOCAL_VARIABLE,//區域性變數  
     ANNOTATION_TYPE,//註解類  
     PACKAGE //包  
} 

從上面程式碼可以看出Target 對應的作用域(Target可以接受多個引數,逗號分隔即可)

例如:
@Target({ElementType.TYPE,ElementType.METHOD,ElementType.FIELD,ElementType.CONSTRUCTOR})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyTestIn {
    String author() default "kaelthas.wang";
    String address() default "山東青島";

}

2.獲取類註解屬性值

 /** 
     * 讀取註解值 
     *  
     * @param
annotationClasss 處理Annotation類名稱 * @param annotationField 處理Annotation類屬性名稱 * @param className 處理Annotation的使用類名稱 * @return * @throws Exception */
@SuppressWarnings("all") public Map<String, String> loadVlaue(Class annotationClasss, String annotationField, String className) throws Exception { System.out.println("處理Annotation類名稱 === "+annotationClasss.getName()); System.out.println("處理Annotation類屬性名稱 === "+annotationField); System.out.println("處理Annotation的呼叫類名稱 === "+className); Map<String, String> map = new HashMap<String, String>(); Method[] methods = Class.forName(className).getDeclaredMethods(); for (Method method : methods) { if (method.isAnnotationPresent(annotationClasss)) { Annotation p = method.getAnnotation(annotationClasss); Method m = p.getClass() .getDeclaredMethod(annotationField, null); //這裡根據屬性引數型別進行強制型別轉換 String[] values = (String[]) m.invoke(p, null); for (String key : values) { System.out.println("註解值 === " + key); map.put(key, key); } } } System.out.println("map數量 === " + map.size()); return map; }

3.獲取方法註解屬性值

這裡的屬性值為int 強制型別轉換時候使用Integer

@SuppressWarnings("all")
    public int loadVlaue(Class annotationClasss,
                         String annotationField, String className) {

        System.out.println("處理Annotation類名稱  === " + annotationClasss.getName());
        System.out.println("處理Annotation類屬性名稱  === " + annotationField);
        System.out.println("處理Annotation的呼叫類名稱  === " + className);
        Map<String, String> map = new HashMap<String, String>();

        try {
            Method[] methods = Class.forName(className).getDeclaredMethods();

            Class test = Class.forName(className);
            if (test.isAnnotationPresent(annotationClasss)) {
                Annotation p = test.getAnnotation(annotationClasss);
                Method m = p.getClass()
                        .getDeclaredMethod(annotationField, null);
                return (Integer) m.invoke(p, null);

            }
        } catch (Exception e) {
            return -1;
        }

        return -1;

    }