zoukankan      html  css  js  c++  java
  • Java学习-024-获取当前类名或方法名二三文

    今天,看朋友编写程序,打印日志时,需要记录当前类的类名以及当前方法的方法名,我发现 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()				" + this.getClass());
            System.out.println("this.getClass().getName()		" + this.getClass().getName());
            System.out.println("this.getClass().getSimpleName()		" + this.getClass().getSimpleName() + "
    ");
        }
        
        /**
         * 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()" + "		" + ste[i].getFileName() + "
    " +
                                   "ste[" + i + "].getClassName()" + "		" + ste[i].getClassName() + "
    " +
                                   "ste[" + i + "].getLineNumber()" + "		" + ste[i].getLineNumber() + "
    " +
                                   "ste[" + i + "].getMethodName()" + "		" + ste[i].getMethodName() + "
    ");
            }
            
            // 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()" + "		" + ste[i].getFileName() + "
    " +
                        "ste[" + i + "].getClassName()" + "		" + ste[i].getClassName() + "
    " +
                        "ste[" + i + "].getLineNumber()" + "		" + ste[i].getLineNumber() + "
    " +
                        "ste[" + i + "].getMethodName()" + "		" + ste[i].getMethodName() + "
    ");
            }
        }
    }
    

    程序执行结果如下所示:

     

    至此, Java学习-024-获取当前类名或方法名二三文 顺利完结,希望此文能够给初学 Java 的您一份参考。

    最后,非常感谢亲的驻足,希望此文能对亲有所帮助。热烈欢迎亲一起探讨,共同进步。非常感谢! ^_^

     

     

  • 相关阅读:
    Python实现MapReduce,wordcount实例,MapReduce实现两表的Join
    structure needs cleaning
    Lifecycle of an ASP.NET MVC 5 Application
    ASP.NET Integration with IIS 7
    Execution order of modules in IIS7
    Assembly Binding redirect: How and Why?
    Cannot See Worker Processes Icon in IIS
    What is the main difference between a key, an IV and a nonce?
    核心玩法的三要素
    ruby各种循环输出数组元素
  • 原文地址:https://www.cnblogs.com/fengpingfan/p/4728844.html
Copyright © 2011-2022 走看看