Java反射中Annotation的

定义Annotation的构造方法的类:

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Created by cuboo on 2016/10/11.
 */

//用于构造方法
@Target(ElementType.CONSTRUCTOR)
//运行时加载贷JVM中
@Retention(RetentionPolicy.RUNTIME)
public @interface constructor_annotation {
    //定义默认值的成员
    String value() default "默认构造方法";
}


定义Annotation的变量、方法、参数的类:

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Created by cuboo on 2016/10/11.
 */
//用于字段、方法、参数
@Target({ElementType.FIELD,ElementType.METHOD,ElementType.PARAMETER} )
//运行时加载到JVM中
@Retention(RetentionPolicy.RUNTIME)
public @interface field_method_parameter_annotation {
    String describe();
    Class type() default void.class;
}


运用定义Annotation的类:
/**
 * Created by cuboo on 2016/10/11.
 */

public class annotation_class {
      //注释变量
    @field_method_parameter_annotation(describe = "编号",type = int.class)
    int id;
    @field_method_parameter_annotation(describe = "姓名",type = String.class)
    String name;

    //构造无参数方法
    @constructor_annotation()
    annotation_class(){
    }
    //构造有参数方法
    @constructor_annotation()
    annotation_class(@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 = "获得姓名",type = String.class)
    public String getName() {
        return name;
    }

    //注释方法的参数
    @field_method_parameter_annotation(describe = "设置编号")
    public void setId(@field_method_parameter_annotation(describe = "编号",type = int.class) int id) {
        this.id = id;
    }
    @field_method_parameter_annotation(describe = "设置姓名")
    public void setName(@field_method_parameter_annotation(describe = "姓名",type = String.class) String name) {
        this.name = name;
    }
}


访问Annotation的内容类:
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * Created by cuboo on 2016/10/11.
 */
public class access_annotation {
    public static void main(String agrs[]){
//       get_constructor();
//        get_field();
        get_method();
    }

    public static void get_constructor(){
        annotation_class exam = new annotation_class();
        Class examC = exam.getClass();
        Constructor[] constructors = examC.getDeclaredConstructors();

        for (int i = 0; i < constructors.length; i++) {
            Constructor constructor = constructors[i];
            //查看指定类型的注释
            if (constructor.isAnnotationPresent(constructor_annotation.class)){
                constructor_annotation ca = (constructor_annotation) constructor.getAnnotation(constructor_annotation.class);
                //获得注释信息
                System.out.println(ca.value());
            }

            //获得参数的注释
            Annotation[][] paras = constructor.getParameterAnnotations();
            for (int j = 0; j < paras.length; j++) {
                //获得指定注释参数的长度
                int length = paras[j].length;
                if (length == 0){
                    System.out.println("未添加注释参数");
                }else {
                    for (int k = 0; k < length; k++) {
                        //获得参数的注释
                        field_method_parameter_annotation fp = (field_method_parameter_annotation) paras[j][k];
                        System.out.println("参数描述:"+fp.describe());
                        System.out.println("参数类型:"+fp.type());
                    }
                }
            }
        }

    }
    public static void get_field(){
        annotation_class exam = new annotation_class();
        Class examC = exam.getClass();
        Field[] fields = examC.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            Field field = fields[i];
            //查看指定注释
            if (field.isAnnotationPresent(field_method_parameter_annotation.class)){
                field_method_parameter_annotation fp = field.getAnnotation(field_method_parameter_annotation.class);
                System.out.println("变量描述:"+fp.describe());
                System.out.println("变量类型:"+fp.type());
            }
        }

    }
    public static void get_method(){
        annotation_class exam = new annotation_class();
        Class examC = exam.getClass();
        Method[] methods = examC.getDeclaredMethods();
        for (int i = 0; i < methods.length; i++) {
            Method method = methods[i];
            //获得指定的注释
            if (method.isAnnotationPresent(field_method_parameter_annotation.class)){
                field_method_parameter_annotation fp = method.getAnnotation(field_method_parameter_annotation.class);
                System.out.println("方法的描述:"+fp.describe());
                System.out.println("方法的返回类型:"+fp.type());
            }

            //获得参数的注释
            Annotation[][] paras = method.getParameterAnnotations();
            for (int j = 0; j < paras.length; j++) {
                //获得指定注释参数的长度
                int length = paras[j].length;
                if (length == 0){
                    System.out.println("未添加注释参数");
                }else {
                    for (int k = 0; k < length; k++) {
                        //获得参数的注释
                        field_method_parameter_annotation fp = (field_method_parameter_annotation) paras[j][k];
                        System.out.println("参数描述:"+fp.describe());
                        System.out.println("参数类型:"+fp.type());
                    }
                }
            }
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值