当前位置:网站首页>自定义注解的使用
自定义注解的使用
2022-07-28 02:52:00 【康康的码农之路】
- 注解类
@Target({
ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface MyMessage {
String name() default "sam";
int num() default 0;
String desc();
}
- 实体类
public class AnnotationTest {
@MyMessage(num = 10, desc = "参数a")
int a;
int b;
@MyMessage(name = "Sam test", desc = "测试方法test")
public void test(String a) {
System.out.println(a);
}
@MyMessage(name = "Sam test", desc = "测试方法test")
public void test111(String a,String b){
System.out.println("fdsfdsafa");
}
}
- 注解解析类
public class MyMessageProcessor {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
// 获取class类
Class<?> aClass = Class.forName("com.AnnotationTest");
// 获取所有属性
Field[] declaredFields = aClass.getDeclaredFields();
// 实例化
AnnotationTest o = (AnnotationTest)aClass.newInstance();
// 便利属性
for (Field declaredField : declaredFields) {
if(declaredField.isAnnotationPresent(MyMessage.class)){
// 得到注解的信息
MyMessage myMessage = declaredField.getAnnotation(MyMessage.class);
// 获取注解上面的所有值
System.out.println("name:" + myMessage.name() + " num:" + myMessage.num() + " desc:" + myMessage.desc());
// 如果是私有属性,打破壁垒
declaredField.setAccessible(true);
// 给属性设置值
declaredField.set(o,myMessage.num());
}
}
//获取类中的方法
Method[] methods = aClass.getMethods();
//遍历方法
for (Method method : methods) {
//判断方法是否带有MyMessage注解
if (method.isAnnotationPresent(MyMessage.class)) {
// 获取所有注解 method.getDeclaredAnnotations();
// 获取MyMessage注解
MyMessage myMessage = method.getAnnotation(MyMessage.class);
if (method.getName().equals("test")){
// 进行方法的调用 ,注意参数的问题
/** * 如果知道方法的名字,可以这样写 * Method test111 = aClass.getMethod("test111", String.class, String.class); * test111.invoke(o,myMessage.name(),myMessage.name()); */
method.invoke(o,myMessage.name());
}
System.out.println("name:" + myMessage.name() + " num:" + myMessage.num() + " desc:" + myMessage.desc());
}
}
}
}
边栏推荐
- 关于权重衰退和丢弃法
- 版本兼容的问题
- The test post changes jobs repeatedly, jumping and jumping, and then it disappears
- ECCV 2022 | open source for generative knowledge distillation of classification, detection and segmentation
- C -- switch case statement
- ThreadLocal usage scenario
- Redis implements distributed locks
- 【ACwing 1064 小国王】状压dp
- "Introduction to engineering electromagnetic field" after class exercises with answers
- Full of dry goods, hurry in!!! Easy to master functions in C language
猜你喜欢

C WinForm development: how to add pictures to project resources

《工程电磁场导论》课后习题附答案

Brush questions every day to consolidate knowledge
![[download file] uniapp develops small programs, downloads files and saves them locally](/img/0f/4963178e44f58ad6e3097844bb6ffd.png)
[download file] uniapp develops small programs, downloads files and saves them locally

基于OpenCV的轮廓检测(3)

mysql存储过程 使用游标实现两张表数据同步数据

Shell编写规范和变量

My approval & signature function of conference OA project

Detailed tutorial of one click reinstallation of win7 system

C -- switch case statement
随机推荐
exness:日本物价上涨收入下降,英镑/日元突破 165
bp svm的缺陷检测 树叶缺陷 叶片缺陷检测的系统设计
Interview experience: first tier cities move bricks and face software testing posts. 5000 is enough
SSM integration (integrated configuration)
What if the word selection box of win11 input method is missing?
【下载文件】uniapp开发小程序,下载文件并保存到本地
redis网络模型解析
Analysis of redis network model
Win11黑色桌面背景如何解决?
【uni-app高级实战】手把手带你学习一个纯实战复杂项目的开发2/100
工程电磁场复习基本知识点
Design of the multi live architecture in different places of the king glory mall
C # set TextBox control not editable
对象数组转成strin再进行,隔开的字符串,包括赛选某个字段的子,或者求和,
《MySQL数据库进阶实战》读后感(SQL 小虚竹)
Interview experience: first tier cities move bricks and face software testing posts. 5000 is enough
Color recognition method and exploration based on MATLAB
My approval of OA project (meeting inquiry & meeting signature)
线程基础
【类的本质(Objective-C语言中)】