本文介紹了如何在注解中使用數(shù)組常量的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我想為注釋值使用常量.
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
可以在注解中使用,但不能在數(shù)組 Info.AB
中使用,因?yàn)樗仨氃谶@個(gè)地方做一個(gè)數(shù)組初始化器.注釋值僅限于可以內(nèi)聯(lián)到類的字節(jié)碼中的值.這對于數(shù)組常量是不可能的,因?yàn)樗仨氃诩虞d Info
時(shí)構(gòu)建.這個(gè)問題有解決辦法嗎?
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?
推薦答案
不,沒有解決方法.
這篇關(guān)于如何在注解中使用數(shù)組常量的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!