Java的自訂註解及通過反射擷取註解

來源:互聯網
上載者:User

標籤:out   ble   bool   stack   獲得   類繼承   body   方法   declare   

一、註解基本知識

  1、元註解:@Retention @Target @Document @Inherited

  2、Annotation型定義為@interface, 所有的Annotation會自動繼承java.lang.Annotation這一介面,並且不能再去繼承別的類或是介面。

  3、參數成員只能用public或預設(default)這兩個訪問權修飾

  4、參數成員只能用基本類型byte,short,char,int,long,float,double,boolean八種基礎資料型別 (Elementary Data Type)和String、Enum、Class、annotations等資料類型,以及這一些類型的數組。

  5、要擷取類、方法和欄位的註解資訊,必須通過Java的反射技術來擷取 Annotation對象,除此之外沒有別的擷取註解對象的方法

  6、註解也可以沒有定義成員, 不過這樣註解就沒啥用了,只起到標識作用

 

自訂註解類時, 可以指定目標 (類、方法、欄位, 建構函式等) , 註解的生命週期(運行時,class檔案或者源碼中有效), 是否將註解包含在javadoc中及是否允許子類繼承父類中的註解, 具體如下:

  1、@Target 表示該註解目標,可能的 ElemenetType 參數包括:

    ElemenetType.CONSTRUCTOR 構造器聲明
    ElemenetType.FIELD 域聲明(包括 enum 執行個體)
    ElemenetType.LOCAL_VARIABLE 局部變數聲明
    ElemenetType.METHOD 方法聲明
    ElemenetType.PACKAGE 包聲明
    ElemenetType.PARAMETER 參數聲明
    ElemenetType.TYPE 類,介面(包括註解類型)或enum聲明

  2、@Retention 表示該註解的生命週期,可選的 RetentionPolicy 參數包括

    RetentionPolicy.SOURCE 註解將被編譯器丟棄
    RetentionPolicy.CLASS 註解在class檔案中可用,但會被JVM丟棄
    RetentionPolicy.RUNTIME JVM將在運行期也保留注釋,因此可以通過反射機制讀取註解的資訊

  3、@Documented 指示將此註解包含在 javadoc 中

  4、@Inherited 指示允許子類繼承父類中的註解

二、在java中的使用

  2.1、定義註解

