注解主要是用來減少代碼的冗余,用較少的代碼就可以實現(xiàn)更多的功能.
java提供了四類元注解,我們構(gòu)建新的注解都是用元注解來實現(xiàn)的,下邊就類注解Type,全局變量注解Filed,方法注解Method分別實現(xiàn),以及對這幾個注解進(jìn)行解析.
注解就運行周期分為三種:Source,Class,Runtime,當(dāng)前的例子都是針對Runtime來實現(xiàn)的.
類注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface TypeAnno {
String table() default "table";
}
方法注解
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@interface MethodAnno {
FiledAnnoName setAnnoName() default @FiledAnnoName;
FiledAnnoAge setAnnoAge() default @FiledAnnoAge;
String method() default "method";
}
filed注解
@Target(ElementType.FIELD)//全局變量
@Retention(RetentionPolicy.RUNTIME)//運行時注解
@interface FiledAnnoAge {
int age() default 0;//注解 構(gòu)造方法無參 必須有默認(rèn)值
}
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface FiledAnnoName {
String value() default "";
}
使用注解的bean
@TypeAnno
public class AnnoBean {
@FiledAnnoName("嘿嘿")
String name = "Lucy";
@FiledAnnoAge(age = 12)
int age = 20;
//value可不需要寫成key=value的樣式,直接寫value,但是必須是value的屬性.
@MethodAnno(setAnnoName = @FiledAnnoName("方法"), setAnnoAge = @FiledAnnoAge(age = 18))
public void testAnno() {
}
}
注解解析器
public class AnnoCreator {
/**
* 解析局部變量的注解
*
* @throws Exception
*/
public static void annoFiled() throws Exception {
Class<AnnoBean> clazz = AnnoBean.class;
//創(chuàng)建實例
AnnoBean annoBean = clazz.newInstance();
//得到類中的所有定義的屬性
for (Field filed : clazz.getDeclaredFields()) {
//得到屬性的注解潜沦,對一個目標(biāo)可以使用多個注解
Annotation[] anns = filed.getAnnotations();//得到所有注解
if (anns.length < 1) {
continue;
}
//MyAge注解分析
if (anns[0] instanceof FiledAnnoAge) {
FiledAnnoAge filedAnnoAge = (FiledAnnoAge) anns[0];//注解的值
String name = filed.getName();
Log.e("filedName_AGE", name);
int age = filed.getInt(annoBean);//實際的值
Log.e("AGE", age + filedAnnoAge.age() + "");//實際的值+注解的值
}
//MyName注解分析
if (anns[0] instanceof FiledAnnoName) {
FiledAnnoName filedAnnoName = (FiledAnnoName) anns[0];
String name = filedAnnoName.value();
String fileName = (String) filed.get(annoBean);
String filedName = filed.getName();
Log.e("filedName_NAME", filedName);
Log.e("Name", name + fileName + "");
}
}
}
/**
* 解析類的注解
*
* @throws Exception
*/
public static void annoType() throws Exception {
Class<AnnoBean> clazz = AnnoBean.class;
TypeAnno typeAnno = clazz.getAnnotation(TypeAnno.class);//得到單個注解
Log.e("TableAnno", typeAnno.table());
}
/**
* 解析方法的注解
*
* @throws Exception
*/
public static void annoMethod() throws Exception {
Class<AnnoBean> clazz = AnnoBean.class;
//根據(jù)反射得到方法
Method[] methods = clazz.getMethods();
for (Method method:methods) {
if (method.getName().equals("testAnno")){
MethodAnno annotation = method.getAnnotation(MethodAnno.class);
FiledAnnoAge filedAnnoAge = annotation.setAnnoAge();
int age = filedAnnoAge.age();
FiledAnnoName filedAnnoName = annotation.setAnnoName();
String name = filedAnnoName.value();
Log.e("Method_ANno", age + name + annotation.method());
}
}
}
}
注解的調(diào)用測試
try {
AnnoCreator.annoFiled();
AnnoCreator.annoType();
AnnoCreator.annoMethod();
} catch (Exception e) {
Log.e("ERROR",e.toString());
}
測試結(jié)果
image.png