Java反射中Annotation的 - 日理万妓 2022-07-16 08:30 116阅读 0赞 定义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()); } } } } } }
还没有评论,来说两句吧...