java反射——反射Annotation

lengtianxue發表於2016-09-14

轉自http://www.cnblogs.com/maxblog/archive/2010/09/01/1814821.html

package reflect;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class AnnotationTest {

    @Field_Method_Parameter_Annotation(describ="欄位編號",type=int.class)  //註釋欄位
    int id;
    @Field_Method_Parameter_Annotation(describ="欄位姓名",type=String.class)//註釋欄位
    String name;
    
    @Constructor_Annotation()//採用預設構造方法
    public AnnotationTest()
    {
        
    }
    @Constructor_Annotation("立即初始化構造方法.")    //註釋構造方法
    public AnnotationTest(
    //註釋構造方法引數
    @Field_Method_Parameter_Annotation(describ="編號",type=int.class)
    int id,
    @Field_Method_Parameter_Annotation(describ="姓名",type=String.class)
    String name
    )
    {
        this.id = id;
        this.name = name;
    }
    
    @Field_Method_Parameter_Annotation(describ="獲得編號",type=int.class)
    public int getId()
    {
        return id;
    }
    @Field_Method_Parameter_Annotation(describ="設定編號")   //成員type,採用預設註釋方法
    public void setId(
    //註釋引數
    @Field_Method_Parameter_Annotation(describ="設定編號",type=int.class)
    int id
    )
    {
        this.id =id;
    }
    @Field_Method_Parameter_Annotation(describ="獲得姓名",type=String.class)
    public String getName()
    {
        return name;
    }
    @Field_Method_Parameter_Annotation(describ="設定姓名")
    public void setName(
    @Field_Method_Parameter_Annotation(describ="姓名",type=String.class)
    String name
    )
    {
        this.name = name;
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //構造方法:
        Constructor[] declaredConstructor = AnnotationTest.class.getDeclaredConstructors();  //獲得所有的構造方法
        for(int i=0;i<declaredConstructor.length;i++)
        {
            Constructor  constructor = declaredConstructor[i];   //遍歷構造方法
            if(constructor.isAnnotationPresent(Constructor_Annotation.class))   //檢視是否指定型別的註釋
            {
            Constructor_Annotation ca = (Constructor_Annotation)constructor.getAnnotation(Constructor_Annotation.class);
            System.out.println("ca.value()=: "+ca.value());
            }
        
            Annotation[][]parameterAnnotations = constructor.getParameterAnnotations();//獲得引數註釋 
            for(int j=0;j<parameterAnnotations.length;j++)
            {
                int length = parameterAnnotations[j].length;
                if(length == 0)   //如果為0,則表示沒有為該引數新增註釋
                {
                    System.out.println("沒有為該引數新增註釋");
                }
                else
                {
                    for(int k=0;k<length;k++)
                    {
                        //獲得引數註釋
                        Field_Method_Parameter_Annotation pa = (Field_Method_Parameter_Annotation)parameterAnnotations[j][k];
                        System.out.print(" "+pa.describ());   //引數描述
                        System.out.println(" "+pa.type());      //引數型別
                    }
                }
            }
            System.out.println("****************");
        }
        
        
        //欄位:
        System.out.println("********欄位的Annotation*************");
        Field[] declaredFields = AnnotationTest.class.getDeclaredFields();   //獲得所有的欄位
        for(int i=0;i<declaredFields.length;i++)
        {
            Field field = declaredFields[i];
            //檢視是否具有指定型別的註釋:
            if(field.isAnnotationPresent(Field_Method_Parameter_Annotation.class))
            {
                Field_Method_Parameter_Annotation fa = (Field_Method_Parameter_Annotation)field.getAnnotation(Field_Method_Parameter_Annotation.class);
                System.out.print(" "+fa.describ());   //獲得欄位描述
                System.out.println(" "+fa.type());    //獲得欄位型別
            }
        }
        
        //方法
        System.out.println("********方法的Annotation*************");
        Method [] methods = AnnotationTest.class.getDeclaredMethods();    //獲得所有的方法
        for(int i=0;i<methods.length;i++)
        {
            Method method = methods[i];
            //檢視是否指定註釋:
            if(method.isAnnotationPresent(Field_Method_Parameter_Annotation.class))  
                
            {
                Field_Method_Parameter_Annotation ma = (Field_Method_Parameter_Annotation)method.getAnnotation(Field_Method_Parameter_Annotation.class);
                System.out.print(" "+ma.describ());   //獲得方法描述
                System.out.println(" "+ma.type());    //獲得方法型別
            }
        
            Annotation[][]parameterAnnotations = method.getParameterAnnotations();    //獲得所有引數
            for(int j=0;j<parameterAnnotations.length;j++)
            {
                int length = parameterAnnotations[j].length; 
                if(length==0)
                {
                    System.out.println("沒有新增Annotation引數");
                }
                else
                {
                    for(int k=0;k<length;k++)
                    {
                        //獲得指定的註釋:
                        Field_Method_Parameter_Annotation pa = (Field_Method_Parameter_Annotation)parameterAnnotations[j][k];
                        System.out.print(" "+pa.describ());   //獲得引數描述
                        System.out.println(" "+pa.type());    //獲得引數型別
                    }
                }
            }
            System.out.println("********************");
            
        }
    }

}

輸出結果

ca.value()=: 預設構造方法
****************
ca.value()=: 立即初始化構造方法.
 編號 int
 姓名 class java.lang.String
****************
********欄位的Annotation*************
 欄位編號 int
 欄位姓名 class java.lang.String
********方法的Annotation*************
沒有新增Annotation引數
********************
 獲得姓名 class java.lang.String
********************
 獲得編號 int
********************
 設定姓名 void
 姓名 class java.lang.String
********************
 設定編號 void
 設定編號 int
********************

相關文章