我想使用常量作为注释值。
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
时构造。这个问题有解决办法吗?
最佳答案
不,没有解决方法。
关于java - 如何在注释中使用数组常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48591301/