java getstacktrace_利用new Exception().getStackTrace()查看谁调用了方法

过时的方法

使用 AS debug模式可以很轻易的获得调用栈

最近项目中遇到一个Bug ,在某次进行数据持久化的时候,对于同一个key的数据存储。

在某次执行持久化时,将一个value为null的值覆盖上去了,但是项目中有非常多的地方对这个key进行了持久化存储。

才开始以为是异步的问题,所以先将存储方式改为同步后发现错误依然存在。

如果要一个一个排查的话那么将会花费大量的精力。

那么能不能在数据持久化的入口中查看到底是哪个方法调用的时候覆盖了不正确的值呢?

这时想起来KLog等log工具类实现自动添加类名,方法名的一个挺巧妙的办法。

通过new Exception().getStackTrace();可以获得方法的调用栈的数组,那么就可以在持久化的入口处获得传入不正确值的方法调用栈了。

//代码大致如下

StackTraceElement[] stackTrace = new Exception().getStackTrace();

for (int i = 0; i < stackTrace.length; i++) {

System.out.println("key = " + stackTrace[i]);

}

当然代码中的层级数i < stackTrace.length 你可以同时再加个判断i < stackTrace.length && i <5 ,不然的话日志就会一直打印到Android进程的入口即ZygoteInit.main(),

就像这样

//...上层代码忽略

android.os.Handler.handleCallback(Handler.java:815)

android.os.Handler.dispatchMessage(Handler.java:104)

android.os.Looper.loop(Looper.java:207)

android.app.ActivityThread.main(ActivityThread.java:5896)

java.lang.reflect.Method.invoke(Native Method)

com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:948)

com.android.internal.os.ZygoteInit.main(ZygoteInit.java:809)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.hexiang.utils; import java.awt.Component; import javax.swing.JOptionPane; /** * This class ExceptionManager and its subclasses are a form of * Exception. It is used to wrap all the Throwable instances * and handle them in a unified way. It will show the information which consists of * StackTraces and Messages by using JOptionPanel. * * @author Estelle * @version 1.0 * @see java.lang.Exception * @since jdk 1.5 */ public class ExceptionManager extends RuntimeException { private static final long serialVersionUID = -6963187366089365790L; /** * This field alerter is used to show the information the Class offered. * * @see javax.swing.JOptionPane */ private JOptionPane alerter; /** * This static method create an instance of the ExceptionManager by invoking the * constructor ExceptionManager(String msg). * * @param msg The message will pass the specified constructor * @return An instance of the ExceptionManager created by invoking the constructor * ExceptionManager(String msg). */ public static ExceptionManager wrap(String msg){ return new ExceptionManager(msg); } /** * This static method create an instance of the ExceptionManager by invoking the * constructor ExceptionManager(Throwable throwable). * * @param throwable The cause will pass the specified constructor * @return An instance of the ExceptionManager created by invoking the constructor * ExceptionManager(Throwable throwable). */ public static ExceptionManager wrap(Throwable throwable){ return new ExceptionManager(throwable); } /** * This static method create an instance of the ExceptionManager by invoking the * constructor ExceptionManager(String msg,Throwable throwable). * * @param msg The message will pass the specified constructor * @param throwable The cause will pass the specified c

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值