Java元數(shù)據(jù)總結(jié):Java注釋的使用和定義
元數(shù)據(jù)從metadata一詞譯來,就是“關(guān)于數(shù)據(jù)的數(shù)據(jù)”的意思。越來越的開源框架都提供了“元數(shù)據(jù)”支持了,其實也就是注釋支持。今天系統(tǒng)學(xué)習(xí)一下Java注釋(Java元數(shù)據(jù))。本文內(nèi)容不限于Javadoc的注釋。
1.什么是Java元數(shù)據(jù),有什么作用?
元數(shù)據(jù),就是“關(guān)于數(shù)據(jù)的數(shù)據(jù)”。功能也有很多啦。你可能用過Javadoc的注釋自動生成文檔。這就是元數(shù)據(jù)功能的一種??偟膩碚f,元數(shù)據(jù)可以用來創(chuàng)建文檔,跟蹤代碼的依賴性,執(zhí)行編譯時格式檢查,代替已有的配置文件(如Hibernate也提供了注釋配置)
注釋有3中基本類型
a.標(biāo)記注釋 --沒有變量,只有名稱標(biāo)識。例如 @annotation
b.單一值注釋 --在標(biāo)記注釋的基礎(chǔ)上提供一段數(shù)據(jù)。如 @annotation(“data”)
c.完整注釋 --可以包括多個數(shù)據(jù)成員,每個數(shù)據(jù)成員由名稱和值構(gòu)成。
@annotation(val1="data1",val2="data2")
2.Java的“注釋”
Java中提供3個內(nèi)置注釋類型
a. Override ,只能用于方法(不能用于類,包聲明或者其他構(gòu)造)
作用:可以保證編譯時候Override函數(shù)的聲明正確性
用法:@Override
public void fun(){..}
b.Deprecated 同樣只能作用與方法
作用:對不應(yīng)再使用的方法進行注解
用法:@Deprecated public void fun{...} //它們說這個注釋跟函數(shù)要同一行
c.SupressWarnings 可以注釋一段代碼
作用:關(guān)閉特定的警告信息,例如你在使用泛型的時候未指定類型
用法: @SupressWarnings(value={"unchecked"})
..代碼
Java中還提供了四種元注釋,專門負責(zé)注釋其他的注釋
@Target 表示該注釋可以用于什么地方??捎玫腅lementType參數(shù)包括:
CONSTRUCTOR : 構(gòu)造器的聲明
FIELD : 域聲明(包括enum實例)
LOCAL_VARIABLE : 局部變量聲明
METHOD : 方法聲明
PACKAGE : 包聲明
PARAMETER : 參數(shù)聲明
TYPE : 類、接口 (包括注解類型) 或enum聲明
@Retention 表示需要在什么級別保存該注釋信息??蛇x的RetentionPoicy參數(shù)包括:
SOURCE : 注釋將被編譯器丟掉
CLASS : 注釋在class文件中可用,但會被VM丟棄
RUNTIME : VM將在運行時也保留注釋,因此可以通過反射機制讀取注釋的信息。
@Documented 將注釋包含在JavaDoc中
@Inheried 允許子類繼承父類中的注釋。
3. 在Java中定義自己的注釋
Java語言支持一種新的類型——注釋類型(annotation type),跟普通類差不多,在類中以符號( @ )的形式注釋其他 Java 代碼
下面將通過一個簡單的例子來實現(xiàn)(代碼是Brett McLaughlin 的)
@interface 申明
i.簡單的注釋類型
- package com.oreilly.tiger.ch06;
- /**
- * Marker annotation to indicate that a method or class
- * is 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è)置默認值
- 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.使用默認值
- @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.改寫默認值
- @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"
- }
這樣就對Java元數(shù)據(jù)/Java注釋進行了總結(jié)。
【編輯推薦】