Thread.currentThread().getStackTrace()[?]

原文地址:点击跳转
 

测试代码:

 public static void main(String[] args) {
        getName4();
    }

    public static void getName1() {
        String methodName0 = Thread.currentThread().getStackTrace()[0].getMethodName();
        String methodName1 = Thread.currentThread().getStackTrace()[1].getMethodName();
        String methodName2 = Thread.currentThread().getStackTrace()[2].getMethodName();
        String methodName3 = Thread.currentThread().getStackTrace()[3].getMethodName();
        String methodName4 = Thread.currentThread().getStackTrace()[4].getMethodName();
        System.out.println("methodName0-------" + methodName0);
        System.out.println("methodName1-------" + methodName1);
        System.out.println("methodName2-------" + methodName2);
        System.out.println("methodName3-------" + methodName3);
        System.out.println("methodName4-------" + methodName4);
        System.out.println("className---------" + Thread.currentThread().getStackTrace()[4].getClassName());
        System.out.println("fileName----------" + Thread.currentThread().getStackTrace()[4].getFileName());
        System.out.println("lineNumber--------" + Thread.currentThread().getStackTrace()[4].getLineNumber());
    }
    public static void getName2() {
        getName1();
    }
    public static void getName3() {
        getName2();
    }
    public static void getName4() {
        getName3();
    }

测试结果:

methodName0-------getStackTrace
methodName1-------getName1
methodName2-------getName2
methodName3-------getName3
methodName4-------getName4
className---------com.base.scrm.controller.ScrmGroupController
fileName----------ScrmGroupController.java
lineNumber--------106

为了展示lineNumber是否正确截图代码:

 结论:

        Thread.currentThread().getStackTrace()[?] ,?表示第几层调用方法

        同时Thread.currentThread().getStackTrace()[?]后面还可以点方法

        方法名:Thread.currentThread().getStackTrace()[?].getMethodName()

        类名及路径:Thread.currentThread().getStackTrace()[?].getClassName()

        文件名:Thread.currentThread().getStackTrace()[?].getFileName()

        执行行数:Thread.currentThread().getStackTrace()[?].getLineNumber()

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值