運行時 Annotation 指 @Retention 爲 RUNTIME 的 Annotation,可手動調用下面常用 API 解析
method.getAnnotation(AnnotationName.class);
method.getAnnotations();
method.isAnnotationPresent(AnnotationName.class);
運行期註解解析案例:
定義一個自定義註解
package org.vincent.maven.annotation.anno;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 自定義註解
*
* @author PengRong
*
*/
@Documented
@Retention(RetentionPolicy.RUNTIME) // 這個自定義註解生命到運行期
@Target(ElementType.METHOD)
@Inherited
public @interface MethodInfo {
String author() default "Vicent";
String date() default "2017/03/03";
int version() default 1;
}
在一個類中使用上面定義的註解:
package org.vincent.maven.annotation;
import org.vincent.maven.annotation.anno.MethodInfo;
/**
* 使用自定義的註解
*
* @ClassName: App
* @Description: TODO(這裏用一句話描述這個類的作用)
* @author PengRong
* @date 2017年3月5日 下午9:52:55
*
*/
public class App {
@MethodInfo(author = "Cindy", date = "day", version = 2)
public static void main(String[] args) {
System.out.println("Hello World!");
}
@Override
public java.lang.String toString() {
// TODO Auto-generated method stub
return super.toString();
}
@Deprecated
private void xx() {
// TODO Auto-generated method stub
}
@SuppressWarnings(value = { "deprecation", "unchecked" })
private void y() {
// TODO Auto-generated method stub
}
}
測試:
package org.vincent.maven.annotation;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
public class AnnotationParser {
public static void main(String[] args) throws ClassNotFoundException {
// 測試AnnotationTest類,得到被註解的類對象
Class c = Class.forName("org.vincent.maven.annotation.App");
// 獲取該類所有聲明的方法
Method[] methods = c.getDeclaredMethods();
// 聲明註解集合
Annotation[] annotations;
// 遍歷所有的方法得到各方法上面的註解信息
for (Method method : methods) {
// 獲取每個方法上面所聲明的所有註解信息
annotations = method.getDeclaredAnnotations();
// 再遍歷所有的註解,打印其基本信息
System.out.println("--------------------------------------------------");
System.out.println("方法: " + method.getName());
for (Annotation an : annotations) {
System.out.println("方法名爲:" + method.getName() + " 其上面的註解爲:" + an.annotationType().getSimpleName());
Method[] meths = an.annotationType().getDeclaredMethods();// 通過反射獲取到註解類聲明的所有成員變量
// 遍歷註解類的所有成員變量
for (Method meth : meths) {
System.out.println("註解的變量名爲:" + meth.getName());
}
}
System.out.println("--------------------------------------------------");
}
}
}
輸出結果:
--------------------------------------------------
方法: main
方法名爲:main 其上面的註解爲:MethodInfo
註解的變量名爲:version
註解的變量名爲:date
註解的變量名爲:author
--------------------------------------------------
--------------------------------------------------
方法: toString
--------------------------------------------------
--------------------------------------------------
方法: xx
方法名爲:xx 其上面的註解爲:Deprecated
--------------------------------------------------
--------------------------------------------------
方法: y
--------------------------------------------------