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\n", 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處理異常


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM