-
了解与运用注解。
-
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(); }
通过反射获取自定义注解信息
最新推荐文章于 2022-01-01 00:09:15 发布