我想为注释值使用常量。
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中使用,因为它必须是此位置中的数组初始值。注释值被限制为可以内联到类的字节代码中的值。这是不可能的数组常量,因为它必须构建时,信息加载。这个问题有解决方法吗?