java - Java 自定义注解采用另一个注解
问题描述
如何编写一个带有另一个注释和值的自定义注释?
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface TestAnnotation{
Class<? extends TestAnnotationChild> annotation();
}
第二个注释
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface TestAnnotationChild{
}
我想做类似的事情
@TestAnnotation(@TestAnnotationChild={values})
我怎么能做这样的事情?
解决方案
这就是它的完成方式。
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface TestAnnotation {
TestAnnotationChild child();
// Or for an array
TestAnnotationChild[] children();
}
用法
@TestAnnotation(
@TestAnnotationChild(
value = "42",
anotherValue = 42
)
)
但是,您声明的这一部分
和价值观
确实让我觉得你想做一些不寻常的事情。
你能澄清一下吗?
推荐阅读
- c# - 如何在 C# 中将数据发送到 ASP.NET Core MVC 控制器?
- codeigniter - 如何修复 codeigniter4 和 date_format 中的错误?
- python - 用英语解释这段代码如何迭代一个数字
- python - 来自两个 numpy 行向量的 pandas DataFrame
- javascript - 使用另一个数组的索引从数组中获取元素
- java - 具有独立共享变量的同步方法
- c++ - 在 C++ 中打印没有强制转换的枚举可以吗?
- c++ - 用另一个父类函数覆盖函数
- javascript - React Native:'TypeError:未定义不是对象(评估'response.headers ['set-cookie']')'
- php - 在 PHP 中打印树中可见的级别时出错