註解(Annotation)又稱爲元數據,在JDK1.5後引入,它的做用是:java
生成文檔 這是註解的原始用途,能夠經過註解生成JavaDoc文檔程序員
跟蹤代碼的依賴性 能夠經過註解替代配置文件,簡化項目的配置。現有的許多框架都採用這個功能減小本身的配置。框架
編譯檢查 在編譯時進行格式檢查,例如@Overrideide
Java目前內置了三種標準註解,以及四種元註解。四種元註解負責建立其餘的註解。spa
@Override,表示當前的方法覆蓋超類中的方法.net
@Deprecated,若是程序員使用被這個註解註釋的元素,則編譯器會進行提示code
@Suppress Warnings,忽略編譯器的警告對象
@Target,表示註解的適用範圍,例如@Target(ElementType.FIELD).繼承
ElementType的枚舉值有接口
CONSTRUCTOR,用於構造方法
FIELD,用於字段聲明,包括常量
LOCAL_VARIABLE,用於局部變量
METHOD,用於方法
PACKAGE,用於包聲明
PARAMETER,用於參數聲明
TYPE,用於類,接口
@Retention,表示註解的保留級別,例如@Retention(RetentionPolicy.RUNTIME).
RetentionPolicy的枚舉值有
Source,註解將被編譯器丟棄
Class,註解能夠在class文件中使用,可是會被VM丟棄
Runtime,在VM的運行期間也會保留
@Document,將次註解在javaDoc文件中可見
@Inherited,容許子類繼承父類中的註解
package com.rainman.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface MethodLog { String method() default "do something"; String opeator() default "someone"; }
@interface標明該類型是一個註解的定義
對於「String method() default "dong something";」來講String是註解參數的數據類型,method是參數名,default用來設置默認值
package com.rainman.controller; import com.rainman.annotation.MethodLog; public class CelebrateController { @MethodLog(method="celebrate birthday",operator="we") public void celebrateBirthday(){ } @MethodLog(operator = "we") public void celebrateNewYears(){ } @MethodLog(method="celebrate harvest") public void celebrateHarvest(){ } @MethodLog public void yeah(){ } }
註解使用的格式爲"@Annotation(para=xxxx,...)",Annotation是註解名,para是註解中設置的參數名,參數若是不設置值,則會使用默認值
package com.rainman.annotation; import com.rainman.controller.CelebrateController; import java.lang.reflect.Method; public class MethodLogParse { public static void parse(Class classType) throws Exception{ Method[] array = classType.getMethods(); for(Method method : array){ System.out.println("=================="+method.getName()+"================="); if(method.isAnnotationPresent(MethodLog.class)){ MethodLog methodLog = method.getDeclaredAnnotation(MethodLog.class); String str = String.valueOf(methodLog.operator()); String str1 = String.valueOf(methodLog.method()); System.out.println(str + " " + str1); } } } public static void main(String[] args){ try { MethodLogParse.parse(CelebrateController.class); }catch(Exception e){ e.printStackTrace(); } } }
輸出結果爲
==================celebrateBirthday================= we celebrate birthday ==================yeah================= someone do something ==================celebrateNewYears================= we do something ==================celebrateHarvest================= someone celebrate harvest ==================wait================= ==================wait================= ==================wait================= ==================equals================= ==================toString================= ==================hashCode================= ==================getClass================= ==================notify================= ==================notifyAll=================
經過反射機制,遍歷方法,而後獲取方法的註解。註解能夠註解類,方法,字段等等,因此也能夠經過相似的方式獲取這些對象的註解,而後進行處理。