Java获取正在执行的函数名
生活随笔
收集整理的這篇文章主要介紹了
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處理異常
總結
以上是生活随笔為你收集整理的Java获取正在执行的函数名的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Git 使用笔记
- 下一篇: Windows 10小娜只显示文字而不发