java:自定义注解以及注解解析

注解主要是用来减少代码的冗余,用较少的代码就可以实现更多的功能.

java提供了四类元注解,我们构建新的注解都是用元注解来实现的,下边就类注解Type,全局变量注解Filed,方法注解Method分别实现,以及对这几个注解进行解析.
注解就运行周期分为三种:Source,Class,Runtime,当前的例子都是针对Runtime来实现的.

类注解
@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;//注解 构造方法无参 必须有默认值
}

@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 clazz = AnnoBean.class;
        //创建实例
        AnnoBean annoBean = clazz.newInstance();
        //得到类中的所有定义的属性
        for (Field filed : clazz.getDeclaredFields()) {
            //得到属性的注解,对一个目标可以使用多个注解
            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 clazz = AnnoBean.class;
        TypeAnno typeAnno = clazz.getAnnotation(TypeAnno.class);//得到单个注解
        Log.e("TableAnno", typeAnno.table());
    }

    /**
     * 解析方法的注解
     *
     * @throws Exception
     */
    public static void annoMethod() throws Exception {
        Class clazz = AnnoBean.class;
        //根据反射得到方法
        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());
            }

        }
    }
}

注解的调用测试

try {
                    AnnoCreator.annoFiled();
                    AnnoCreator.annoType();
                    AnnoCreator.annoMethod();
                } catch (Exception e) {
                    Log.e("ERROR",e.toString());
                }
测试结果
java:自定义注解以及注解解析_第1张图片
image.png

你可能感兴趣的:(java:自定义注解以及注解解析)