Java學習-024-擷取當前類名或方法名二三文

來源:互聯網
上載者:User

標籤:

今天,看朋友編寫程式,列印日誌時,需要記錄當前類的類名以及當前方法的方法名,我發現 TA 將類名或者方法名直接寫死在了代碼中。。。雖說這樣可以實現記錄類名和方法名,但是當有特殊情況需要修改類名或者方法名時,源碼中涉及類名或者方法名的地方必須同步變更,若修改的地方比較多,難免可能發生有遺漏的地方,那麼後續通過日誌查看分析原因時,就會找不到相應的地方,導致無法分析,尋找原因。

為何要擷取類名?

  1. 調試源碼
  2. 記錄日誌
  3. 產生報告
  4. 統計分析,對調用比例佔比大的方法,增強單元測試
  5. 構建系統調用關係鏈,對主要關係鏈或多分支方法重點測試,加強業務、功能、安全、效能等方面的測試

上述各作用,會逐步進行相應的簡述,敬請期待!感興趣的小主可先自行研究。

那麼我們該如何擷取對應的類名和方法名呢?其實,在 JDK 中早就提供了相關的方法或者途徑擷取相應的類名或方法名。不知道大家之前在調試 Java 源碼的時候,在異常報錯的時候,有沒有查看過相應的報錯資訊,若是注意到此,那麼相信你肯定也知道如何通過異常錯誤擷取當前的類名和方法名了。

 

擷取類名或者方法名的方法比較簡單,在此不作詳細的說明,直接上碼了,敬請各位小主參閱。若有不足之處,敬請大神指正,不勝感激!

/** * Aaron.ffp Inc. * Copyright (c) 2004-2015 All Rights Reserved. */package com.java.demo;import org.testng.annotations.Test;/** * Get information of class and method *  * @author Aaron.ffp * @version V1.0.0: Jsoup com.java.demo GetClassMethodName.java, 2015-8-13 10:58:39 Exp $ */public class GetClassMethodName extends InvokeClass{    /**     * Get information of class      *      * @author Aaron.ffp     * @version V1.0.0: Jsoup com.java.demo GetClassMethodName.java test_getClassName, 2015-8-14 12:09:07 Exp $     *     */    @Test    public void test_getClassName(){        System.out.println("this.getClass()\t\t\t\t" + this.getClass());        System.out.println("this.getClass().getName()\t\t" + this.getClass().getName());        System.out.println("this.getClass().getSimpleName()\t\t" + this.getClass().getSimpleName() + "\n");    }        /**     * Get information of method     *      * @author Aaron.ffp     * @version V1.0.0: Jsoup com.java.demo GetClassMethodName.java test_getMethodName, 2015-8-14 12:10:02 Exp $     *     */    @Test    public void test_getMethodName(){        // get info by exception        StackTraceElement[] ste = new Exception().getStackTrace();                for (int i = 0; i < ste.length; i++) {            if (i > 0) {                break;            }            System.out.println("ste[" + i + "].getFileName()" + "\t\t" + ste[i].getFileName() + "\n" +                               "ste[" + i + "].getClassName()" + "\t\t" + ste[i].getClassName() + "\n" +                               "ste[" + i + "].getLineNumber()" + "\t\t" + ste[i].getLineNumber() + "\n" +                               "ste[" + i + "].getMethodName()" + "\t\t" + ste[i].getMethodName() + "\n");        }                // get info by Thread        ste = Thread.currentThread().getStackTrace();                for (int i = 0; i < ste.length; i++) {            if (i != 1) {                continue;            }                        System.out.println("ste[" + i + "].getFileName()" + "\t\t" + ste[i].getFileName() + "\n" +                    "ste[" + i + "].getClassName()" + "\t\t" + ste[i].getClassName() + "\n" +                    "ste[" + i + "].getLineNumber()" + "\t\t" + ste[i].getLineNumber() + "\n" +                    "ste[" + i + "].getMethodName()" + "\t\t" + ste[i].getMethodName() + "\n");        }    }}

程式執行結果如下所示:

 

至此, Java學習-024-擷取當前類名或方法名二三文 順利完結,希望此文能夠給初學 Java 的您一份參考。

最後,非常感謝親的駐足,希望此文能對親有所協助。熱烈歡迎親一起探討,共同進步。非常感謝! ^_^

 

 

Java學習-024-擷取當前類名或方法名二三文

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.