自定義註解的基本使用

0.使用自定義註解前,先了解相關元註解

java.lang.annotation 提供了四種元註解,專門註解其餘的註解(在自定義註解的時候,須要使用到元註解):
   @Documented – 註解是否將包含在JavaDoc中
   @Retention – 何時使用該註解
   @Target – 註解用於什麼地方
   @Inherited – 是否容許子類繼承該註解html

  1.)@Retention – 定義該註解的生命週期
  ●   RetentionPolicy.SOURCE : 在編譯階段丟棄。這些註解在編譯結束以後就再也不有任何意義,因此它們不會寫入字節碼。@Override, @SuppressWarnings都屬於這類註解。
  ●   RetentionPolicy.CLASS : 在類加載的時候丟棄。在字節碼文件的處理中有用。註解默認使用這種方式
  ●   RetentionPolicy.RUNTIME : 始終不會丟棄,運行期也保留該註解,所以可使用反射機制讀取該註解的信息。咱們自定義的註解一般使用這種方式。java

  2.)Target – 表示該註解用於什麼地方。默認值爲任何元素,表示該註解用於什麼地方。可用的ElementType 參數包括
  ● ElementType.CONSTRUCTOR: 用於描述構造器
  ● ElementType.FIELD: 成員變量、對象、屬性(包括enum實例)
  ● ElementType.LOCAL_VARIABLE: 用於描述局部變量
  ● ElementType.METHOD: 用於描述方法
  ● ElementType.PACKAGE: 用於描述包
  ● ElementType.PARAMETER: 用於描述參數
  ● ElementType.TYPE: 用於描述類、接口(包括註解類型) 或enum聲明數組

 3.)@Documented – 一個簡單的Annotations 標記註解,表示是否將註解信息添加在java 文檔中。app

 4.)@Inherited – 定義該註釋和子類的關係
     @Inherited 元註解是一個標記註解,@Inherited 闡述了某個被標註的類型是被繼承的。若是一個使用了@Inherited 修飾的annotation 類型被用於一個class,則這個annotation 將被用於該class 的子類。ide

1.自定義註解類編寫的一些規則

1. Annotation 型定義爲@interface, 全部的Annotation 會自動繼承java.lang.Annotation這一接口,而且不能再去繼承別的類或是接口.
  2. 參數成員只能用public 或默認(default) 這兩個訪問權修飾
  3. 參數成員只能用基本類型byte、short、char、int、long、float、double、boolean八種基本數據類型和String、Enum、Class、annotations等數據類型,以及這一些類型的數組.
  4. 要獲取類方法和字段的註解信息,必須經過Java的反射技術來獲取 Annotation 對象,由於你除此以外沒有別的獲取註解對象的方法
  5. 註解也能夠沒有定義成員,,不過這樣註解就沒啥用了
PS:自定義註解須要使用到元註解測試

2.實例

AnimalName.java

package com.xiaojian.basics.annotation;
/**
 * 動物種類
 */
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Target(FIELD)
@Retention(RUNTIME)
@Documented
public @interface AnimalName {
    String value() default "";
}

AnimalColor.java

package com.xiaojian.basics.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
 * 動物顏色
 */
@Target(FIELD)
@Retention(RUNTIME)
@Documented
public @interface AnimalColor {
    /**
     * 顏色枚舉
     */
    public enum Color{黑色,白色,灰色,雜色};

    Color animalColor() default Color.白色;
}

AnimalMaster.javaui

package com.xiaojian.basics.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
 * 動物主人
 */

@Target(FIELD)
@Retention(RUNTIME)
@Documented
public @interface AnimalMaster {

    int id() default -1;

    String name() default "";

    String address() default "";
}

AnimalInfoUtil.javaspa

package com.xiaojian.basics.annotation;

import java.lang.reflect.Field;

/**
 * 返回動物信息
 */
public class AnimalInfoUtil {

    public static void getInfo(Class<?> clazz){

        StringBuilder strAnimalName = new StringBuilder( "動物種類:");
        StringBuilder strAnimalColor = new StringBuilder( "動物顏色:");
        StringBuilder strAnimalProvider = new StringBuilder( "主人信息:");

        Field[] fields = clazz.getDeclaredFields();

        for(Field field : fields){
            if(field.isAnnotationPresent(AnimalName.class)){
                AnimalName animalAnimal = (AnimalName)field.getAnnotation(AnimalName.class);
                strAnimalName.append(animalAnimal.value());
                System.out.println(strAnimalName);
            } else if(field.isAnnotationPresent(AnimalColor.class)){
                AnimalColor animalColor = (AnimalColor)field.getAnnotation(AnimalColor.class);
                strAnimalColor.append(animalColor.animalColor().toString());
                System.out.println(strAnimalColor);
            } else if(field.isAnnotationPresent(AnimalMaster.class)){
                AnimalMaster animalMaster = (AnimalMaster)field.getAnnotation(AnimalMaster.class);
                strAnimalProvider.append("[編號:" + animalMaster.id() + ",主人名字:" + animalMaster.name() + ",地址:" + animalMaster.address() + "]");
                System.out.println(strAnimalProvider);
            }

        }

    }
}

測試:

package com.xiaojian.basics.annotation;

/**
  * 測試類
  */
public class Test {
    public static void main(String[] args) {
        AnimalInfoUtil.getInfo(Animal.class);
    }

}

返回:

動物種類:狗
動物顏色:黑色
主人信息:[編號:1,主人名字:小明,地址:浙江省杭州市XXXXXXXX]

【參考連接】code

https://www.cnblogs.com/acm-bingzi/p/javaAnnotation.htmlhtm

相關文章
相關標籤/搜索