How to use an array constant in an annotation(如何在注解中使用数组常量)
问题描述
我想为注释值使用常量.
I would like to use constants for annotation values.
interface Client {
@Retention(RUNTIME)
@Target(METHOD)
@interface SomeAnnotation { String[] values(); }
interface Info {
String A = "a";
String B = "b";
String[] AB = new String[] { A, B };
}
@SomeAnnotation(values = { Info.A, Info.B })
void works();
@SomeAnnotation(values = Info.AB)
void doesNotWork();
}
常量 Info.A
和 Info.B
可以在注解中使用,但不能在数组 Info.AB
中使用,因为它必须在这个地方做一个数组初始化器.注释值仅限于可以内联到类的字节码中的值.这对于数组常量是不可能的,因为它必须在加载 Info
时构建.这个问题有解决办法吗?
The constants Info.A
and Info.B
can be used in the annotation but not the array Info.AB
as it has to be an array initializer in this place. Annotation values are restricted to values that could be inlined into the byte code of a class. This is not possible for the array constant as it has to be constructed when Info
is loaded. Is there a workaround for this problem?
推荐答案
不,没有解决方法.
这篇关于如何在注解中使用数组常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在注解中使用数组常量


- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01