Java的自定義注解及通過反射獲取注解


一、注解基本知識

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

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

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

  4、參數成員只能用基本類型byte,short,char,int,long,float,double,boolean八種基本數據類型和StringEnumClassannotations等數據類型,以及這一些類型的數組。

  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);
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM