Android -- Annotation

來源:互聯網
上載者:User

Android -- Annotation
Override Annotation @Overridepublic void onCreate(Bundle savedInstanceState){};概念 An annotation is a form of metadata, that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated. Annotations have no direct effect on the operation of the code they annotate. 分類 標準 Annotation Override, Deprecated, SuppressWarnings 元 Annotation @Retention, @Target, @Inherited, @Documented 自訂 Annotation 自訂 Annotation 表示自己根據需要定義的 Annotation,定義時需要用到上面的元 Annotation。 自訂 調用 

   @DBTableY(name = "YYD__DB")   public class DB {       @DBColumnY(name = "column11xx")       public String column1;       @DBColumnY(name = "column22xx")       public String column2;

 

   }定義
   @Retention(RetentionPolicy.RUNTIME)   @Target(ElementType.TYPE)   public @interface DBTableY {       String name() default "YUYIDONG11";   }   @Retention(RetentionPolicy.RUNTIME)   @Target({ElementType.FIELD})   public @interface DBColumnY {       String name() default "YUYIDONG";   }

 

通過 @interface 定義,註解名即為自訂註解名註解配置參數名為註解類的方法名,且:a. 所有方法沒有方法體,沒有參數沒有修飾符,實際只允許 public & abstract 修飾符,預設為 public ,不允許拋異常 b. 方法傳回值只能是基本類型,String, Class, annotation, enumeration 或者是他們的一維數組 c. 若只有一個預設屬性,可直接用 value() 函數。一個屬性都沒有表示該 Annotation 為 Mark Annotation 可以加 default 表示預設值元 Annotation @Documented 是否會儲存到 Javadoc 文檔中 @Retention 保留時間,可選值 SOURCE(源碼時),CLASS(編譯時間),RUNTIME(運行時),預設為 CLASS,SOURCE 大都為 Mark Annotation,這類 Annotation 大都用來校正,比如 Override, SuppressWarnings 作用:表示需要在什麼層級儲存該注釋資訊,用於描述註解的生命週期(即:被描述的註解在什麼範圍內有效) 取值(RetentionPoicy)有:     1.SOURCE:在源檔案中有效(即源檔案保留)     2.CLASS:在class檔案中有效(即class保留)     3.RUNTIME:在運行時有效(即運行時保留) ​ @Target 可以用來修飾哪些程式元素,如 TYPE, METHOD, CONSTRUCTOR, FIELD, PARAMETER 等,未標註則表示可修飾所有。 作用:用於描述註解的使用範圍(即:被描述的註解可以用在什麼地方) 取值(ElementType)有:     1.CONSTRUCTOR:用於描述構造器     2.FIELD:用於描述域     3.LOCAL_VARIABLE:用於描述局部變數     4.METHOD:用於描述方法     5.PACKAGE:用於描述包     6.PARAMETER:用於描述參數     7.TYPE:用於描述類、介面(包括註解類型) 或enum聲明 ​ @Inherited 是否可以被繼承,預設為 false 當 @Inherited annotation類型標註的annotation的Retention是RetentionPolicy.RUNTIME,則反射API增強了這種繼承性。如果我們使用java.lang.reflect去查詢一個 @Inherited annotation類型的annotation時,反射代碼檢查將展開工作:檢查class和其父類,直到發現指定的annotation類型被發現,或者到達類繼承結構的頂層。 註解 使用 @interface自訂註解時,自動繼承了java.lang.annotation.Annotation介面,由編譯器自動完成其他細節。在定義註解時,不能繼承其他的註解或介面。 @interface用來聲明一個註解,其中的每一個方法實際上是聲明了一個配置參數。方法的名稱就是參數的名稱,傳回值類型就是參數的類型(傳回值類型只能是基本類型、Class、String、enum)。可以通過default來聲明參數的預設值。    public @interface 註解名 {定義體}註解參數的可支援資料類型:     1.所有基礎資料型別 (Elementary Data Type)(int,float,boolean,byte,double,char,long,short)     2.String類型     3.Class類型     4.enum類型     5.Annotation類型     6.以上所有類型的數組 解析 運行時 Annotation 解析 運行時 Annotation 指 @Retention 為 RUNTIME 的 Annotation 
      method.getAnnotation(AnnotationName.class);      method.getAnnotations();      method.isAnnotationPresent(AnnotationName.class);getAnnotation(AnnotationName.class) 表示得到該 Target 某個 Annotation 的資訊,因為一個 Target 可以被多個 Annotation 修飾getAnnotations() 則表示得到該 Target 所有 AnnotationisAnnotationPresent(AnnotationName.class) 表示該 Target 是否被某個 Annotation 修飾      Class clazz = DB.class;              if (clazz.isAnnotationPresent(DBTableY.class)) {                  DBTableY dbTableY = (DBTableY) clazz.getAnnotation(DBTableY.class);                  Log.i("yyd", "dbTableY.name()--->" + dbTableY.name());                  Field[] fields = clazz.getFields();                  for (Field field : fields) {                      DBColumnY dbColumnY = field.getAnnotation(DBColumnY.class);                      Log.i("yyd", "dbColumnY.name()--->" + dbColumnY.name());                      Log.i("yyd", "field.getName()--->" + field.getName());                  }              }

 

編譯時間 Annotation 解析 編譯時間 Annotation 指 @Retention 為 CLASS 的 Annotation,甴編譯器自動解析。需要做的 自訂類整合自 AbstractProcessor重寫其中的 process 函數編譯器在編譯時間自動尋找所有繼承自 AbstractProcessor 的類,然後調用他們的 process 方法去處理。   
    SupportedAnnotationTypes({ "com.yydcdut.test.annotation.DBColumnY" })      public class DBColumnProcessor extends AbstractProcessor {                @Override          public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment env) {              for (TypeElement te : annotations) {                  for (Element element : env.getElementsAnnotatedWith(te)) {                      DBColumnY dbColumnY = element.getAnnotation(DBColumnY.class);                     Log.i("yyd",element.getEnclosingElement().toString());                  }              }              return false;          }      }

 

SupportedAnnotationTypes 表示這個 Processor 要處理的 Annotation 名字。process 函數中參數 annotations 表示待處理的 Annotations,參數 env 表示當前或是之前的運行環境。process 函數傳回值表示這組 annotations 是否被這個 Processor 接受,如果接受後續子的 rocessor 不會再對這個 Annotations 進行處理。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.