DSP

Java获取正在执行的函数名

2019-07-13 17:45发布

利用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处理异常