package annotation;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;public class MyAnnotation {    /**     * 註解類     *      * @author suguoliang     *     */    @Target(ElementType.TYPE)    @Retention(RetentionPolicy.RUNTIME)    public @interface MyClassAnnotation {        String uri();        String desc();    }    /**     * 構造方法註解     *      * @author suguoliang     *     */    @Target(ElementType.CONSTRUCTOR)    @Retention(RetentionPolicy.RUNTIME)    public @interface MyConstructorAnnotation {        String uri();        String desc();    }    /**     * 方法註解     *      * @author suguoliang     *     */    @Target(ElementType.METHOD)    @Retention(RetentionPolicy.RUNTIME)    public @interface MyMethodAnnotation {        String uri();        String desc();    }    /**     * 欄位註解     *      * @author suguoliang     *     */    @Target(ElementType.FIELD)    @Retention(RetentionPolicy.RUNTIME)    public @interface MyFieldAnnotation {        String uri();        String desc();    }    /**     * 可以同時應用到類和方法上     *      * @author 尐蘇     *     */    @Target({ ElementType.TYPE, ElementType.METHOD })    @Retention(RetentionPolicy.RUNTIME)    public @interface MyClassAndMethodAnnotation {        // 定義枚舉        public enum EnumType {            util, entity, service, model        }        // 設定預設值        public EnumType classType() default EnumType.util;        // 數組        int[] arr() default { 3, 7, 5 };        String color() default "blue";    }}

  2.2基本測試

package annotation;import java.lang.reflect.Constructor;import java.lang.reflect.Field;import java.lang.reflect.Method;import annotation.MyAnnotation.MyClassAndMethodAnnotation;import annotation.MyAnnotation.MyClassAndMethodAnnotation.EnumType;import annotation.MyAnnotation.MyClassAnnotation;import annotation.MyAnnotation.MyConstructorAnnotation;import annotation.MyAnnotation.MyFieldAnnotation;import annotation.MyAnnotation.MyMethodAnnotation;@MyClassAnnotation(desc = "The Class", uri = "com.sgl.annotation")@MyClassAndMethodAnnotation(classType = EnumType.util)public class TestAnnotation {    @MyFieldAnnotation(desc = "The Class Field", uri = "com.sgl.annotation#id")    private String id;    @MyConstructorAnnotation(desc = "The Class Constructor", uri = "com.sgl.annotation#constructor")    public TestAnnotation() {    }    public String getId() {        return id;    }    @MyMethodAnnotation(desc = "The Class Method", uri = "com.sgl.annotation#setId")    public void setId(String id) {        this.id = id;    }    @MyMethodAnnotation(desc = "The Class Method sayHello", uri = "com.sgl.annotation#sayHello")    public void sayHello(String name) {        if (name == null || name.equals("")) {            System.out.println("hello world!");        } else {            System.out.println(name + "\t:say hello world");        }    }    public static void main(String[] args) throws Exception {        Class<TestAnnotation> clazz = TestAnnotation.class;        // 擷取類註解        MyClassAnnotation myClassAnnotation = clazz.getAnnotation(MyClassAnnotation.class);        System.out.println(myClassAnnotation.desc() + "+" + myClassAnnotation.uri());        // 獲得構造方法註解        Constructor<TestAnnotation> constructors = clazz.getConstructor(new Class[] {});// 先獲得構造方法對象        MyConstructorAnnotation myConstructorAnnotation = constructors.getAnnotation(MyConstructorAnnotation.class);// 拿到構造方法上面的註解執行個體        System.out.println(myConstructorAnnotation.desc() + "+" + myConstructorAnnotation.uri());        // 獲得方法註解        Method method = clazz.getMethod("setId", new Class[] { String.class });// 獲得方法對象        MyMethodAnnotation myMethodAnnotation = method.getAnnotation(MyMethodAnnotation.class);        System.out.println(myMethodAnnotation.desc() + "+" + myMethodAnnotation.uri());        // 獲得欄位註解        Field field = clazz.getDeclaredField("id");// 暴力擷取private修飾的成員變數        MyFieldAnnotation myFieldAnnotation = field.getAnnotation(MyFieldAnnotation.class);        System.out.println(myFieldAnnotation.desc() + "+" + myFieldAnnotation.uri());    }}

  2.3通過反射解析

package annotation;import java.lang.reflect.Method;import java.util.Arrays;import annotation.MyAnnotation.MyClassAndMethodAnnotation;import annotation.MyAnnotation.MyClassAndMethodAnnotation.EnumType;import annotation.MyAnnotation.MyClassAnnotation;import annotation.MyAnnotation.MyMethodAnnotation;public class ParseAnnotation {    /**     * 解析方法註解     *      * @param clazz     */    public static <T> void parseMethod(Class<T> clazz) {        try {            T obj = clazz.newInstance();            for (Method method : clazz.getDeclaredMethods()) {                MyMethodAnnotation methodAnnotation = method.getAnnotation(MyMethodAnnotation.class);                if (methodAnnotation != null) {                    // 通過反射調用帶有此註解的方法                    method.invoke(obj, methodAnnotation.uri());                }                MyClassAndMethodAnnotation myClassAndMethodAnnotation = method                        .getAnnotation(MyClassAndMethodAnnotation.class);                if (myClassAndMethodAnnotation != null) {                    if (EnumType.util.equals(myClassAndMethodAnnotation.classType())) {                        System.out.println("this is a util method");                    } else {                        System.out.println("this is a other method");                    }                    System.out.println(Arrays.toString(myClassAndMethodAnnotation.arr()));// 列印數組                    System.out.println(myClassAndMethodAnnotation.color());// 輸出顏色                }                System.out.println("\t\t-----------------------");            }        } catch (Exception e) {            e.printStackTrace();        }    }    public static <T> void parseType(Class<T> clazz) {        try {            MyClassAndMethodAnnotation myClassAndMethodAnnotation = clazz                    .getAnnotation(MyClassAndMethodAnnotation.class);            if (myClassAndMethodAnnotation != null) {                if (EnumType.util.equals(myClassAndMethodAnnotation.classType())) {                    System.out.println("this is a util class");                } else {                    System.out.println("this is a other class");                }            }            MyClassAnnotation myClassAnnotation = clazz.getAnnotation(MyClassAnnotation.class);            if (myClassAnnotation != null) {                System.err.println(" class info: " + myClassAnnotation.uri());            }        } catch (Exception e) {            e.printStackTrace();        }    }    public static void main(String[] args) {        parseMethod(TestAnnotation.class);        parseType(TestAnnotation.class);    }}

 

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.