本文介紹了如何在注解中使用數組常量的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我想為注釋值使用常量.
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?
推薦答案
不,沒有解決方法.
這篇關于如何在注解中使用數組常量的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!