1. 程式人生 > >android反射元件 (一)java 自定義annotation基礎知識

android反射元件 (一)java 自定義annotation基礎知識

自定義annotation包括三部分: 自定義annotation、使用annotation的類、annotation的處理方法。

一、自定義annotation

       元註解包括以下:

           1)@Target    描述註解使用的範圍

 取值包括:

1.CONSTRUCTOR:用於描述構造器
2.FIELD:用於描述域
3.LOCAL_VARIABLE:用於描述區域性變數
4.METHOD:用於描述方法
5.PACKAGE:用於描述包
6.PARAMETER:用於描述引數
7.TYPE:用於描述類、介面(包括註解型別) 或enum宣告    

          2) @Retention  定義了註解被保留的時間段

取值包括:

1.SOURCE:在原始檔中有效(即原始檔保留)
2.CLASS:在class檔案中有效(即class保留)
3.RUNTIME:在執行時有效(即執行時保留)

           3)@Documented   是否可以被工具文件化

           4)@Inherited  註解是否可以被子類繼承

   例項程式碼:

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FruitName {
    
    public String value() default "";
    
}

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FruitColor {

    public enum Color {BULE, RED, GREEN}
    
    public Color fruitColor() default Color.GREEN;
    
}
二、使用annotation的類

   例項程式碼:

public class Apple {
    
    @FruitName("Apple")
    private String appleName;
    
    @FruitColor(fruitColor=FruitColor.Color.RED)
    private String appleColor;
    
    public void setAppleColor(String appleColor) {
        this.appleColor = appleColor;
    }
    public String getAppleColor() {
        return appleColor;
    }
    
    
    public void setAppleName(String appleName) {
        this.appleName = appleName;
    }
    public String getAppleName() {
        return appleName;
    }
    
    public void displayName(){
        System.out.println("水果的名字是: "+ appleName + " 水果的顏色是:"+ appleColor);
    }
    
}

三、annotation處理方法

   例項程式碼:

public class FruitInfoUtil {

    public static void getFruitInfo(Object obj){
        Class<?> clazz = obj.getClass();
        Field[] fields = clazz.getDeclaredFields();
        
        for(Field field : fields){
            if(field.isAnnotationPresent(FruitName.class)){
                FruitName fruitName = (FruitName)field.getAnnotation(FruitName.class);
                try {
                    field.setAccessible(true);
                    field.set(obj, fruitName.value());
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }else if(field.isAnnotationPresent(FruitColor.class)){
                FruitColor fruitColor = (FruitColor)field.getAnnotation(FruitColor.class);
                try {
                    field.setAccessible(true);
                    field.set(obj, fruitColor.fruitColor().toString());
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
}

 LAST: main方法
public class Main {
    
    public static void main(String[] args) {
        Apple apple = new Apple();
        
        FruitInfoUtil.getFruitInfo(apple);
        
        apple.displayName();
    
    }
}
執行結果:

水果的名字是: Apple 水果的顏色是:RED

上傳下別人總結的annotation的一張圖,非常直觀