Java中的反射與注解總結(jié)

Java反射機(jī)制經(jīng)常與注解搭配,出現(xiàn)在各大框架中,是框架的靈魂所在,貫穿了整個(gè)框架的構(gòu)成。在此對這兩個(gè)Java特性做一個(gè)總結(jié)。

反射

能夠分析類能力的程序成為反射(reflective)---from Java核心技術(shù)卷Ⅰ

通過Java的反射機(jī)制,可以在程序中訪問已經(jīng)加載到JVM中的Java對象的描述,實(shí)現(xiàn)訪問、檢測、修復(fù)和修改描述Java本身對象的功能,Java中的java.lang.reflect包提供使用注解功能。

那么,++如何才能分析類能力呢?++首先需要得到類對象,獲取類對象有三種方法,分別是:

1. Class cl = Class.forName("全類名");
2. Class cl = 類名.class;
3. Class cl = 對象.getClass();

快速使用,獲取MyObject類的所有方法并打?。?/p>

Method[] methods = MyObject.class.getMethods();
for(Method method : methods){
  System.out.println("method = " + method.getName());
}

通過獲取到的類對象,就能輕松的得到類的成員變量、構(gòu)造方法、成員方法、類名、注解等等。具體獲取方法有很多,不在此一一例舉了。

注解

注解(也被稱為元數(shù)據(jù))為我們在代碼中添加信息提供了一種形式化的方法,使我們可以在稍后某個(gè)時(shí)刻非常方便地使用這些數(shù)據(jù)。 ---from Java編程思想

注解是眾多引入到JavaSE5中的重要的語言變化之一。它們可以提供用來完整地描述程序所需地信息,而這些信息是無法用Java來表達(dá)的。注解使得我們能夠以將由編譯器來測試和驗(yàn)證的格式,存儲(chǔ)有關(guān)程序的額外信息。注解可以用來生成描述符文件,甚至或是新的類的定義。

注解形式:

public @interface Action {  // 無屬性值
}
 

public @interface Action { // 有屬性值 
   String action() default "A";
   int value() default  0;
}

元注解:用于描述注解的注解

  1. @Target:描述注解能用作用的位置

    • ANNOTATION_TYPE:表示用于Annotation的類型
    • TYPE:作用與類、接口、枚舉
    • CONSTRUCTOR:作用于構(gòu)造方法
    • FIELD:作用于屬性
    • METHOD:作用于方法
    • PARAMETER:作用于參數(shù)
    • LOCAL_VARIABLE:表示局部變量
    • PACKAGE:表示用于包
  2. @Retention:設(shè)置注解的有效范圍

    1. SOURCE:不編譯到Annotation類的文件中
    2. CLASS:編譯到Annotation的文件中,運(yùn)行時(shí)不加在到JVM中
    3. RUNTING:運(yùn)行時(shí)加載到JVM中,有效范圍最大
  3. Decomented: 注解會(huì)在API文檔中體現(xiàn)

  4. Inherited: 描述注解是否被子類繼承

注解與反射:實(shí)例

聲明兩個(gè)注解:

@Target(ElementType.CONSTRUCTOR)
@Retention(RetentionPolicy.RUNTIME)
public @interface Action {
   String action() default "構(gòu)造函數(shù)";
}
 
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface FieldAnnotation {
   String describe() default "描述";
   Class type() default void.class;
}

注解@Action 使用于構(gòu)造函數(shù),有一個(gè)描述屬性,@FieldAnnotation適用于注解屬性常量,包含一個(gè)String類型的描述和修飾的類型


使用注解:


public class ConstructorExample {
 
   @FieldAnnotation(describe = "名字" ,type = String.class)
   public String name;
   @FieldAnnotation(describe = "分?jǐn)?shù)",type = int.class)
   protected int core;
 
   @Action()
   public ConstructorExample(String name, int core) throws NumberFormatException {
      this.core = core;
      this.name = name;
   }
}

反射獲取:

public class ReflectConstructor {
 
   public static void main(String[] args) {
      ConstructorExample constructorExample = new ConstructorExample("ABC",10);
      Class<? extends ConstructorExample> classExample = constructorExample.getClass();
      Constructor[] constructors = classExample.getDeclaredConstructors();
      for (int i = 0; i < constructors.length; i++) {
         Constructor constructor = constructors[i];
         if (constructor.isAnnotationPresent(Action.class)){//是否包含Action.class注解
            System.out.println("包含Action注解");
            Action action = (Action) constructor.getAnnotation(Action.class);
            System.out.println("注解值為 :"+action.action());
         }
         System.out.println("-------------");
      }
 
      Field[] fields = classExample.getDeclaredFields();
      for (Field f: fields) {
         System.out.println("屬性名稱 = "+f.getName());
         System.out.println("屬性類型 = "+f.getType());
         try {
            if (f.isAnnotationPresent(FieldAnnotation.class)) {
               System.out.println("使用FieldAnnotation注解");
               FieldAnnotation annotation = f.getAnnotation(FieldAnnotation.class);
               System.out.println("FieldAnnotation注解參數(shù):");
               System.out.println("Describe:"+annotation.describe());
               System.out.println("Type:"+annotation.type());
            }
 
         } catch (Exception e) {
            e.printStackTrace();
         }
         System.out.println("-------------");
      }
   }
}
結(jié)果

參考文獻(xiàn):

Java核心技術(shù)卷Ⅰ
Java編程思想
Alex@W

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容