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

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

查找被注釋處理覆蓋的方法

查找被注釋處理覆蓋的方法

SMILET 2021-12-22 20:14:09
代碼結(jié)構(gòu)假設(shè)我們有一個(gè)這樣的結(jié)構(gòu):class A {    @AMethodAnnotation("my-data")    public void myMethod() {    }}@MyClassAnnotationclass B extends A {    @Override    public void myMethod() {    }}我正在努力實(shí)現(xiàn)的目標(biāo)使用注釋處理,我試圖從AMethodAnnotation位于myMethodclass 內(nèi)部方法上的注釋中提取數(shù)據(jù)A。類B擴(kuò)展了這個(gè)類并覆蓋了它的方法myMethod。扭曲的是我想要來(lái)自方法的數(shù)據(jù),AMethodAnnotation如果它在里面的類有 annotation MyClassAnnotation。我正在獲取帶有注釋的類MyClassAnnotation并循環(huán)遍歷 ,在enclosedElements那里我可以檢查它是否有Override注釋,但我不確定如何獲取它覆蓋的方法,因?yàn)槟鞘茿MethodAnnotation我想要的數(shù)據(jù)所在的位置。ExecutableElement似乎沒(méi)有提供方法來(lái)獲得這個(gè)。for (Element classElement : roundEnv.getElementsAnnotatedWith(MyClassAnnotation.class)) {    // Make sure it's a class    if (classElement.getKind() != ElementKind.CLASS) {        continue;    }    // Loop through methods inside class    for (Element methodElement : classElement.getEnclosedElements()) {        // Make sure the element is a method & has a @Path annotation        if (methodElement.getKind() != ElementKind.METHOD) {            continue;        }        // If method has @Override annotation do stuff    }}問(wèn)題有沒(méi)有辦法獲得對(duì)被覆蓋的方法的引用?有一種方法,您獲取Bis的超類A并循環(huán)遍歷enclosedElementsin A,然后您必須驗(yàn)證方法名稱是否相同,以及參數(shù)是否相同且順序相同。但是我發(fā)現(xiàn)這種方法需要大量檢查,因此我的問(wèn)題是是否有更好的方法。
查看完整描述

1 回答

?
慕妹3242003

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

我根據(jù)評(píng)論中發(fā)布的鏈接@rmuller 編寫了以下方法。如 Javadoc 和下圖所示,此方法有大量文檔,其中更具可讀性。

http://img1.sycdn.imooc.com//61c316af0001655e06510337.jpg

/**

 * Will find the method which the method <strong>methodElement</strong> is overriding, if any.

 * It does this by recursively traversing up the superclass tree of the

 * <strong>classElement</strong> and checking if there are methods which override the

 * <strong>methodElement</strong>. It will return after it finds the first method with the

 * annotation <strong>annotation</strong>.

 *

 * @param originalClassElement The original class inside which the

 *                             <strong>methodElement</strong> is located.

 * @param classElement         The class which represents the superclass while recursively

 *                             looking up the tree, it should be equal to the

 *                             <strong>originalClassElement</strong> when first calling this

 *                             method.

 * @param methodElement        The method for which should be checked if it's overriding

 *                             anything.

 * @param annotation           The annotation which must be matched before returning the result

 * @return Will return the following, the list is written in order:

 *         <ul>

 *         <li>The method <strong>methodElement</strong> if <strong>methodElement</strong>

 *         already has an annotation of the type <strong>annotation</strong></li>

 *         <li>Null if the method <strong>methodElement</strong> does not have an @Override

 *         annotation</li>

 *         <li>Null if the class <strong>classElement</strong> does not have a superclass</li>

 *         <li>The method element which was found to have the annotation

 *         <strong>annotation</strong></li>

 *         </ul>

 */

public ExecutableElement getMethodOverride(TypeElement originalClassElement,

        TypeElement classElement, ExecutableElement methodElement,

        Class<? extends Annotation> annotation) {


    if (methodElement.getAnnotation(annotation) != null) {

        // The methodElement which was passed has the required annotation already

        return methodElement;

    }


    if (methodElement.getAnnotation(Override.class) == null) {

        // The methodElement which was passed does not have an @Override annotation and is

        // therefore not overriding anything.

        return null;

    }


    if (classElement.getSuperclass().getKind() == TypeKind.NONE) {

        // Class has no superclass

        return null;

    }


    for (Element elem : classElement.getEnclosedElements()) {

        if (elem.getKind() != ElementKind.METHOD) {

            // Not a method

            continue;

        }


        // Check if the method inside the superclass overrids the method inside the original

        // class

        if (this.processingEnv.getElementUtils().overrides(methodElement,

                (ExecutableElement) elem, classElement)) {


            // Recursively go up the tree and check since this method might also override

            // another method

            return getMethodOverride(originalClassElement,

                    this.env.getElementUtils()

                            .getTypeElement(classElement.getSuperclass().toString()),

                    (ExecutableElement) elem, annotation);

        }


    }


    // Recursively go up the tree and check there

    return getMethodOverride(originalClassElement,

            this.env.getElementUtils().getTypeElement(classElement.getSuperclass().toString()),

            methodElement, annotation);

}


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

添加回答

舉報(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)