Java領(lǐng)域,選數(shù)據(jù)是一個(gè)重要的概念。中培偉業(yè)《JAVA高級(jí)開發(fā)技術(shù)實(shí)戰(zhàn)》培訓(xùn)專家龔老師在這里對(duì)元數(shù)據(jù)相關(guān)概念進(jìn)行了詳細(xì)介紹。龔老師指出,元數(shù)據(jù)從metadata一詞譯來,就是關(guān)于數(shù)據(jù)的數(shù)據(jù)的意思。越來越的開源框架都提供了元數(shù)據(jù)支持了,其實(shí)也就是注釋支持。今天系統(tǒng)學(xué)習(xí)一下Java注釋(Java元數(shù)據(jù)。本文內(nèi)容不限于Javadoc的注釋。
1.什么是Java元數(shù)據(jù),有什么作用
元數(shù)據(jù),就是“關(guān)于數(shù)據(jù)的數(shù)據(jù)。功能也有很多啦。你可能用過Javadoc的注釋自動(dòng)生成文檔。這就是元數(shù)據(jù)功能的一種。總的來說,元數(shù)據(jù)可以用來創(chuàng)建文檔,跟蹤代碼的依賴性,執(zhí)行編譯時(shí)格式檢查,代替已有的配置文件Hibernate也提供了注釋配置
注釋有3中基本類型
a.標(biāo)記注釋 --沒有變量,只有名稱標(biāo)識(shí)。例如 @annotation
b.單一值注釋 --在標(biāo)記注釋的基礎(chǔ)上提供一段數(shù)據(jù)。如 @annotation(“data”)
c.完整注釋 --可以包括多個(gè)數(shù)據(jù)成員,每個(gè)數(shù)據(jù)成員由名稱和值構(gòu)成。
@annotation(val1="data1",val2="data2")
2.Java注釋
Java中提供個(gè)內(nèi)置注釋類型
a. Override ,只能用于方法不能用于類,包聲明或者其他構(gòu)造
作用:可以保證編譯時(shí)候Override函數(shù)的聲明正確性
用法:@Override
public void fun(){..}
b.Deprecated 同樣只能作用與方法
作用:對(duì)不應(yīng)再使用的方法進(jìn)行注解
用法:@Deprecated public void fun{...} //它們說這個(gè)注釋跟函數(shù)要同一行
c.SupressWarnings 可以注釋一段代碼
作用:關(guān)閉特定的警告信息,例如你在使用泛型的時(shí)候未指定類型
用法: @SupressWarnings(value={"unchecked"})
..代碼
Java中還提供了四種元注釋,專門負(fù)責(zé)注釋其他的注釋
@Target 表示該注釋可以用于什么地方。可用的ElementType參數(shù)包括:
CONSTRUCTOR : 構(gòu)造器的聲明
FIELD : 域聲明包括enum實(shí)例
LOCAL_VARIABLE : 局部變量聲明
METHOD : 方法聲明
PACKAGE : 包聲明
PARAMETER : 參數(shù)聲明
TYPE : 類、接口 包括注解類型) enum聲明
@Retention 表示需要在什么級(jí)別保存該注釋信息。可選的RetentionPoicy參數(shù)包括:
SOURCE : 注釋將被編譯器丟掉
CLASS : 注釋在class文件中可用,但會(huì)被VM丟棄
RUNTIME : VM將在運(yùn)行時(shí)也保留注釋,因此可以通過反射機(jī)制讀取注釋的信息。
@Documented 將注釋包含在JavaDoc
@Inheried 允許子類繼承父類中的注釋。
3. Java中定義自己的注釋
Java語言支持一種新的類型——注釋類型(annotation type),跟普通類差不多,在類中以符號(hào)( @ )的形式注釋其他 Java 代碼
下面將通過一個(gè)簡(jiǎn)單的例子來實(shí)現(xiàn)(代碼是Brett McLaughlin
@interface 申明
i.簡(jiǎn)單的注釋類型
package com.oreilly.tiger.ch06; /** * Marker annotation to indicate that a method or class *still in progress. */ public @interface InProgress { }
ii.使用定制的注釋類型
@com.oreilly.tiger.ch06.InProgress public void calculateInterest(float amout,float rate) { //Need to finish this method later }
iii.添加成員
package com.oreilly.tiger.ch06; /** * Marker annotation to indicate that a method or class * is still in progress. */ public @interface InProgress { String value(); } @com.oreilly.tiger.ch06.InProgress @TODO("Figure out the amount of interest per month") //或者@TODO(value="Figure out the amount of interest per month") public void calculateInterest(float amount,float rate) { }
iv.設(shè)置默認(rèn)值
package com.oreilly.tiger.ch06; public @interface GroupTODO { public enum Serverity { CRITICAL,IMPORTANT,IRIVIAL,DOCMENTATION }; Severity severity() default Severity.IMPORTANT; String item (); String assignedTo(); String dateAssigned(); } }
v.使用默認(rèn)值
@com.oreilly.tiger.ch06.InProgress @GroupTODO( item="Figure out the amount of interest per month", assignedTo = "Brett McLaughlin", dateAssigned = "08/04/2004" ) public void calculateInterest(float amount, float rate) { //Need to finish this method later }
vi.改寫默認(rèn)值
@com.oreilly.tiger.ch06.InProgress @GroupTODO { severity = GroupTODO.Severity.DOCUMENTATION, item = "Need to explain how this rather unusal method works", assignedTo = "Jon Stevens", dateAssigned = "07/30/2004" }