Java 註解總結

來源:互聯網
上載者:User

標籤:annotation   註解   動態   java   

作用
  • 標記,告訴編譯器一些資訊。比如@Override,@Deprecated,@SuppressWarnings等。
  • 運行時動態處理。可以通過相應的函數得到註解資訊。

    • getAnnotation(AnnotationName.class) 表示得到該 Target 某個 Annotation 的資訊,因為一個 Target 可以被多個 Annotation 修飾
    • getAnnotations() 則表示得到該 Target 所有 Annotation
    • isAnnotationPresent(AnnotationName.class) 表示該 Target 是否被某個 Annotation 修飾
  • 編譯時間動態處理。編譯時間動態產生代碼。

分類
  • 標準Annotation,比如經常用到的@Override,@Deprecated,@SuppressWarnings,所謂標準Annotation即指Java內建的幾個Annotation
  • 元Annotation,如@Retention, @Target, @Inherited, @Documented,所謂元Annotation即指用來定義Annotation的Annnotation

    • @Retention
      • 用來指定註解的保留時間
      • 可選值:RetentionPolicy.SOURCE(源碼時),RetentionPolicy.CLASS(編譯時間),RetentionPolicy.RUNTIME(運行時)
    • @ Target
      • 用來指定註解可以修飾哪些程式元素
      • 可選值:ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.PARAMETER 等
    • @ Inherited
      • 用來指定是否可被繼承,預設為false
    • @ Documented
      • 用來指定是否儲存到Javadoc文檔中
  • 自訂Annotation,有時候有這樣的需求需要自己自訂Annotation,已定義流程中需要使用到元Annotation

標準Annotation使用

Java內建了一部分註解,平時其實在不經意的使用,使用的最多的莫過於@Override了,用於表示重寫某個函數,然後還有@Deprecated,表示這個函數已經廢棄了,不再推薦使用了。@SuppressWarnings用於表示忽略某個錯誤。

運行時Annotation解析

對某個類,欄位或者函數加上運行時註解後,我們可以在運行時動態擷取註解的資訊。

Class<?> clazz=SomeClass.getClass();  Annotation[] annotations = clazz.getAnnotations();for (Annotation annotation : annotations) {    System.out.println(annotation.annotationType());}
編譯時間Annotation解析

此類註解用於編譯時間動態產生代碼。

編寫註解
@Target({ElementType.FIELD,ElementType.METHOD})@Retention(RetentionPolicy.CLASS)public @interface TestAnnotation {    String name();    int version() default 1;}
  • 使用@interface來定義一個註解,註解名即自訂註解名
  • 所有方法沒有方法體,參數,修飾符
  • 方法傳回值只能是基礎資料型別 (Elementary Data Type)
  • 使用default添加預設值
使用註解
public class Person {    @TestAnnotation(name="name")    private String name;    @TestAnnotation(name="walk")    public void walk(){        System.out.println("walk");    }}
  • 使用@註解名在對應的Target上使用,屬性值通過 屬性=”“ 進行使用
解析註解
@SupportedAnnotationTypes("cn.edu.zafu.TestAnnotation")public class TestProcessor extends AbstractProcessor {    @Override    public boolean process(Set<? extends TypeElement> annotations,            RoundEnvironment roundEnv) {        for (TypeElement te : annotations) {            for (Element element : roundEnv.getElementsAnnotatedWith(te)) {                TestAnnotation testAnnotation = element                        .getAnnotation(TestAnnotation.class);                //testAnnotation.name()                //testAnnotation.version()            }        }        return true;    }}
  • 使用@SupportedAnnotationTypes註解表示該類支援的註解的完整類路徑,支援萬用字元
  • 繼承AbstractProcessor 類,所有處理在process中進行
  • process 函數傳回值表示這組 annotations 是否被這個 Processor 接受,如果接受後續 processor 不會再對這個 Annotations 進行處理
參考文章
  1. 公用技術點之 Java 註解 Annotation

Java 註解總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.