第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

我可以獲得任何注釋值,而不使用注釋的名稱嗎?

我可以獲得任何注釋值,而不使用注釋的名稱嗎?

藍(lán)山帝景 2022-09-28 10:09:19
我想編寫一個(gè)返回注釋方法值的方法。我試圖使用這個(gè)變體,沒有任何成功參數(shù):clazz - 具有注釋的 Сlass注釋克拉茲 - 我的注釋.class參數(shù)名稱 - 方法的名稱這是我的代碼:public static Object getAnnotationValue(Class clazz, Class annotationClazz, String parametersName) {    Annotation an = clazz.getAnnotation(annotationClazz);    if (an.equals(null)) {        throw new CoreError("Класс " + clazz + " не содержит аннотацию " + annotationClazz);    }    PageName pn = (PageName) an;    try {        //its working!        System.out.println(pn.value());        //not working :(        System.out.println(an.getClass().getMethod(parametersName).getDefaultValue()); //not working :(        System.out.println(an.annotationType().getDeclaredMethod(parametersName, annotationClazz).getDefaultValue());        System.out.println(pn.getClass().getMethod(parametersName).getDefaultValue());        System.out.println(pn.annotationType().getDeclaredMethod(parametersName, annotationClazz).getDefaultValue());    } catch (NoSuchMethodException e) {        e.printStackTrace();    }}這有可能嗎?
查看完整描述

1 回答

?
偶然的你

TA貢獻(xiàn)1841條經(jīng)驗(yàn) 獲得超3個(gè)贊

您的代碼一直在尋找默認(rèn)值,但您的問題從未提及默認(rèn)值是否存在。若要獲取提供的值,必須在實(shí)例上使用該方法。invoke

此外,在大多數(shù)情況下,您正在使用和錯(cuò)誤。getMethodgetDeclaredMethod

下面是一個(gè)工作示例:

public static Object getAnnotationValue(Class clazz, Class annotationClazz, String parameterName) {  
  Annotation an = clazz.getAnnotation(annotationClazz);
    System.out.println(an.annotationType().getMethod(parameterName).invoke(an));
}

因此,對(duì)于這樣的類:

@PageName("testPage") //Same as @PageName(value = "testPage")public class Example {}

getAnnotationValue(Example.class, PageName.class, "value")

將打印

測(cè)試頁


查看完整回答
反對(duì) 回復(fù) 2022-09-28
  • 1 回答
  • 0 關(guān)注
  • 112 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)