通过注释Annotation扩展及构造方法参数名

本文介绍了一种使用Java注解来标记类、构造方法、字段及方法,并通过反射读取这些注解信息的方法。具体包括自定义注解@Constructor_Annotation和@Field_Method_Parameter_Annotation,以及如何在运行时通过反射获取这些注解的数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、定义构造方法注释运行时加载Annotation到JVM

import java.lang.annotation.*;
@Target(ElementType.CONSTRUCTOR) //用于构造方法
@Retention(RetentionPolicy.RUNTIME) //在运行时加载到Annotation到JVM中
public @interface Constructor_Annotation {
String value() default "默认构造方法"; //定义一个具有默认值的String型成员
}


二、定义字段、方法及方法参数注释运行时加载Annotation到JVM

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成员
}



三、添加一个需要反射测试ORM对象

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(">>>>>>>>>>>>>>>>>>方法结束<<<<<<<<<<<<<<<<<<<");

}

}


更多的注释类型可以自行扩展
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值