通过反射获取自定义注解信息

  • 了解与运用注解。

  • 1.自定义注解接口,应用于构造方法、成员方法和成员参数

    @Target(ElementType.CONSTRUCTOR) //用于构造方法
    @Retention(RetentionPolicy.RUNTIME) //有效范围:运行时加载到jvm
    @interface Constructor_Annotation{
        String value() default "默认构造方法"; //定义一个具有默认值的String型成员
    }
    
    @Target({ElementType.FIELD,ElementType.METHOD,ElementType.PARAMETER})
    @Retention(RetentionPolicy.RUNTIME) //有效范围:运行时加载到jvm
    @interface Field_Method_Parameter_Annotation{
        String describe();    //定义一个没有默认值的String型成员
        Class type() default void.class; //定义一个具有默认值的Class型成员
    }
    
  • 2.在类中使用注解

    class Record{
        @Field_Method_Parameter_Annotation(describe = "编号",type = int.class)
        int id;
        @Field_Method_Parameter_Annotation(describe = "姓名",type = String.class)
        String name;
    
        @Constructor_Annotation()
        public Record() {
        }
    
        @Constructor_Annotation("初始化构造方法")
        public Record(@Field_Method_Parameter_Annotation(describe = "编号",type = int.class) int id
                ,@Field_Method_Parameter_Annotation(describe = "姓名",type = String.class) String name) {
            this.id = id;
            this.name = name;
        }
    
        @Field_Method_Parameter_Annotation(describe = "获得编号",type = int.class)
        public int getId() {
            return id;
        }
        @Field_Method_Parameter_Annotation(describe = "设置编号") //默认void类型
        public void setId(@Field_Method_Parameter_Annotation(describe = "编号",type = int.class) int id) {
            this.id = id;
        }
        @Field_Method_Parameter_Annotation(describe = "获得姓名",type = String.class)
        public String getName() {
            return name;
        }
        @Field_Method_Parameter_Annotation(describe = "设置姓名") //默认void类型
        public void setName(@Field_Method_Parameter_Annotation(describe = "姓名",type = String.class) String name) {
            this.name = name;
        }
    }
    
  • 3.通过反射获取注解信息

    Record record = new Record();
            Class recordClass = record.getClass();
            System.out.println("=========构造方法的描述如下=========");
            Constructor[] declaredConstructors = recordClass.getDeclaredConstructors();//获得所有构造方法
            for (int i = 0; i < declaredConstructors.length; i++) {
                Constructor constructor = declaredConstructors[i];  //遍历所有构造方法
                if (constructor.isAnnotationPresent(Constructor_Annotation.class)){ //查看是否有指定类型的注解
                    //getAnnotation()获得指定类型的注解
                    Constructor_Annotation ca=(Constructor_Annotation) constructor.getAnnotation(Constructor_Annotation.class);
                    System.out.println("构造方法注解信息:"+ca.value()); //获取注解信息
                }
                Annotation[][] parameterAnnotations = constructor.getParameterAnnotations();
                for (int j = 0; j < parameterAnnotations.length; j++) {
                    int length=parameterAnnotations[j].length;
                    if (length==0){
                        System.out.println("构造方法中没有参数");
                    }else {
                        for (int k = 0; k < length; k++) {
                            Field_Method_Parameter_Annotation pa=(Field_Method_Parameter_Annotation) parameterAnnotations[j][k];
                            System.out.println("有参构造方法参数描述:"+pa.describe()); //获得参数描述
                            System.out.println("有参构造方法参数类型:"+pa.type()); //获得参数类型
                        }
                    }
    
                }
                System.out.println();
            }
    
            System.out.println("=========字段的描述如下=============");
            Field[] declaredFields = recordClass.getDeclaredFields();
            for (int i = 0; i < declaredFields.length; i++) {
                Field field = declaredFields[i];
                if (field.isAnnotationPresent(Field_Method_Parameter_Annotation.class)){ //查看是否有指定类型的注解
                    Field_Method_Parameter_Annotation fa=field.getAnnotation(Field_Method_Parameter_Annotation.class);
                    System.out.println("字段的描述:"+fa.describe()); //获得字段的描述
                    System.out.println("字段的类型:"+fa.type());  //获得字段的类型
                }
            }
            System.out.println();
    
            System.out.println("===========方法的描述如下============");
            Method[] declaredMethods = recordClass.getDeclaredMethods();
            for (int i = 0; i < declaredMethods.length; i++) {
                Method method=declaredMethods[i];
                if (method.isAnnotationPresent(Field_Method_Parameter_Annotation.class)){ //查看是否有指定类型的注解
                    Field_Method_Parameter_Annotation ma=method.getAnnotation(Field_Method_Parameter_Annotation.class);
                    System.out.println(i+"方法的描述:"+ma.describe());
                    System.out.println(i+"方法的类型:"+ma.type());
                }
                Annotation[][] parameterAnnotations = method.getParameterAnnotations();
                for (int j = 0; j < parameterAnnotations.length; j++) {
                    int length=parameterAnnotations[j].length;
                    if (length==0){
                        System.out.println("该方法中没有参数");
                    }else {
                        for (int k=0;k<length;k++){
                            Field_Method_Parameter_Annotation pa=(Field_Method_Parameter_Annotation) parameterAnnotations[j][k];
                            System.out.println(i+"有参方法参数的描述:"+pa.describe());
                            System.out.println(i+"有参方法参数的类型:"+pa.type());
                        }
                    }
                }
                System.out.println();
            }
    
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值