我想为注释值使用常量.
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?
不,没有解决方法.
这篇关于如何在注解中使用数组常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!