java - 如何将调用注释的类名和方法作为“值”传递给注释属性
问题描述
public class Demo {
public Demo(){}
@Annotaion(name = "DemoClass::method1")
public void method1(params...)
{
}
}
我不想硬编码“名称”属性,而是需要将其作为类似 this.getClass().getName().NAME_OF_THE_METHOD_ON_WHICH_INVOKED 的内容传递
解决方案
一、创建自定义注解
@Retention(RetentionPolicy.RUNTIME)
@interface Annotation {
public int value1();
public int value2();
}
然后使用它
public class TestClass {
@Annotation(value1 = 15, value2 = 30)
public static void test(){
Class cl = TestClass.class;
Method[] allMethods = cl.getMethods();
Method thisMethod = null;
for (Method m : allMethods)
if (m.getName().equals("test")) thisMethod = m;
Annotation a = thisMethod.getAnnotation(Annotation.class);
a.value1(); //returns 15
a.value2(); //returns 30
}
}
推荐阅读
- git - 部署分支时出现 Bitbucket 冲突问题
- ruby-on-rails - 通过链接连接查询?
- javascript - -webkit-scrollbar 仅适用于 X 轴
- java - 当数据库值等于某个值时,有没有办法从 firebase 发送通知?
- playwright-python - 如何在使用 playwright-python 运行的代码结束时保持浏览器打开?
- javascript - 将html类转换为属性的正则表达式
- javascript - 执行 JS 的 PHP 函数 - Woocommerce
- r - 如何将 scale_colour_steps 与 aes 颜色变量一起使用而不会出现错误:分箱比例仅支持连续数据?
- java - WSO2 IdentityServer,字段“REG_VERSION”没有默认值
- elasticsearch - Spring Data Elasticsearch with RestHighLevelClient for ES 6.0.1