• Java获取正在执行的函数名


    利用StackTrace堆栈轨迹获取某个时间的调用堆栈状态。

     1 package com.dsp.demo;
     2 
     3 public class TechDemo {
     4 
     5     public static void main(String[] args) {
     6         System.out.println("Hello dsp!");
     7 
     8         System.out.printf("%x
    ", 2129);
     9 
    10         aMethod();
    11     }
    12 
    13     private static String getExecutingMethodName() {
    14         StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
    15         StackTraceElement e = stackTrace[2];
    16         return e.getMethodName();
    17     }
    18 
    19     private static void aMethod() {
    20         System.out.println("######### aMethod #########");
    21         //String executingMethodName = Thread.currentThread().getStackTrace()[2].getMethodName();
    22         String executingMethodName = getExecutingMethodName();
    23         System.out.println(executingMethodName);
    24         String className = Thread.currentThread().getStackTrace()[2].getClassName();
    25         System.out.println(className);
    26         String fileName = Thread.currentThread().getStackTrace()[2].getFileName();
    27         System.out.println(fileName);
    28         System.out.println("******** aMethod ******");
    29 
    30         bMethod();
    31     }
    32 
    33     private static void bMethod() {
    34         System.out.println("######### bMethod #########");
    35         // String executingMethodName = Thread.currentThread().getStackTrace()[2].getMethodName();
    36         String executingMethodName = getExecutingMethodName();
    37         System.out.println(executingMethodName);
    38         String className = Thread.currentThread().getStackTrace()[2].getClassName();
    39         System.out.println(className);
    40         String fileName = Thread.currentThread().getStackTrace()[2].getFileName();
    41         System.out.println(fileName);
    42         System.out.println("******** bMethod ******");
    43 
    44         cMethod();
    45     }
    46 
    47     private static void cMethod() {
    48         System.out.println("######### cMethod #########");
    49         String executingMethodName = getExecutingMethodName();
    50         System.out.println(executingMethodName);
    51         String className = Thread.currentThread().getStackTrace()[2].getClassName();
    52         System.out.println(className);
    53         String fileName = Thread.currentThread().getStackTrace()[2].getFileName();
    54         System.out.println(fileName);
    55 
    56         saveA();
    57         updateB();
    58 
    59         System.out.println("******** cMethod ******");
    60     }
    61 
    62     public static void saveA() {
    63         System.out.println("######### saveA #########");
    64         // ###
    65         String executingMethodName = getExecutingMethodName();
    66         System.out.println(executingMethodName);
    67 
    68         // ###
    69         String name = new Object(){}.getClass().getEnclosingMethod().getName();
    70         System.out.println(name);
    71         System.out.println("******** saveA ******");
    72     }
    73 
    74     public static void updateB() {
    75         System.out.println("######### updateB #########");
    76         String executingMethodName = getExecutingMethodName();
    77         System.out.println(executingMethodName);
    78         System.out.println("******** updateB ******");
    79     }
    80 
    81 }

    执行结果:

    Hello dsp!
    851
    ######### aMethod #########
    aMethod
    com.dsp.demo.TechDemo
    TechDemo.java
    ******** aMethod ******
    ######### bMethod #########
    bMethod
    com.dsp.demo.TechDemo
    TechDemo.java
    ******** bMethod ******
    ######### cMethod #########
    cMethod
    com.dsp.demo.TechDemo
    TechDemo.java
    ######### saveA #########
    saveA
    saveA
    ******** saveA ******
    ######### updateB #########
    updateB
    ******** updateB ******
    ******** cMethod ******

    另附:

    Stack Trace - 百度百科

    Java异常的栈轨迹(Stack Trace)

    使用Stacktrace处理异常

  • 相关阅读:
    KMP算法的Next数组详解(转)
    公开封尘已久的即时通讯源码(转)
    《C语言编写 学生成绩管理系统》
    随想录(从编程语言到库、框架、软件)
    Effective Objective-C 2.0 笔记三(Literal Syntax简写语法)
    Java Swing 探索(一)LayoutManager
    Codeforces Round #FF 446 C. DZY Loves Fibonacci Numbers
    ARM体系结构与编程
    div:给div加滚动栏 div的滚动栏设置
    DS18B20
  • 原文地址:https://www.cnblogs.com/gotodsp/p/7357697.html
Copyright © 2020-2023  润新知