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

 package com.dsp.demo;

 public class TechDemo {

     public static void main(String[] args) {
System.out.println("Hello dsp!"); System.out.printf("%x\n", 2129); aMethod();
} private static String getExecutingMethodName() {
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
StackTraceElement e = stackTrace[2];
return e.getMethodName();
} private static void aMethod() {
System.out.println("######### aMethod #########");
//String executingMethodName = Thread.currentThread().getStackTrace()[2].getMethodName();
String executingMethodName = getExecutingMethodName();
System.out.println(executingMethodName);
String className = Thread.currentThread().getStackTrace()[2].getClassName();
System.out.println(className);
String fileName = Thread.currentThread().getStackTrace()[2].getFileName();
System.out.println(fileName);
System.out.println("******** aMethod ******"); bMethod();
} private static void bMethod() {
System.out.println("######### bMethod #########");
// String executingMethodName = Thread.currentThread().getStackTrace()[2].getMethodName();
String executingMethodName = getExecutingMethodName();
System.out.println(executingMethodName);
String className = Thread.currentThread().getStackTrace()[2].getClassName();
System.out.println(className);
String fileName = Thread.currentThread().getStackTrace()[2].getFileName();
System.out.println(fileName);
System.out.println("******** bMethod ******"); cMethod();
} private static void cMethod() {
System.out.println("######### cMethod #########");
String executingMethodName = getExecutingMethodName();
System.out.println(executingMethodName);
String className = Thread.currentThread().getStackTrace()[2].getClassName();
System.out.println(className);
String fileName = Thread.currentThread().getStackTrace()[2].getFileName();
System.out.println(fileName); saveA();
updateB(); System.out.println("******** cMethod ******");
} public static void saveA() {
System.out.println("######### saveA #########");
// ###
String executingMethodName = getExecutingMethodName();
System.out.println(executingMethodName); // ###
String name = new Object(){}.getClass().getEnclosingMethod().getName();
System.out.println(name);
System.out.println("******** saveA ******");
} public static void updateB() {
System.out.println("######### updateB #########");
String executingMethodName = getExecutingMethodName();
System.out.println(executingMethodName);
System.out.println("******** updateB ******");
} }

执行结果:

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获取正在执行的函数名的更多相关文章

  1. C#获得当前执行的函数名、当前代码行、源代码文件名

    http://blog.csdn.net/newegg2009/article/details/6220385 C#获得当前执行的函数名.当前代码行.源代码文件名 [日期:2010-10-18 11: ...

  2. Java: 获取当前执行位置的文件名/类名/方法名/行号

    在 JAVA 程序有时需要获取当前代码位置, 于是就利用 Thread.currentThread().getStackTrace() 写了下面这个工具类, 用来获取当前执行位置处代码的文件名/类名/ ...

  3. PHP获取当前类名、函数名、方法名

    PHP获取当前类名.方法名  __CLASS__ 获取当前类名  __FUNCTION__ 当前函数名(confirm)  __METHOD__ 当前方法名 (bankcard::confirm) _ ...

  4. PHP获取类名及所有函数名

    PHP获取当前类名.方法名  __CLASS__ 获取当前类名  __FUNCTION__ 当前函数名(confirm)  __METHOD__ 当前方法名 (bankcard::confirm) _ ...

  5. Java获取多线程执行结果方式的归纳与总结

    在日常的项目开发中,我们会经常遇到通过多线程执行程序并需要返回执行结果的场景,下面我们就对获取多线程返回结果的几种方式进行一下归纳,并进行简要的分析与总结. 一.Thread.join 在一些简单的应 ...

  6. java 获取 正在执行的方法名

    //获取调用该方法的方法名.... String method = Thread.currentThread().getStackTrace()[2].getMethodName(); //获取正在执 ...

  7. java获取当前执行文件的路径

    需要知道执行jar包时,jar包所在的路径. 开始使用了 p.getClass().getResource("/").getPath(); 结果在IDE里面使用是好的,但是在命令行 ...

  8. 【Python】动态获取python类名、函数名&多线程

    import time import random import threading import inspect def get_current_function_name(): return in ...

  9. Python学习总结11:获取当前运行类名和函数名

    一. 使用内置方法和修饰器方法获取类名.函数名 1. 外部获取 从外部的情况好获取,可以使用指向函数的对象,然后用__name__属性. def a(): pass a.__name__ 或者 get ...

随机推荐

  1. shell编程:for 循环

    hell 编程——for in 循环   -------for in 格式-------     for 无$变量 in 字符串 do $变量 done   一简单的字符串 枚举遍历法,利用for i ...

  2. _ENV和_G

    5.1之前, 全局变量存储在_G这个table中, 这样的操作:a = 1 相当于:_G['a'] = 1 但在5.2之后, 引入了_ENV叫做环境,与_G全局变量表产生了一些混淆,需要从原理上做一个 ...

  3. ActionScript 3操作XML 详解

    AS3引入了E4X ,它是根据ECMAScript标准处理XML 数据的全新机制.这使得程序员在程序中无缝地操作XML.在AS3中可以使用XML字面值将XML数据直接写入代码,该字面值将被自动解析. ...

  4. 【教程】ubuntu中配置tftp

    1. 安装 tftpd ( tftp 服务器). tftp ( tftp 客户端)以及 xinetd (超级服务器) 1. 安装 tftpd ( tftp 服务器). tftp ( tftp 客户端) ...

  5. 在java代码中,用xslt处理xml文件

    http://blog.csdn.net/zhou_lei/article/details/2661735 ********************************************** ...

  6. Django服务端读取excel文件并且传输到接口

    path_name = "opboss_download_" + str(int(time.time())) + ".csv" print(path_name) ...

  7. KVM初探之一--存储池与存储卷,使用virt-install创建虚拟机

    在机器上装了一个Centos6.2用来测试KVM,对比Windows下的VMware worstation,KVM功能强大,定制性高,只要入了门,绝对是功能强大.用来作云计算不错.不过就是操作性差了点 ...

  8. Jenkins + Django 完整实战,细化到每一步操作

    Reference:  http://blog.csdn.net/GitChat/article/details/78271099?locationNum=3&fps=1 [不要错过文末彩蛋] ...

  9. jQuery 学习笔记2 点击时弹出一个对话框

    上次学习的是页面加载完成后弹出一个警告框,这里我们改为当用户点击后弹出一个警告框. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Trans ...

  10. bootstrap插件(对话框)bootbox参数和自定义弹出框宽度设置

    参考:https://hisune.com/post/view/17/bootstrap-plugin-bootbox-and-custom-width-dialog 官网:http://bootbo ...