一、概述
注解(Annotation)是Jdk5開始增加的對元數(shù)據(jù)(描述數(shù)據(jù)屬性的信息)的支持,注解我們可以理解為標(biāo)記,這些標(biāo)記可以在編譯,類加載,運行時被讀取,并執(zhí)行相應(yīng)的處理,以便于其他工具補充信息或者進行部署。注解和注釋可不同,注釋是給我們開發(fā)者看的,注解是給程序看的,
二、內(nèi)置注解
@Override:定義在java.lang.Override中,用于修飾一個方法,表示重寫父類中的方法。
@Deprecated:定義在java.lang.Deprecated中,用于修飾方法、屬性、類,表示棄用或過時。
@SuppressWarnings:定義在java.lang.SuppressWadning中,表示忽略編譯時的警告。
三、元注解
元注解就是注解其他注解的注解。
@Target:用于描述注解的使用范圍。
@Retention:表示需要在什么級別保存改注解信息(Source,Class,RunTime)
@Document:說明該注解將被包含在javadoc中。
@Inherited:標(biāo)記這個注解是繼承于哪個注解類。
四、認(rèn)識注解
我們來拿@Deprecated這個注解來舉例,看看它的源碼結(jié)構(gòu)。
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE})
public @interface Deprecated {
}
第一行:@Documented 說明這個注解將被包含在javadoc中。
第二行:@Retention(RetentionPolicy.RUNTIME) 說明這個注解在運行時有效。
第三行:@Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE})說明這個注解的使用范圍。
@Retention中參數(shù)如下:
public enum RetentionPolicy {
/**
* Annotations are to be discarded by the compiler.
* 注釋將被編譯器丟棄
*/
SOURCE,
/**
* Annotations are to be recorded in the class file by the compiler
* but need not be retained by the VM at run time. This is the default
* behavior.
* 注釋將由編譯器記錄在類文件中,但不需要在運行時由 VM 保留。這是默認(rèn)行為。
*/
CLASS,
/**
* Annotations are to be recorded in the class file by the compiler and
* retained by the VM at run time, so they may be read reflectively.
* 注釋將由編譯器記錄在類文件中,并在運行時由 VM 保留,因此可以反射性地讀取它們。
* @see java.lang.reflect.AnnotatedElement
*/
RUNTIME
}
@Target中參數(shù)如下:
public enum ElementType {
/** Class, interface (including annotation type), or enum declaration */
/** 類、接口(包括注解類型)或枚舉聲明 */
TYPE,
/** Field declaration (includes enum constants) */
/** 字段聲明(包括枚舉常量)*/
FIELD,
/** Method declaration */
/** 方法聲明 */
METHOD,
/** Formal parameter declaration */
/** 形式參數(shù)聲明 */
PARAMETER,
/** Constructor declaration */
/** 構(gòu)造函數(shù)聲明 */
CONSTRUCTOR,
/** Local variable declaration */
/** 局部變量聲明 */
LOCAL_VARIABLE,
/** Annotation type declaration */
/** 注解類型聲明 */
ANNOTATION_TYPE,
/** Package declaration */
/** 包聲明 */
PACKAGE,
/**
* Type parameter declaration
* 類型參數(shù)聲明
* @since 1.8
*/
TYPE_PARAMETER,
/**
* Use of a type
* 使用類型
* @since 1.8
*/
TYPE_USE
}
綜合上面的參數(shù)我們可以知道 @**Deprecated **注解將被包含在javadoc中,在運行時有效,注解會被保留,可以聲明在構(gòu)造函數(shù),字段,本地變量,方法,包,形式參數(shù),類或接口中。
五、自定義注解
學(xué)習(xí)了上面的知識,我們可以很輕松的自定義一個注解,只需要按照內(nèi)置注解照葫蘆畫瓢就可以啦。
自定義注解格式:
-元注解
-@interface 注解名稱 {
定義內(nèi)容
}
我們來定義一個Runtime注解,且只可以聲明在方法和類中,并接收兩個參數(shù)id和type。
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE,ElementType.METHOD})
public @interface MyAnnotation {
int id() default 0;
String type();
}
其中接收int類型名為id的參數(shù)和String類型名為type的參數(shù)。default代表默認(rèn)值。如果注解中的參數(shù)有默認(rèn)值時,使用時可以不傳此參數(shù)而使用默認(rèn)值。
使用:
@MyAnnotation(id = 1,type = "這是類")
public class MyTest {
@MyAnnotation(type = "這是一個方法")
public void test(){
}
}
六、總結(jié)
到這里我們已經(jīng)學(xué)會了java中的內(nèi)置注解、元注解以及自定義注解,那么問題來了,如果自定義注解中傳入了參數(shù),那么這個參數(shù)如何獲取呢?其實獲取注解中的參數(shù)是通過反射來實現(xiàn)的,請看我的下一篇文章 Java 反射