java 反射 annotation_java从入门到精髓 - 反射Annotation

importjava.lang.annotation.Annotation;importjava.lang.reflect.Constructor;importjava.lang.reflect.Field;importjava.lang.reflect.Method;publicclassAnnotationTest {

@Field_Method_Parameter_Annotation(describ="字段编号",type=int.class)//注释字段intid;

@Field_Method_Parameter_Annotation(describ="字段姓名",type=String.class)//注释字段String name;

@Constructor_Annotation()//采用默认构造方法publicAnnotationTest()

{

}

@Constructor_Annotation("立即初始化构造方法.")//注释构造方法publicAnnotationTest(//注释构造方法参数@Field_Method_Parameter_Annotation(describ="编号",type=int.class)intid,

@Field_Method_Parameter_Annotation(describ="姓名",type=String.class)

String name

)

{this.id=id;this.name=name;

}

@Field_Method_Parameter_Annotation(describ="获得编号",type=int.class)publicintgetId()

{returnid;

}

@Field_Method_Parameter_Annotation(describ="设置编号")//成员type,采用默认注释方法publicvoidsetId(//注释参数@Field_Method_Parameter_Annotation(describ="设置编号",type=int.class)intid

)

{this.id=id;

}

@Field_Method_Parameter_Annotation(describ="获得姓名",type=String.class)publicString getName()

{returnname;

}

@Field_Method_Parameter_Annotation(describ="设置姓名")publicvoidsetName(

@Field_Method_Parameter_Annotation(describ="姓名",type=String.class)

String name

)

{this.name=name;

}/***@paramargs*/publicstaticvoidmain(String[] args) {//TODO Auto-generated method stub//构造方法:Constructor[] declaredConstructor=AnnotationTest.class.getDeclaredConstructors();//获得所有的构造方法for(inti=0;i

{

Constructor  constructor=declaredConstructor[i];//遍历构造方法if(constructor.isAnnotationPresent(Constructor_Annotation.class))//查看是否指定类型的注释{

Constructor_Annotation ca=(Constructor_Annotation)constructor.getAnnotation(Constructor_Annotation.class);

System.out.println("ca.value()=:"+ca.value());

}

Annotation[][]parameterAnnotations=constructor.getParameterAnnotations();//获得参数注释for(intj=0;j

{intlength=parameterAnnotations[j].length;if(length==0)//如果为0,则表示没有为该参数添加注释{

System.out.println("没有为该参数添加注释");

}else{for(intk=0;k

{//获得参数注释Field_Method_Parameter_Annotation pa=(Field_Method_Parameter_Annotation)parameterAnnotations[j][k];

System.out.print(""+pa.describ());//参数描述System.out.println(""+pa.type());//参数类型}

}

}

System.out.println("****************");

}//字段:System.out.println("********字段的Annotation*************");

Field[] declaredFields=AnnotationTest.class.getDeclaredFields();//获得所有的字段for(inti=0;i

{

Field field=declaredFields[i];//查看是否具有指定类型的注释:if(field.isAnnotationPresent(Field_Method_Parameter_Annotation.class))

{

Field_Method_Parameter_Annotation fa=(Field_Method_Parameter_Annotation)field.getAnnotation(Field_Method_Parameter_Annotation.class);

System.out.print(""+fa.describ());//获得字段描述System.out.println(""+fa.type());//获得字段类型}

}//方法System.out.println("********方法的Annotation*************");

Method [] methods=AnnotationTest.class.getDeclaredMethods();//获得所有的方法for(inti=0;i

{

Method method=methods[i];//查看是否指定注释:if(method.isAnnotationPresent(Field_Method_Parameter_Annotation.class))

{

Field_Method_Parameter_Annotation ma=(Field_Method_Parameter_Annotation)method.getAnnotation(Field_Method_Parameter_Annotation.class);

System.out.print(""+ma.describ());//获得方法描述System.out.println(""+ma.type());//获得方法类型}

Annotation[][]parameterAnnotations=method.getParameterAnnotations();//获得所有参数for(intj=0;j

{intlength=parameterAnnotations[j].length;if(length==0)

{

System.out.println("没有添加Annotation参数");

}else{for(intk=0;k

{//获得指定的注释:Field_Method_Parameter_Annotation pa=(Field_Method_Parameter_Annotation)parameterAnnotations[j][k];

System.out.print(""+pa.describ());//获得参数描述System.out.println(""+pa.type());//获得参数类型}

}

}

System.out.println("********************");

}

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值