通过注释Annotation扩展及构造方法参数名
一、定义构造方法注释运行时加载Annotation到JVM
import java.lang.annotation.*;@Target(ElementType.CONSTRUCTOR) //用于构造方法@Retention(RetentionPolicy.RUNTIME) //在运行时加载到Annotation到JVM中public @interface Constructor_Annotation { String value() default "默认构造方法"; //定义一个具有默认值的String型成员}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成员}package test;public class AnnotationEntity {//注释属性名@Field_Method_Parameter_Annotation(describ = "字段编号", type = int.class,name = "id")int id;//注释属性名 name@Field_Method_Parameter_Annotation(describ = "字段姓名", type = String.class,name = "name")String name;//注释默认构造函数@Constructor_Annotation()public AnnotationEntity(){}//注释有传入参数构造方法@Constructor_Annotation("注释有传入参数构造方法")public AnnotationEntity(@Field_Method_Parameter_Annotation(describ = "编号", type = int.class,name = "id")int id){}@Field_Method_Parameter_Annotation(describ = "获取编号", type = int.class,name = "getId")/** * @return the id */public int getId() {return id;}@Field_Method_Parameter_Annotation(describ = "设置编号", name = "setId")/** * @param id the id to set */public void setId(@Field_Method_Parameter_Annotation(describ = "传入编号值", type = int.class ,name = "id")int id) {this.id = id;}@Field_Method_Parameter_Annotation(describ = "获取名称", type = String.class,name = "getName")/** * @return the name */public String getName() {return name;}@Field_Method_Parameter_Annotation(describ = "设置名称", name = "setName")/** * @param name the name to set */public void setName(@Field_Method_Parameter_Annotation(describ = "传入的名称", type=String.class,name = "name")String name) {this.name = name;}}import java.lang.annotation.Annotation;import java.lang.reflect.Constructor;import java.lang.reflect.Field;import java.lang.reflect.Method;public class AnnotationTest {/** * @param args */@SuppressWarnings("unchecked")public static void main(String[] args) {Class testClass = AnnotationEntity.class;Class annClass = Constructor_Annotation.class;Class FMPAnn = Field_Method_Parameter_Annotation.class;Constructor_Annotation ca;// 构造注释Field_Method_Parameter_Annotation pa;// 属性名、方法及传入参数注释Annotation[][] parameterAnnotations;// 注释存放变量// 获取构造方法:Constructor[] declaredConstructor = testClass.getDeclaredConstructors(); // 获得所有的构造方法System.out.println("+++++++++构造注释开始+++++++++");for (Constructor constructor : declaredConstructor) {// 遍历构造方法// 查看是否指定类型的注释if (constructor.isAnnotationPresent(annClass)) {ca = (Constructor_Annotation) constructor.getAnnotation(annClass);System.out.println(ca.value());}// 获得构造函数参数注释parameterAnnotations = constructor.getParameterAnnotations();for (Annotation[] anns : parameterAnnotations) {System.out.println("传入参数个数" + anns.length);for (Annotation annotation : anns) {pa = (Field_Method_Parameter_Annotation) annotation;System.out.println("描述>>>>>>>" + pa.describ()); // 参数描述System.out.println("类型>>>>>>>" + pa.type()); // 参数类型System.out.println("属性名>>>>>" + pa.name());}}}System.out.println("+++++++++构造注释结束+++++++++");System.out.println("");System.out.println("");System.out.println("xxxxxxxxxxxxxxxxx字段开始xxxxxxxxxxxxxx");Field[] declaredFields = testClass.getDeclaredFields(); // 获得所有的字段for (Field field : declaredFields) {if (field.isAnnotationPresent(FMPAnn)) {pa = (Field_Method_Parameter_Annotation) field.getAnnotation(FMPAnn);System.out.println("描述>>>>>>>" + pa.describ()); // 参数描述System.out.println("类型>>>>>>>" + pa.type()); // 参数类型System.out.println("属性名>>>>>" + pa.name());}}System.out.println("xxxxxxxxxxxxxxxxx字段结束xxxxxxxxxxxxxx");System.out.println("");System.out.println("");// 方法System.out.println(">>>>>>>>>>>>>>>>>>方法开始<<<<<<<<<<<<<<<<<<<");Method[] methods = testClass.getDeclaredMethods(); // 获得所有的方法for (Method method : methods) {if (method.isAnnotationPresent(FMPAnn)) {pa = (Field_Method_Parameter_Annotation) method.getAnnotation(FMPAnn);System.out.println("描述>>>>>>>" + pa.describ()); // 参数描述System.out.println("类型>>>>>>>" + pa.type()); // 参数类型System.out.println("属性名>>>>>" + pa.name());}// 获得所有参数parameterAnnotations = method.getParameterAnnotations();for (Annotation[] anns : parameterAnnotations) {System.out.println("传入参数个数" + anns.length);for (Annotation annotation : anns) {pa = (Field_Method_Parameter_Annotation) annotation;System.out.println("描述>>>>>>>" + pa.describ()); // 参数描述System.out.println("类型>>>>>>>" + pa.type()); // 参数类型System.out.println("属性名>>>>>" + pa.name());}}}System.out.println(">>>>>>>>>>>>>>>>>>方法结束<<<<<<<<<<<<<<<<<<<");}}