java 反射 annotation_Java从入门到精通 – 反射Annotation | 学步园

Constructor_Annotation :

import java.lang.annotation.*;

@Target(ElementType.CONSTRUCTOR)   //用于构造方法

@Retention(RetentionPolicy.RUNTIME) //在运行时加载到Annotation到JVM中

public @interface Constructor_Annotation {

String value() default "默认构造方法";    //定义一个具有默认值的String型成员

}

Field_Method_Parameter_Annotation :

import java.lang.annotation.*;

@Target({ElementType.FIELD,ElementType.METHOD,ElementType.PARAMETER})   //用于字段,方法,参数

@Retention(RetentionPolicy.RUNTIME) //在运行时加载到Annotation到JVM中

public @interface Field_Method_Parameter_Annotation {

Class type() default void.class;  //定义一个具有默认值的Class型成员

String describ();    //定义一个没有默认值的String成员

}

AnnotationTest:

import java.lang.annotation.Annotation;

import java.lang.reflect.Constructor;

import java.lang.reflect.Field;

import java.lang.reflect.Method;

public class AnnotationTest {

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

int id;

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

String name;

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

public AnnotationTest()

{

}

@Constructor_Annotation("立即初始化构造方法.")    //注释构造方法

public AnnotationTest(

//注释构造方法参数

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

int id,

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

String name

)

{

this.id = id;

this.name = name;

}

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

public int getId()

{

return id;

}

@Field_Method_Parameter_Annotation(describ="设置编号")   //成员type,采用默认注释方法

public void setId(

//注释参数

@Field_Method_Parameter_Annotation(describ="设置编号",type=int.class)

int id

)

{

this.id =id;

}

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

public String getName()

{

return name;

}

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

public void setName(

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

String name

)

{

this.name = name;

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

//构造方法:

Constructor[] declaredConstructor = AnnotationTest.class.getDeclaredConstructors();  //获得所有的构造方法

for(int i=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(int j=0;j

{

int length = parameterAnnotations[j].length;

if(length == 0)   //如果为0,则表示没有为该参数添加注释

{

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

}

else

{

for(int k=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(int i=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(int i=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(int j=0;j

{

int length = parameterAnnotations[j].length;

if(length==0)

{

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

}

else

{

for(int k=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("********************");

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值